-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
37 lines (28 loc) · 678 Bytes
/
graph.js
File metadata and controls
37 lines (28 loc) · 678 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//adjacency list
var AdjList = function(numVerts) {
var table = [];
var Constructor = function() {};
Constructor.prototype.init = function() {
for (var i = 0; i <= numVerts; i++) { //not using index 0
table.push([]);
}
};
Constructor.prototype.addEdge = function(v1,v2) {
if (!table[v1]) {
table[v1] = [];
}
table[v1].push(v2);
if (!table[v2]) {
table[v2] = [];
}
table[v2].push(v1);
};
Constructor.prototype.getNeighbors = function(v) {
return table[v];
};
Constructor.prototype.numVertices = function() {
return numVerts;
};
return new Constructor();
}
module.exports.AdjList = AdjList;