Def improveLabels(val):
«»» change the labels, and maintain minSlack.
«»»
for u in S:
lu[u] -= val
for v in V:
if v in T:
lv[v] += val
else:
minSlack[v] [0] -= val
def improveMatching(v):
«»» apply the alternating path from v to the root in the tree.
«»»
u = T[v]
if u in Mu:
improveMatching (Mu[u])
Mu[u] = v
Mv[v] = u
def slack (u, v): return lu[u]+lv[v] - w[u] [v]
def augment():
«»» augment the matching, possibly improving the lablels on the way.
«»»
while True:
# select edge (u, v) with u in S, v not in T and min slack
((val, u), v) = min([(minSlack[v], v) for v in V if v not in T])
assert u in S
if val>0:
improveLabels(val)
# now we are sure that (u, v) is saturated
assert slack (u, v)==0
T[v] = u # add (u, v) to the tree
if v in Mv:
u1 = Mv[v] # matched edge,
assert not u1 in S
S[u1] = True #… add endpoint to tree
for v in V: # maintain minSlack
if not v in T and minSlack[v] [0] > slack (u1, v):
minSlack[v] = [slack (u1, v), u1]
else:
improveM