-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortestPath.js
93 lines (82 loc) · 1.96 KB
/
shortestPath.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
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
const edges2adjmatrix = (edge_list) => {
const adjMatrix = {};
for (let edge of edge_list){
const [a,b] = edge;
if (!(a in adjMatrix)) adjMatrix[a] = [];
if (!(b in adjMatrix)) adjMatrix[b] = [];
adjMatrix[a].push(b);
adjMatrix[b].push(a);
}
return adjMatrix;
}
const shortestPath_depthFirst = (edges, src, dst) => {
adjMatrix = edges2adjmatrix(edges);
var shortestPath = [];
var shortestPathLength = 10000;
const stack = [src];
var currentPath = [];
var currentLength = 0;
const visitedNodes = new Set();
while (stack.length > 0){
const currentNode = stack.pop();
currentPath.push(currentNode);
currentLength += 1;
if (currentNode === dst){
if (currentLength < shortestPathLength){
shortestPath = currentPath;
shortestPathLength = currentLength;
currentPath = [];
currentLength = 0;
}
}
else{
for (let neighbor of adjMatrix[currentNode]){
if (!(visitedNodes.has(neighbor))){
stack.push(neighbor);
}
}
}
visitedNodes.add(currentNode);
}
console.log("Shortest path : ",shortestPath);
console.log("Shortest length : ",shortestPathLength);
}
const shortestPath_breadthFirst = (edges, src, dst) => {
adjMatrix = edges2adjmatrix(edges);
console.log(adjMatrix);
var shortestPaths = {};
const queue = [[src, 0, 'root']];
while (queue.length > 0){
const [node, distance, previousNode] = queue.shift();
if (!(node in shortestPaths)){
shortestPaths[node] = [distance, previousNode];
}
else{
if (distance < shortestPaths[node][0]){
shortestpaths[node] = [distance, previousNode];
}
}
if (node === dst){
break
}
else{
const newDistance = distance + 1;
for (let neighbor of adjMatrix[node]){
const toAdd = [neighbor, newDistance, node];
queue.push(toAdd)
}
}
}
console.log(shortestPaths);
}
const edges = [
['w', 'x'],
['x', 'y'],
['z', 'y'],
['z', 'v'],
['w', 'v'],
['z', 'a'],
['a', 'b'],
['b', 'c']
]
shortestPath_breadthFirst(edges, 'x', 'z')