matching.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. """Weighted maximum matching in general graphs.
  2. The algorithm is taken from "Efficient Algorithms for Finding Maximum
  3. Matching in Graphs" by Zvi Galil, ACM Computing Surveys, 1986.
  4. It is based on the "blossom" method for finding augmenting paths and
  5. the "primal-dual" method for finding a matching of maximum weight, both
  6. due to Jack Edmonds.
  7. Some ideas came from "Implementation of algorithms for maximum matching
  8. on non-bipartite graphs" by H.J. Gabow, Standford Ph.D. thesis, 1973.
  9. A C program for maximum weight matching by Ed Rothberg was used extensively
  10. to validate this new code.
  11. """
  12. #
  13. # Changes:
  14. #
  15. # 2013-04-07
  16. # * Added Python 3 compatibility with contributions from Daniel Saunders.
  17. #
  18. # 2008-06-08
  19. # * First release.
  20. #
  21. from __future__ import print_function
  22. # If assigned, DEBUG(str) is called with lots of debug messages.
  23. DEBUG = None
  24. """def DEBUG(s):
  25. from sys import stderr
  26. print('DEBUG:', s, file=stderr)
  27. """
  28. # Check delta2/delta3 computation after every substage;
  29. # only works on integer weights, slows down the algorithm to O(n^4).
  30. CHECK_DELTA = False
  31. # Check optimality of solution before returning; only works on integer weights.
  32. CHECK_OPTIMUM = False
  33. def maxWeightMatching(edges, maxcardinality=False):
  34. """Compute a maximum-weighted matching in the general undirected
  35. weighted graph given by "edges". If "maxcardinality" is true,
  36. only maximum-cardinality matchings are considered as solutions.
  37. Edges is a sequence of tuples (i, j, wt) describing an undirected
  38. edge between vertex i and vertex j with weight wt. There is at most
  39. one edge between any two vertices; no vertex has an edge to itself.
  40. Vertices are identified by consecutive, non-negative integers.
  41. Return a list "mate", such that mate[i] == j if vertex i is
  42. matched to vertex j, and mate[i] == -1 if vertex i is not matched.
  43. This function takes time O(n ** 3)."""
  44. #
  45. # Vertices are numbered 0 .. (nvertex-1).
  46. # Non-trivial blossoms are numbered nvertex .. (2*nvertex-1)
  47. #
  48. # Edges are numbered 0 .. (nedge-1).
  49. # Edge endpoints are numbered 0 .. (2*nedge-1), such that endpoints
  50. # (2*k) and (2*k+1) both belong to edge k.
  51. #
  52. # Many terms used in the comments (sub-blossom, T-vertex) come from
  53. # the paper by Galil; read the paper before reading this code.
  54. #
  55. # Python 2/3 compatibility.
  56. from sys import version as sys_version
  57. if sys_version < '3':
  58. integer_types = (int, long)
  59. else:
  60. integer_types = (int,)
  61. # Deal swiftly with empty graphs.
  62. if not edges:
  63. return [ ]
  64. # Count vertices.
  65. nedge = len(edges)
  66. nvertex = 0
  67. for (i, j, w) in edges:
  68. assert i >= 0 and j >= 0 and i != j
  69. if i >= nvertex:
  70. nvertex = i + 1
  71. if j >= nvertex:
  72. nvertex = j + 1
  73. # Find the maximum edge weight.
  74. maxweight = max(0, max([ wt for (i, j, wt) in edges ]))
  75. # If p is an edge endpoint,
  76. # endpoint[p] is the vertex to which endpoint p is attached.
  77. # Not modified by the algorithm.
  78. endpoint = [ edges[p//2][p%2] for p in range(2*nedge) ]
  79. # If v is a vertex,
  80. # neighbend[v] is the list of remote endpoints of the edges attached to v.
  81. # Not modified by the algorithm.
  82. neighbend = [ [ ] for i in range(nvertex) ]
  83. for k in range(len(edges)):
  84. (i, j, w) = edges[k]
  85. neighbend[i].append(2*k+1)
  86. neighbend[j].append(2*k)
  87. # If v is a vertex,
  88. # mate[v] is the remote endpoint of its matched edge, or -1 if it is single
  89. # (i.e. endpoint[mate[v]] is v's partner vertex).
  90. # Initially all vertices are single; updated during augmentation.
  91. mate = nvertex * [ -1 ]
  92. # If b is a top-level blossom,
  93. # label[b] is 0 if b is unlabeled (free);
  94. # 1 if b is an S-vertex/blossom;
  95. # 2 if b is a T-vertex/blossom.
  96. # The label of a vertex is found by looking at the label of its
  97. # top-level containing blossom.
  98. # If v is a vertex inside a T-blossom,
  99. # label[v] is 2 iff v is reachable from an S-vertex outside the blossom.
  100. # Labels are assigned during a stage and reset after each augmentation.
  101. label = (2 * nvertex) * [ 0 ]
  102. # If b is a labeled top-level blossom,
  103. # labelend[b] is the remote endpoint of the edge through which b obtained
  104. # its label, or -1 if b's base vertex is single.
  105. # If v is a vertex inside a T-blossom and label[v] == 2,
  106. # labelend[v] is the remote endpoint of the edge through which v is
  107. # reachable from outside the blossom.
  108. labelend = (2 * nvertex) * [ -1 ]
  109. # If v is a vertex,
  110. # inblossom[v] is the top-level blossom to which v belongs.
  111. # If v is a top-level vertex, v is itself a blossom (a trivial blossom)
  112. # and inblossom[v] == v.
  113. # Initially all vertices are top-level trivial blossoms.
  114. inblossom = list(range(nvertex))
  115. # If b is a sub-blossom,
  116. # blossomparent[b] is its immediate parent (sub-)blossom.
  117. # If b is a top-level blossom, blossomparent[b] is -1.
  118. blossomparent = (2 * nvertex) * [ -1 ]
  119. # If b is a non-trivial (sub-)blossom,
  120. # blossomchilds[b] is an ordered list of its sub-blossoms, starting with
  121. # the base and going round the blossom.
  122. blossomchilds = (2 * nvertex) * [ None ]
  123. # If b is a (sub-)blossom,
  124. # blossombase[b] is its base VERTEX (i.e. recursive sub-blossom).
  125. blossombase = list(range(nvertex)) + nvertex * [ -1 ]
  126. # If b is a non-trivial (sub-)blossom,
  127. # blossomendps[b] is a list of endpoints on its connecting edges,
  128. # such that blossomendps[b][i] is the local endpoint of blossomchilds[b][i]
  129. # on the edge that connects it to blossomchilds[b][wrap(i+1)].
  130. blossomendps = (2 * nvertex) * [ None ]
  131. # If v is a free vertex (or an unreached vertex inside a T-blossom),
  132. # bestedge[v] is the edge to an S-vertex with least slack,
  133. # or -1 if there is no such edge.
  134. # If b is a (possibly trivial) top-level S-blossom,
  135. # bestedge[b] is the least-slack edge to a different S-blossom,
  136. # or -1 if there is no such edge.
  137. # This is used for efficient computation of delta2 and delta3.
  138. bestedge = (2 * nvertex) * [ -1 ]
  139. # If b is a non-trivial top-level S-blossom,
  140. # blossombestedges[b] is a list of least-slack edges to neighbouring
  141. # S-blossoms, or None if no such list has been computed yet.
  142. # This is used for efficient computation of delta3.
  143. blossombestedges = (2 * nvertex) * [ None ]
  144. # List of currently unused blossom numbers.
  145. unusedblossoms = list(range(nvertex, 2*nvertex))
  146. # If v is a vertex,
  147. # dualvar[v] = 2 * u(v) where u(v) is the v's variable in the dual
  148. # optimization problem (multiplication by two ensures integer values
  149. # throughout the algorithm if all edge weights are integers).
  150. # If b is a non-trivial blossom,
  151. # dualvar[b] = z(b) where z(b) is b's variable in the dual optimization
  152. # problem.
  153. dualvar = nvertex * [ maxweight ] + nvertex * [ 0 ]
  154. # If allowedge[k] is true, edge k has zero slack in the optimization
  155. # problem; if allowedge[k] is false, the edge's slack may or may not
  156. # be zero.
  157. allowedge = nedge * [ False ]
  158. # Queue of newly discovered S-vertices.
  159. queue = [ ]
  160. # Return 2 * slack of edge k (does not work inside blossoms).
  161. def slack(k):
  162. (i, j, wt) = edges[k]
  163. return dualvar[i] + dualvar[j] - 2 * wt
  164. # Generate the leaf vertices of a blossom.
  165. def blossomLeaves(b):
  166. if b < nvertex:
  167. yield b
  168. else:
  169. for t in blossomchilds[b]:
  170. if t < nvertex:
  171. yield t
  172. else:
  173. for v in blossomLeaves(t):
  174. yield v
  175. # Assign label t to the top-level blossom containing vertex w
  176. # and record the fact that w was reached through the edge with
  177. # remote endpoint p.
  178. def assignLabel(w, t, p):
  179. if DEBUG: DEBUG('assignLabel(%d,%d,%d)' % (w, t, p))
  180. b = inblossom[w]
  181. assert label[w] == 0 and label[b] == 0
  182. label[w] = label[b] = t
  183. labelend[w] = labelend[b] = p
  184. bestedge[w] = bestedge[b] = -1
  185. if t == 1:
  186. # b became an S-vertex/blossom; add it(s vertices) to the queue.
  187. queue.extend(blossomLeaves(b))
  188. if DEBUG: DEBUG('PUSH ' + str(list(blossomLeaves(b))))
  189. elif t == 2:
  190. # b became a T-vertex/blossom; assign label S to its mate.
  191. # (If b is a non-trivial blossom, its base is the only vertex
  192. # with an external mate.)
  193. base = blossombase[b]
  194. assert mate[base] >= 0
  195. assignLabel(endpoint[mate[base]], 1, mate[base] ^ 1)
  196. # Trace back from vertices v and w to discover either a new blossom
  197. # or an augmenting path. Return the base vertex of the new blossom or -1.
  198. def scanBlossom(v, w):
  199. if DEBUG: DEBUG('scanBlossom(%d,%d)' % (v, w))
  200. # Trace back from v and w, placing breadcrumbs as we go.
  201. path = [ ]
  202. base = -1
  203. while v != -1 or w != -1:
  204. # Look for a breadcrumb in v's blossom or put a new breadcrumb.
  205. b = inblossom[v]
  206. if label[b] & 4:
  207. base = blossombase[b]
  208. break
  209. assert label[b] == 1
  210. path.append(b)
  211. label[b] = 5
  212. # Trace one step back.
  213. assert labelend[b] == mate[blossombase[b]]
  214. if labelend[b] == -1:
  215. # The base of blossom b is single; stop tracing this path.
  216. v = -1
  217. else:
  218. v = endpoint[labelend[b]]
  219. b = inblossom[v]
  220. assert label[b] == 2
  221. # b is a T-blossom; trace one more step back.
  222. assert labelend[b] >= 0
  223. v = endpoint[labelend[b]]
  224. # Swap v and w so that we alternate between both paths.
  225. if w != -1:
  226. v, w = w, v
  227. # Remove breadcrumbs.
  228. for b in path:
  229. label[b] = 1
  230. # Return base vertex, if we found one.
  231. return base
  232. # Construct a new blossom with given base, containing edge k which
  233. # connects a pair of S vertices. Label the new blossom as S; set its dual
  234. # variable to zero; relabel its T-vertices to S and add them to the queue.
  235. def addBlossom(base, k):
  236. (v, w, wt) = edges[k]
  237. bb = inblossom[base]
  238. bv = inblossom[v]
  239. bw = inblossom[w]
  240. # Create blossom.
  241. b = unusedblossoms.pop()
  242. if DEBUG: DEBUG('addBlossom(%d,%d) (v=%d w=%d) -> %d' % (base, k, v, w, b))
  243. blossombase[b] = base
  244. blossomparent[b] = -1
  245. blossomparent[bb] = b
  246. # Make list of sub-blossoms and their interconnecting edge endpoints.
  247. blossomchilds[b] = path = [ ]
  248. blossomendps[b] = endps = [ ]
  249. # Trace back from v to base.
  250. while bv != bb:
  251. # Add bv to the new blossom.
  252. blossomparent[bv] = b
  253. path.append(bv)
  254. endps.append(labelend[bv])
  255. assert (label[bv] == 2 or
  256. (label[bv] == 1 and labelend[bv] == mate[blossombase[bv]]))
  257. # Trace one step back.
  258. assert labelend[bv] >= 0
  259. v = endpoint[labelend[bv]]
  260. bv = inblossom[v]
  261. # Reverse lists, add endpoint that connects the pair of S vertices.
  262. path.append(bb)
  263. path.reverse()
  264. endps.reverse()
  265. endps.append(2*k)
  266. # Trace back from w to base.
  267. while bw != bb:
  268. # Add bw to the new blossom.
  269. blossomparent[bw] = b
  270. path.append(bw)
  271. endps.append(labelend[bw] ^ 1)
  272. assert (label[bw] == 2 or
  273. (label[bw] == 1 and labelend[bw] == mate[blossombase[bw]]))
  274. # Trace one step back.
  275. assert labelend[bw] >= 0
  276. w = endpoint[labelend[bw]]
  277. bw = inblossom[w]
  278. # Set label to S.
  279. assert label[bb] == 1
  280. label[b] = 1
  281. labelend[b] = labelend[bb]
  282. # Set dual variable to zero.
  283. dualvar[b] = 0
  284. # Relabel vertices.
  285. for v in blossomLeaves(b):
  286. if label[inblossom[v]] == 2:
  287. # This T-vertex now turns into an S-vertex because it becomes
  288. # part of an S-blossom; add it to the queue.
  289. queue.append(v)
  290. inblossom[v] = b
  291. # Compute blossombestedges[b].
  292. bestedgeto = (2 * nvertex) * [ -1 ]
  293. for bv in path:
  294. if blossombestedges[bv] is None:
  295. # This subblossom does not have a list of least-slack edges;
  296. # get the information from the vertices.
  297. nblists = [ [ p // 2 for p in neighbend[v] ]
  298. for v in blossomLeaves(bv) ]
  299. else:
  300. # Walk this subblossom's least-slack edges.
  301. nblists = [ blossombestedges[bv] ]
  302. for nblist in nblists:
  303. for k in nblist:
  304. (i, j, wt) = edges[k]
  305. if inblossom[j] == b:
  306. i, j = j, i
  307. bj = inblossom[j]
  308. if (bj != b and label[bj] == 1 and
  309. (bestedgeto[bj] == -1 or
  310. slack(k) < slack(bestedgeto[bj]))):
  311. bestedgeto[bj] = k
  312. # Forget about least-slack edges of the subblossom.
  313. blossombestedges[bv] = None
  314. bestedge[bv] = -1
  315. blossombestedges[b] = [ k for k in bestedgeto if k != -1 ]
  316. # Select bestedge[b].
  317. bestedge[b] = -1
  318. for k in blossombestedges[b]:
  319. if bestedge[b] == -1 or slack(k) < slack(bestedge[b]):
  320. bestedge[b] = k
  321. if DEBUG: DEBUG('blossomchilds[%d]=' % b + repr(blossomchilds[b]))
  322. # Expand the given top-level blossom.
  323. def expandBlossom(b, endstage):
  324. if DEBUG: DEBUG('expandBlossom(%d,%d) %s' % (b, endstage, repr(blossomchilds[b])))
  325. # Convert sub-blossoms into top-level blossoms.
  326. for s in blossomchilds[b]:
  327. blossomparent[s] = -1
  328. if s < nvertex:
  329. inblossom[s] = s
  330. elif endstage and dualvar[s] == 0:
  331. # Recursively expand this sub-blossom.
  332. expandBlossom(s, endstage)
  333. else:
  334. for v in blossomLeaves(s):
  335. inblossom[v] = s
  336. # If we expand a T-blossom during a stage, its sub-blossoms must be
  337. # relabeled.
  338. if (not endstage) and label[b] == 2:
  339. # Start at the sub-blossom through which the expanding
  340. # blossom obtained its label, and relabel sub-blossoms untili
  341. # we reach the base.
  342. # Figure out through which sub-blossom the expanding blossom
  343. # obtained its label initially.
  344. assert labelend[b] >= 0
  345. entrychild = inblossom[endpoint[labelend[b] ^ 1]]
  346. # Decide in which direction we will go round the blossom.
  347. j = blossomchilds[b].index(entrychild)
  348. if j & 1:
  349. # Start index is odd; go forward and wrap.
  350. j -= len(blossomchilds[b])
  351. jstep = 1
  352. endptrick = 0
  353. else:
  354. # Start index is even; go backward.
  355. jstep = -1
  356. endptrick = 1
  357. # Move along the blossom until we get to the base.
  358. p = labelend[b]
  359. while j != 0:
  360. # Relabel the T-sub-blossom.
  361. label[endpoint[p ^ 1]] = 0
  362. label[endpoint[blossomendps[b][j-endptrick]^endptrick^1]] = 0
  363. assignLabel(endpoint[p ^ 1], 2, p)
  364. # Step to the next S-sub-blossom and note its forward endpoint.
  365. allowedge[blossomendps[b][j-endptrick]//2] = True
  366. j += jstep
  367. p = blossomendps[b][j-endptrick] ^ endptrick
  368. # Step to the next T-sub-blossom.
  369. allowedge[p//2] = True
  370. j += jstep
  371. # Relabel the base T-sub-blossom WITHOUT stepping through to
  372. # its mate (so don't call assignLabel).
  373. bv = blossomchilds[b][j]
  374. label[endpoint[p ^ 1]] = label[bv] = 2
  375. labelend[endpoint[p ^ 1]] = labelend[bv] = p
  376. bestedge[bv] = -1
  377. # Continue along the blossom until we get back to entrychild.
  378. j += jstep
  379. while blossomchilds[b][j] != entrychild:
  380. # Examine the vertices of the sub-blossom to see whether
  381. # it is reachable from a neighbouring S-vertex outside the
  382. # expanding blossom.
  383. bv = blossomchilds[b][j]
  384. if label[bv] == 1:
  385. # This sub-blossom just got label S through one of its
  386. # neighbours; leave it.
  387. j += jstep
  388. continue
  389. for v in blossomLeaves(bv):
  390. if label[v] != 0:
  391. break
  392. # If the sub-blossom contains a reachable vertex, assign
  393. # label T to the sub-blossom.
  394. if label[v] != 0:
  395. assert label[v] == 2
  396. assert inblossom[v] == bv
  397. label[v] = 0
  398. label[endpoint[mate[blossombase[bv]]]] = 0
  399. assignLabel(v, 2, labelend[v])
  400. j += jstep
  401. # Recycle the blossom number.
  402. label[b] = labelend[b] = -1
  403. blossomchilds[b] = blossomendps[b] = None
  404. blossombase[b] = -1
  405. blossombestedges[b] = None
  406. bestedge[b] = -1
  407. unusedblossoms.append(b)
  408. # Swap matched/unmatched edges over an alternating path through blossom b
  409. # between vertex v and the base vertex. Keep blossom bookkeeping consistent.
  410. def augmentBlossom(b, v):
  411. if DEBUG: DEBUG('augmentBlossom(%d,%d)' % (b, v))
  412. # Bubble up through the blossom tree from vertex v to an immediate
  413. # sub-blossom of b.
  414. t = v
  415. while blossomparent[t] != b:
  416. t = blossomparent[t]
  417. # Recursively deal with the first sub-blossom.
  418. if t >= nvertex:
  419. augmentBlossom(t, v)
  420. # Decide in which direction we will go round the blossom.
  421. i = j = blossomchilds[b].index(t)
  422. if i & 1:
  423. # Start index is odd; go forward and wrap.
  424. j -= len(blossomchilds[b])
  425. jstep = 1
  426. endptrick = 0
  427. else:
  428. # Start index is even; go backward.
  429. jstep = -1
  430. endptrick = 1
  431. # Move along the blossom until we get to the base.
  432. while j != 0:
  433. # Step to the next sub-blossom and augment it recursively.
  434. j += jstep
  435. t = blossomchilds[b][j]
  436. p = blossomendps[b][j-endptrick] ^ endptrick
  437. if t >= nvertex:
  438. augmentBlossom(t, endpoint[p])
  439. # Step to the next sub-blossom and augment it recursively.
  440. j += jstep
  441. t = blossomchilds[b][j]
  442. if t >= nvertex:
  443. augmentBlossom(t, endpoint[p ^ 1])
  444. # Match the edge connecting those sub-blossoms.
  445. mate[endpoint[p]] = p ^ 1
  446. mate[endpoint[p ^ 1]] = p
  447. if DEBUG: DEBUG('PAIR %d %d (k=%d)' % (endpoint[p], endpoint[p^1], p//2))
  448. # Rotate the list of sub-blossoms to put the new base at the front.
  449. blossomchilds[b] = blossomchilds[b][i:] + blossomchilds[b][:i]
  450. blossomendps[b] = blossomendps[b][i:] + blossomendps[b][:i]
  451. blossombase[b] = blossombase[blossomchilds[b][0]]
  452. assert blossombase[b] == v
  453. # Swap matched/unmatched edges over an alternating path between two
  454. # single vertices. The augmenting path runs through edge k, which
  455. # connects a pair of S vertices.
  456. def augmentMatching(k):
  457. (v, w, wt) = edges[k]
  458. if DEBUG: DEBUG('augmentMatching(%d) (v=%d w=%d)' % (k, v, w))
  459. if DEBUG: DEBUG('PAIR %d %d (k=%d)' % (v, w, k))
  460. for (s, p) in ((v, 2*k+1), (w, 2*k)):
  461. # Match vertex s to remote endpoint p. Then trace back from s
  462. # until we find a single vertex, swapping matched and unmatched
  463. # edges as we go.
  464. while 1:
  465. bs = inblossom[s]
  466. assert label[bs] == 1
  467. assert labelend[bs] == mate[blossombase[bs]]
  468. # Augment through the S-blossom from s to base.
  469. if bs >= nvertex:
  470. augmentBlossom(bs, s)
  471. # Update mate[s]
  472. mate[s] = p
  473. # Trace one step back.
  474. if labelend[bs] == -1:
  475. # Reached single vertex; stop.
  476. break
  477. t = endpoint[labelend[bs]]
  478. bt = inblossom[t]
  479. assert label[bt] == 2
  480. # Trace one step back.
  481. assert labelend[bt] >= 0
  482. s = endpoint[labelend[bt]]
  483. j = endpoint[labelend[bt] ^ 1]
  484. # Augment through the T-blossom from j to base.
  485. assert blossombase[bt] == t
  486. if bt >= nvertex:
  487. augmentBlossom(bt, j)
  488. # Update mate[j]
  489. mate[j] = labelend[bt]
  490. # Keep the opposite endpoint;
  491. # it will be assigned to mate[s] in the next step.
  492. p = labelend[bt] ^ 1
  493. if DEBUG: DEBUG('PAIR %d %d (k=%d)' % (s, t, p//2))
  494. # Verify that the optimum solution has been reached.
  495. def verifyOptimum():
  496. if maxcardinality:
  497. # Vertices may have negative dual;
  498. # find a constant non-negative number to add to all vertex duals.
  499. vdualoffset = max(0, -min(dualvar[:nvertex]))
  500. else:
  501. vdualoffset = 0
  502. # 0. all dual variables are non-negative
  503. assert min(dualvar[:nvertex]) + vdualoffset >= 0
  504. assert min(dualvar[nvertex:]) >= 0
  505. # 0. all edges have non-negative slack and
  506. # 1. all matched edges have zero slack;
  507. for k in range(nedge):
  508. (i, j, wt) = edges[k]
  509. s = dualvar[i] + dualvar[j] - 2 * wt
  510. iblossoms = [ i ]
  511. jblossoms = [ j ]
  512. while blossomparent[iblossoms[-1]] != -1:
  513. iblossoms.append(blossomparent[iblossoms[-1]])
  514. while blossomparent[jblossoms[-1]] != -1:
  515. jblossoms.append(blossomparent[jblossoms[-1]])
  516. iblossoms.reverse()
  517. jblossoms.reverse()
  518. for (bi, bj) in zip(iblossoms, jblossoms):
  519. if bi != bj:
  520. break
  521. s += 2 * dualvar[bi]
  522. assert s >= 0
  523. if mate[i] // 2 == k or mate[j] // 2 == k:
  524. assert mate[i] // 2 == k and mate[j] // 2 == k
  525. assert s == 0
  526. # 2. all single vertices have zero dual value;
  527. for v in range(nvertex):
  528. assert mate[v] >= 0 or dualvar[v] + vdualoffset == 0
  529. # 3. all blossoms with positive dual value are full.
  530. for b in range(nvertex, 2*nvertex):
  531. if blossombase[b] >= 0 and dualvar[b] > 0:
  532. assert len(blossomendps[b]) % 2 == 1
  533. for p in blossomendps[b][1::2]:
  534. assert mate[endpoint[p]] == p ^ 1
  535. assert mate[endpoint[p ^ 1]] == p
  536. # Ok.
  537. # Check optimized delta2 against a trivial computation.
  538. def checkDelta2():
  539. for v in range(nvertex):
  540. if label[inblossom[v]] == 0:
  541. bd = None
  542. bk = -1
  543. for p in neighbend[v]:
  544. k = p // 2
  545. w = endpoint[p]
  546. if label[inblossom[w]] == 1:
  547. d = slack(k)
  548. if bk == -1 or d < bd:
  549. bk = k
  550. bd = d
  551. if DEBUG and (bestedge[v] != -1 or bk != -1) and (bestedge[v] == -1 or bd != slack(bestedge[v])):
  552. DEBUG('v=' + str(v) + ' bk=' + str(bk) + ' bd=' + str(bd) + ' bestedge=' + str(bestedge[v]) + ' slack=' + str(slack(bestedge[v])))
  553. assert (bk == -1 and bestedge[v] == -1) or (bestedge[v] != -1 and bd == slack(bestedge[v]))
  554. # Check optimized delta3 against a trivial computation.
  555. def checkDelta3():
  556. bk = -1
  557. bd = None
  558. tbk = -1
  559. tbd = None
  560. for b in range(2 * nvertex):
  561. if blossomparent[b] == -1 and label[b] == 1:
  562. for v in blossomLeaves(b):
  563. for p in neighbend[v]:
  564. k = p // 2
  565. w = endpoint[p]
  566. if inblossom[w] != b and label[inblossom[w]] == 1:
  567. d = slack(k)
  568. if bk == -1 or d < bd:
  569. bk = k
  570. bd = d
  571. if bestedge[b] != -1:
  572. (i, j, wt) = edges[bestedge[b]]
  573. assert inblossom[i] == b or inblossom[j] == b
  574. assert inblossom[i] != b or inblossom[j] != b
  575. assert label[inblossom[i]] == 1 and label[inblossom[j]] == 1
  576. if tbk == -1 or slack(bestedge[b]) < tbd:
  577. tbk = bestedge[b]
  578. tbd = slack(bestedge[b])
  579. if DEBUG and bd != tbd:
  580. DEBUG('bk=%d tbk=%d bd=%s tbd=%s' % (bk, tbk, repr(bd), repr(tbd)))
  581. assert bd == tbd
  582. # Main loop: continue until no further improvement is possible.
  583. for t in range(nvertex):
  584. # Each iteration of this loop is a "stage".
  585. # A stage finds an augmenting path and uses that to improve
  586. # the matching.
  587. if DEBUG: DEBUG('STAGE %d' % t)
  588. # Remove labels from top-level blossoms/vertices.
  589. label[:] = (2 * nvertex) * [ 0 ]
  590. # Forget all about least-slack edges.
  591. bestedge[:] = (2 * nvertex) * [ -1 ]
  592. blossombestedges[nvertex:] = nvertex * [ None ]
  593. # Loss of labeling means that we can not be sure that currently
  594. # allowable edges remain allowable througout this stage.
  595. allowedge[:] = nedge * [ False ]
  596. # Make queue empty.
  597. queue[:] = [ ]
  598. # Label single blossoms/vertices with S and put them in the queue.
  599. for v in range(nvertex):
  600. if mate[v] == -1 and label[inblossom[v]] == 0:
  601. assignLabel(v, 1, -1)
  602. # Loop until we succeed in augmenting the matching.
  603. augmented = 0
  604. while 1:
  605. # Each iteration of this loop is a "substage".
  606. # A substage tries to find an augmenting path;
  607. # if found, the path is used to improve the matching and
  608. # the stage ends. If there is no augmenting path, the
  609. # primal-dual method is used to pump some slack out of
  610. # the dual variables.
  611. if DEBUG: DEBUG('SUBSTAGE')
  612. # Continue labeling until all vertices which are reachable
  613. # through an alternating path have got a label.
  614. while queue and not augmented:
  615. # Take an S vertex from the queue.
  616. v = queue.pop()
  617. if DEBUG: DEBUG('POP v=%d' % v)
  618. assert label[inblossom[v]] == 1
  619. # Scan its neighbours:
  620. for p in neighbend[v]:
  621. k = p // 2
  622. w = endpoint[p]
  623. # w is a neighbour to v
  624. if inblossom[v] == inblossom[w]:
  625. # this edge is internal to a blossom; ignore it
  626. continue
  627. if not allowedge[k]:
  628. kslack = slack(k)
  629. if kslack <= 0:
  630. # edge k has zero slack => it is allowable
  631. allowedge[k] = True
  632. if allowedge[k]:
  633. if label[inblossom[w]] == 0:
  634. # (C1) w is a free vertex;
  635. # label w with T and label its mate with S (R12).
  636. assignLabel(w, 2, p ^ 1)
  637. elif label[inblossom[w]] == 1:
  638. # (C2) w is an S-vertex (not in the same blossom);
  639. # follow back-links to discover either an
  640. # augmenting path or a new blossom.
  641. base = scanBlossom(v, w)
  642. if base >= 0:
  643. # Found a new blossom; add it to the blossom
  644. # bookkeeping and turn it into an S-blossom.
  645. addBlossom(base, k)
  646. else:
  647. # Found an augmenting path; augment the
  648. # matching and end this stage.
  649. augmentMatching(k)
  650. augmented = 1
  651. break
  652. elif label[w] == 0:
  653. # w is inside a T-blossom, but w itself has not
  654. # yet been reached from outside the blossom;
  655. # mark it as reached (we need this to relabel
  656. # during T-blossom expansion).
  657. assert label[inblossom[w]] == 2
  658. label[w] = 2
  659. labelend[w] = p ^ 1
  660. elif label[inblossom[w]] == 1:
  661. # keep track of the least-slack non-allowable edge to
  662. # a different S-blossom.
  663. b = inblossom[v]
  664. if bestedge[b] == -1 or kslack < slack(bestedge[b]):
  665. bestedge[b] = k
  666. elif label[w] == 0:
  667. # w is a free vertex (or an unreached vertex inside
  668. # a T-blossom) but we can not reach it yet;
  669. # keep track of the least-slack edge that reaches w.
  670. if bestedge[w] == -1 or kslack < slack(bestedge[w]):
  671. bestedge[w] = k
  672. if augmented:
  673. break
  674. # There is no augmenting path under these constraints;
  675. # compute delta and reduce slack in the optimization problem.
  676. # (Note that our vertex dual variables, edge slacks and delta's
  677. # are pre-multiplied by two.)
  678. deltatype = -1
  679. delta = deltaedge = deltablossom = None
  680. # Verify data structures for delta2/delta3 computation.
  681. if CHECK_DELTA:
  682. checkDelta2()
  683. checkDelta3()
  684. # Compute delta1: the minumum value of any vertex dual.
  685. if not maxcardinality:
  686. deltatype = 1
  687. delta = min(dualvar[:nvertex])
  688. # Compute delta2: the minimum slack on any edge between
  689. # an S-vertex and a free vertex.
  690. for v in range(nvertex):
  691. if label[inblossom[v]] == 0 and bestedge[v] != -1:
  692. d = slack(bestedge[v])
  693. if deltatype == -1 or d < delta:
  694. delta = d
  695. deltatype = 2
  696. deltaedge = bestedge[v]
  697. # Compute delta3: half the minimum slack on any edge between
  698. # a pair of S-blossoms.
  699. for b in range(2 * nvertex):
  700. if ( blossomparent[b] == -1 and label[b] == 1 and
  701. bestedge[b] != -1 ):
  702. kslack = slack(bestedge[b])
  703. if isinstance(kslack, integer_types):
  704. assert (kslack % 2) == 0
  705. d = kslack // 2
  706. else:
  707. d = kslack / 2
  708. if deltatype == -1 or d < delta:
  709. delta = d
  710. deltatype = 3
  711. deltaedge = bestedge[b]
  712. # Compute delta4: minimum z variable of any T-blossom.
  713. for b in range(nvertex, 2*nvertex):
  714. if ( blossombase[b] >= 0 and blossomparent[b] == -1 and
  715. label[b] == 2 and
  716. (deltatype == -1 or dualvar[b] < delta) ):
  717. delta = dualvar[b]
  718. deltatype = 4
  719. deltablossom = b
  720. if deltatype == -1:
  721. # No further improvement possible; max-cardinality optimum
  722. # reached. Do a final delta update to make the optimum
  723. # verifyable.
  724. assert maxcardinality
  725. deltatype = 1
  726. delta = max(0, min(dualvar[:nvertex]))
  727. # Update dual variables according to delta.
  728. for v in range(nvertex):
  729. if label[inblossom[v]] == 1:
  730. # S-vertex: 2*u = 2*u - 2*delta
  731. dualvar[v] -= delta
  732. elif label[inblossom[v]] == 2:
  733. # T-vertex: 2*u = 2*u + 2*delta
  734. dualvar[v] += delta
  735. for b in range(nvertex, 2*nvertex):
  736. if blossombase[b] >= 0 and blossomparent[b] == -1:
  737. if label[b] == 1:
  738. # top-level S-blossom: z = z + 2*delta
  739. dualvar[b] += delta
  740. elif label[b] == 2:
  741. # top-level T-blossom: z = z - 2*delta
  742. dualvar[b] -= delta
  743. # Take action at the point where minimum delta occurred.
  744. if DEBUG: DEBUG('delta%d=%f' % (deltatype, delta))
  745. if deltatype == 1:
  746. # No further improvement possible; optimum reached.
  747. break
  748. elif deltatype == 2:
  749. # Use the least-slack edge to continue the search.
  750. allowedge[deltaedge] = True
  751. (i, j, wt) = edges[deltaedge]
  752. if label[inblossom[i]] == 0:
  753. i, j = j, i
  754. assert label[inblossom[i]] == 1
  755. queue.append(i)
  756. elif deltatype == 3:
  757. # Use the least-slack edge to continue the search.
  758. allowedge[deltaedge] = True
  759. (i, j, wt) = edges[deltaedge]
  760. assert label[inblossom[i]] == 1
  761. queue.append(i)
  762. elif deltatype == 4:
  763. # Expand the least-z blossom.
  764. expandBlossom(deltablossom, False)
  765. # End of a this substage.
  766. # Stop when no more augmenting path can be found.
  767. if not augmented:
  768. break
  769. # End of a stage; expand all S-blossoms which have dualvar = 0.
  770. for b in range(nvertex, 2*nvertex):
  771. if ( blossomparent[b] == -1 and blossombase[b] >= 0 and
  772. label[b] == 1 and dualvar[b] == 0 ):
  773. expandBlossom(b, True)
  774. # Verify that we reached the optimum solution.
  775. if CHECK_OPTIMUM:
  776. verifyOptimum()
  777. # Transform mate[] such that mate[v] is the vertex to which v is paired.
  778. for v in range(nvertex):
  779. if mate[v] >= 0:
  780. mate[v] = endpoint[mate[v]]
  781. for v in range(nvertex):
  782. assert mate[v] == -1 or mate[mate[v]] == v
  783. return mate
  784. # Unit tests
  785. if __name__ == '__main__':
  786. x = maxWeightMatching([(1,4,10),(1,5,20),(2,5,40),(2,6,60),(3,4,30)])
  787. print(x)
  788. # end