-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweighted.js
More file actions
100 lines (94 loc) · 2.7 KB
/
weighted.js
File metadata and controls
100 lines (94 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
class WeightedGraph {
constructor(){
this.adjacencyList = {};
}
addVertex(name){
this.adjacencyList[name] = [];
}
addEdge(vertex1, vertex2, weight){
this.adjacencyList[vertex1].push({node:vertex2, weight});
this.adjacencyList[vertex2].push({node:vertex1, weight});
}
Dijkstra(start, finish){
const distances = {};
const nodes = new PriorityQueue();
const previous = {};
let path = []; // to return at the end
let smallest;
// Build up initial state
for(let vertex in this.adjacencyList){
if(vertex === start){
distances[vertex] = 0;
nodes.enqueue(vertex, 0);
} else {
distances[vertex] = Infinity;
nodes.enqueue(vertex, Infinity);
}
previous[vertex] = null;
}
// run as long as there is something in the queue
while(nodes.values.length){
smallest = nodes.dequeue().val;
if(smallest === finish){
//We are done! Build up path to return at the end
while(previous[smallest]){
path.push(smallest);
smallest = previous[smallest];
}
break;
}
if(smallest || distances[smallest] !== Infinity){
for(let neighbor in this.adjacencyList[smallest]){
//Find neighboring node
let nextNode = this.adjacencyList[smallest][neighbor];
console.log(nextNode);
//calculate new distances to neighboring node
let candidate = distances[smallest] + nextNode.weight;
let nextNeighbor = nextNode.node;
if(candidate < distances[nextNeighbor]){
//updating new smallest distance to neighbor
distances[nextNeighbor] = candidate;
//updating previous - How we got to neighbor
previous[nextNeighbor] = smallest;
//enqueue in priority queue with new priority
nodes.enqueue(nextNeighbor, candidate);
}
}
}
}
console.log(distances)
console.log(previous);
return path.concat(smallest).reverse();
}
}
class PriorityQueue{
constructor(){
this.values = [];
}
enqueue(val, priority){
this.values.push({val, priority});
this.sort();
};
dequeue(){
return this.values.shift();
};
sort(){
this.values.sort((a,b) => a.priority - b.priority);
};
}
let graph = new WeightedGraph();
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addVertex("E");
graph.addVertex("F");
graph.addEdge("A", "B", 4);
graph.addEdge("A", "C", 2);
graph.addEdge("B", "E", 3);
graph.addEdge("C", "D", 2);
graph.addEdge("C", "F", 4);
graph.addEdge("D", "E", 3);
graph.addEdge("D", "F", 1);
graph.addEdge("E", "F", 1);
console.log(graph.Dijkstra("A", "E"));