-
Notifications
You must be signed in to change notification settings - Fork 0
/
breath-first-search.js
190 lines (160 loc) · 4.65 KB
/
breath-first-search.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
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
179
180
181
182
183
184
185
186
187
188
189
190
// https://github.com/williamfiset/Algorithms/blob/f674f5f8b25f2d186ad2e38caded5feaebbfc055/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchRecursive.java
console.clear();
class PriorityQueue {
constructor(on) {
this.array = [];
this.on = on;
}
offer(element) {
this.array.push(element);
}
poll() {
if (!this.array.length) return;
let smallest = this.array.reduce(
(r, c) => c[this.on] < r[this.on] ? c: r,
this.array[0]
);
this.array.splice(this.array.indexOf(smallest), 1);
return smallest;
}
}
const DEPTH_LAYER = -1;
class Node {
constructor(id, value) {
this.id = id;
this.value = value;
}
}
class Edge {
constructor(from, to, cost) {
this.from = from;
this.to = to;
this.cost = cost;
}
}
class Graph {
constructor() {
this.graph = new Map();
this.set = new Set();
}
get nodeCount() {
return this.set.size;
}
addNode(node) {
if (node != null) this.set.add(node);
}
addUndirectedEdge(from, to, cost) {
this.addDirectedEdge(from, to, cost);
this.addDirectedEdge(to, from, cost);
}
addDirectedEdge(from, to, cost) {
let list = this.graph.get(from);
this.addNode(from);
this.addNode(to);
if (!list) {
list = [];
this.graph.set(from, list);
}
list.push(new Edge(from, to, cost));
}
bfsIterative(start) {
let prev = [];
let visited = [];
let queue = [start];
while (queue.length) {
let node = queue.shift();
if (!visited[node]) {
visited[node] = true;
let edges = this.graph.get(node);
if (edges != null)
for (let edge of edges)
if (!visited[edge.to]) {
prev[edge.to] = node;
queue.push(edge.to);
}
}
}
return prev;
}
reconstructPath(start, end) {
let prev = this.bfsIterative(start);
let path = [];
for (let at = end; at != undefined; at = prev[at]) path.push(at);
path.reverse();
if (path[0] === start) return path;
path = [];
return path;
}
bfsRecursive(start, n) {
function bfs(queue, visited) {
let at = queue.shift();
if (at == DEPTH_LAYER) {
queue.push(DEPTH_LAYER);
return 1;
}
if (visited[at]) return 0;
visited[at] = true;
let neighbours = this.graph.get(at);
if (neighbours != null)
for (let next of neighbours)
if (!visited[next.to]) queue.push(next.to);
let depth = 0;
while (true) {
if (queue.length == 1 && queue[0] == DEPTH_LAYER) break;
depth += this.bfs(queue, visited);
}
return depth;
}
let queue = [start, DEPTH_LAYER],
visited = new Array(n).fill(false);
return this.bfs(queue, visited);
}
shortestPathBtw(start, end) {
let { prev, distance } = this.dijkstra(start, end);
let path = [];
for (let at = end; at != null; at = prev[at]) path.push(at);
path.reverse();
if (path[0] === start) return { arr: path, distance: distance };
return { arr: [], distance: distance };
}
dijkstra(start, end) {
let dist = new Array(this.nodeCount).fill(Infinity); // store distance
let pq = new PriorityQueue(this.nodeCount, "value"); // pq for fetching the least distance
let visited = new Array(this.nodeCount).fill(false); // visited tracking
let prev = new Array(this.nodeCount).fill(null); // to regenrate path
dist[start] = 0;
pq.offer(new Node(start, 0));
while (pq.array.length) {
let node = pq.poll();
visited[node.id] = true;
if (dist[node.id] < node.value) continue;
let edges = this.graph.get(node.id) || [];
for (let edge of edges) {
if (visited[edge.to]) continue;
let newDist = dist[edge.from] + edge.cost;
if (newDist < dist[edge.to]) {
prev[edge.to] = edge.from;
dist[edge.to] = newDist;
pq.offer(new Node(edge.to, dist[edge.to]));
}
}
if (node.id == end) return { distance: dist[end], prev: prev };
}
return { distance: Infinity, prev: prev };
}
// end of class
}
let graph = new Graph();
graph.addUndirectedEdge(3, 4, 2);
graph.addUndirectedEdge(2, 4, 5);
graph.addUndirectedEdge(12, 8, 3);
graph.addUndirectedEdge(1, 10, 3);
graph.addUndirectedEdge(10, 9, 6);
graph.addUndirectedEdge(9, 8, 7);
graph.addUndirectedEdge(8, 2, 7);
// console.log(graph.reconstructPath(3, 2));
// console.log(graph.bfsIterative(2));
// console.log(graph.bfsRecursive(2, graph.nodeCount));
// console.log(graph.dijkstra(3, 2));
let path = graph.shortestPathBtw(3, 2);
console.log(path.arr.join(" -> ") + " with the total cost of " + path.distance);