|
| 1 | +/* * * * * * * * * * * * * * * * * * * * * * * * |
| 2 | +
|
| 3 | + A sample distance matrix |
| 4 | + |
| 5 | +var graph = [[NaN, 7, 9, NaN, NaN, 16], |
| 6 | + [7, NaN, 10, 15, NaN, NaN], |
| 7 | + [9, 10, NaN, 11, NaN, 2], |
| 8 | + [NaN, 15, 11, NaN, 6, NaN], |
| 9 | + [NaN, NaN, NaN, 6, NaN, 9], |
| 10 | + [16, NaN, 2, NaN, 9, NaN]]; |
| 11 | +
|
| 12 | +* * * * * * * * * * * * * * * * * * * * * * * */ |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * Dijstra's shortest path algorithm. |
| 17 | + * For the implementation is not used the most suitable data structure (Fibonacci heap) |
| 18 | + * but the binary heap gives also good results. The implementation bellow finds |
| 19 | + * the minimum distance between two given nodes using a distance matrix. |
| 20 | + */ |
| 21 | +var dijstra = function () { |
| 22 | + |
| 23 | + var Heap = require('../data-structures/heap.js').Heap, |
| 24 | + current, |
| 25 | + visited, |
| 26 | + distance, |
| 27 | + unvisited; |
| 28 | + |
| 29 | + |
| 30 | + /** |
| 31 | + * Creates a new node instance |
| 32 | + * |
| 33 | + * @constructor |
| 34 | + * @private |
| 35 | + * @param {number} id The id of the node |
| 36 | + * @param {number} distance The distance from the beginning |
| 37 | + */ |
| 38 | + function Node(id, distance) { |
| 39 | + this.node = id; |
| 40 | + this.distance = distance; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Compares the distances between two nodes. |
| 45 | + * |
| 46 | + * @private |
| 47 | + * @param {object} a A node |
| 48 | + * @param {object} b A graph node |
| 49 | + * @returns {number} The |
| 50 | + */ |
| 51 | + function compareNodesDistance(a, b) { |
| 52 | + return b.distance - a.distance; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Initialize all variables used for the algorithm |
| 57 | + * |
| 58 | + * @private |
| 59 | + * @param {number} src A start node |
| 60 | + */ |
| 61 | + function init(src) { |
| 62 | + var currentTemp; |
| 63 | + current = {}; |
| 64 | + visited = []; |
| 65 | + distance = []; |
| 66 | + unvisited = new Heap(compareNodesDistance); |
| 67 | + for (var i = 0; i < graph.length; i += 1) { |
| 68 | + currentTemp = new Node(); |
| 69 | + if (src === i) { |
| 70 | + currentTemp.distance = 0; |
| 71 | + } else { |
| 72 | + currentTemp.distance = Infinity; |
| 73 | + } |
| 74 | + currentTemp.node = i; |
| 75 | + visited[i] = false; |
| 76 | + distance[i] = currentTemp; |
| 77 | + unvisited.add(currentTemp); |
| 78 | + } |
| 79 | + current.node = src; |
| 80 | + current.distance = 0; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Dijkstra's shortest path algorithm |
| 85 | + * |
| 86 | + * @public |
| 87 | + * @param {number} src Source node |
| 88 | + * @param {number} dest Destination node |
| 89 | + * @param {array} graph A distance matrix of the graph |
| 90 | + * @returns {number} The shortest distance between the nodes |
| 91 | + */ |
| 92 | + return function (src, dest, graph) { |
| 93 | + var tempDistance = 0; |
| 94 | + init(src); |
| 95 | + while (current.node != dest && current.distance != Infinity) { |
| 96 | + for (var i = 0; i < graph.length; i += 1) { |
| 97 | + if (current.node !== i && //if it's not the current node |
| 98 | + !visited[i] && //and if we haven't visited this node |
| 99 | + !isNaN(graph[i][current.node])) { //and this node is sibling of the current... |
| 100 | + |
| 101 | + tempDistance = current.distance + graph[i][current.node]; |
| 102 | + if (tempDistance < distance[i].distance) { |
| 103 | + distance[i].distance = tempDistance; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + visited[current.node] = true; |
| 108 | + current = unvisited.extract(); |
| 109 | + } |
| 110 | + return distance[dest].distance; |
| 111 | + }; |
| 112 | +}(); |
0 commit comments