1
1
from collections import deque , defaultdict
2
- from typing import List , Tuple , Dict
3
-
4
2
5
3
UNMATCHED = - 1 # Constant to represent unmatched vertices
6
4
7
5
8
6
class EdmondsBlossomAlgorithm :
9
7
@staticmethod
10
- def maximum_matching (
11
- edges : List [Tuple [int , int ]], vertex_count : int
12
- ) -> List [Tuple [int , int ]]:
8
+ def maximum_matching (edges : list [tuple [int , int ]], vertex_count : int ) -> list [tuple [int , int ]]:
13
9
"""
14
10
Finds the maximum matching in a general graph using Edmonds' Blossom Algorithm.
15
11
@@ -20,7 +16,7 @@ def maximum_matching(
20
16
>>> EdmondsBlossomAlgorithm.maximum_matching([(0, 1), (1, 2), (2, 3)], 4)
21
17
[(0, 1), (2, 3)]
22
18
"""
23
- graph : Dict [int , List [int ]] = defaultdict (list )
19
+ graph : dict [int , list [int ]] = defaultdict (list )
24
20
25
21
# Populate the graph with the edges
26
22
for vertex_u , vertex_v in edges :
@@ -84,16 +80,10 @@ def maximum_matching(
84
80
EdmondsBlossomAlgorithm .contract_blossom (
85
81
BlossomData (
86
82
BlossomAuxData (
87
- queue ,
88
- parent ,
89
- base ,
90
- in_blossom ,
91
- match ,
92
- in_queue ,
83
+ queue , parent , base , in_blossom ,
84
+ match , in_queue
93
85
),
94
- current_vertex ,
95
- neighbor ,
96
- base_vertex ,
86
+ current_vertex , neighbor , base_vertex
97
87
)
98
88
)
99
89
@@ -106,9 +96,7 @@ def maximum_matching(
106
96
return matching_result
107
97
108
98
@staticmethod
109
- def update_matching (
110
- match : List [int ], parent : List [int ], current_vertex : int
111
- ) -> None :
99
+ def update_matching (match : list [int ], parent : list [int ], current_vertex : int ) -> None :
112
100
"""
113
101
Updates the matching along the augmenting path found.
114
102
@@ -131,7 +119,7 @@ def update_matching(
131
119
132
120
@staticmethod
133
121
def find_base (
134
- base : List [int ], parent : List [int ], vertex_u : int , vertex_v : int
122
+ base : list [int ], parent : list [int ], vertex_u : int , vertex_v : int
135
123
) -> int :
136
124
"""
137
125
Finds the base of a node in the blossom.
@@ -167,7 +155,7 @@ def find_base(
167
155
current_vertex_v = parent [current_vertex_v ]
168
156
169
157
@staticmethod
170
- def contract_blossom (blossom_data : " BlossomData" ) -> None :
158
+ def contract_blossom (blossom_data : ' BlossomData' ) -> None :
171
159
"""
172
160
Contracts a blossom in the graph, modifying the base array
173
161
and marking the vertices involved.
@@ -183,9 +171,7 @@ def contract_blossom(blossom_data: "BlossomData") -> None:
183
171
current_vertex_u = blossom_data .u
184
172
while blossom_data .aux_data .base [current_vertex_u ] != blossom_data .lca :
185
173
base_u = blossom_data .aux_data .base [current_vertex_u ]
186
- match_base_u = blossom_data .aux_data .base [
187
- blossom_data .aux_data .match [current_vertex_u ]
188
- ]
174
+ match_base_u = blossom_data .aux_data .base [blossom_data .aux_data .match [current_vertex_u ]]
189
175
blossom_data .aux_data .in_blossom [base_u ] = True
190
176
blossom_data .aux_data .in_blossom [match_base_u ] = True
191
177
current_vertex_u = blossom_data .aux_data .parent [
@@ -195,9 +181,7 @@ def contract_blossom(blossom_data: "BlossomData") -> None:
195
181
current_vertex_v = blossom_data .v
196
182
while blossom_data .aux_data .base [current_vertex_v ] != blossom_data .lca :
197
183
base_v = blossom_data .aux_data .base [current_vertex_v ]
198
- match_base_v = blossom_data .aux_data .base [
199
- blossom_data .aux_data .match [current_vertex_v ]
200
- ]
184
+ match_base_v = blossom_data .aux_data .base [blossom_data .aux_data .match [current_vertex_v ]]
201
185
blossom_data .aux_data .in_blossom [base_v ] = True
202
186
blossom_data .aux_data .in_blossom [match_base_v ] = True
203
187
current_vertex_v = blossom_data .aux_data .parent [
@@ -219,13 +203,8 @@ class BlossomAuxData:
219
203
"""
220
204
221
205
def __init__ (
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 ],
206
+ self , queue : deque , parent : list [int ], base : list [int ], in_blossom : list [bool ],
207
+ match : list [int ], in_queue : list [bool ]
229
208
) -> None :
230
209
self .queue = queue
231
210
self .parent = parent
0 commit comments