-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra-algorithm.js
More file actions
178 lines (143 loc) · 3.93 KB
/
Copy pathdijkstra-algorithm.js
File metadata and controls
178 lines (143 loc) · 3.93 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
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
174
175
176
177
178
class Graph {
// defining vertex array and
// adjacent list
constructor(noOfVertices)
{
this.noOfVertices = noOfVertices;
this.AdjList = new Map();
}
// functions to be implemented
// addVertex(v)
addVertex(v)
{
// initialize the adjacent list with a
// null array
this.AdjList.set(v, []);
}
// addEdge(v, w)
// add edge to the graph
addEdge(v, w)
{
// get the list for vertex v and put the
// vertex w denoting edge between v and w
this.AdjList.get(v).push(w);
// Since graph is undirected,
// add an edge from w to v also
this.AdjList.get(w).push(v);
}
getEdge(name){
return this.AdjList.get(name);
}
list(){
return this.AdjList;
}
// printGraph()
printGraph()
{
// get all the vertices
var get_keys = this.AdjList.keys();
// iterate over the vertices
for (var i of get_keys)
{
// great the corresponding adjacency list
// for the vertex
var get_values = this.AdjList.get(i);
var conc = "";
// iterate over the adjacency list
// concatenate the values into a string
for (var j of get_values)
conc += j + " ";
// Dijkstra of A dynamic programming
// Dijksr
// print the vertex and its adjacency list
console.log(i + " -> " + conc);
}
}
// bfs(v)
// dfs(v)
}
// Using the above implemented graph class
// var g = new Graph(6);
// var vertices = [ 'A', 'B', 'C', 'D', 'E', 'F' ];
// // adding vertices
// for (var i = 0; i < vertices.length; i++) {
// g.addVertex(vertices[i]);
// }
// // adding edges
// g.addEdge('A', {B:2});
// g.addEdge('A', {C:4});
// g.addEdge('A', 'E');
// g.addEdge('B', 'C');
// g.addEdge('D', 'E');
// g.addEdge('E', 'F');
// g.addEdge('E', 'C');
// g.addEdge('C', 'F');
//Lets implement Dijkstra Algorithm
// DS to consider [ distances, visited];
class Dijkstra{
distances = { }
visited = [];
graph = null;
start = null;
prev = [];
constructor(graph, start){
this.graph = graph;
this.start = start;
}
getSmallest(){
return Object.keys(this.distances).reduce((lowest, node) => {
if (lowest === Infinity || this.distances[node] < this.distances[lowest]) {
if (!this.visited.includes(node)) {
lowest = node;
}
}
return lowest;
}, Infinity);
}
setAllNodeToInfinty(){
for(let x of Object.keys(this.graph)){
if(x === this.start) this.distances[x] = 0;
else{
this.distances[x] = Infinity;
}
}
}
relaxNeighbours(){
let smallS =this.getSmallest();
let smallest = this.graph[smallS];
// console.log(smallest)
let arr = Object.keys(smallest);
arr.forEach((v,i,a)=>{
let sum = this.distances[smallS]+ smallest[v];
if(this.distances[smallS]+ smallest[v] <this.distances[v] ){
//Update distance
this.distances[v] = sum;
}
});
this.visited.push(smallS);
// for(let )
}
run(){
this.setAllNodeToInfinty()
let keys = Object.keys(this.graph);
for(let x of keys){
this.relaxNeighbours();
}
}
}
// g.printGraph();
// console.log(g.list())
const problem = {
start: {A: 5, B: 2},
A: {C: 4, D: 2},
B: {A: 8, D: 7},
C: {D: 6, finish: 3},
D: {finish: 1},
finish: {}
};
let dj = new Dijkstra(problem, 'A');
// dj.setAllNodeToInfinty();
// dj.relaxNeighbours()
dj.run();
console.log(dj.distances)
console.log(dj.prev)