Skip to content

Commit b60e18e

Browse files
Corrected ruff checks
1 parent 6918d4c commit b60e18e

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

graphs/edmonds_blossom_algorithm.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from collections import deque, defaultdict
2-
from typing import List, Tuple, Dict
3-
42

53
UNMATCHED = -1 # Constant to represent unmatched vertices
64

75

86
class EdmondsBlossomAlgorithm:
97
@staticmethod
10-
def maximum_matching(edges: List[Tuple[int, int]], vertex_count: int) -> List[Tuple[int, int]]:
8+
def maximum_matching(edges: list[tuple[int, int]], vertex_count: int) -> list[tuple[int, int]]:
119
"""
1210
Finds the maximum matching in a general graph using Edmonds' Blossom Algorithm.
1311
@@ -18,7 +16,7 @@ def maximum_matching(edges: List[Tuple[int, int]], vertex_count: int) -> List[Tu
1816
>>> EdmondsBlossomAlgorithm.maximum_matching([(0, 1), (1, 2), (2, 3)], 4)
1917
[(0, 1), (2, 3)]
2018
"""
21-
graph: Dict[int, List[int]] = defaultdict(list)
19+
graph: dict[int, list[int]] = defaultdict(list)
2220

2321
# Populate the graph with the edges
2422
for vertex_u, vertex_v in edges:
@@ -61,7 +59,9 @@ def maximum_matching(edges: List[Tuple[int, int]], vertex_count: int) -> List[Tu
6159
if match[neighbor] == UNMATCHED:
6260
parent[neighbor] = current_vertex
6361
augmenting_path_found = True
64-
EdmondsBlossomAlgorithm.update_matching(match, parent, neighbor)
62+
EdmondsBlossomAlgorithm.update_matching(
63+
match, parent, neighbor
64+
)
6565
break
6666

6767
# Case 2: neighbor is matched, add neighbor's match to the queue
@@ -73,12 +73,19 @@ def maximum_matching(edges: List[Tuple[int, int]], vertex_count: int) -> List[Tu
7373
in_queue[matched_vertex] = True
7474
else:
7575
# 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)
76+
base_vertex = EdmondsBlossomAlgorithm.find_base(
77+
base, parent, current_vertex, neighbor
78+
)
7779
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-
))
80+
EdmondsBlossomAlgorithm.contract_blossom(
81+
BlossomData(
82+
BlossomAuxData(
83+
queue, parent, base, in_blossom,
84+
match, in_queue
85+
),
86+
current_vertex, neighbor, base_vertex
87+
)
88+
)
8289

8390
# Create result list of matched pairs
8491
matching_result = []
@@ -89,7 +96,7 @@ def maximum_matching(edges: List[Tuple[int, int]], vertex_count: int) -> List[Tu
8996
return matching_result
9097

9198
@staticmethod
92-
def update_matching(match: List[int], parent: List[int], current_vertex: int) -> None:
99+
def update_matching(match: list[int], parent: list[int], current_vertex: int) -> None:
93100
"""
94101
Updates the matching along the augmenting path found.
95102
@@ -111,7 +118,9 @@ def update_matching(match: List[int], parent: List[int], current_vertex: int) ->
111118
current_vertex = next_vertex
112119

113120
@staticmethod
114-
def find_base(base: List[int], parent: List[int], vertex_u: int, vertex_v: int) -> int:
121+
def find_base(
122+
base: list[int], parent: list[int], vertex_u: int, vertex_v: int
123+
) -> int:
115124
"""
116125
Finds the base of a node in the blossom.
117126
@@ -165,15 +174,19 @@ def contract_blossom(blossom_data: 'BlossomData') -> None:
165174
match_base_u = blossom_data.aux_data.base[blossom_data.aux_data.match[current_vertex_u]]
166175
blossom_data.aux_data.in_blossom[base_u] = True
167176
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]]
177+
current_vertex_u = blossom_data.aux_data.parent[
178+
blossom_data.aux_data.match[current_vertex_u]
179+
]
169180

170181
current_vertex_v = blossom_data.v
171182
while blossom_data.aux_data.base[current_vertex_v] != blossom_data.lca:
172183
base_v = blossom_data.aux_data.base[current_vertex_v]
173184
match_base_v = blossom_data.aux_data.base[blossom_data.aux_data.match[current_vertex_v]]
174185
blossom_data.aux_data.in_blossom[base_v] = True
175186
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]]
187+
current_vertex_v = blossom_data.aux_data.parent[
188+
blossom_data.aux_data.match[current_vertex_v]
189+
]
177190

178191
# Update the base for all marked vertices
179192
for i in range(len(blossom_data.aux_data.base)):
@@ -189,8 +202,10 @@ class BlossomAuxData:
189202
Auxiliary data class to encapsulate common parameters for the blossom operations.
190203
"""
191204

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:
205+
def __init__(
206+
self, queue: deque, parent: list[int], base: list[int], in_blossom: list[bool],
207+
match: list[int], in_queue: list[bool]
208+
) -> None:
194209
self.queue = queue
195210
self.parent = parent
196211
self.base = base

0 commit comments

Comments
 (0)