@@ -29,7 +29,7 @@ The sample code below shows how to create a undirected and unweighted graph:
2929``` javascript
3030var jsgraphs = require (' js-graph-algorithms' );
3131
32- var g = new jsgraphs.Graph (6 );
32+ var g = new jsgraphs.Graph (6 );// 6 is the number vertices in the graph
3333g .addEdge (0 , 5 ); // add undirected edge connecting vertex 0 to vertex 5
3434g .addEdge (2 , 4 );
3535g .addEdge (2 , 3 );
@@ -39,6 +39,9 @@ g.addEdge(3, 4);
3939g .addEdge (3 , 5 );
4040g .addEdge (0 , 2 );
4141
42+ g .node (2 ).label = ' Hello' ; // assigned 'Hello' as label for node 2
43+ g .edge (0 , 2 ).label = ' World' ; // edge between 0 and 2
44+ 4245console .log (g .V ); // display 6, which is the number of vertices in g
4346console .log (g .adj (0 )); // display [5, 1, 2], which is the adjacent list to vertex 0
4447```
@@ -50,7 +53,7 @@ The sample code below shows how to create a direted and unweighted graph:
5053``` javascript
5154var jsgraphs = require (' js-graph-algorithms' );
5255
53- var g = new jsgraphs.DiGraph (13 );
56+ var g = new jsgraphs.DiGraph (13 );// 13 is the number vertices in the graph
5457g .addEdge (4 , 2 ); // add directed edge from 4 to 2
5558g .addEdge (2 , 3 );
5659g .addEdge (3 , 2 );
@@ -74,6 +77,9 @@ g.addEdge(6, 4);
7477g .addEdge (6 , 9 );
7578g .addEdge (7 , 6 );
7679
80+ g .node (2 ).label = ' Hello' ; // assign 'Hello' as label for node 2
81+ g .edge (0 , 5 ).label = ' World' ; // edge from 0 to 5
82+ 7783console .log (g .V ); // display 13, which is the number of vertices in g
7884console .log (g .adj (0 )); // display the adjacency list which are vertices directed from vertex 0
7985```
@@ -84,7 +90,7 @@ The sample code below shows show to create undirected weighted graph:
8490
8591``` javascript
8692var jsgraphs = require (' js-graph-algorithms' );
87- var g = new jsgraphs.WeightedGraph (8 );
93+ var g = new jsgraphs.WeightedGraph (8 );// 8 is the number vertices in the graph
8894g .addEdge (new jsgraphs.Edge (0 , 7 , 0.16 ));
8995g .addEdge (new jsgraphs.Edge (2 , 3 , 0.17 ));
9096g .addEdge (new jsgraphs.Edge (1 , 7 , 0.19 ));
@@ -102,6 +108,9 @@ g.addEdge(new jsgraphs.Edge(3, 6, 0.52));
102108g .addEdge (new jsgraphs.Edge (6 , 0 , 0.58 ));
103109g .addEdge (new jsgraphs.Edge (6 , 4 , 0.93 ));
104110
111+ g .node (2 ).label = ' Hello' ; // assign 'Hello' as label for node 2
112+ g .edge (4 , 5 ).label = ' World' ; // edge between node 4 and 5
113+ 105114console .log (g .V ); // display 13, which is the number of vertices in g
106115console .log (g .adj (0 )); // display the adjacency list which are undirected edges connected to vertex 0
107116```
@@ -112,7 +121,7 @@ The sample code below shows show to create directed weighted graph:
112121
113122``` javascript
114123var jsgraphs = require (' js-graph-algorithms' );
115- var g = new jsgraphs.WeightedDiGraph (8 );
124+ var g = new jsgraphs.WeightedDiGraph (8 );// 8 is the number vertices in the graph
116125g .addEdge (new jsgraphs.Edge (0 , 7 , 0.16 ));
117126g .addEdge (new jsgraphs.Edge (2 , 3 , 0.17 ));
118127g .addEdge (new jsgraphs.Edge (1 , 7 , 0.19 ));
@@ -130,6 +139,9 @@ g.addEdge(new jsgraphs.Edge(3, 6, 0.52));
130139g .addEdge (new jsgraphs.Edge (6 , 0 , 0.58 ));
131140g .addEdge (new jsgraphs.Edge (6 , 4 , 0.93 ));
132141
142+ g .node (2 ).label = ' Hello' ;
143+ g .edge (4 , 5 ).label = ' World' ; // edge from node 4 to node 5
144+ 133145console .log (g .V ); // display 13, which is the number of vertices in g
134146console .log (g .adj (0 )); // display the adjacency list which are directed edges from vertex 0
135147```
0 commit comments