| 1 |
var _ = require("./lodash"); |
| 2 |
var Graph = require("./graphlib").Graph; |
| 3 |
var List = require("./data/list"); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
module.exports = greedyFAS; |
| 13 |
|
| 14 |
var DEFAULT_WEIGHT_FN = _.constant(1); |
| 15 |
|
| 16 |
function greedyFAS(g, weightFn) { |
| 17 |
if (g.nodeCount() <= 1) { |
| 18 |
return []; |
| 19 |
} |
| 20 |
var state = buildState(g, weightFn || DEFAULT_WEIGHT_FN); |
| 21 |
var results = doGreedyFAS(state.graph, state.buckets, state.zeroIdx); |
| 22 |
|
| 23 |
|
| 24 |
return _.flatten(_.map(results, function(e) { |
| 25 |
return g.outEdges(e.v, e.w); |
| 26 |
}), true); |
| 27 |
} |
| 28 |
|
| 29 |
function doGreedyFAS(g, buckets, zeroIdx) { |
| 30 |
var results = []; |
| 31 |
var sources = buckets[buckets.length - 1]; |
| 32 |
var sinks = buckets[0]; |
| 33 |
|
| 34 |
var entry; |
| 35 |
while (g.nodeCount()) { |
| 36 |
while ((entry = sinks.dequeue())) { removeNode(g, buckets, zeroIdx, entry); } |
| 37 |
while ((entry = sources.dequeue())) { removeNode(g, buckets, zeroIdx, entry); } |
| 38 |
if (g.nodeCount()) { |
| 39 |
for (var i = buckets.length - 2; i > 0; --i) { |
| 40 |
entry = buckets[i].dequeue(); |
| 41 |
if (entry) { |
| 42 |
results = results.concat(removeNode(g, buckets, zeroIdx, entry, true)); |
| 43 |
break; |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return results; |
| 50 |
} |
| 51 |
|
| 52 |
function removeNode(g, buckets, zeroIdx, entry, collectPredecessors) { |
| 53 |
var results = collectPredecessors ? [] : undefined; |
| 54 |
|
| 55 |
_.forEach(g.inEdges(entry.v), function(edge) { |
| 56 |
var weight = g.edge(edge); |
| 57 |
var uEntry = g.node(edge.v); |
| 58 |
|
| 59 |
if (collectPredecessors) { |
| 60 |
results.push({ v: edge.v, w: edge.w }); |
| 61 |
} |
| 62 |
|
| 63 |
uEntry.out -= weight; |
| 64 |
assignBucket(buckets, zeroIdx, uEntry); |
| 65 |
}); |
| 66 |
|
| 67 |
_.forEach(g.outEdges(entry.v), function(edge) { |
| 68 |
var weight = g.edge(edge); |
| 69 |
var w = edge.w; |
| 70 |
var wEntry = g.node(w); |
| 71 |
wEntry["in"] -= weight; |
| 72 |
assignBucket(buckets, zeroIdx, wEntry); |
| 73 |
}); |
| 74 |
|
| 75 |
g.removeNode(entry.v); |
| 76 |
|
| 77 |
return results; |
| 78 |
} |
| 79 |
|
| 80 |
function buildState(g, weightFn) { |
| 81 |
var fasGraph = new Graph(); |
| 82 |
var maxIn = 0; |
| 83 |
var maxOut = 0; |
| 84 |
|
| 85 |
_.forEach(g.nodes(), function(v) { |
| 86 |
fasGraph.setNode(v, { v: v, "in": 0, out: 0 }); |
| 87 |
}); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
_.forEach(g.edges(), function(e) { |
| 92 |
var prevWeight = fasGraph.edge(e.v, e.w) || 0; |
| 93 |
var weight = weightFn(e); |
| 94 |
var edgeWeight = prevWeight + weight; |
| 95 |
fasGraph.setEdge(e.v, e.w, edgeWeight); |
| 96 |
maxOut = Math.max(maxOut, fasGraph.node(e.v).out += weight); |
| 97 |
maxIn = Math.max(maxIn, fasGraph.node(e.w)["in"] += weight); |
| 98 |
}); |
| 99 |
|
| 100 |
var buckets = _.range(maxOut + maxIn + 3).map(function() { return new List(); }); |
| 101 |
var zeroIdx = maxIn + 1; |
| 102 |
|
| 103 |
_.forEach(fasGraph.nodes(), function(v) { |
| 104 |
assignBucket(buckets, zeroIdx, fasGraph.node(v)); |
| 105 |
}); |
| 106 |
|
| 107 |
return { graph: fasGraph, buckets: buckets, zeroIdx: zeroIdx }; |
| 108 |
} |
| 109 |
|
| 110 |
function assignBucket(buckets, zeroIdx, entry) { |
| 111 |
if (!entry.out) { |
| 112 |
buckets[0].enqueue(entry); |
| 113 |
} else if (!entry["in"]) { |
| 114 |
buckets[buckets.length - 1].enqueue(entry); |
| 115 |
} else { |
| 116 |
buckets[entry.out - entry["in"] + zeroIdx].enqueue(entry); |
| 117 |
} |
| 118 |
} |