Skip to content

Commit d286529

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 991a37e commit d286529

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

graphs/edmonds_blossom_algorithm.py

+42-18
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,31 @@
22

33

44
class BlossomAuxData:
5-
def __init__(self, queue: deque, parent: list[int], base: list[int],
6-
in_blossom: list[bool], match: list[int], in_queue: list[bool]):
5+
def __init__(
6+
self,
7+
queue: deque,
8+
parent: list[int],
9+
base: list[int],
10+
in_blossom: list[bool],
11+
match: list[int],
12+
in_queue: list[bool],
13+
):
714
self.queue = queue
815
self.parent = parent
916
self.base = base
1017
self.in_blossom = in_blossom
1118
self.match = match
1219
self.in_queue = in_queue
1320

21+
1422
class BlossomData:
1523
def __init__(self, aux_data: BlossomAuxData, u: int, v: int, lca: int):
1624
self.aux_data = aux_data
1725
self.u = u
1826
self.v = v
1927
self.lca = lca
2028

29+
2130
class EdmondsBlossomAlgorithm:
2231
UNMATCHED = -1 # Constant to represent unmatched vertices
2332

@@ -72,8 +81,11 @@ def maximum_matching(edges: list[list[int]], vertex_count: int) -> list[list[int
7281
parent[y] = current
7382
augmenting_path_found = True
7483
# Augment along this path
75-
(EdmondsBlossomAlgorithm
76-
.update_matching(match, parent, y))
84+
(
85+
EdmondsBlossomAlgorithm.update_matching(
86+
match, parent, y
87+
)
88+
)
7789
break
7890

7991
# Case 2: y is matched, add y's match to the queue
@@ -86,17 +98,25 @@ def maximum_matching(edges: list[list[int]], vertex_count: int) -> list[list[int
8698
else:
8799
# Case 3: Both current and y have a parent;
88100
# check for a cycle/blossom
89-
base_u = EdmondsBlossomAlgorithm.find_base(base,
90-
parent, current, y)
101+
base_u = EdmondsBlossomAlgorithm.find_base(
102+
base, parent, current, y
103+
)
91104
if base_u != EdmondsBlossomAlgorithm.UNMATCHED:
92-
EdmondsBlossomAlgorithm.contract_blossom(BlossomData(
93-
BlossomAuxData(queue,
94-
parent,
95-
base,
96-
in_blossom,
97-
match,
98-
in_queue),
99-
current, y, base_u))
105+
EdmondsBlossomAlgorithm.contract_blossom(
106+
BlossomData(
107+
BlossomAuxData(
108+
queue,
109+
parent,
110+
base,
111+
in_blossom,
112+
match,
113+
in_queue,
114+
),
115+
current,
116+
y,
117+
base_u,
118+
)
119+
)
100120

101121
# Create result list of matched pairs
102122
matching_result = []
@@ -138,15 +158,19 @@ def find_base(base: list[int], parent: list[int], u: int, v: int) -> int:
138158

139159
@staticmethod
140160
def contract_blossom(blossom_data: BlossomData):
141-
for x in range(blossom_data.u,
142-
blossom_data.aux_data.base[blossom_data.u] != blossom_data.lca):
161+
for x in range(
162+
blossom_data.u,
163+
blossom_data.aux_data.base[blossom_data.u] != blossom_data.lca,
164+
):
143165
base_x = blossom_data.aux_data.base[x]
144166
match_base_x = blossom_data.aux_data.base[blossom_data.aux_data.match[x]]
145167
blossom_data.aux_data.in_blossom[base_x] = True
146168
blossom_data.aux_data.in_blossom[match_base_x] = True
147169

148-
for x in range(blossom_data.v,
149-
blossom_data.aux_data.base[blossom_data.v] != blossom_data.lca):
170+
for x in range(
171+
blossom_data.v,
172+
blossom_data.aux_data.base[blossom_data.v] != blossom_data.lca,
173+
):
150174
base_x = blossom_data.aux_data.base[x]
151175
match_base_x = blossom_data.aux_data.base[blossom_data.aux_data.match[x]]
152176
blossom_data.aux_data.in_blossom[base_x] = True

graphs/tests/test_edmonds_blossom_algorithm.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55

66
class EdmondsBlossomAlgorithmTest(unittest.TestCase):
7-
87
def convert_matching_to_array(self, matching):
9-
""" Helper method to convert a
8+
"""Helper method to convert a
109
list of matching pairs into a sorted 2D array.
1110
"""
1211
# Convert the list of pairs into a list of lists
@@ -21,39 +20,39 @@ def convert_matching_to_array(self, matching):
2120
return result
2221

2322
def test_case_1(self):
24-
""" Test Case 1: A triangle graph where vertices 0, 1, and 2 form a cycle. """
23+
"""Test Case 1: A triangle graph where vertices 0, 1, and 2 form a cycle."""
2524
edges = [[0, 1], [1, 2], [2, 0]]
2625
matching = EdmondsBlossomAlgorithm.maximum_matching(edges, 3)
2726

2827
expected = [[0, 1]]
2928
assert expected == self.convert_matching_to_array(matching)
3029

3130
def test_case_2(self):
32-
""" Test Case 2: A disconnected graph with two components. """
31+
"""Test Case 2: A disconnected graph with two components."""
3332
edges = [[0, 1], [1, 2], [3, 4]]
3433
matching = EdmondsBlossomAlgorithm.maximum_matching(edges, 5)
3534

3635
expected = [[0, 1], [3, 4]]
3736
assert expected == self.convert_matching_to_array(matching)
3837

3938
def test_case_3(self):
40-
""" Test Case 3: A cycle graph with an additional edge outside the cycle. """
39+
"""Test Case 3: A cycle graph with an additional edge outside the cycle."""
4140
edges = [[0, 1], [1, 2], [2, 3], [3, 0], [4, 5]]
4241
matching = EdmondsBlossomAlgorithm.maximum_matching(edges, 6)
4342

4443
expected = [[0, 1], [2, 3], [4, 5]]
4544
assert expected == self.convert_matching_to_array(matching)
4645

4746
def test_case_no_matching(self):
48-
""" Test Case 4: A graph with no edges. """
47+
"""Test Case 4: A graph with no edges."""
4948
edges = [] # No edges
5049
matching = EdmondsBlossomAlgorithm.maximum_matching(edges, 3)
5150

5251
expected = []
5352
assert expected == self.convert_matching_to_array(matching)
5453

5554
def test_case_large_graph(self):
56-
""" Test Case 5: A complex graph with multiple cycles and extra edges. """
55+
"""Test Case 5: A complex graph with multiple cycles and extra edges."""
5756
edges = [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4], [2, 5]]
5857
matching = EdmondsBlossomAlgorithm.maximum_matching(edges, 6)
5958

@@ -69,5 +68,5 @@ def test_case_large_graph(self):
6968
assert result in (possible_matching_1, possible_matching_2)
7069

7170

72-
if __name__ == '__main__':
71+
if __name__ == "__main__":
7372
unittest.main()

0 commit comments

Comments
 (0)