mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-02 17:31:08 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
ebb8055450
commit
17506709f6
|
@ -4,9 +4,15 @@ from collections import deque
|
||||||
class BlossomAuxData:
|
class BlossomAuxData:
|
||||||
"""Class to hold auxiliary data during the blossom algorithm's execution."""
|
"""Class to hold auxiliary data during the blossom algorithm's execution."""
|
||||||
|
|
||||||
def __init__(self, queue: deque, parent: list[int], base: list[int],
|
def __init__(
|
||||||
in_blossom: list[bool],
|
self,
|
||||||
match: list[int], in_queue: list[bool]) -> None:
|
queue: deque,
|
||||||
|
parent: list[int],
|
||||||
|
base: list[int],
|
||||||
|
in_blossom: list[bool],
|
||||||
|
match: list[int],
|
||||||
|
in_queue: list[bool],
|
||||||
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Initializes the BlossomAuxData instance.
|
Initializes the BlossomAuxData instance.
|
||||||
|
|
||||||
|
@ -29,9 +35,13 @@ class BlossomAuxData:
|
||||||
class BlossomData:
|
class BlossomData:
|
||||||
"""Class to encapsulate data related to a blossom in the graph."""
|
"""Class to encapsulate data related to a blossom in the graph."""
|
||||||
|
|
||||||
def __init__(self, aux_data: BlossomAuxData,
|
def __init__(
|
||||||
vertex_u: int, vertex_v: int,
|
self,
|
||||||
lowest_common_ancestor: int) -> None:
|
aux_data: BlossomAuxData,
|
||||||
|
vertex_u: int,
|
||||||
|
vertex_v: int,
|
||||||
|
lowest_common_ancestor: int,
|
||||||
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Initializes the BlossomData instance.
|
Initializes the BlossomData instance.
|
||||||
|
|
||||||
|
@ -152,16 +162,17 @@ class EdmondsBlossomAlgorithm:
|
||||||
# Create result list of matched pairs
|
# Create result list of matched pairs
|
||||||
matching_result: list[list[int]] = []
|
matching_result: list[list[int]] = []
|
||||||
for v in range(vertex_count):
|
for v in range(vertex_count):
|
||||||
if (match[v] != EdmondsBlossomAlgorithm.UNMATCHED
|
if (
|
||||||
and v < match[v]): # Ensure pairs are unique
|
match[v] != EdmondsBlossomAlgorithm.UNMATCHED and v < match[v]
|
||||||
|
): # Ensure pairs are unique
|
||||||
matching_result.append([v, match[v]])
|
matching_result.append([v, match[v]])
|
||||||
|
|
||||||
return matching_result
|
return matching_result
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_matching(match: list[int],
|
def update_matching(
|
||||||
parent: list[int],
|
match: list[int], parent: list[int], matched_vertex: int
|
||||||
matched_vertex: int) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Updates the matching based on the augmenting path found.
|
Updates the matching based on the augmenting path found.
|
||||||
|
|
||||||
|
@ -178,9 +189,9 @@ class EdmondsBlossomAlgorithm:
|
||||||
matched_vertex = next_match # Move to the next vertex
|
matched_vertex = next_match # Move to the next vertex
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def find_base(base: list[int],
|
def find_base(
|
||||||
parent: list[int],
|
base: list[int], parent: list[int], vertex_u: int, vertex_v: int
|
||||||
vertex_u: int, vertex_v: int) -> int:
|
) -> int:
|
||||||
"""
|
"""
|
||||||
Finds the base of the blossom.
|
Finds the base of the blossom.
|
||||||
|
|
||||||
|
@ -223,18 +234,22 @@ class EdmondsBlossomAlgorithm:
|
||||||
blossom_data: The data related to the blossom to be contracted.
|
blossom_data: The data related to the blossom to be contracted.
|
||||||
"""
|
"""
|
||||||
# Mark vertices in the blossom
|
# Mark vertices in the blossom
|
||||||
for x in range(blossom_data.vertex_u,
|
for x in range(
|
||||||
blossom_data.aux_data.base
|
blossom_data.vertex_u,
|
||||||
[blossom_data.vertex_u] != blossom_data.lowest_common_ancestor):
|
blossom_data.aux_data.base[blossom_data.vertex_u]
|
||||||
|
!= blossom_data.lowest_common_ancestor,
|
||||||
|
):
|
||||||
base_x = blossom_data.aux_data.base[x]
|
base_x = blossom_data.aux_data.base[x]
|
||||||
match_base_x = blossom_data.aux_data.base[blossom_data.aux_data.match[x]]
|
match_base_x = blossom_data.aux_data.base[blossom_data.aux_data.match[x]]
|
||||||
# Mark the base as in a blossom
|
# Mark the base as in a blossom
|
||||||
blossom_data.aux_data.in_blossom[base_x] = True
|
blossom_data.aux_data.in_blossom[base_x] = True
|
||||||
blossom_data.aux_data.in_blossom[match_base_x] = True
|
blossom_data.aux_data.in_blossom[match_base_x] = True
|
||||||
|
|
||||||
for x in range(blossom_data.vertex_v,
|
for x in range(
|
||||||
blossom_data.aux_data.base
|
blossom_data.vertex_v,
|
||||||
[blossom_data.vertex_v] != blossom_data.lowest_common_ancestor):
|
blossom_data.aux_data.base[blossom_data.vertex_v]
|
||||||
|
!= blossom_data.lowest_common_ancestor,
|
||||||
|
):
|
||||||
base_x = blossom_data.aux_data.base[x]
|
base_x = blossom_data.aux_data.base[x]
|
||||||
match_base_x = blossom_data.aux_data.base[blossom_data.aux_data.match[x]]
|
match_base_x = blossom_data.aux_data.base[blossom_data.aux_data.match[x]]
|
||||||
# Mark the base as in a blossom
|
# Mark the base as in a blossom
|
||||||
|
|
Loading…
Reference in New Issue
Block a user