Skip to content

Instantly share code, notes, and snippets.

@hoorayimhelping
Created September 15, 2015 17:05
Show Gist options
  • Save hoorayimhelping/3f01ee2a22b860a36c58 to your computer and use it in GitHub Desktop.
Save hoorayimhelping/3f01ee2a22b860a36c58 to your computer and use it in GitHub Desktop.
diff --git a/gulpfile.js b/gulpfile.js
index cc55817..1df0002 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -8,7 +8,7 @@ var tape = require('gulp-tape');
var shell = require('gulp-shell');
var paths = {
- 'js_source': 'js/src/',
+ 'js_source': 'js/graph/',
'js_test': 'js/test/',
'js_dist': 'dist/'
};
diff --git a/js/graph/edge.js b/js/graph/edge.js
new file mode 100644
index 0000000..a9a16e9
--- /dev/null
+++ b/js/graph/edge.js
@@ -0,0 +1,25 @@
+var Edge = function(value, name) {
+ this.value = value;
+ this.name = name;
+
+ this.id = name + '-' + value;
+ this.nodes = {};
+};
+
+Edge.prototype = {
+ add: function(node1, node2) {
+ this.nodes = {};
+
+ this.nodes = {
+ head: node1,
+ tail: node2
+ };
+ },
+
+ areAdjacent: function(node1, node2) {
+ return this.nodes.head.id === node1.id &&
+ this.nodes.tail.id === node2.id;
+ }
+};
+
+module.exports = Edge;
\ No newline at end of file
diff --git a/js/graph/graph.js b/js/graph/graph.js
new file mode 100644
index 0000000..c34e3cd
--- /dev/null
+++ b/js/graph/graph.js
@@ -0,0 +1,58 @@
+var Node = require('./node');
+var Edge = require('./edge');
+
+var Graph = function() {
+ this.edges = [];
+ this.nodes = [];
+};
+
+Graph.prototype = {
+ addEdge: function(edge, head_node, tail_node) {
+ edge.add(head_node, tail_node);
+
+ this.nodes.push(head_node, tail_node);
+
+ head_node.addEdge(edge);
+ tail_node.addEdge(edge);
+
+ this.edges.push(edge);
+ },
+
+ areAdjacent: function(head_node, tail_node) {
+ return this.edges.some(function(edge) {
+ return edge.areAdjacent(head_node, tail_node);
+ });
+ },
+
+ // depth first search
+ walk: function(start_node, destination_node) {
+ var total_value = 0;
+
+ start_node.visited = true;
+
+ start_node.edges.forEach(function(edge) {
+ var node = edge.nodes.tail;
+
+ if (node.id === destination_node.id) {
+ total_value += edge.value;
+ return;
+ }
+
+ if (!node.visited) {
+ total_value = edge.value;
+
+ total_value += this.walk(node, destination_node);
+ }
+ }, this);
+
+ return total_value;
+ },
+
+ resetNodes: function() {
+ this.nodes.forEach(function(node) {
+ node.visited = false;
+ });
+ }
+};
+
+module.exports = Graph;
\ No newline at end of file
diff --git a/js/graph/node.js b/js/graph/node.js
new file mode 100644
index 0000000..9c03707
--- /dev/null
+++ b/js/graph/node.js
@@ -0,0 +1,13 @@
+var Node = function(id) {
+ this.id = id;
+ this.edges = [];
+ this.visited = false;
+};
+
+Node.prototype = {
+ addEdge: function(edge) {
+ this.edges.push(edge);
+ }
+};
+
+module.exports = Node;
\ No newline at end of file
diff --git a/js/maps/kerbol_system.js b/js/maps/kerbol_system.js
index 351a877..def8374 100644
--- a/js/maps/kerbol_system.js
+++ b/js/maps/kerbol_system.js
@@ -1,5 +1,5 @@
-var Node = require('../src/node');
-var Edge = require('../src/edge');
+var Node = require('../graph/node');
+var Edge = require('../graph/edge');
var newNode = function(id) {
var node = new Node();
diff --git a/js/maps/solar_system.js b/js/maps/solar_system.js
index c39442d..ffed717 100644
--- a/js/maps/solar_system.js
+++ b/js/maps/solar_system.js
@@ -1,5 +1,5 @@
-var Node = require('../src/node');
-var Edge = require('../src/edge');
+var Node = require('../graph/node');
+var Edge = require('../graph/edge');
var newEdge = function(options) {
return new Edge(options.deltav, options.name);
diff --git a/js/src/edge.js b/js/src/edge.js
deleted file mode 100644
index a9a16e9..0000000
--- a/js/src/edge.js
+++ /dev/null
@@ -1,25 +0,0 @@
-var Edge = function(value, name) {
- this.value = value;
- this.name = name;
-
- this.id = name + '-' + value;
- this.nodes = {};
-};
-
-Edge.prototype = {
- add: function(node1, node2) {
- this.nodes = {};
-
- this.nodes = {
- head: node1,
- tail: node2
- };
- },
-
- areAdjacent: function(node1, node2) {
- return this.nodes.head.id === node1.id &&
- this.nodes.tail.id === node2.id;
- }
-};
-
-module.exports = Edge;
\ No newline at end of file
diff --git a/js/src/graph.js b/js/src/graph.js
deleted file mode 100644
index c34e3cd..0000000
--- a/js/src/graph.js
+++ /dev/null
@@ -1,58 +0,0 @@
-var Node = require('./node');
-var Edge = require('./edge');
-
-var Graph = function() {
- this.edges = [];
- this.nodes = [];
-};
-
-Graph.prototype = {
- addEdge: function(edge, head_node, tail_node) {
- edge.add(head_node, tail_node);
-
- this.nodes.push(head_node, tail_node);
-
- head_node.addEdge(edge);
- tail_node.addEdge(edge);
-
- this.edges.push(edge);
- },
-
- areAdjacent: function(head_node, tail_node) {
- return this.edges.some(function(edge) {
- return edge.areAdjacent(head_node, tail_node);
- });
- },
-
- // depth first search
- walk: function(start_node, destination_node) {
- var total_value = 0;
-
- start_node.visited = true;
-
- start_node.edges.forEach(function(edge) {
- var node = edge.nodes.tail;
-
- if (node.id === destination_node.id) {
- total_value += edge.value;
- return;
- }
-
- if (!node.visited) {
- total_value = edge.value;
-
- total_value += this.walk(node, destination_node);
- }
- }, this);
-
- return total_value;
- },
-
- resetNodes: function() {
- this.nodes.forEach(function(node) {
- node.visited = false;
- });
- }
-};
-
-module.exports = Graph;
\ No newline at end of file
diff --git a/js/src/node.js b/js/src/node.js
deleted file mode 100644
index 9c03707..0000000
--- a/js/src/node.js
+++ /dev/null
@@ -1,13 +0,0 @@
-var Node = function(id) {
- this.id = id;
- this.edges = [];
- this.visited = false;
-};
-
-Node.prototype = {
- addEdge: function(edge) {
- this.edges.push(edge);
- }
-};
-
-module.exports = Node;
\ No newline at end of file
diff --git a/js/test/graph.js b/js/test/graph.js
index 47e6618..42c4da0 100644
--- a/js/test/graph.js
+++ b/js/test/graph.js
@@ -1,7 +1,7 @@
var describe = require('tape');
-var Graph = require('../src/graph');
-var Node = require('../src/node');
-var Edge = require('../src/edge');
+var Graph = require('../graph/graph');
+var Node = require('../graph/node');
+var Edge = require('../graph/edge');
var solar_system = require('../maps/solar_system');
var kerbol_system = require('../maps/kerbol_system');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment