Skip to content

Instantly share code, notes, and snippets.

@skeep
Last active August 13, 2018 19:43
Show Gist options
  • Save skeep/6cdfa9de0a408b7af78ba496e90ea53a to your computer and use it in GitHub Desktop.
Save skeep/6cdfa9de0a408b7af78ba496e90ea53a to your computer and use it in GitHub Desktop.

Revisions

  1. skeep revised this gist Sep 21, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion Dijkstra.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    // Basic Graph data structure implementation
    /**
    * Basic Graph data structure implementation
    * @constructor
    */
    var Graph = function() {
    this.vertices = {};
    };
  2. skeep revised this gist Sep 21, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Dijkstra.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,6 @@ Graph.prototype.length = function(u, v) {
    * Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph
    * @param source
    * @returns {{}}
    * @constructor
    */
    Graph.prototype.Dijkstra = function(source) {
    // create vertex set Q
  3. skeep revised this gist Sep 21, 2016. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion Dijkstra.js
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,12 @@ Graph.prototype.length = function(u, v) {
    return (this.vertices[u][v]);
    };

    // function Dijkstra(Graph, source):
    /**
    * Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph
    * @param source
    * @returns {{}}
    * @constructor
    */
    Graph.prototype.Dijkstra = function(source) {
    // create vertex set Q
    var Q = {},
  4. skeep revised this gist Sep 21, 2016. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions Dijkstra.js
    Original file line number Diff line number Diff line change
    @@ -20,10 +20,10 @@ Graph.prototype.Dijkstra = function(source) {
    prev = {};

    /**
    * 5 for each vertex v in Graph: // Initialization
    * 6 dist[v] ← INFINITY // Unknown distance from source to v
    * 7 prev[v] ← UNDEFINED // Previous node in optimal path from source
    * 8 add v to Q // All nodes initially in Q (unvisited nodes)
    * for each vertex v in Graph: // Initialization
    * dist[v] ← INFINITY // Unknown distance from source to v
    * prev[v] ← UNDEFINED // Previous node in optimal path from source
    * add v to Q // All nodes initially in Q (unvisited nodes)
    */
    for (var vertex in this.vertices) {
    dist[vertex] = Infinity;
    @@ -50,8 +50,8 @@ Graph.prototype.Dijkstra = function(source) {

    /**
    * if alt < dist[v]: // A shorter path to v has been found
    * dist[v] ← alt
    * prev[v] ← u
    * dist[v] ← alt
    * prev[v] ← u
    */
    if (alt < dist[neighbor]) {
    dist[neighbor] = alt;
  5. skeep revised this gist Sep 21, 2016. 1 changed file with 31 additions and 22 deletions.
    53 changes: 31 additions & 22 deletions Dijkstra.js
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,3 @@
    function _isEmpty(obj) {
    return Object.keys(obj).length === 0;
    }

    /**
    * Extract the node with minimum distance from active vertex.
    * This should not be required if using a priority queue
    */

    function _extractMin(Q, dist) {
    var minimumDistance = Infinity;
    var nodeWithMinimumDistance;

    for (var node in Q) {
    if (dist[node] <= minimumDistance) {
    minimumDistance = dist[node];
    nodeWithMinimumDistance = node;
    }
    }
    return nodeWithMinimumDistance;
    }

    // Basic Graph data structure implementation
    var Graph = function() {
    this.vertices = {};
    @@ -92,3 +70,34 @@ G.add('V', { W: 2, T: 6 });
    G.add('T');

    console.log(G.Dijkstra('S'));

    /**
    * Just a utility method to check if an Object is empty or not
    * @param obj
    * @returns {boolean}
    * @private
    */
    function _isEmpty(obj) {
    return Object.keys(obj).length === 0;
    }

    /**
    * Extract the node with minimum distance from active vertex.
    * This should not be required if using a priority queue
    * @param Q
    * @param dist
    * @returns {*}
    * @private
    */
    function _extractMin(Q, dist) {
    var minimumDistance = Infinity;
    var nodeWithMinimumDistance;

    for (var node in Q) {
    if (dist[node] <= minimumDistance) {
    minimumDistance = dist[node];
    nodeWithMinimumDistance = node;
    }
    }
    return nodeWithMinimumDistance;
    }
  6. skeep revised this gist Sep 21, 2016. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions Dijkstra.js
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,11 @@ function _isEmpty(obj) {
    return Object.keys(obj).length === 0;
    }

    /**
    * Extract the node with minimum distance from active vertex.
    * This should not be required if using a priority queue
    */

    function _extractMin(Q, dist) {
    var minimumDistance = Infinity;
    var nodeWithMinimumDistance;
    @@ -15,6 +20,7 @@ function _extractMin(Q, dist) {
    return nodeWithMinimumDistance;
    }

    // Basic Graph data structure implementation
    var Graph = function() {
    this.vertices = {};
    };
  7. skeep revised this gist Sep 21, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Dijkstra.js
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@ Graph.prototype.length = function(u, v) {
    return (this.vertices[u][v]);
    };

    // function Dijkstra(Graph, source):
    Graph.prototype.Dijkstra = function(source) {
    // create vertex set Q
    var Q = {},
  8. skeep revised this gist Sep 21, 2016. 1 changed file with 20 additions and 1 deletion.
    21 changes: 20 additions & 1 deletion Dijkstra.js
    Original file line number Diff line number Diff line change
    @@ -34,26 +34,45 @@ Graph.prototype.Dijkstra = function(source) {
    dist = {},
    prev = {};

    /**
    * 5 for each vertex v in Graph: // Initialization
    * 6 dist[v] ← INFINITY // Unknown distance from source to v
    * 7 prev[v] ← UNDEFINED // Previous node in optimal path from source
    * 8 add v to Q // All nodes initially in Q (unvisited nodes)
    */
    for (var vertex in this.vertices) {
    dist[vertex] = Infinity;
    prev[vertex] = undefined;
    Q[vertex] = this.vertices[vertex];
    }

    // dist[source] ← 0 // Distance from source to source

    dist[source] = 0;

    // while Q is not empty:
    while (!_isEmpty(Q)) {
    // u ← vertex in Q with min dist[u] // Source node will be selected first
    var u = _extractMin(Q, dist);

    // remove u from Q
    delete Q[u];

    // for each neighbor v of u: // where v is still in Q.
    for (var neighbor in this.vertices[u]) {
    // alt ← dist[u] + length(u, v)
    var alt = dist[u] + G.length(u, neighbor);

    /**
    * if alt < dist[v]: // A shorter path to v has been found
    * dist[v] ← alt
    * prev[v] ← u
    */
    if (alt < dist[neighbor]) {
    dist[neighbor] = alt;
    prev[neighbor] = u;
    }
    }

    }
    return dist;
    };
  9. skeep revised this gist Sep 21, 2016. No changes.
  10. skeep created this gist Sep 21, 2016.
    68 changes: 68 additions & 0 deletions Dijkstra.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    function _isEmpty(obj) {
    return Object.keys(obj).length === 0;
    }

    function _extractMin(Q, dist) {
    var minimumDistance = Infinity;
    var nodeWithMinimumDistance;

    for (var node in Q) {
    if (dist[node] <= minimumDistance) {
    minimumDistance = dist[node];
    nodeWithMinimumDistance = node;
    }
    }
    return nodeWithMinimumDistance;
    }

    var Graph = function() {
    this.vertices = {};
    };

    Graph.prototype.add = function(name, edges) {
    edges = edges || null;
    this.vertices[name] = edges;
    };

    Graph.prototype.length = function(u, v) {
    return (this.vertices[u][v]);
    };

    Graph.prototype.Dijkstra = function(source) {
    // create vertex set Q
    var Q = {},
    dist = {},
    prev = {};

    for (var vertex in this.vertices) {
    dist[vertex] = Infinity;
    prev[vertex] = undefined;
    Q[vertex] = this.vertices[vertex];
    }

    dist[source] = 0;

    while (!_isEmpty(Q)) {
    var u = _extractMin(Q, dist);
    delete Q[u];

    for (var neighbor in this.vertices[u]) {
    var alt = dist[u] + G.length(u, neighbor);
    if (alt < dist[neighbor]) {
    dist[neighbor] = alt;
    prev[neighbor] = u;
    }
    }

    }
    return dist;
    };

    // Usage
    var G = new Graph();
    G.add('S', { V: 1, W: 4 });
    G.add('W', { T: 3 });
    G.add('V', { W: 2, T: 6 });
    G.add('T');

    console.log(G.Dijkstra('S'));