| 1 |
"use strict"; |
| 2 |
|
| 3 |
var _ = require("../lodash"); |
| 4 |
var feasibleTree = require("./feasible-tree"); |
| 5 |
var slack = require("./util").slack; |
| 6 |
var initRank = require("./util").longestPath; |
| 7 |
var preorder = require("../graphlib").alg.preorder; |
| 8 |
var postorder = require("../graphlib").alg.postorder; |
| 9 |
var simplify = require("../util").simplify; |
| 10 |
|
| 11 |
module.exports = networkSimplex; |
| 12 |
|
| 13 |
|
| 14 |
networkSimplex.initLowLimValues = initLowLimValues; |
| 15 |
networkSimplex.initCutValues = initCutValues; |
| 16 |
networkSimplex.calcCutValue = calcCutValue; |
| 17 |
networkSimplex.leaveEdge = leaveEdge; |
| 18 |
networkSimplex.enterEdge = enterEdge; |
| 19 |
networkSimplex.exchangeEdges = exchangeEdges; |
| 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 |
function networkSimplex(g) { |
| 55 |
g = simplify(g); |
| 56 |
initRank(g); |
| 57 |
var t = feasibleTree(g); |
| 58 |
initLowLimValues(t); |
| 59 |
initCutValues(t, g); |
| 60 |
|
| 61 |
var e, f; |
| 62 |
while ((e = leaveEdge(t))) { |
| 63 |
f = enterEdge(t, g, e); |
| 64 |
exchangeEdges(t, g, e, f); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
function initCutValues(t, g) { |
| 72 |
var vs = postorder(t, t.nodes()); |
| 73 |
vs = vs.slice(0, vs.length - 1); |
| 74 |
_.forEach(vs, function(v) { |
| 75 |
assignCutValue(t, g, v); |
| 76 |
}); |
| 77 |
} |
| 78 |
|
| 79 |
function assignCutValue(t, g, child) { |
| 80 |
var childLab = t.node(child); |
| 81 |
var parent = childLab.parent; |
| 82 |
t.edge(child, parent).cutvalue = calcCutValue(t, g, child); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
function calcCutValue(t, g, child) { |
| 90 |
var childLab = t.node(child); |
| 91 |
var parent = childLab.parent; |
| 92 |
|
| 93 |
var childIsTail = true; |
| 94 |
|
| 95 |
var graphEdge = g.edge(child, parent); |
| 96 |
|
| 97 |
var cutValue = 0; |
| 98 |
|
| 99 |
if (!graphEdge) { |
| 100 |
childIsTail = false; |
| 101 |
graphEdge = g.edge(parent, child); |
| 102 |
} |
| 103 |
|
| 104 |
cutValue = graphEdge.weight; |
| 105 |
|
| 106 |
_.forEach(g.nodeEdges(child), function(e) { |
| 107 |
var isOutEdge = e.v === child, |
| 108 |
other = isOutEdge ? e.w : e.v; |
| 109 |
|
| 110 |
if (other !== parent) { |
| 111 |
var pointsToHead = isOutEdge === childIsTail, |
| 112 |
otherWeight = g.edge(e).weight; |
| 113 |
|
| 114 |
cutValue += pointsToHead ? otherWeight : -otherWeight; |
| 115 |
if (isTreeEdge(t, child, other)) { |
| 116 |
var otherCutValue = t.edge(child, other).cutvalue; |
| 117 |
cutValue += pointsToHead ? -otherCutValue : otherCutValue; |
| 118 |
} |
| 119 |
} |
| 120 |
}); |
| 121 |
|
| 122 |
return cutValue; |
| 123 |
} |
| 124 |
|
| 125 |
function initLowLimValues(tree, root) { |
| 126 |
if (arguments.length < 2) { |
| 127 |
root = tree.nodes()[0]; |
| 128 |
} |
| 129 |
dfsAssignLowLim(tree, {}, 1, root); |
| 130 |
} |
| 131 |
|
| 132 |
function dfsAssignLowLim(tree, visited, nextLim, v, parent) { |
| 133 |
var low = nextLim; |
| 134 |
var label = tree.node(v); |
| 135 |
|
| 136 |
visited[v] = true; |
| 137 |
_.forEach(tree.neighbors(v), function(w) { |
| 138 |
if (!_.has(visited, w)) { |
| 139 |
nextLim = dfsAssignLowLim(tree, visited, nextLim, w, v); |
| 140 |
} |
| 141 |
}); |
| 142 |
|
| 143 |
label.low = low; |
| 144 |
label.lim = nextLim++; |
| 145 |
if (parent) { |
| 146 |
label.parent = parent; |
| 147 |
} else { |
| 148 |
|
| 149 |
delete label.parent; |
| 150 |
} |
| 151 |
|
| 152 |
return nextLim; |
| 153 |
} |
| 154 |
|
| 155 |
function leaveEdge(tree) { |
| 156 |
return _.find(tree.edges(), function(e) { |
| 157 |
return tree.edge(e).cutvalue < 0; |
| 158 |
}); |
| 159 |
} |
| 160 |
|
| 161 |
function enterEdge(t, g, edge) { |
| 162 |
var v = edge.v; |
| 163 |
var w = edge.w; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
if (!g.hasEdge(v, w)) { |
| 169 |
v = edge.w; |
| 170 |
w = edge.v; |
| 171 |
} |
| 172 |
|
| 173 |
var vLabel = t.node(v); |
| 174 |
var wLabel = t.node(w); |
| 175 |
var tailLabel = vLabel; |
| 176 |
var flip = false; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
if (vLabel.lim > wLabel.lim) { |
| 181 |
tailLabel = wLabel; |
| 182 |
flip = true; |
| 183 |
} |
| 184 |
|
| 185 |
var candidates = _.filter(g.edges(), function(edge) { |
| 186 |
return flip === isDescendant(t, t.node(edge.v), tailLabel) && |
| 187 |
flip !== isDescendant(t, t.node(edge.w), tailLabel); |
| 188 |
}); |
| 189 |
|
| 190 |
return _.minBy(candidates, function(edge) { return slack(g, edge); }); |
| 191 |
} |
| 192 |
|
| 193 |
function exchangeEdges(t, g, e, f) { |
| 194 |
var v = e.v; |
| 195 |
var w = e.w; |
| 196 |
t.removeEdge(v, w); |
| 197 |
t.setEdge(f.v, f.w, {}); |
| 198 |
initLowLimValues(t); |
| 199 |
initCutValues(t, g); |
| 200 |
updateRanks(t, g); |
| 201 |
} |
| 202 |
|
| 203 |
function updateRanks(t, g) { |
| 204 |
var root = _.find(t.nodes(), function(v) { return !g.node(v).parent; }); |
| 205 |
var vs = preorder(t, root); |
| 206 |
vs = vs.slice(1); |
| 207 |
_.forEach(vs, function(v) { |
| 208 |
var parent = t.node(v).parent, |
| 209 |
edge = g.edge(v, parent), |
| 210 |
flipped = false; |
| 211 |
|
| 212 |
if (!edge) { |
| 213 |
edge = g.edge(parent, v); |
| 214 |
flipped = true; |
| 215 |
} |
| 216 |
|
| 217 |
g.node(v).rank = g.node(parent).rank + (flipped ? edge.minlen : -edge.minlen); |
| 218 |
}); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
function isTreeEdge(tree, u, v) { |
| 225 |
return tree.hasEdge(u, v); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
function isDescendant(tree, vLabel, rootLabel) { |
| 233 |
return rootLabel.low <= vLabel.lim && vLabel.lim <= rootLabel.lim; |
| 234 |
} |