2024-10-14 15:31:01 +00:00
|
|
|
from collections import deque
|
2024-10-14 06:09:47 +00:00
|
|
|
|
|
|
|
|
2024-10-14 15:31:01 +00:00
|
|
|
class BlossomAuxData:
|
2024-10-14 15:42:19 +00:00
|
|
|
"""Class to hold auxiliary data during the blossom algorithm's execution."""
|
|
|
|
|
2024-10-14 15:43:53 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
queue: deque,
|
|
|
|
parent: list[int],
|
|
|
|
base: list[int],
|
|
|
|
in_blossom: list[bool],
|
|
|
|
match: list[int],
|
|
|
|
in_queue: list[bool],
|
|
|
|
):
|
2024-10-14 15:31:01 +00:00
|
|
|
self.queue = queue
|
|
|
|
self.parent = parent
|
|
|
|
self.base = base
|
|
|
|
self.in_blossom = in_blossom
|
|
|
|
self.match = match
|
|
|
|
self.in_queue = in_queue
|
2024-10-14 06:09:47 +00:00
|
|
|
|
2024-10-14 15:42:19 +00:00
|
|
|
|
2024-10-14 15:31:01 +00:00
|
|
|
class BlossomData:
|
2024-10-14 15:42:19 +00:00
|
|
|
"""Class to encapsulate data related to a blossom in the graph."""
|
|
|
|
|
2024-10-14 15:31:01 +00:00
|
|
|
def __init__(self, aux_data: BlossomAuxData, u: int, v: int, lca: int):
|
|
|
|
self.aux_data = aux_data
|
|
|
|
self.u = u
|
|
|
|
self.v = v
|
|
|
|
self.lca = lca
|
2024-10-14 06:09:47 +00:00
|
|
|
|
2024-10-14 15:42:19 +00:00
|
|
|
|
2024-10-14 15:31:01 +00:00
|
|
|
class EdmondsBlossomAlgorithm:
|
|
|
|
UNMATCHED = -1 # Constant to represent unmatched vertices
|
2024-10-14 06:09:47 +00:00
|
|
|
|
2024-10-14 15:31:01 +00:00
|
|
|
@staticmethod
|
|
|
|
def maximum_matching(edges: list[list[int]], vertex_count: int) -> list[list[int]]:
|
2024-10-14 15:42:19 +00:00
|
|
|
"""
|
|
|
|
Finds the maximum matching in a graph using the Edmonds Blossom Algorithm.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
edges: A list of edges represented as pairs of vertices.
|
|
|
|
vertex_count: The total number of vertices in the graph.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of matched pairs in the form of a list of lists.
|
|
|
|
"""
|
|
|
|
# Create an adjacency list for the graph
|
2024-10-14 15:31:01 +00:00
|
|
|
graph = [[] for _ in range(vertex_count)]
|
2024-10-14 06:09:47 +00:00
|
|
|
|
|
|
|
# Populate the graph with the edges
|
2024-10-14 15:31:01 +00:00
|
|
|
for edge in edges:
|
|
|
|
u, v = edge
|
|
|
|
graph[u].append(v)
|
|
|
|
graph[v].append(u)
|
|
|
|
|
|
|
|
# All vertices are initially unmatched
|
|
|
|
match = [EdmondsBlossomAlgorithm.UNMATCHED] * vertex_count
|
|
|
|
parent = [EdmondsBlossomAlgorithm.UNMATCHED] * vertex_count
|
|
|
|
base = list(range(vertex_count)) # Each vertex is its own base initially
|
2024-10-14 06:09:47 +00:00
|
|
|
in_blossom = [False] * vertex_count
|
2024-10-14 15:31:01 +00:00
|
|
|
in_queue = [False] * vertex_count # Tracks vertices in the BFS queue
|
2024-10-14 06:09:47 +00:00
|
|
|
|
|
|
|
# Main logic for finding maximum matching
|
2024-10-14 15:31:01 +00:00
|
|
|
for u in range(vertex_count):
|
2024-10-14 15:42:19 +00:00
|
|
|
# Only consider unmatched vertices
|
2024-10-14 15:31:01 +00:00
|
|
|
if match[u] == EdmondsBlossomAlgorithm.UNMATCHED:
|
2024-10-14 06:09:47 +00:00
|
|
|
# BFS initialization
|
2024-10-14 15:31:01 +00:00
|
|
|
parent = [EdmondsBlossomAlgorithm.UNMATCHED] * vertex_count
|
2024-10-14 06:09:47 +00:00
|
|
|
base = list(range(vertex_count))
|
|
|
|
in_blossom = [False] * vertex_count
|
|
|
|
in_queue = [False] * vertex_count
|
|
|
|
|
2024-10-14 15:42:19 +00:00
|
|
|
queue = deque([u]) # Start BFS from the unmatched vertex
|
2024-10-14 15:31:01 +00:00
|
|
|
in_queue[u] = True
|
2024-10-14 06:09:47 +00:00
|
|
|
|
|
|
|
augmenting_path_found = False
|
|
|
|
|
|
|
|
# BFS to find augmenting paths
|
|
|
|
while queue and not augmenting_path_found:
|
2024-10-14 15:42:19 +00:00
|
|
|
current = queue.popleft() # Get the current vertex
|
|
|
|
for y in graph[current]: # Explore adjacent vertices
|
|
|
|
# Skip if we're looking at the current match
|
2024-10-14 15:31:01 +00:00
|
|
|
if match[current] == y:
|
2024-10-14 06:09:47 +00:00
|
|
|
continue
|
|
|
|
|
2024-10-14 15:42:19 +00:00
|
|
|
if base[current] == base[y]: # Avoid self-loops
|
|
|
|
continue
|
2024-10-14 06:09:47 +00:00
|
|
|
|
2024-10-14 15:31:01 +00:00
|
|
|
if parent[y] == EdmondsBlossomAlgorithm.UNMATCHED:
|
2024-10-14 15:42:19 +00:00
|
|
|
# Case 1: y is unmatched; we've found an augmenting path
|
2024-10-14 15:31:01 +00:00
|
|
|
if match[y] == EdmondsBlossomAlgorithm.UNMATCHED:
|
2024-10-14 15:42:19 +00:00
|
|
|
parent[y] = current # Update the parent
|
2024-10-14 06:09:47 +00:00
|
|
|
augmenting_path_found = True
|
2024-10-14 15:31:01 +00:00
|
|
|
# Augment along this path
|
2024-10-14 15:43:53 +00:00
|
|
|
EdmondsBlossomAlgorithm.update_matching(
|
|
|
|
match, parent, y
|
|
|
|
)
|
2024-10-14 06:09:47 +00:00
|
|
|
break
|
|
|
|
|
2024-10-14 15:42:19 +00:00
|
|
|
# Case 2: y is matched; add y's match to the queue
|
2024-10-14 15:31:01 +00:00
|
|
|
z = match[y]
|
|
|
|
parent[y] = current
|
|
|
|
parent[z] = y
|
2024-10-14 15:42:19 +00:00
|
|
|
if not in_queue[z]: # If z is not already in the queue
|
2024-10-14 15:31:01 +00:00
|
|
|
queue.append(z)
|
|
|
|
in_queue[z] = True
|
2024-10-14 06:09:47 +00:00
|
|
|
else:
|
2024-10-14 15:31:01 +00:00
|
|
|
# Case 3: Both current and y have a parent;
|
2024-10-14 06:09:47 +00:00
|
|
|
# check for a cycle/blossom
|
2024-10-14 15:43:53 +00:00
|
|
|
base_u = EdmondsBlossomAlgorithm.find_base(
|
|
|
|
base, parent, current, y
|
|
|
|
)
|
2024-10-14 15:31:01 +00:00
|
|
|
if base_u != EdmondsBlossomAlgorithm.UNMATCHED:
|
2024-10-14 15:43:53 +00:00
|
|
|
EdmondsBlossomAlgorithm.contract_blossom(
|
|
|
|
BlossomData(
|
|
|
|
BlossomAuxData(
|
|
|
|
queue,
|
|
|
|
parent,
|
|
|
|
base,
|
|
|
|
in_blossom,
|
|
|
|
match,
|
|
|
|
in_queue,
|
|
|
|
),
|
|
|
|
current,
|
|
|
|
y,
|
|
|
|
base_u,
|
|
|
|
)
|
|
|
|
)
|
2024-10-14 06:09:47 +00:00
|
|
|
|
|
|
|
# Create result list of matched pairs
|
|
|
|
matching_result = []
|
2024-10-14 15:31:01 +00:00
|
|
|
for v in range(vertex_count):
|
2024-10-14 15:42:19 +00:00
|
|
|
# Ensure pairs are unique
|
2024-10-14 15:31:01 +00:00
|
|
|
if match[v] != EdmondsBlossomAlgorithm.UNMATCHED and v < match[v]:
|
|
|
|
matching_result.append([v, match[v]])
|
2024-10-14 06:09:47 +00:00
|
|
|
|
|
|
|
return matching_result
|
|
|
|
|
|
|
|
@staticmethod
|
2024-10-14 15:31:01 +00:00
|
|
|
def update_matching(match: list[int], parent: list[int], u: int):
|
2024-10-14 15:42:19 +00:00
|
|
|
"""
|
|
|
|
Updates the matching based on the augmenting path found.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
match: The current match list.
|
|
|
|
parent: The parent list from BFS traversal.
|
|
|
|
u: The vertex where the augmenting path ends.
|
|
|
|
"""
|
2024-10-14 15:31:01 +00:00
|
|
|
while u != EdmondsBlossomAlgorithm.UNMATCHED:
|
2024-10-14 15:42:19 +00:00
|
|
|
v = parent[u] # Get the parent vertex
|
|
|
|
next_match = match[v] # Store the next match
|
|
|
|
match[v] = u # Update match for v
|
|
|
|
match[u] = v # Update match for u
|
|
|
|
u = next_match # Move to the next vertex
|
2024-10-14 06:09:47 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2024-10-14 15:31:01 +00:00
|
|
|
def find_base(base: list[int], parent: list[int], u: int, v: int) -> int:
|
2024-10-14 15:42:19 +00:00
|
|
|
"""
|
|
|
|
Finds the base of the blossom.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
base: The base array for each vertex.
|
|
|
|
parent: The parent array from BFS.
|
|
|
|
u: One endpoint of the blossom.
|
|
|
|
v: The other endpoint of the blossom.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The lowest common ancestor of u and v in the blossom.
|
|
|
|
"""
|
2024-10-14 06:09:47 +00:00
|
|
|
visited = [False] * len(base)
|
|
|
|
|
2024-10-14 15:31:01 +00:00
|
|
|
# Mark ancestors of u
|
|
|
|
current_u = u
|
2024-10-14 06:09:47 +00:00
|
|
|
while True:
|
2024-10-14 15:31:01 +00:00
|
|
|
current_u = base[current_u]
|
2024-10-14 15:42:19 +00:00
|
|
|
visited[current_u] = True # Mark this base as visited
|
2024-10-14 15:31:01 +00:00
|
|
|
if parent[current_u] == EdmondsBlossomAlgorithm.UNMATCHED:
|
2024-10-14 06:09:47 +00:00
|
|
|
break
|
2024-10-14 15:31:01 +00:00
|
|
|
current_u = parent[current_u]
|
2024-10-14 06:09:47 +00:00
|
|
|
|
2024-10-14 15:31:01 +00:00
|
|
|
# Find the common ancestor of v
|
|
|
|
current_v = v
|
2024-10-14 06:09:47 +00:00
|
|
|
while True:
|
2024-10-14 15:31:01 +00:00
|
|
|
current_v = base[current_v]
|
2024-10-14 15:42:19 +00:00
|
|
|
if visited[current_v]: # Check if we've already visited this base
|
2024-10-14 15:31:01 +00:00
|
|
|
return current_v
|
|
|
|
current_v = parent[current_v]
|
2024-10-14 06:09:47 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2024-10-14 15:31:01 +00:00
|
|
|
def contract_blossom(blossom_data: BlossomData):
|
2024-10-14 15:42:19 +00:00
|
|
|
"""
|
|
|
|
Contracts a blossom found during the matching process.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
blossom_data: The data related to the blossom to be contracted.
|
|
|
|
"""
|
|
|
|
# Mark vertices in the blossom
|
2024-10-14 15:43:53 +00:00
|
|
|
for x in range(
|
|
|
|
blossom_data.u,
|
|
|
|
blossom_data.aux_data.base[blossom_data.u] != blossom_data.lca,
|
|
|
|
):
|
2024-10-14 15:31:01 +00:00
|
|
|
base_x = blossom_data.aux_data.base[x]
|
|
|
|
match_base_x = blossom_data.aux_data.base[blossom_data.aux_data.match[x]]
|
2024-10-14 15:42:19 +00:00
|
|
|
# Mark the base as in a blossom
|
2024-10-14 15:31:01 +00:00
|
|
|
blossom_data.aux_data.in_blossom[base_x] = True
|
|
|
|
blossom_data.aux_data.in_blossom[match_base_x] = True
|
|
|
|
|
2024-10-14 15:43:53 +00:00
|
|
|
for x in range(
|
|
|
|
blossom_data.v,
|
|
|
|
blossom_data.aux_data.base[blossom_data.v] != blossom_data.lca,
|
|
|
|
):
|
2024-10-14 15:31:01 +00:00
|
|
|
base_x = blossom_data.aux_data.base[x]
|
|
|
|
match_base_x = blossom_data.aux_data.base[blossom_data.aux_data.match[x]]
|
2024-10-14 15:42:19 +00:00
|
|
|
# Mark the base as in a blossom
|
2024-10-14 15:31:01 +00:00
|
|
|
blossom_data.aux_data.in_blossom[base_x] = True
|
|
|
|
blossom_data.aux_data.in_blossom[match_base_x] = True
|
2024-10-14 06:09:47 +00:00
|
|
|
|
|
|
|
# Update the base for all marked vertices
|
|
|
|
for i in range(len(blossom_data.aux_data.base)):
|
|
|
|
if blossom_data.aux_data.in_blossom[blossom_data.aux_data.base[i]]:
|
2024-10-14 15:31:01 +00:00
|
|
|
# Contract to the lowest common ancestor
|
2024-10-14 06:09:47 +00:00
|
|
|
blossom_data.aux_data.base[i] = blossom_data.lca
|
|
|
|
if not blossom_data.aux_data.in_queue[i]:
|
2024-10-14 15:31:01 +00:00
|
|
|
# Add to queue if not already present
|
2024-10-14 06:09:47 +00:00
|
|
|
blossom_data.aux_data.queue.append(i)
|
|
|
|
blossom_data.aux_data.in_queue[i] = True
|