Skip to content

Commit f9e143c

Browse files
committed
topo sort example
1 parent 05f68a8 commit f9e143c

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ for (var v = 0; v < g.V; ++v) {
208208

209209
### Topological Sort
210210

211-
The sample code below show how to obtain the reverse post order of a topological sort in a directed acyclic graph:
211+
The sample code below show how to obtain the reverse post order of a topological sort in a directed acyclic graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-topo-sort.html)):
212212

213213
```javascript
214214
var jsgraphs = require('js-graph-algorithms');

examples/example-connected-components.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<html>
22
<head>
33
<title>
4-
Graph
4+
Connected Components on Graph
55
</title>
66
<script src="../third-party-libs/vis/vis.js" type="text/javascript"></script>
77
<script src="../src/jsgraphs.js" type="text/javascript"></script>
88
<link href="../third-party-libs/vis/vis.css" type="text/css" />
99
</head>
1010

1111
<body>
12-
<h2>Graph</h2>
12+
<h2>Connected Components on Graph</h2>
1313
<div id="mynetwork"></div>
1414

1515
<script type="text/javascript">

examples/example-dfs.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<html>
22
<head>
33
<title>
4-
Graph
4+
Depth First Search on Graph
55
</title>
66
<script src="../third-party-libs/vis/vis.js" type="text/javascript"></script>
77
<script src="../src/jsgraphs.js" type="text/javascript"></script>
88
<link href="../third-party-libs/vis/vis.css" type="text/css" />
99
</head>
1010

1111
<body>
12-
<h2>Graph</h2>
12+
<h2>Depth First Search on Graph</h2>
1313
<div id="mynetwork"></div>
1414

1515
<script type="text/javascript">

examples/example-topo-sort.html

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+
Topological Sort on Directed Acyclic 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>Topological Sort on Directed Acyclic Graph</h2>
13+
<div id="mynetwork"></div>
14+
15+
<script type="text/javascript">
16+
(function(){
17+
var g = new jsgraphs.DiGraph(7); // must be directed acyclic graph
18+
19+
g.addEdge(0, 5);
20+
g.addEdge(0, 2);
21+
g.addEdge(0, 1);
22+
g.addEdge(3, 6);
23+
g.addEdge(3, 5);
24+
g.addEdge(3, 4);
25+
g.addEdge(5, 4);
26+
g.addEdge(6, 4);
27+
g.addEdge(6, 0);
28+
g.addEdge(3, 2);
29+
g.addEdge(1, 4);
30+
31+
var ts = new jsgraphs.TopologicalSort(g);
32+
33+
var order = ts.order();
34+
console.log(order); // display array which is the topological sort order
35+
36+
var g_nodes = [];
37+
var g_edges = [];
38+
for(var v=0; v < g.V; ++v){
39+
g.node(v).label = 'Node ' + v; // assigned 'Node {v}' as label for node v
40+
g_nodes.push({
41+
id: v,
42+
label: g.node(v).label
43+
});
44+
45+
var adj_v = g.adj(v);
46+
for(var i = 0; i < adj_v.length; ++i) {
47+
var w = adj_v[i];
48+
g_edges.push({
49+
from: v,
50+
to: w,
51+
arrows:'to'
52+
});
53+
};
54+
}
55+
56+
for(var i = 1; i < order.length; ++i) {
57+
var x1 = order[i-1];
58+
var x2 = order[i];
59+
g_edges.push({
60+
from: x1,
61+
to: x2,
62+
color: '#ff0000',
63+
arrows: 'to'
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)