-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra_Algorithm.ts
173 lines (162 loc) · 5.83 KB
/
Dijkstra_Algorithm.ts
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Description: Function to perform the Dijkstra algorithm on a weighted graph
// Expected Output: returns the shorted path
// class for new node
class NewNode {
val: string;
priority: number;
constructor(val: string, priority: number) {
this.val = val;
this.priority = priority;
}
}
// class for graph
class WeightedGraph {
adjacencyList: {};
constructor() {
this.adjacencyList = {};
}
// function to add vertex on graph
addVertex = (vertex: string) => {
if (!this.adjacencyList[vertex]) this.adjacencyList[vertex] = [];
};
// function to add weight of the edge between two vertices
addEdge = (vertex1: string, vertex2: string, weight: number) => {
this.adjacencyList[vertex1].push({ node: vertex2, weight });
this.adjacencyList[vertex2].push({ node: vertex1, weight });
};
// main function to implement Dijkstra Algorithm from start to end node
DijkstraFunction = (start: string, finish: string) => {
const nodes: PriorityQueue = new PriorityQueue();
const distances = {};
const previous = {};
// to return at end
let path = [];
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;
}
// as long as there is something to visit, visit the node
while (nodes.values.length) {
smallest = nodes.dequeue().val;
if (smallest === finish) {
// reached the end , need to form the path now and return
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];
// calculate new distance 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);
}
}
}
}
return path.concat(smallest).reverse();
};
}
// class defnition for Priority Queue
class PriorityQueue {
values: NewNode[];
constructor() {
this.values = [];
}
enqueue = (val: string, priority: number) => {
let newNode = new NewNode(val, priority);
this.values.push(newNode);
this.bubbleUp();
};
bubbleUp = () => {
let idx: number = this.values.length - 1;
const element: NewNode = this.values[idx];
while (idx > 0) {
let parentIdx: number = Math.floor((idx - 1) / 2);
let parent: NewNode = this.values[parentIdx];
if (element.priority >= parent.priority) break;
this.values[parentIdx] = element;
this.values[idx] = parent;
idx = parentIdx;
}
};
dequeue = () => {
const min: NewNode = this.values[0];
const end: NewNode = this.values.pop();
if (this.values.length > 0) {
this.values[0] = end;
this.sinkDown();
}
return min;
};
sinkDown = () => {
let idx: number = 0;
const length: number = this.values.length;
const element: NewNode = this.values[0];
while (true) {
let leftChildIdx: number = 2 * idx + 1;
let rightChildIdx: number = 2 * idx + 2;
let leftChild: NewNode, rightChild: NewNode;
let swap: number = null;
if (leftChildIdx < length) {
leftChild = this.values[leftChildIdx];
if (leftChild.priority < element.priority) swap = leftChildIdx;
}
if (rightChildIdx < length) {
rightChild = this.values[rightChildIdx];
if (
(swap === null && rightChild.priority < element.priority) ||
(swap !== null && rightChild.priority < leftChild.priority)
)
swap = rightChildIdx;
}
if (swap === null) break;
this.values[idx] = this.values[swap];
this.values[swap] = element;
idx = swap;
}
};
}
function main() {
const wtdGraph = new WeightedGraph();
// INPUT WEIGHTED GRAPH
// adding vertices of the graph
wtdGraph.addVertex("A");
wtdGraph.addVertex("B");
wtdGraph.addVertex("C");
wtdGraph.addVertex("D");
wtdGraph.addVertex("E");
wtdGraph.addVertex("F");
// addition weight of the edges
wtdGraph.addEdge("A", "B", 1);
wtdGraph.addEdge("A", "C", 3);
wtdGraph.addEdge("B", "E", 2);
wtdGraph.addEdge("C", "D", 4);
wtdGraph.addEdge("C", "F", 5);
wtdGraph.addEdge("D", "E", 3);
wtdGraph.addEdge("D", "F", 1);
wtdGraph.addEdge("E", "F", 2);
// running the algorithm for the ghraph
const output = wtdGraph.DijkstraFunction("A", "F");
// OUTPUT OF wtdGraph
console.log(output); //[ 'A', 'B', 'E', 'F']
}
main();