Skip to content

Commit

Permalink
bipartite matching with timestamp marks
Browse files Browse the repository at this point in the history
  • Loading branch information
xtof-durr committed Dec 25, 2023
1 parent a8049eb commit e12b47e
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion tryalgo/bipartite_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ def max_bipartite_matching(bigraph):
return match
# snip}

def augment2(u, bigraph, visit, timestamp, match):
"""augment """
for v in bigraph[u]:
if visit[v] < timestamp:
visit[v] = timestamp
if match[v] is None or augment2(match[v], bigraph,
visit, timestamp, match):
match[v] = u # found an augmenting path
return True
return False


def max_bipartite_matching2(bigraph):
"""Bipartie maximum matching
Expand All @@ -52,8 +63,9 @@ def max_bipartite_matching2(bigraph):
nU = len(bigraph)
nV = max(max(adjlist, default=-1) for adjlist in bigraph) + 1
match = [None] * nV
visit = [-1] * nV
for u in range(nU):
if bigraph[u]: # if u is not an isolated vertex
augment(u, bigraph, [False] * nV, match)
augment2(u, bigraph, visit, u, match)
return match
# snip}

0 comments on commit e12b47e

Please sign in to comment.