Skip to content

Instantly share code, notes, and snippets.

@4db
Last active May 30, 2018 20:18
Show Gist options
  • Save 4db/2c1dce8fdb804fe24388dfaef3e75310 to your computer and use it in GitHub Desktop.
Save 4db/2c1dce8fdb804fe24388dfaef3e75310 to your computer and use it in GitHub Desktop.
unweighted_graph.js
function Graph() {
var vm = this;
vm.vertices = [];
vm.edges = [];
vm.numberOfEdges = 0;
vm.addVertex = function(vertex) {
vm.vertices.push(vertex);
vm.edges[vertex] = [];
};
vm.removeVertex = function(vertex) {
var index = vm.vertices.indexOf(vertex);
if (index !== -1) {
vm.vertices.splice(index, 1);
}
while (vm.edges[vertex].length) {
var adjacentVertex = vm.edges[vertex].pop();
vm.removeEdge(adjacentVertex, vertex);
}
};
vm.addEdge = function(a, b) {
vm.edges[a].push(b);
vm.edges[b].push(a);
vm.numberOfEdges++;
};
vm.removeEdge = function(a, b) {
var index = vm.edges[a] ? vm.edges[a].indexOf(b) : -1;
if (index !== -1) {
vm.edges[a].splice(index, 1);
vm.numberOfEdges--;
}
var index = vm.edges[b] ? vm.edges[b].indexOf(a) : -1;
if (index !== -1) {
vm.edges[b].splice(index, 1);
}
};
vm.relations = function() {
return vm.numberOfEdges;
};
}
/* Uncomment for testing
var graph = new Graph();
graph.addVertex(1);
graph.addVertex(2);
graph.addVertex(3);
graph.addVertex(4);
graph.addVertex(5);
graph.addVertex(6);
// 1 -> | 2 -> | 3 -> | 4 -> | 5 -> | 6 ->
console.log(graph);
graph.addEdge(1, 2);
graph.addEdge(1, 5);
graph.addEdge(2, 3);
graph.addEdge(2, 5);
graph.addEdge(3, 4);
graph.addEdge(4, 5);
graph.addEdge(4, 6);
// 1 -> 2, 5 | 2 -> 1, 3, 5 | 3 -> 2, 4 | 4 -> 3, 5, 6 | 5 -> 1, 2, 4 | 6 -> 4
console.log(graph);
graph.removeEdge(1, 2);
graph.removeEdge(4, 5);
graph.removeEdge(10, 11);
graph.addEdge(1, 2);
graph.addEdge(4, 5);
graph.removeVertex(5);
// Size 5, relations 4
console.log(graph);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment