11from collections import deque , defaultdict
2+ from typing import List , Tuple , Dict
3+
24
35UNMATCHED = - 1 # Constant to represent unmatched vertices
46
57
68class EdmondsBlossomAlgorithm :
79 @staticmethod
8- def maximum_matching (edges : list [tuple [int , int ]], vertex_count : int ) -> list [tuple [int , int ]]:
10+ def maximum_matching (
11+ edges : List [Tuple [int , int ]], vertex_count : int
12+ ) -> List [Tuple [int , int ]]:
913 """
1014 Finds the maximum matching in a general graph using Edmonds' Blossom Algorithm.
1115
@@ -16,7 +20,7 @@ def maximum_matching(edges: list[tuple[int, int]], vertex_count: int) -> list[tu
1620 >>> EdmondsBlossomAlgorithm.maximum_matching([(0, 1), (1, 2), (2, 3)], 4)
1721 [(0, 1), (2, 3)]
1822 """
19- graph : dict [int , list [int ]] = defaultdict (list )
23+ graph : Dict [int , List [int ]] = defaultdict (list )
2024
2125 # Populate the graph with the edges
2226 for vertex_u , vertex_v in edges :
@@ -80,10 +84,16 @@ def maximum_matching(edges: list[tuple[int, int]], vertex_count: int) -> list[tu
8084 EdmondsBlossomAlgorithm .contract_blossom (
8185 BlossomData (
8286 BlossomAuxData (
83- queue , parent , base , in_blossom ,
84- match , in_queue
87+ queue ,
88+ parent ,
89+ base ,
90+ in_blossom ,
91+ match ,
92+ in_queue ,
8593 ),
86- current_vertex , neighbor , base_vertex
94+ current_vertex ,
95+ neighbor ,
96+ base_vertex ,
8797 )
8898 )
8999
@@ -96,7 +106,9 @@ def maximum_matching(edges: list[tuple[int, int]], vertex_count: int) -> list[tu
96106 return matching_result
97107
98108 @staticmethod
99- def update_matching (match : list [int ], parent : list [int ], current_vertex : int ) -> None :
109+ def update_matching (
110+ match : List [int ], parent : List [int ], current_vertex : int
111+ ) -> None :
100112 """
101113 Updates the matching along the augmenting path found.
102114
@@ -119,7 +131,7 @@ def update_matching(match: list[int], parent: list[int], current_vertex: int) ->
119131
120132 @staticmethod
121133 def find_base (
122- base : list [int ], parent : list [int ], vertex_u : int , vertex_v : int
134+ base : List [int ], parent : List [int ], vertex_u : int , vertex_v : int
123135 ) -> int :
124136 """
125137 Finds the base of a node in the blossom.
@@ -155,7 +167,7 @@ def find_base(
155167 current_vertex_v = parent [current_vertex_v ]
156168
157169 @staticmethod
158- def contract_blossom (blossom_data : ' BlossomData' ) -> None :
170+ def contract_blossom (blossom_data : " BlossomData" ) -> None :
159171 """
160172 Contracts a blossom in the graph, modifying the base array
161173 and marking the vertices involved.
@@ -171,7 +183,9 @@ def contract_blossom(blossom_data: 'BlossomData') -> None:
171183 current_vertex_u = blossom_data .u
172184 while blossom_data .aux_data .base [current_vertex_u ] != blossom_data .lca :
173185 base_u = blossom_data .aux_data .base [current_vertex_u ]
174- match_base_u = blossom_data .aux_data .base [blossom_data .aux_data .match [current_vertex_u ]]
186+ match_base_u = blossom_data .aux_data .base [
187+ blossom_data .aux_data .match [current_vertex_u ]
188+ ]
175189 blossom_data .aux_data .in_blossom [base_u ] = True
176190 blossom_data .aux_data .in_blossom [match_base_u ] = True
177191 current_vertex_u = blossom_data .aux_data .parent [
@@ -181,7 +195,9 @@ def contract_blossom(blossom_data: 'BlossomData') -> None:
181195 current_vertex_v = blossom_data .v
182196 while blossom_data .aux_data .base [current_vertex_v ] != blossom_data .lca :
183197 base_v = blossom_data .aux_data .base [current_vertex_v ]
184- match_base_v = blossom_data .aux_data .base [blossom_data .aux_data .match [current_vertex_v ]]
198+ match_base_v = blossom_data .aux_data .base [
199+ blossom_data .aux_data .match [current_vertex_v ]
200+ ]
185201 blossom_data .aux_data .in_blossom [base_v ] = True
186202 blossom_data .aux_data .in_blossom [match_base_v ] = True
187203 current_vertex_v = blossom_data .aux_data .parent [
@@ -203,8 +219,13 @@ class BlossomAuxData:
203219 """
204220
205221 def __init__ (
206- self , queue : deque , parent : list [int ], base : list [int ], in_blossom : list [bool ],
207- match : list [int ], in_queue : list [bool ]
222+ self ,
223+ queue : deque ,
224+ parent : List [int ],
225+ base : List [int ],
226+ in_blossom : List [bool ],
227+ match : List [int ],
228+ in_queue : List [bool ],
208229 ) -> None :
209230 self .queue = queue
210231 self .parent = parent
0 commit comments