1- from collections import deque , defaultdict
1+ from collections import defaultdict , deque
22
33UNMATCHED = - 1 # Constant to represent unmatched vertices
44
55
66class EdmondsBlossomAlgorithm :
77 @staticmethod
8- def maximum_matching (edges : list [tuple [int , int ]], vertex_count : int ) -> list [tuple [int , int ]]:
8+ def maximum_matching (
9+ edges : list [tuple [int , int ]], vertex_count : int
10+ ) -> list [tuple [int , int ]]:
911 """
1012 Finds the maximum matching in a general graph using Edmonds' Blossom Algorithm.
1113
@@ -55,7 +57,8 @@ def maximum_matching(edges: list[tuple[int, int]], vertex_count: int) -> list[tu
5557 continue # Avoid self-loops
5658
5759 if parent [neighbor ] == UNMATCHED :
58- # Case 1: neighbor is unmatched, we've found an augmenting path
60+ # Case 1: neighbor is unmatched, we've found
61+ # an augmenting path
5962 if match [neighbor ] == UNMATCHED :
6063 parent [neighbor ] = current_vertex
6164 augmenting_path_found = True
@@ -64,15 +67,17 @@ def maximum_matching(edges: list[tuple[int, int]], vertex_count: int) -> list[tu
6467 )
6568 break
6669
67- # Case 2: neighbor is matched, add neighbor's match to the queue
70+ # Case 2: neighbor is matched, add neighbor's
71+ # match to the queue
6872 matched_vertex = match [neighbor ]
6973 parent [neighbor ] = current_vertex
7074 parent [matched_vertex ] = neighbor
7175 if not in_queue [matched_vertex ]:
7276 queue .append (matched_vertex )
7377 in_queue [matched_vertex ] = True
7478 else :
75- # Case 3: Both current_vertex and neighbor have a parent; check for a cycle/blossom
79+ # Case 3: Both current_vertex and neighbor
80+ # have a parent; check for a cycle/blossom
7681 base_vertex = EdmondsBlossomAlgorithm .find_base (
7782 base , parent , current_vertex , neighbor
7883 )
@@ -96,7 +101,9 @@ def maximum_matching(edges: list[tuple[int, int]], vertex_count: int) -> list[tu
96101 return matching_result
97102
98103 @staticmethod
99- def update_matching (match : list [int ], parent : list [int ], current_vertex : int ) -> None :
104+ def update_matching (
105+ match : list [int ], parent : list [int ], current_vertex : int
106+ ) -> None :
100107 """
101108 Updates the matching along the augmenting path found.
102109
@@ -171,7 +178,9 @@ def contract_blossom(blossom_data: 'BlossomData') -> None:
171178 current_vertex_u = blossom_data .u
172179 while blossom_data .aux_data .base [current_vertex_u ] != blossom_data .lca :
173180 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 ]]
181+ match_base_u = blossom_data .aux_data .base [
182+ blossom_data .aux_data .match [current_vertex_u ]
183+ ]
175184 blossom_data .aux_data .in_blossom [base_u ] = True
176185 blossom_data .aux_data .in_blossom [match_base_u ] = True
177186 current_vertex_u = blossom_data .aux_data .parent [
@@ -181,7 +190,9 @@ def contract_blossom(blossom_data: 'BlossomData') -> None:
181190 current_vertex_v = blossom_data .v
182191 while blossom_data .aux_data .base [current_vertex_v ] != blossom_data .lca :
183192 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 ]]
193+ match_base_v = blossom_data .aux_data .base [
194+ blossom_data .aux_data .match [current_vertex_v ]
195+ ]
185196 blossom_data .aux_data .in_blossom [base_v ] = True
186197 blossom_data .aux_data .in_blossom [match_base_v ] = True
187198 current_vertex_v = blossom_data .aux_data .parent [
0 commit comments