Skip to content

Commit 17e832b

Browse files
committed
dfs example
1 parent 183e176 commit 17e832b

6 files changed

Lines changed: 240 additions & 4 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ console.log(g.adj(0)); // display the adjacency list which are vertices directed
8686

8787
### Create undirected weighted graph
8888

89-
The sample code below shows show to create undirected weighted graph:
89+
The sample code below shows show to create undirected weighted graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-weighted-graph.html)):
90+
9091

9192
```javascript
9293
var jsgraphs = require('js-graph-algorithms');
@@ -117,7 +118,7 @@ console.log(g.adj(0)); // display the adjacency list which are undirected edges
117118

118119
### Create directed weighted graph
119120

120-
The sample code below shows show to create directed weighted graph:
121+
The sample code below shows show to create directed weighted graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-weighted-digraph.html)):
121122

122123
```javascript
123124
var jsgraphs = require('js-graph-algorithms');
@@ -148,7 +149,7 @@ console.log(g.adj(0)); // display the adjacency list which are directed edges fr
148149

149150
### Depth First Search
150151

151-
The sample code below show how to perform depth first search of an undirected graph
152+
The sample code below show how to perform depth first search of an undirected graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-dfs.html)):
152153

153154
```javascript
154155
var jsgraphs = require('js-graph-algorithms');

examples/example-dfs.html

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<html>
2+
<head>
3+
<title>
4+
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>Graph</h2>
13+
<div id="mynetwork"></div>
14+
15+
<script type="text/javascript">
16+
(function(){
17+
var g = new jsgraphs.Graph(6);
18+
g.addEdge(0, 5);
19+
g.addEdge(2, 4);
20+
g.addEdge(2, 3);
21+
g.addEdge(1, 2);
22+
g.addEdge(0, 1);
23+
g.addEdge(3, 4);
24+
g.addEdge(3, 5);
25+
g.addEdge(0, 2);
26+
var s = 0;
27+
var dfs = new jsgraphs.DepthFirstSearch(g, s);
28+
29+
30+
31+
32+
var g_nodes = [];
33+
var g_edges = [];
34+
for(var v=0; v < g.V; ++v){
35+
g.node(v).label = 'Node ' + v; // assigned 'Node {v}' as label for node v
36+
g_nodes.push({
37+
id: v,
38+
label: g.node(v).label
39+
});
40+
41+
if(v != s && dfs.hasPathTo(v)) {
42+
console.log(s + " is connected to " + v);
43+
var path = dfs.pathTo(v);
44+
for(var j = 1; j < path.length; ++j) {
45+
var x1 = path[j-1];
46+
var x2 = path[j];
47+
g_edges.push({
48+
from: x1,
49+
to: x2,
50+
arrows: 'to'
51+
});
52+
}
53+
} else {
54+
console.log('No path from ' + s + ' to ' + v);
55+
}
56+
57+
}
58+
59+
60+
console.log(g.V); // display 6, which is the number of vertices in g
61+
console.log(g.adj(0)); // display [5, 1, 2], which is the adjacent list to vertex 0
62+
63+
var nodes = new vis.DataSet(g_nodes);
64+
65+
// create an array with edges
66+
var edges = new vis.DataSet(g_edges);
67+
68+
// create a network
69+
var container = document.getElementById('mynetwork');
70+
var data = {
71+
nodes: nodes,
72+
edges: edges
73+
};
74+
var options = {};
75+
var network = new vis.Network(container, data, options);
76+
})();
77+
</script>
78+
</body>
79+
</html>

examples/example-digraph.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<html>
22
<head>
33
<title>
4-
Graph
4+
DiGraph
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>Directed Graph</h2>
1213
<div id="mynetwork"></div>
1314

1415
<script type="text/javascript">

examples/example-graph.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</head>
1010

1111
<body>
12+
<h2>Graph</h2>
1213
<div id="mynetwork"></div>
1314

1415
<script type="text/javascript">
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<html>
2+
<head>
3+
<title>
4+
Weighted DiGraph
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>Weighted DiGraph</h2>
13+
<div id="mynetwork"></div>
14+
15+
<script type="text/javascript">
16+
(function(){
17+
var g = new jsgraphs.WeightedDiGraph(8); // 8 is the number vertices in the graph
18+
g.addEdge(new jsgraphs.Edge(0, 7, 0.16));
19+
g.addEdge(new jsgraphs.Edge(2, 3, 0.17));
20+
g.addEdge(new jsgraphs.Edge(1, 7, 0.19));
21+
g.addEdge(new jsgraphs.Edge(0, 2, 0.26));
22+
g.addEdge(new jsgraphs.Edge(5, 7, 0.28));
23+
g.addEdge(new jsgraphs.Edge(1, 3, 0.29));
24+
g.addEdge(new jsgraphs.Edge(1, 5, 0.32));
25+
g.addEdge(new jsgraphs.Edge(2, 7, 0.34));
26+
g.addEdge(new jsgraphs.Edge(4, 5, 0.35));
27+
g.addEdge(new jsgraphs.Edge(1, 2, 0.36));
28+
g.addEdge(new jsgraphs.Edge(4, 7, 0.37));
29+
g.addEdge(new jsgraphs.Edge(0, 4, 0.38));
30+
g.addEdge(new jsgraphs.Edge(6, 2, 0.4));
31+
g.addEdge(new jsgraphs.Edge(3, 6, 0.52));
32+
g.addEdge(new jsgraphs.Edge(6, 0, 0.58));
33+
g.addEdge(new jsgraphs.Edge(6, 4, 0.93));
34+
35+
var g_nodes = [];
36+
var g_edges = [];
37+
for(var v=0; v < g.V; ++v){
38+
g.node(v).label = 'Node ' + v; // assigned 'Node {v}' as label for node v
39+
g_nodes.push({
40+
id: v,
41+
label: g.node(v).label
42+
});
43+
44+
var adj_v = g.adj(v);
45+
for(var i = 0; i < adj_v.length; ++i) {
46+
var e = adj_v[i];
47+
var w = e.other(v);
48+
g_edges.push({
49+
from: v,
50+
to: w,
51+
length: e.weight,
52+
label: '' + e.weight,
53+
arrows:'to'
54+
});
55+
};
56+
}
57+
58+
console.log(g.V); // display 6, which is the number of vertices in g
59+
console.log(g.adj(0)); // display [5, 1, 2], which is the adjacent list to vertex 0
60+
61+
var nodes = new vis.DataSet(g_nodes);
62+
63+
// create an array with edges
64+
var edges = new vis.DataSet(g_edges);
65+
66+
// create a network
67+
var container = document.getElementById('mynetwork');
68+
var data = {
69+
nodes: nodes,
70+
edges: edges
71+
};
72+
var options = {};
73+
var network = new vis.Network(container, data, options);
74+
})();
75+
</script>
76+
</body>
77+
</html>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<html>
2+
<head>
3+
<title>
4+
Weighted 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>Weighted Graph</h2>
13+
<div id="mynetwork"></div>
14+
15+
<script type="text/javascript">
16+
(function(){
17+
var g = new jsgraphs.WeightedGraph(8); // 8 is the number vertices in the graph
18+
g.addEdge(new jsgraphs.Edge(0, 7, 0.16));
19+
g.addEdge(new jsgraphs.Edge(2, 3, 0.17));
20+
g.addEdge(new jsgraphs.Edge(1, 7, 0.19));
21+
g.addEdge(new jsgraphs.Edge(0, 2, 0.26));
22+
g.addEdge(new jsgraphs.Edge(5, 7, 0.28));
23+
g.addEdge(new jsgraphs.Edge(1, 3, 0.29));
24+
g.addEdge(new jsgraphs.Edge(1, 5, 0.32));
25+
g.addEdge(new jsgraphs.Edge(2, 7, 0.34));
26+
g.addEdge(new jsgraphs.Edge(4, 5, 0.35));
27+
g.addEdge(new jsgraphs.Edge(1, 2, 0.36));
28+
g.addEdge(new jsgraphs.Edge(4, 7, 0.37));
29+
g.addEdge(new jsgraphs.Edge(0, 4, 0.38));
30+
g.addEdge(new jsgraphs.Edge(6, 2, 0.4));
31+
g.addEdge(new jsgraphs.Edge(3, 6, 0.52));
32+
g.addEdge(new jsgraphs.Edge(6, 0, 0.58));
33+
g.addEdge(new jsgraphs.Edge(6, 4, 0.93));
34+
35+
var g_nodes = [];
36+
var g_edges = [];
37+
for(var v=0; v < g.V; ++v){
38+
g.node(v).label = 'Node ' + v; // assigned 'Node {v}' as label for node v
39+
g_nodes.push({
40+
id: v,
41+
label: g.node(v).label
42+
});
43+
44+
var adj_v = g.adj(v);
45+
for(var i = 0; i < adj_v.length; ++i) {
46+
var e = adj_v[i];
47+
var w = e.other(v);
48+
if(w > v) continue; // make sure only one edge between w and v since the graph is undirected
49+
g_edges.push({
50+
from: v,
51+
to: w,
52+
length: e.weight,
53+
label: '' + e.weight
54+
});
55+
};
56+
}
57+
58+
console.log(g.V); // display 6, which is the number of vertices in g
59+
console.log(g.adj(0)); // display [5, 1, 2], which is the adjacent list to vertex 0
60+
61+
var nodes = new vis.DataSet(g_nodes);
62+
63+
// create an array with edges
64+
var edges = new vis.DataSet(g_edges);
65+
66+
// create a network
67+
var container = document.getElementById('mynetwork');
68+
var data = {
69+
nodes: nodes,
70+
edges: edges
71+
};
72+
var options = {};
73+
var network = new vis.Network(container, data, options);
74+
})();
75+
</script>
76+
</body>
77+
</html>

0 commit comments

Comments
 (0)