|
| 1 | +class WeightedGraph { |
| 2 | + constructor() { |
| 3 | + this.adjacencyList = {}; |
| 4 | + } |
| 5 | + addVertex(vertex){ |
| 6 | + if(!this.adjacencyList[vertex]) this.adjacencyList[vertex] = []; |
| 7 | + } |
| 8 | + addEdge(vertex1,vertex2, weight){ |
| 9 | + this.adjacencyList[vertex1].push({node:vertex2,weight}); |
| 10 | + this.adjacencyList[vertex2].push({node:vertex1, weight}); |
| 11 | + } |
| 12 | + Dijkstra(start, finish){ |
| 13 | + const nodes = new PriorityQueue(); |
| 14 | + const distances = {}; |
| 15 | + const previous = {}; |
| 16 | + let path = [] //to return at end |
| 17 | + let smallest; |
| 18 | + //build up initial state |
| 19 | + for(let vertex in this.adjacencyList){ |
| 20 | + if(vertex === start){ |
| 21 | + distances[vertex] = 0; |
| 22 | + nodes.enqueue(vertex, 0); |
| 23 | + } else { |
| 24 | + distances[vertex] = Infinity; |
| 25 | + nodes.enqueue(vertex, Infinity); |
| 26 | + } |
| 27 | + previous[vertex] = null; |
| 28 | + } |
| 29 | + // as long as there is something to visit |
| 30 | + while(nodes.values.length){ |
| 31 | + smallest = nodes.dequeue().val; |
| 32 | + if(smallest === finish){ |
| 33 | + //WE ARE DONE |
| 34 | + //BUILD UP PATH TO RETURN AT END |
| 35 | + while(previous[smallest]){ |
| 36 | + path.push(smallest); |
| 37 | + smallest = previous[smallest]; |
| 38 | + } |
| 39 | + break; |
| 40 | + } |
| 41 | + if(smallest || distances[smallest] !== Infinity){ |
| 42 | + for(let neighbor in this.adjacencyList[smallest]){ |
| 43 | + //find neighboring node |
| 44 | + let nextNode = this.adjacencyList[smallest][neighbor]; |
| 45 | + //calculate new distance to neighboring node |
| 46 | + let candidate = distances[smallest] + nextNode.weight; |
| 47 | + let nextNeighbor = nextNode.node; |
| 48 | + if(candidate < distances[nextNeighbor]){ |
| 49 | + //updating new smallest distance to neighbor |
| 50 | + distances[nextNeighbor] = candidate; |
| 51 | + //updating previous - How we got to neighbor |
| 52 | + previous[nextNeighbor] = smallest; |
| 53 | + //enqueue in priority queue with new priority |
| 54 | + nodes.enqueue(nextNeighbor, candidate); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return path.concat(smallest).reverse(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +class PriorityQueue { |
| 64 | + constructor(){ |
| 65 | + this.values = []; |
| 66 | + } |
| 67 | + enqueue(val, priority){ |
| 68 | + let newNode = new Node(val, priority); |
| 69 | + this.values.push(newNode); |
| 70 | + this.bubbleUp(); |
| 71 | + } |
| 72 | + bubbleUp(){ |
| 73 | + let idx = this.values.length - 1; |
| 74 | + const element = this.values[idx]; |
| 75 | + while(idx > 0){ |
| 76 | + let parentIdx = Math.floor((idx - 1)/2); |
| 77 | + let parent = this.values[parentIdx]; |
| 78 | + if(element.priority >= parent.priority) break; |
| 79 | + this.values[parentIdx] = element; |
| 80 | + this.values[idx] = parent; |
| 81 | + idx = parentIdx; |
| 82 | + } |
| 83 | + } |
| 84 | + dequeue(){ |
| 85 | + const min = this.values[0]; |
| 86 | + const end = this.values.pop(); |
| 87 | + if(this.values.length > 0){ |
| 88 | + this.values[0] = end; |
| 89 | + this.sinkDown(); |
| 90 | + } |
| 91 | + return min; |
| 92 | + } |
| 93 | + sinkDown(){ |
| 94 | + let idx = 0; |
| 95 | + const length = this.values.length; |
| 96 | + const element = this.values[0]; |
| 97 | + while(true){ |
| 98 | + let leftChildIdx = 2 * idx + 1; |
| 99 | + let rightChildIdx = 2 * idx + 2; |
| 100 | + let leftChild,rightChild; |
| 101 | + let swap = null; |
| 102 | + |
| 103 | + if(leftChildIdx < length){ |
| 104 | + leftChild = this.values[leftChildIdx]; |
| 105 | + if(leftChild.priority < element.priority) { |
| 106 | + swap = leftChildIdx; |
| 107 | + } |
| 108 | + } |
| 109 | + if(rightChildIdx < length){ |
| 110 | + rightChild = this.values[rightChildIdx]; |
| 111 | + if( |
| 112 | + (swap === null && rightChild.priority < element.priority) || |
| 113 | + (swap !== null && rightChild.priority < leftChild.priority) |
| 114 | + ) { |
| 115 | + swap = rightChildIdx; |
| 116 | + } |
| 117 | + } |
| 118 | + if(swap === null) break; |
| 119 | + this.values[idx] = this.values[swap]; |
| 120 | + this.values[swap] = element; |
| 121 | + idx = swap; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class Node { |
| 127 | + constructor(val, priority){ |
| 128 | + this.val = val; |
| 129 | + this.priority = priority; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +var graph = new WeightedGraph() |
| 134 | +graph.addVertex("A"); |
| 135 | +graph.addVertex("B"); |
| 136 | +graph.addVertex("C"); |
| 137 | +graph.addVertex("D"); |
| 138 | +graph.addVertex("E"); |
| 139 | +graph.addVertex("F"); |
| 140 | + |
| 141 | +graph.addEdge("A","B", 4); |
| 142 | +graph.addEdge("A","C", 2); |
| 143 | +graph.addEdge("B","E", 3); |
| 144 | +graph.addEdge("C","D", 2); |
| 145 | +graph.addEdge("C","F", 4); |
| 146 | +graph.addEdge("D","E", 3); |
| 147 | +graph.addEdge("D","F", 1); |
| 148 | +graph.addEdge("E","F", 1); |
| 149 | + |
| 150 | + |
| 151 | +graph.Dijkstra("A", "E"); |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
0 commit comments