import * as _ from 'lodash-es';
import * as alg from '../../graphlib/alg/index.js';
import { simplify } from '../util.js';
import { feasibleTree } from './feasible-tree.js';
import { longestPath, slack } from './util.js';
export { networkSimplex };
networkSimplex.initLowLimValues = initLowLimValues;
networkSimplex.initCutValues = initCutValues;
networkSimplex.calcCutValue = calcCutValue;
networkSimplex.leaveEdge = leaveEdge;
networkSimplex.enterEdge = enterEdge;
networkSimplex.exchangeEdges = exchangeEdges;
function networkSimplex(g) {
g = simplify(g);
longestPath(g);
var t = feasibleTree(g);
initLowLimValues(t);
initCutValues(t, g);
var e, f;
while ((e = leaveEdge(t))) {
f = enterEdge(t, g, e);
exchangeEdges(t, g, e, f);
}
}
function initCutValues(t, g) {
var vs = alg.postorder(t, t.nodes());
vs = vs.slice(0, vs.length - 1);
_.forEach(vs, function (v) {
assignCutValue(t, g, v);
});
}
function assignCutValue(t, g, child) {
var childLab = t.node(child);
var parent = childLab.parent;
t.edge(child, parent).cutvalue = calcCutValue(t, g, child);
}
function calcCutValue(t, g, child) {
var childLab = t.node(child);
var parent = childLab.parent;
var childIsTail = true;
var graphEdge = g.edge(child, parent);
var cutValue = 0;
if (!graphEdge) {
childIsTail = false;
graphEdge = g.edge(parent, child);
}
cutValue = graphEdge.weight;
_.forEach(g.nodeEdges(child), function (e) {
var isOutEdge = e.v === child,
other = isOutEdge ? e.w : e.v;
if (other !== parent) {
var pointsToHead = isOutEdge === childIsTail,
otherWeight = g.edge(e).weight;
cutValue += pointsToHead ? otherWeight : -otherWeight;
if (isTreeEdge(t, child, other)) {
var otherCutValue = t.edge(child, other).cutvalue;
cutValue += pointsToHead ? -otherCutValue : otherCutValue;
}
}
});
return cutValue;
}
function initLowLimValues(tree, root) {
if (arguments.length < 2) {
root = tree.nodes()[0];
}
dfsAssignLowLim(tree, {}, 1, root);
}
function dfsAssignLowLim(tree, visited, nextLim, v, parent) {
var low = nextLim;
var label = tree.node(v);
visited[v] = true;
_.forEach(tree.neighbors(v), function (w) {
if (!_.has(visited, w)) {
nextLim = dfsAssignLowLim(tree, visited, nextLim, w, v);
}
});
label.low = low;
label.lim = nextLim++;
if (parent) {
label.parent = parent;
} else {
delete label.parent;
}
return nextLim;
}
function leaveEdge(tree) {
return _.find(tree.edges(), function (e) {
return tree.edge(e).cutvalue < 0;
});
}
function enterEdge(t, g, edge) {
var v = edge.v;
var w = edge.w;
if (!g.hasEdge(v, w)) {
v = edge.w;
w = edge.v;
}
var vLabel = t.node(v);
var wLabel = t.node(w);
var tailLabel = vLabel;
var flip = false;
if (vLabel.lim > wLabel.lim) {
tailLabel = wLabel;
flip = true;
}
var candidates = _.filter(g.edges(), function (edge) {
return (
flip === isDescendant(t, t.node(edge.v), tailLabel) &&
flip !== isDescendant(t, t.node(edge.w), tailLabel)
);
});
return _.minBy(candidates, function (edge) {
return slack(g, edge);
});
}
function exchangeEdges(t, g, e, f) {
var v = e.v;
var w = e.w;
t.removeEdge(v, w);
t.setEdge(f.v, f.w, {});
initLowLimValues(t);
initCutValues(t, g);
updateRanks(t, g);
}
function updateRanks(t, g) {
var root = _.find(t.nodes(), function (v) {
return !g.node(v).parent;
});
var vs = alg.preorder(t, root);
vs = vs.slice(1);
_.forEach(vs, function (v) {
var parent = t.node(v).parent,
edge = g.edge(v, parent),
flipped = false;
if (!edge) {
edge = g.edge(parent, v);
flipped = true;
}
g.node(v).rank = g.node(parent).rank + (flipped ? edge.minlen : -edge.minlen);
});
}
function isTreeEdge(tree, u, v) {
return tree.hasEdge(u, v);
}
function isDescendant(tree, vLabel, rootLabel) {
return rootLabel.low <= vLabel.lim && vLabel.lim <= rootLabel.lim;
}