Skip to content

Instantly share code, notes, and snippets.

@ryanseys
Created October 18, 2014 23:37
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 ryanseys/31f0302c63eeb8e476af to your computer and use it in GitHub Desktop.
Save ryanseys/31f0302c63eeb8e476af to your computer and use it in GitHub Desktop.
Graph 💥
function Graph() {
this.nodes = [];
this.edges = [];
};
Graph.prototype.addNode = function(node) {
if(node) {
this.nodes.push(node);
}
};
Graph.prototype.addEdge = function(edge) {
if(edge && edge.start && edge.end) {
this.edges.push(edge);
}
}
function Node(value) {
this.value = value;
};
Node.prototype.setValue = function(value) {
this.value = value;
};
Node.prototype.getValue = function() {
return value;
};
function Edge(start, end) {
this.start = start;
this.end = end;
};
Edge.prototype.getStart = function() {
return this.start;
};
Edge.prototype.getEnd = function() {
return this.end;
};
Edge.prototype.setStart = function(start) {
this.start = start;
};
Edge.prototype.setEnd = function(end) {
this.end = end;
};
var g = new Graph();
var n1 = new Node("world");
var n2 = new Node("hello");
var e1 = new Edge(n1, n2);
g.addNode(n1);
g.addNode(n2);
g.addEdge(e1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment