Skip to content

Instantly share code, notes, and snippets.

@jenjwong
Created June 8, 2016 06:25
Show Gist options
  • Save jenjwong/ac9609bbacb087b38da65901861de406 to your computer and use it in GitHub Desktop.
Save jenjwong/ac9609bbacb087b38da65901861de406 to your computer and use it in GitHub Desktop.
class Graph {
constructor() {
this._storage = {};
}
addNode(node) {
this._storage[node] = node;
}
contains(node) {
return !!this._storage[node];
}
remove(node) {
if (this._storage[node]) {
for (var i = 0; i < this._storage[node].edges.length; i++) {
if (this._storage[node].edges[i] === node){
delete this._storage[node].edges[i]
}
}
delete this._storage[node]
}
}
addEdge(fromNode, toNode) {
if (this._storage[fromNode] && this._storage[toNode]) {
this._storage[fromNode][edges][toNode] = toNode;
this._storage[toNode][edges][fromNode] = fromNode;
}
}
hasEdge(fromNode, toNode) {
if (this._storage[fromNode][edges][toNode]) {
return !!this._storage[fromNode][edges][toNode];
}
}
removeEdge(fromNode, toNode) {
if (this._storage[fromNode][edges][toNode] && this._storage[fromNode][edges][toNode]) {
delete this._storage[fromNode][edges][toNode];
delete this._storage[toNode][edges][fromNode];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment