Skip to content

Commit 2005e61

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

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

graphs/edmonds_karp_multiple_source_and_sink.py

+52-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
class EdmondsBlossomAlgorithm:
99
@staticmethod
10-
def maximum_matching(edges: List[Tuple[int, int]], vertex_count: int) -> List[Tuple[int, int]]:
10+
def maximum_matching(
11+
edges: List[Tuple[int, int]], vertex_count: int
12+
) -> List[Tuple[int, int]]:
1113
"""
1214
Finds the maximum matching in a general graph using Edmonds' Blossom Algorithm.
1315
@@ -61,7 +63,9 @@ def maximum_matching(edges: List[Tuple[int, int]], vertex_count: int) -> List[Tu
6163
if match[neighbor] == UNMATCHED:
6264
parent[neighbor] = current_vertex
6365
augmenting_path_found = True
64-
EdmondsBlossomAlgorithm.update_matching(match, parent, neighbor)
66+
EdmondsBlossomAlgorithm.update_matching(
67+
match, parent, neighbor
68+
)
6569
break
6670

6771
# Case 2: neighbor is matched, add neighbor's match to the queue
@@ -73,12 +77,25 @@ def maximum_matching(edges: List[Tuple[int, int]], vertex_count: int) -> List[Tu
7377
in_queue[matched_vertex] = True
7478
else:
7579
# Case 3: Both current_vertex and neighbor have a parent; check for a cycle/blossom
76-
base_vertex = EdmondsBlossomAlgorithm.find_base(base, parent, current_vertex, neighbor)
80+
base_vertex = EdmondsBlossomAlgorithm.find_base(
81+
base, parent, current_vertex, neighbor
82+
)
7783
if base_vertex != UNMATCHED:
78-
EdmondsBlossomAlgorithm.contract_blossom(BlossomData(
79-
BlossomAuxData(queue, parent, base, in_blossom, match, in_queue),
80-
current_vertex, neighbor, base_vertex
81-
))
84+
EdmondsBlossomAlgorithm.contract_blossom(
85+
BlossomData(
86+
BlossomAuxData(
87+
queue,
88+
parent,
89+
base,
90+
in_blossom,
91+
match,
92+
in_queue,
93+
),
94+
current_vertex,
95+
neighbor,
96+
base_vertex,
97+
)
98+
)
8299

83100
# Create result list of matched pairs
84101
matching_result = []
@@ -89,7 +106,9 @@ def maximum_matching(edges: List[Tuple[int, int]], vertex_count: int) -> List[Tu
89106
return matching_result
90107

91108
@staticmethod
92-
def update_matching(match: List[int], parent: List[int], current_vertex: int) -> None:
109+
def update_matching(
110+
match: List[int], parent: List[int], current_vertex: int
111+
) -> None:
93112
"""
94113
Updates the matching along the augmenting path found.
95114
@@ -111,7 +130,9 @@ def update_matching(match: List[int], parent: List[int], current_vertex: int) ->
111130
current_vertex = next_vertex
112131

113132
@staticmethod
114-
def find_base(base: List[int], parent: List[int], vertex_u: int, vertex_v: int) -> int:
133+
def find_base(
134+
base: List[int], parent: List[int], vertex_u: int, vertex_v: int
135+
) -> int:
115136
"""
116137
Finds the base of a node in the blossom.
117138
@@ -146,7 +167,7 @@ def find_base(base: List[int], parent: List[int], vertex_u: int, vertex_v: int)
146167
current_vertex_v = parent[current_vertex_v]
147168

148169
@staticmethod
149-
def contract_blossom(blossom_data: 'BlossomData') -> None:
170+
def contract_blossom(blossom_data: "BlossomData") -> None:
150171
"""
151172
Contracts a blossom in the graph, modifying the base array
152173
and marking the vertices involved.
@@ -162,18 +183,26 @@ def contract_blossom(blossom_data: 'BlossomData') -> None:
162183
current_vertex_u = blossom_data.u
163184
while blossom_data.aux_data.base[current_vertex_u] != blossom_data.lca:
164185
base_u = blossom_data.aux_data.base[current_vertex_u]
165-
match_base_u = blossom_data.aux_data.base[blossom_data.aux_data.match[current_vertex_u]]
186+
match_base_u = blossom_data.aux_data.base[
187+
blossom_data.aux_data.match[current_vertex_u]
188+
]
166189
blossom_data.aux_data.in_blossom[base_u] = True
167190
blossom_data.aux_data.in_blossom[match_base_u] = True
168-
current_vertex_u = blossom_data.aux_data.parent[blossom_data.aux_data.match[current_vertex_u]]
191+
current_vertex_u = blossom_data.aux_data.parent[
192+
blossom_data.aux_data.match[current_vertex_u]
193+
]
169194

170195
current_vertex_v = blossom_data.v
171196
while blossom_data.aux_data.base[current_vertex_v] != blossom_data.lca:
172197
base_v = blossom_data.aux_data.base[current_vertex_v]
173-
match_base_v = blossom_data.aux_data.base[blossom_data.aux_data.match[current_vertex_v]]
198+
match_base_v = blossom_data.aux_data.base[
199+
blossom_data.aux_data.match[current_vertex_v]
200+
]
174201
blossom_data.aux_data.in_blossom[base_v] = True
175202
blossom_data.aux_data.in_blossom[match_base_v] = True
176-
current_vertex_v = blossom_data.aux_data.parent[blossom_data.aux_data.match[current_vertex_v]]
203+
current_vertex_v = blossom_data.aux_data.parent[
204+
blossom_data.aux_data.match[current_vertex_v]
205+
]
177206

178207
# Update the base for all marked vertices
179208
for i in range(len(blossom_data.aux_data.base)):
@@ -189,8 +218,15 @@ class BlossomAuxData:
189218
Auxiliary data class to encapsulate common parameters for the blossom operations.
190219
"""
191220

192-
def __init__(self, queue: deque, parent: List[int], base: List[int],
193-
in_blossom: List[bool], match: List[int], in_queue: List[bool]) -> None:
221+
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],
229+
) -> None:
194230
self.queue = queue
195231
self.parent = parent
196232
self.base = base

0 commit comments

Comments
 (0)