Skip to content

Commit b4cf6eb

Browse files
Corrected ruff checks
1 parent a9a9d47 commit b4cf6eb

File tree

1 file changed

+12
-33
lines changed

1 file changed

+12
-33
lines changed

graphs/edmonds_blossom_algorithm.py

+12-33
Original file line numberDiff line numberDiff line change
@@ -1,15 +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(
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]]:
139
"""
1410
Finds the maximum matching in a general graph using Edmonds' Blossom Algorithm.
1511
@@ -20,7 +16,7 @@ def maximum_matching(
2016
>>> EdmondsBlossomAlgorithm.maximum_matching([(0, 1), (1, 2), (2, 3)], 4)
2117
[(0, 1), (2, 3)]
2218
"""
23-
graph: Dict[int, List[int]] = defaultdict(list)
19+
graph: dict[int, list[int]] = defaultdict(list)
2420

2521
# Populate the graph with the edges
2622
for vertex_u, vertex_v in edges:
@@ -84,16 +80,10 @@ def maximum_matching(
8480
EdmondsBlossomAlgorithm.contract_blossom(
8581
BlossomData(
8682
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
9385
),
94-
current_vertex,
95-
neighbor,
96-
base_vertex,
86+
current_vertex, neighbor, base_vertex
9787
)
9888
)
9989

@@ -106,9 +96,7 @@ def maximum_matching(
10696
return matching_result
10797

10898
@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:
112100
"""
113101
Updates the matching along the augmenting path found.
114102
@@ -131,7 +119,7 @@ def update_matching(
131119

132120
@staticmethod
133121
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
135123
) -> int:
136124
"""
137125
Finds the base of a node in the blossom.
@@ -167,7 +155,7 @@ def find_base(
167155
current_vertex_v = parent[current_vertex_v]
168156

169157
@staticmethod
170-
def contract_blossom(blossom_data: "BlossomData") -> None:
158+
def contract_blossom(blossom_data: 'BlossomData') -> None:
171159
"""
172160
Contracts a blossom in the graph, modifying the base array
173161
and marking the vertices involved.
@@ -183,9 +171,7 @@ def contract_blossom(blossom_data: "BlossomData") -> None:
183171
current_vertex_u = blossom_data.u
184172
while blossom_data.aux_data.base[current_vertex_u] != blossom_data.lca:
185173
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]]
189175
blossom_data.aux_data.in_blossom[base_u] = True
190176
blossom_data.aux_data.in_blossom[match_base_u] = True
191177
current_vertex_u = blossom_data.aux_data.parent[
@@ -195,9 +181,7 @@ def contract_blossom(blossom_data: "BlossomData") -> None:
195181
current_vertex_v = blossom_data.v
196182
while blossom_data.aux_data.base[current_vertex_v] != blossom_data.lca:
197183
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]]
201185
blossom_data.aux_data.in_blossom[base_v] = True
202186
blossom_data.aux_data.in_blossom[match_base_v] = True
203187
current_vertex_v = blossom_data.aux_data.parent[
@@ -219,13 +203,8 @@ class BlossomAuxData:
219203
"""
220204

221205
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]
229208
) -> None:
230209
self.queue = queue
231210
self.parent = parent

0 commit comments

Comments
 (0)