5
5
6
6
class EdmondsBlossomAlgorithm :
7
7
@staticmethod
8
- def maximum_matching (edges : list [tuple [int , int ]], vertex_count : int ) \
9
- -> list [tuple [int , int ]]:
8
+ def maximum_matching (
9
+ edges : list [tuple [int , int ]], vertex_count : int
10
+ ) -> list [tuple [int , int ]]:
10
11
"""
11
12
Finds the maximum matching in a general graph using Edmonds' Blossom Algorithm.
12
13
@@ -84,10 +85,16 @@ def maximum_matching(edges: list[tuple[int, int]], vertex_count: int) \
84
85
EdmondsBlossomAlgorithm .contract_blossom (
85
86
BlossomData (
86
87
BlossomAuxData (
87
- queue , parent , base , in_blossom ,
88
- match , in_queue
88
+ queue ,
89
+ parent ,
90
+ base ,
91
+ in_blossom ,
92
+ match ,
93
+ in_queue ,
89
94
),
90
- current_vertex , neighbor , base_vertex
95
+ current_vertex ,
96
+ neighbor ,
97
+ base_vertex ,
91
98
)
92
99
)
93
100
@@ -100,8 +107,9 @@ def maximum_matching(edges: list[tuple[int, int]], vertex_count: int) \
100
107
return matching_result
101
108
102
109
@staticmethod
103
- def update_matching (match : list [int ],
104
- parent : list [int ], current_vertex : int ) -> None :
110
+ def update_matching (
111
+ match : list [int ], parent : list [int ], current_vertex : int
112
+ ) -> None :
105
113
"""
106
114
Updates the matching along the augmenting path found.
107
115
@@ -160,7 +168,7 @@ def find_base(
160
168
current_vertex_v = parent [current_vertex_v ]
161
169
162
170
@staticmethod
163
- def contract_blossom (blossom_data : ' BlossomData' ) -> None :
171
+ def contract_blossom (blossom_data : " BlossomData" ) -> None :
164
172
"""
165
173
Contracts a blossom in the graph, modifying the base array
166
174
and marking the vertices involved.
@@ -176,9 +184,9 @@ def contract_blossom(blossom_data: 'BlossomData') -> None:
176
184
current_vertex_u = blossom_data .vertex_u
177
185
while blossom_data .aux_data .base [current_vertex_u ] != blossom_data .lca :
178
186
base_u = blossom_data .aux_data .base [current_vertex_u ]
179
- match_base_u = blossom_data .aux_data .base [blossom_data . aux_data . match
180
- [current_vertex_u ]
181
- ]
187
+ match_base_u = blossom_data .aux_data .base [
188
+ blossom_data . aux_data . match [current_vertex_u ]
189
+ ]
182
190
blossom_data .aux_data .in_blossom [base_u ] = True
183
191
blossom_data .aux_data .in_blossom [match_base_u ] = True
184
192
current_vertex_u = blossom_data .aux_data .parent [
@@ -188,9 +196,9 @@ def contract_blossom(blossom_data: 'BlossomData') -> None:
188
196
current_vertex_v = blossom_data .vertex_v
189
197
while blossom_data .aux_data .base [current_vertex_v ] != blossom_data .lca :
190
198
base_v = blossom_data .aux_data .base [current_vertex_v ]
191
- match_base_v = blossom_data .aux_data .base [blossom_data . aux_data . match
192
- [current_vertex_v ]
193
- ]
199
+ match_base_v = blossom_data .aux_data .base [
200
+ blossom_data . aux_data . match [current_vertex_v ]
201
+ ]
194
202
blossom_data .aux_data .in_blossom [base_v ] = True
195
203
blossom_data .aux_data .in_blossom [match_base_v ] = True
196
204
current_vertex_v = blossom_data .aux_data .parent [
@@ -212,8 +220,13 @@ class BlossomAuxData:
212
220
"""
213
221
214
222
def __init__ (
215
- self , queue : deque , parent : list [int ], base : list [int ], in_blossom : list [bool ],
216
- match : list [int ], in_queue : list [bool ]
223
+ self ,
224
+ queue : deque ,
225
+ parent : list [int ],
226
+ base : list [int ],
227
+ in_blossom : list [bool ],
228
+ match : list [int ],
229
+ in_queue : list [bool ],
217
230
) -> None :
218
231
self .queue = queue
219
232
self .parent = parent
@@ -243,3 +256,4 @@ def __init__(self, aux_data: BlossomAuxData, vertex_u: int,
243
256
self .vertex_u = vertex_u
244
257
self .vertex_v = vertex_v
245
258
self .lca = lca
259
+
0 commit comments