Skip to content

Commit e0f92a0

Browse files
committed
strongly connected components
1 parent f9e143c commit e0f92a0

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ console.log(order); // display array which is the topological sort order
236236

237237
### Strongly Connected Components for Directed Graph
238238

239-
The sample code below show how to obtain the strongly connected components from a directed graph:
239+
The sample code below show how to obtain the strongly connected components from a directed graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-strongly-connected-components.html)):
240240

241241
```javascript
242242
var jsgraphs = require('js-graph-algorithms');
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<html>
2+
<head>
3+
<title>
4+
Strongly Connected Components on Graph
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>Strongly Connected Components on Graph</h2>
13+
<div id="mynetwork"></div>
14+
15+
<script type="text/javascript">
16+
(function(){
17+
var g = new jsgraphs.DiGraph(13);
18+
g.addEdge(4, 2);
19+
g.addEdge(2, 3);
20+
g.addEdge(3, 2);
21+
g.addEdge(6, 0);
22+
g.addEdge(0, 1);
23+
g.addEdge(2, 0);
24+
g.addEdge(11, 12);
25+
g.addEdge(12, 9);
26+
g.addEdge(9, 10);
27+
g.addEdge(9, 11);
28+
g.addEdge(8, 9);
29+
g.addEdge(10, 12);
30+
g.addEdge(11, 4);
31+
g.addEdge(4, 3);
32+
g.addEdge(3, 5);
33+
g.addEdge(7, 8);
34+
g.addEdge(8, 7);
35+
g.addEdge(5, 4);
36+
g.addEdge(0, 5);
37+
g.addEdge(6, 4);
38+
g.addEdge(6, 9);
39+
g.addEdge(7, 6);
40+
var scc = new jsgraphs.StronglyConnectedComponents(g);
41+
console.log(scc.componentCount()); // display 5
42+
for (var v = 0; v < g.V; ++v) {
43+
console.log('id[' + v + ']: ' + scc.componentId(v));
44+
}
45+
46+
var g_nodes = [];
47+
var g_edges = [];
48+
for(var v=0; v < g.V; ++v){
49+
g.node(v).label = 'Node ' + v; // assigned 'Node {v}' as label for node v
50+
g_nodes.push({
51+
id: v,
52+
label: g.node(v).label,
53+
group: scc.componentId(v)
54+
});
55+
56+
var adj_v = g.adj(v);
57+
for(var i = 0; i < adj_v.length; ++i) {
58+
var w = adj_v[i];
59+
g_edges.push({
60+
from: v,
61+
to: w,
62+
arrows: 'to'
63+
});
64+
};
65+
}
66+
67+
console.log(g.V); // display 6, which is the number of vertices in g
68+
console.log(g.adj(0)); // display [5, 1, 2], which is the adjacent list to vertex 0
69+
70+
var nodes = new vis.DataSet(g_nodes);
71+
72+
// create an array with edges
73+
var edges = new vis.DataSet(g_edges);
74+
75+
// create a network
76+
var container = document.getElementById('mynetwork');
77+
var data = {
78+
nodes: nodes,
79+
edges: edges
80+
};
81+
var options = {};
82+
var network = new vis.Network(container, data, options);
83+
})();
84+
</script>
85+
</body>
86+
</html>

0 commit comments

Comments
 (0)