Skip to content

Commit c332f6a

Browse files
committed
add node and edge for the graph
1 parent 9bc09ab commit c332f6a

7 files changed

Lines changed: 96 additions & 4 deletions

File tree

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The sample code below shows how to create a undirected and unweighted graph:
2929
```javascript
3030
var 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
3333
g.addEdge(0, 5); // add undirected edge connecting vertex 0 to vertex 5
3434
g.addEdge(2, 4);
3535
g.addEdge(2, 3);
@@ -39,6 +39,9 @@ g.addEdge(3, 4);
3939
g.addEdge(3, 5);
4040
g.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+
4245
console.log(g.V); // display 6, which is the number of vertices in g
4346
console.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
5154
var 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
5457
g.addEdge(4, 2); // add directed edge from 4 to 2
5558
g.addEdge(2, 3);
5659
g.addEdge(3, 2);
@@ -74,6 +77,9 @@ g.addEdge(6, 4);
7477
g.addEdge(6, 9);
7578
g.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+
7783
console.log(g.V); // display 13, which is the number of vertices in g
7884
console.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
8692
var 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
8894
g.addEdge(new jsgraphs.Edge(0, 7, 0.16));
8995
g.addEdge(new jsgraphs.Edge(2, 3, 0.17));
9096
g.addEdge(new jsgraphs.Edge(1, 7, 0.19));
@@ -102,6 +108,9 @@ g.addEdge(new jsgraphs.Edge(3, 6, 0.52));
102108
g.addEdge(new jsgraphs.Edge(6, 0, 0.58));
103109
g.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+
105114
console.log(g.V); // display 13, which is the number of vertices in g
106115
console.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
114123
var 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
116125
g.addEdge(new jsgraphs.Edge(0, 7, 0.16));
117126
g.addEdge(new jsgraphs.Edge(2, 3, 0.17));
118127
g.addEdge(new jsgraphs.Edge(1, 7, 0.19));
@@ -130,6 +139,9 @@ g.addEdge(new jsgraphs.Edge(3, 6, 0.52));
130139
g.addEdge(new jsgraphs.Edge(6, 0, 0.58));
131140
g.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+
133145
console.log(g.V); // display 13, which is the number of vertices in g
134146
console.log(g.adj(0)); // display the adjacency list which are directed edges from vertex 0
135147
```

examples/example-graph.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Graph
55
</title>
66
<script src="../third-party-libs/vis/vis.js" type="text/javascript"></script>
7+
<script src="../src/jsgraphs.js" type="text/javascript"></script>
78
<link href="../third-party-libs/vis/vis.css" type="text/css" />
89
</head>
910

src/jsgraphs.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,38 +345,79 @@ var jsgraphs = jsgraphs || {};
345345
var Graph = function (V) {
346346
this.V = V;
347347
this.adjList = [];
348+
this.nodeInfo = [];
349+
this.edges = {};
348350
for (var i = 0; i < V; ++i) {
349351
this.adjList.push([]);
352+
this.nodeInfo.push({});
350353
}
351354
};
352355

353356
Graph.prototype.addEdge = function(v, w){
354357
this.adjList[v].push(w);
355358
this.adjList[w].push(v);
359+
var edge_id = v + '_' + w;
360+
if(v > w) {
361+
edge_id = w + '_' + v;
362+
}
363+
this.edges[edge_id] = new jss.Edge(v, w, 0);
356364
};
357365

358366
Graph.prototype.adj = function(v) {
359367
return this.adjList[v];
360368
};
361369

370+
Graph.prototype.node = function(v) {
371+
return this.nodeInfo[v];
372+
};
373+
374+
Graph.prototype.edge = function(v, w) {
375+
var edge_id = v + '_' + w;
376+
if(v > w) {
377+
edge_id = w + '_' + v;
378+
}
379+
if (edge_id in this.edges) {
380+
return this.edges[edge_id];
381+
}
382+
return null;
383+
};
384+
362385
jss.Graph = Graph;
363386

364387
var DiGraph = function(V) {
365388
this.V = V;
366389
this.adjList = [];
390+
this.nodeInfo = [];
391+
this.edges = {};
367392
for (var v = 0; v < V; ++v){
368393
this.adjList.push([]);
394+
this.nodeInfo.push({});
369395
}
370396
};
371397

