1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
Maximum weight matching
=======================
>>> import math
>>> from networkx import *
>>> def sortdict(d):
... s = d.items()
... s.sort()
... print '{' + ', '.join(map(lambda t: ': '.join(map(repr, t)), s)) + '}'
Trivial cases
-------------
>>> G = Graph()
>>> max_weight_matching(G)
{}
>>> G = Graph()
>>> G.add_edge(0, 0, weight=100)
>>> max_weight_matching(G)
{}
>>> G = Graph()
>>> G.add_edge(0, 1)
>>> sortdict(max_weight_matching(G))
{0: 1, 1: 0}
>>> G = Graph()
>>> G.add_edge('one', 'two', weight=10)
>>> G.add_edge('two', 'three', weight=11)
>>> sortdict(max_weight_matching(G))
{'three': 'two', 'two': 'three'}
>>> G = Graph()
>>> G.add_edge((1,), 2, weight=5)
>>> G.add_edge(2, 3, weight=11)
>>> G.add_edge(3, 4, weight=5)
>>> sortdict(max_weight_matching(G))
{2: 3, 3: 2}
>>> sortdict(max_weight_matching(G, 1))
{2: (1,), 3: 4, 4: 3, (1,): 2}
Floating point weights:
>>> G = Graph()
>>> G.add_edge(1, 2, weight=math.pi)
>>> G.add_edge(2, 3, weight=math.exp(1))
>>> G.add_edge(1, 3, weight=3.0)
>>> G.add_edge(1, 4, weight=math.sqrt(2.0))
>>> sortdict(max_weight_matching(G))
{1: 4, 2: 3, 3: 2, 4: 1}
Negative weights:
>>> G = Graph()
>>> G.add_edge(1, 2, weight=2)
>>> G.add_edge(1, 3, weight=-2)
>>> G.add_edge(2, 3, weight=1)
>>> G.add_edge(2, 4, weight=-1)
>>> G.add_edge(3, 4, weight=-6)
>>> sortdict(max_weight_matching(G))
{1: 2, 2: 1}
>>> sortdict(max_weight_matching(G, 1))
{1: 3, 2: 4, 3: 1, 4: 2}
Blossoms
--------
Create S-blossom and use it for augmentation:
>>> G = Graph()
>>> G.add_weighted_edges_from([ (1, 2, 8), (1, 3, 9), (2, 3, 10), (3, 4, 7) ])
>>> sortdict(max_weight_matching(G))
{1: 2, 2: 1, 3: 4, 4: 3}
>>> G.add_weighted_edges_from([ (1, 6, 5), (4, 5, 6) ])
>>> sortdict(max_weight_matching(G))
{1: 6, 2: 3, 3: 2, 4: 5, 5: 4, 6: 1}
Create S-blossom, relabel as T-blossom, use for augmentation:
>>> G = Graph()
>>> G.add_weighted_edges_from([ (1, 2, 9), (1, 3, 8), (2, 3, 10), (1, 4, 5),
... (4, 5, 4), (1, 6, 3) ])
>>> sortdict(max_weight_matching(G))
{1: 6, 2: 3, 3: 2, 4: 5, 5: 4, 6: 1}
>>> G.add_edge(4, 5, weight=3)
>>> G.add_edge(1, 6, weight=4)
>>> sortdict(max_weight_matching(G))
{1: 6, 2: 3, 3: 2, 4: 5, 5: 4, 6: 1}
>>> G.remove_edge(1, 6)
>>> G.add_edge(3, 6, weight=4)
>>> sortdict(max_weight_matching(G))
{1: 2, 2: 1, 3: 6, 4: 5, 5: 4, 6: 3}
Create nested S-blossom, use for augmentation:
>>> G = Graph()
>>> G.add_weighted_edges_from([ (1, 2, 9), (1, 3, 9), (2, 3, 10), (2, 4, 8),
... (3, 5, 8), (4, 5, 10), (5, 6, 6) ])
>>> sortdict(max_weight_matching(G))
{1: 3, 2: 4, 3: 1, 4: 2, 5: 6, 6: 5}
Create S-blossom, relabel as S, include in nested S-blossom:
>>> G = Graph()
>>> G.add_weighted_edges_from([ (1, 2, 10), (1, 7, 10), (2, 3, 12), (3, 4, 20),
... (3, 5, 20), (4, 5, 25), (5, 6, 10), (6, 7, 10),
... (7, 8, 8) ])
>>> sortdict(max_weight_matching(G))
{1: 2, 2: 1, 3: 4, 4: 3, 5: 6, 6: 5, 7: 8, 8: 7}
Create nested S-blossom, augment, expand recursively:
>>> G = Graph()
>>> G.add_weighted_edges_from([ (1, 2, 8), (1, 3, 8), (2, 3, 10), (2, 4, 12),
... (3, 5, 12), (4, 5, 14), (4, 6, 12), (5, 7, 12),
... (6, 7, 14), (7, 8, 12) ])
>>> sortdict(max_weight_matching(G))
{1: 2, 2: 1, 3: 5, 4: 6, 5: 3, 6: 4, 7: 8, 8: 7}
Create S-blossom, relabel as T, expand:
>>> G = Graph()
>>> G.add_weighted_edges_from([ (1, 2, 23), (1, 5, 22), (1, 6, 15), (2, 3, 25),
... (3, 4, 22), (4, 5, 25), (4, 8, 14), (5, 7, 13) ])
>>> sortdict(max_weight_matching(G))
{1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4}
Create nested S-blossom, relabel as T, expand:
>>> G = Graph()
>>> G.add_weighted_edges_from([ (1, 2, 19), (1, 3, 20), (1, 8, 8), (2, 3, 25),
... (2, 4, 18), (3, 5, 18), (4, 5, 13), (4, 7, 7),
... (5, 6, 7) ])
>>> sortdict(max_weight_matching(G))
{1: 8, 2: 3, 3: 2, 4: 7, 5: 6, 6: 5, 7: 4, 8: 1}
Nasty cases
-----------
Create blossom, relabel as T in more than one way, expand, augment:
>>> G = Graph()
>>> G.add_weighted_edges_from([ (1, 2, 45), (1, 5, 45), (2, 3, 50), (3, 4, 45),
... (4, 5, 50), (1, 6, 30), (3, 9, 35), (4, 8, 35),
... (5, 7, 26), (9, 10, 5) ])
>>> sortdict(max_weight_matching(G))
{1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4, 9: 10, 10: 9}
Again but slightly different:
>>> G = Graph()
>>> G.add_weighted_edges_from([ (1, 2, 45), (1, 5, 45), (2, 3, 50), (3, 4, 45),
... (4, 5, 50), (1, 6, 30), (3, 9, 35), (4, 8, 26),
... (5, 7, 40), (9, 10, 5) ])
>>> sortdict(max_weight_matching(G))
{1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4, 9: 10, 10: 9}
Create blossom, relabel as T, expand such that a new least-slack S-to-free
edge is produced, augment:
>>> G = Graph()
>>> G.add_weighted_edges_from([ (1, 2, 45), (1, 5, 45), (2, 3, 50), (3, 4, 45),
... (4, 5, 50), (1, 6, 30), (3, 9, 35), (4, 8, 28),
... (5, 7, 26), (9, 10, 5) ])
>>> sortdict(max_weight_matching(G))
{1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4, 9: 10, 10: 9}
Create nested blossom, relabel as T in more than one way, expand outer
blossom such that inner blossom ends up on an augmenting path:
>>> G = Graph()
>>> G.add_weighted_edges_from([ (1, 2, 45), (1, 7, 45), (2, 3, 50), (3, 4, 45),
... (4, 5, 95), (4, 6, 94), (5, 6, 94), (6, 7, 50),
... (1, 8, 30), (3, 11, 35), (5, 9, 36), (7, 10, 26),
... (11, 12, 5) ])
>>> sortdict(max_weight_matching(G))
{1: 8, 2: 3, 3: 2, 4: 6, 5: 9, 6: 4, 7: 10, 8: 1, 9: 5, 10: 7, 11: 12, 12: 11}
Create nested S-blossom, relabel as S, expand recursively:
>>> G = Graph()
>>> G.add_weighted_edges_from([ (1, 2, 40), (1, 3, 40), (2, 3, 60), (2, 4, 55),
... (3, 5, 55), (4, 5, 50), (1, 8, 15), (5, 7, 30),
... (7, 6, 10), (8, 10, 10), (4, 9, 30) ])
>>> sortdict(max_weight_matching(G, 1))
{1: 2, 2: 1, 3: 5, 4: 9, 5: 3, 6: 7, 7: 6, 8: 10, 9: 4, 10: 8}
|