Created
June 8, 2014 22:09
-
-
Save ivanoats/42e01b465b20caa47dab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Vertex = function(label) { | |
this.label = label; | |
}; | |
var Graph = function(v) { | |
this.vertices = v; | |
this.edges = 0; | |
this.adjacencies = []; | |
var i; | |
for (i = 0; i < this.vertices; ++i ) { | |
this.adjacencies[i] = []; | |
this.adjacencies[i].push(''); | |
} | |
}; | |
Graph.prototype.addEdge = function(from,to) { | |
this.adjacencies[from].push(to); | |
this.adjacencies[to].push(from); | |
}; | |
Graph.prototype.showGraph = function() { | |
var i,j; | |
for (i = 0; i < this.vertices; ++i) { | |
console.log(i + ' ->'); | |
for (j = 0; j < this.vertices; ++j) { | |
if (this.adjacencies[i][j] !== undefined) { | |
console.log(this.adjacencies[i][j] + ' '); | |
} | |
} | |
console.log('\n'); | |
} | |
}; | |
module.exports.Vertex = Vertex; | |
module.exports.Graph = Graph; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Graph = require('./graph').Graph; | |
var g = new Graph(5); | |
g.addEdge(0,1); | |
g.addEdge(0,2); | |
g.addEdge(1,3); | |
g.addEdge(2,4); | |
g.showGraph(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not sure if I like how he's creating the adjacencies array here. Maybe
adjacencies = new Array(v)
?