372398
DiGraph.prototype.addEdge = function(v, w){
373399
this.adjList[v].push(w);
400+
var edge_id = v + '_' + w;
401+
this.edges[edge_id] = new jss.Edge(v, w, 0);
402+
};
403+
404+
DiGraph.prototype.edge = function(v, w) {
405+
var edge_id = v + '_' + w;
406+
if(edge_id in this.edges) {
407+
return this.edges[edge_id];
408+
} else {
409+
return null;
410+
}
374411
};
375412

376413
DiGraph.prototype.adj = function(v) {
377414
return this.adjList[v];
378415
};
379416

417+
DiGraph.prototype.node = function(v) {
418+
return this.nodeInfo[v];
419+
};
420+
380421
DiGraph.prototype.reverse = function(){
381422
var g = new DiGraph(this.V);
382423
for (var v = 0; v < this.V; ++v) {
@@ -418,16 +459,33 @@ var jsgraphs = jsgraphs || {};
418459
var WeightedGraph = function(V) {
419460
this.V = V;
420461
this.adjList = [];
462+
this.nodeInfo = [];
421463

422464
for ( var v = 0; v < V; ++v) {
423465
this.adjList.push([]);
466+
this.nodeInfo.push({});
424467
}
425468
};
426469

427470
WeightedGraph.prototype.adj = function(v) {
428471
return this.adjList[v];
429472
};
430473

474+
WeightedGraph.prototype.edge = function(v, w) {
475+
var adj_v = this.adjList[v];
476+
for(var i=0; i < adj_v.length; ++i) {
477+
var x = adj_v[i].other(v);
478+
if(x == w) {
479+
return adj_v[i];
480+
}
481+
}
482+
return null;
483+
};
484+
485+
WeightedGraph.prototype.node = function(v) {
486+
return this.nodeInfo[v];
487+
};
488+
431489
WeightedGraph.prototype.addEdge = function(e) {
432490
var v = e.either();
433491
var w = e.other(v);
@@ -448,6 +506,17 @@ var jsgraphs = jsgraphs || {};
448506
this.adjList[v].push(e);
449507
};
450508

509+
WeightedDiGraph.prototype.edge = function(v, w) {
510+
var adj_v = this.adjList[v];
511+
for(var i=0; i < adj_v.length; ++i) {
512+
var x = adj_v[i].other(v);
513+
if(x == w) {
514+
return adj_v[i];
515+
}
516+
}
517+
return null;
518+
};
519+
451520
WeightedDiGraph.prototype.toDiGraph = function() {
452521
var g = new jss.DiGraph(this.V);
453522
for(var v = 0; v < this.V; ++v) {

test/directed-graph-spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ describe("Create various types of DiGraph", function() {
2626
g.addEdge(6, 4);
2727
g.addEdge(6, 9);
2828
g.addEdge(7, 6);
29+
30+
g.edge(2, 0).label = 'World';
2931
it("should has 13 vertices", function() {
3032
expect(g.V).to.equal(13);
3133
});

test/graph-spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe("Create various types of Graphs", function() {
1212
g.addEdge(3, 4);
1313
g.addEdge(3, 5);
1414
g.addEdge(0, 2);
15+
g.node(2).label = 'Hello';
1516
it("should has 6 vertices", function() {
1617
expect(g.V).to.equal(6);
1718
});

test/weighted-digraph-spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ describe('Weigthed Directed Graph', function(){
2121
g.addEdge(new jsgraphs.Edge(7, 5, 6.0));
2222
g.addEdge(new jsgraphs.Edge(7, 2, 7.0));
2323

24+
g.node(4).label = 'Hello';
25+
g.edge(4, 5).label = 'World';
26+
2427
expect(g.V).to.equal(8);
2528
var edgeCount = 0;
2629
for(var v = 0; v < g.V; ++v){

test/weighted-graph-spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ describe("Create various types of Weighted Graphs", function() {
2020
g.addEdge(new jsgraphs.Edge(3, 6, 0.52));
2121
g.addEdge(new jsgraphs.Edge(6, 0, 0.58));
2222
g.addEdge(new jsgraphs.Edge(6, 4, 0.93));
23+
24+
g.node(4).label = 'Hello';
25+
g.edge(4, 5).label = 'World';
26+
2327
it("should has 8 vertices", function() {
2428
expect(g.V).to.equal(8);
2529
});

0 commit comments

Comments
 (0)