Skip to content

Instantly share code, notes, and snippets.

@ivanoats
Created June 8, 2014 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanoats/42e01b465b20caa47dab to your computer and use it in GitHub Desktop.
Save ivanoats/42e01b465b20caa47dab to your computer and use it in GitHub Desktop.
'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;
'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();
@ivanoats
Copy link
Author

ivanoats commented Jun 8, 2014

I'm not sure if I like how he's creating the adjacencies array here. Maybe adjacencies = new Array(v) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment