-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal-shortest-dist.js
49 lines (40 loc) · 1.62 KB
/
cal-shortest-dist.js
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
function calShortestDist(graph, sourceDestination, finDestination) {
const distances = {};
const visited = {};
const queue = [];
// Initialize distances and visited hashmaps
for (let vertex in graph) {
distances[vertex] = Infinity;
visited[vertex] = false;
}
// Set distance from sourceDestination to sourceDestination as 0
distances[sourceDestination] = 0;
// Add sourceDestination vertex to the queue
queue.push(sourceDestination);
// Loop until queue is empty
while (queue.length) {
// Get the vertex with the minimum distance
let currentVertex = queue.shift();
// Mark the current vertex as visited
visited[currentVertex] = true;
// Loop through the neighbors of the current vertex
for (let neighbor in graph[currentVertex]) {
// Calculate the distance to the neighbor
let distance = graph[currentVertex][neighbor];
// Check if the neighbor is not visited and the distance is not 0
if (!visited[neighbor] && distance !== 0) {
// Calculate the total distance from sourceDestination to the neighbor
let totalDistance = distances[currentVertex] + distance;
// Check if the total distance is less than the current distance
if (totalDistance < distances[neighbor]) {
// Update the distance of the neighbor
distances[neighbor] = totalDistance;
// Add the neighbor to the queue
queue.push(neighbor);
}
}
}
}
return distances[finDestination];
}
window.calShortestDist = calShortestDist;