Skip to content

Commit 46e3512

Browse files
committed
ford fulkerson example
1 parent ce7ac75 commit 46e3512

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ for(var v = 1; v < g.V; ++v){
501501

502502
### Find the MaxFlow-MinCut using Ford-Fulkerson algorithm
503503

504-
The sample code below show how to obtain the MaxFlow-MinCut of a directed weighted graph using ford-fulkerson algorithm:
504+
The sample code below show how to obtain the MaxFlow-MinCut of a directed weighted graph using ford-fulkerson algorithm (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-ford-fulkerson.html)):
505505

506506
```javascript
507507
var jsgraphs = require('js-graph-algorithms');
@@ -522,6 +522,9 @@ g.addEdge(new jsgraphs.FlowEdge(5, 6, 15));
522522
g.addEdge(new jsgraphs.FlowEdge(6, 2, 6));
523523
g.addEdge(new jsgraphs.FlowEdge(6, 7, 10));
524524

525+
g.node(2).label = 'Hello';
526+
g.edge(0, 1).label = 'World';
527+
525528
var source = 0;
526529
var target = 7;
527530
var ff = new jsgraphs.FordFulkerson(g, source, target);

examples/example-ford-fulkerson.html

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<html>
2+
<head>
3+
<title>
4+
MaxFlow-MinCut (Ford-Fulkerson)
5+
</title>
6+
<script src="../third-party-libs/vis/vis.js" type="text/javascript"></script>
7+
<script src="../src/jsgraphs.js" type="text/javascript"></script>
8+
<link href="../third-party-libs/vis/vis.css" type="text/css" />
9+
</head>
10+
11+
<body>
12+
<h2>MaxFlow-MinCut (Ford-Fulkerson)</h2>
13+
<div id="mynetwork"></div>
14+
15+
<script type="text/javascript">
16+
(function(){
17+
var g = new jsgraphs.FlowNetwork(8);
18+
g.addEdge(new jsgraphs.FlowEdge(0, 1, 10));
19+
g.addEdge(new jsgraphs.FlowEdge(0, 2, 5));
20+
g.addEdge(new jsgraphs.FlowEdge(0, 3, 15));
21+
g.addEdge(new jsgraphs.FlowEdge(1, 4, 9));
22+
g.addEdge(new jsgraphs.FlowEdge(1, 5, 15));
23+
g.addEdge(new jsgraphs.FlowEdge(1, 2, 4));
24+
g.addEdge(new jsgraphs.FlowEdge(2, 5, 8));
25+
g.addEdge(new jsgraphs.FlowEdge(2, 3, 4));
26+
g.addEdge(new jsgraphs.FlowEdge(3, 6, 16));
27+
g.addEdge(new jsgraphs.FlowEdge(4, 5, 15));
28+
g.addEdge(new jsgraphs.FlowEdge(4, 7, 10));
29+
g.addEdge(new jsgraphs.FlowEdge(5, 7, 10));
30+
g.addEdge(new jsgraphs.FlowEdge(5, 6, 15));
31+
g.addEdge(new jsgraphs.FlowEdge(6, 2, 6));
32+
g.addEdge(new jsgraphs.FlowEdge(6, 7, 10));
33+
34+
var source = 0;
35+
var target = 7;
36+
var ff = new jsgraphs.FordFulkerson(g, source, target);
37+
console.log('max-flow: ' + ff.value);
38+
39+
var minCut = ff.minCut(g);
40+
41+
42+
var g_nodes = [];
43+
var g_edges = [];
44+
for(var v=0; v < g.V; ++v){
45+
g.node(v).label = 'Node ' + v; // assigned 'Node {v}' as label for node v
46+
if(v == source) g.node(v).label = 'Source ' + v;
47+
if(v == target) g.node(v).label = 'Target ' + v;
48+
g_nodes.push({
49+
id: v,
50+
label: g.node(v).label,
51+
group: (v == source || v == target ? 2 : 5)
52+
});
53+
}
54+
55+
for(var i = 0; i < minCut.length; ++i) {
56+
var e = minCut[i];
57+
e.highlighted = true;
58+
console.log('min-cut: (' + e.from() + ", " + e.to() + ')');
59+
var v = e.from();
60+
var w = e.to();
61+
g_edges.push({
62+
from: v,
63+
to: w,
64+
label: '' + e.flow + '/' + e.capacity,
65+
arrows:'to',
66+
color: '#ff0000'
67+
});
68+
}
69+
70+
71+
for(var v = 0; v < g.V; ++v) {
72+
var adj_v = g.adj(v);
73+
for(var i = 0; i < adj_v.length; ++i) {
74+
var e = adj_v[i];
75+
var w = e.other(v);
76+
if(e.highlighted) continue;
77+
g_edges.push({
78+
from: v,
79+
to: w,
80+
label: '' + e.flow + '/' + e.capacity,
81+
arrows:'to'
82+
});
83+
};
84+
}
85+
86+
console.log(g.V); // display 6, which is the number of vertices in g
87+
console.log(g.adj(0)); // display [5, 1, 2], which is the adjacent list to vertex 0
88+
89+
var nodes = new vis.DataSet(g_nodes);
90+
91+
// create an array with edges
92+
var edges = new vis.DataSet(g_edges);
93+
94+
// create a network
95+
var container = document.getElementById('mynetwork');
96+
var data = {
97+
nodes: nodes,
98+
edges: edges
99+
};
100+
var options = {};
101+
var network = new vis.Network(container, data, options);
102+
})();
103+
</script>
104+
</body>
105+
</html>

src/jsgraphs.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,26 @@ var jsgraphs = jsgraphs || {};
573573
var FlowNetwork = function(V) {
574574
this.V = V;
575575
this.adjList = [];
576+
this.nodeInfo = [];
576577
for(var v = 0; v < V; ++v) {
577578
this.adjList.push([]);
579+
this.nodeInfo.push({});
580+
}
581+
};
582+
583+
FlowNetwork.prototype.node = function(v) {
584+
return this.nodeInfo[v];
585+
};
586+
587+
FlowNetwork.prototype.edge = function(v, w) {
588+
var adj_v = this.adjList[v];
589+
for(var i=0; i < adj_v.length; ++i) {
590+
var x = adj_v[i].other(v);
591+
if(x == w) {
592+
return adj_v[i];
593+
}
578594
}
595+
return null;
579596
};
580597

581598
FlowNetwork.prototype.addEdge = function(e) {

0 commit comments

Comments
 (0)