Skip to content

Commit 936257d

Browse files
Merge remote-tracking branch 'origin/master'
# Conflicts: # graphs/edmonds_blossom_algorithm.py
2 parents 872469c + 9e1d5f3 commit 936257d

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

graphs/edmonds_blossom_algorithm.py

+23-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from collections import defaultdict, deque
1+
from collections import deque, defaultdict
22

33
UNMATCHED = -1 # Constant to represent unmatched vertices
44

55

66
class EdmondsBlossomAlgorithm:
77
@staticmethod
88
def maximum_matching(
9-
edges: list[tuple[int, int]], vertex_count: int
9+
edges: list[tuple[int, int]], vertex_count: int
1010
) -> list[tuple[int, int]]:
1111
"""
1212
Finds the maximum matching in a general graph using Edmonds' Blossom Algorithm.
@@ -57,8 +57,7 @@ def maximum_matching(
5757
continue # Avoid self-loops
5858

5959
if parent[neighbor] == UNMATCHED:
60-
# Case 1: neighbor is unmatched, we've found
61-
# an augmenting path
60+
# Case 1: neighbor is unmatched, we've found an augmenting path
6261
if match[neighbor] == UNMATCHED:
6362
parent[neighbor] = current_vertex
6463
augmenting_path_found = True
@@ -67,28 +66,32 @@ def maximum_matching(
6766
)
6867
break
6968

70-
# Case 2: neighbor is matched, add neighbor's
71-
# match to the queue
69+
# Case 2: neighbor is matched, add neighbor's match to the queue
7270
matched_vertex = match[neighbor]
7371
parent[neighbor] = current_vertex
7472
parent[matched_vertex] = neighbor
7573
if not in_queue[matched_vertex]:
7674
queue.append(matched_vertex)
7775
in_queue[matched_vertex] = True
7876
else:
79-
# Case 3: Both current_vertex and neighbor
80-
# have a parent; check for a cycle/blossom
77+
# Case 3: Both current_vertex and neighbor have a parent; check for a cycle/blossom
8178
base_vertex = EdmondsBlossomAlgorithm.find_base(
8279
base, parent, current_vertex, neighbor
8380
)
8481
if base_vertex != UNMATCHED:
8582
EdmondsBlossomAlgorithm.contract_blossom(
8683
BlossomData(
8784
BlossomAuxData(
88-
queue, parent, base, in_blossom,
89-
match, in_queue
85+
queue,
86+
parent,
87+
base,
88+
in_blossom,
89+
match,
90+
in_queue,
9091
),
91-
current_vertex, neighbor, base_vertex
92+
current_vertex,
93+
neighbor,
94+
base_vertex,
9295
)
9396
)
9497

@@ -102,7 +105,7 @@ def maximum_matching(
102105

103106
@staticmethod
104107
def update_matching(
105-
match: list[int], parent: list[int], current_vertex: int
108+
match: list[int], parent: list[int], current_vertex: int
106109
) -> None:
107110
"""
108111
Updates the matching along the augmenting path found.
@@ -162,7 +165,7 @@ def find_base(
162165
current_vertex_v = parent[current_vertex_v]
163166

164167
@staticmethod
165-
def contract_blossom(blossom_data: 'BlossomData') -> None:
168+
def contract_blossom(blossom_data: "BlossomData") -> None:
166169
"""
167170
Contracts a blossom in the graph, modifying the base array
168171
and marking the vertices involved.
@@ -214,8 +217,13 @@ class BlossomAuxData:
214217
"""
215218

216219
def __init__(
217-
self, queue: deque, parent: list[int], base: list[int], in_blossom: list[bool],
218-
match: list[int], in_queue: list[bool]
220+
self,
221+
queue: deque,
222+
parent: list[int],
223+
base: list[int],
224+
in_blossom: list[bool],
225+
match: list[int],
226+
in_queue: list[bool],
219227
) -> None:
220228
self.queue = queue
221229
self.parent = parent

0 commit comments

Comments
 (0)