Skip to content

Commit 183e176

Browse files
committed
digraph example
1 parent 571d50d commit 183e176

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ npm install js-graph-algorithms
2424

2525
### Create an undirected unweighted graph
2626

27-
The sample code below shows how to create a undirected and unweighted graph (HTML DEMO at [HERE](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-graph.html):
27+
The sample code below shows how to create a undirected and unweighted graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-graph.html)):
2828

2929
```javascript
3030
var jsgraphs = require('js-graph-algorithms');
@@ -48,7 +48,7 @@ console.log(g.adj(0)); // display [5, 1, 2], which is the adjacent list to verte
4848

4949
### Create directed unweighted graph
5050

51-
The sample code below shows how to create a direted and unweighted graph:
51+
The sample code below shows how to create a direted and unweighted graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-digraph.html)):
5252

5353
```javascript
5454
var jsgraphs = require('js-graph-algorithms');

examples/example-digraph.html

+79
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+
<div id="mynetwork"></div>
13+
14+
<script type="text/javascript">
15+
(function(){
16+
var g = new jsgraphs.DiGraph(13); // 13 is the number vertices in the graph
17+
g.addEdge(4, 2); // add directed edge from 4 to 2
18+
g.addEdge(2, 3);
19+
g.addEdge(3, 2);
20+
g.addEdge(6, 0);
21+
g.addEdge(0, 1);
22+
g.addEdge(2, 0);
23+
g.addEdge(11, 12);
24+
g.addEdge(12, 9);
25+
g.addEdge(9, 10);
26+
g.addEdge(9, 11);
27+
g.addEdge(7, 9);
28+
g.addEdge(10, 12);
29+
g.addEdge(11, 4);
30+
g.addEdge(4, 3);
31+
g.addEdge(3, 5);
32+
g.addEdge(6, 8);
33+
g.addEdge(8, 6);
34+
g.addEdge(5, 4);
35+
g.addEdge(0, 5);
36+
g.addEdge(6, 4);
37+
g.addEdge(6, 9);
38+
g.addEdge(7, 6);
39+
40+
var g_nodes = [];
41+
var g_edges = [];
42+
for(var v=0; v < g.V; ++v){
43+
g.node(v).label = 'Node ' + v; // assigned 'Node {v}' as label for node v
44+
g_nodes.push({
45+
id: v,
46+
label: g.node(v).label
47+
});
48+
49+
var adj_v = g.adj(v);
50+
for(var i = 0; i < adj_v.length; ++i) {
51+
var w = adj_v[i];
52+
g_edges.push({
53+
from: v,
54+
to: w,
55+
arrows:'to'
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>

0 commit comments

Comments
 (0)