-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphLibrary.ts
355 lines (309 loc) · 11.3 KB
/
GraphLibrary.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { MinPriorityQueue } from "@datastructures-js/priority-queue";
type GraphType = {
adjacencyList: { [key: string]: { vertex: string; weight: number }[] };
vertexList: string[];
edgeList: { start: string; end: string; weight: number }[];
};
type DjQueueType = {
priority: number;
element: string;
};
class Graph implements GraphType {
adjacencyList: { [key: string]: { vertex: string; weight: number }[] };
vertexList: string[];
edgeList: { start: string; end: string; weight: number }[];
constructor() {
this.adjacencyList = {};
this.vertexList = [];
this.edgeList = [];
}
/**
* BFS - Breadth First Search
* O(V+E) - Time Complexity (V = Vertices, E = Edges)
* O(V) - Space Complexity
* @param startVertex
* @param visited
*/
BFS(startVertex: string, visited: { [key: string]: boolean }) {
let queue = [startVertex]; // start with the first vertex
while (queue.length) {
// Pop from the queue, and mark as visited
let currentVertex = queue.shift() as string;
visited[currentVertex] = true;
// For each neighbor of the current vertex, if it hasn't been visited, add it to the queue
// Mark as visited
this.adjacencyList[currentVertex].map((neighbor) => {
if (!visited[neighbor.vertex]) {
queue.push(neighbor.vertex);
visited[neighbor.vertex] = true;
}
});
}
}
/**
* DFS - Depth First Search
* O(V+E) - Time Complexity (V = Vertices, E = Edges)
* O(V) - Space Complexity
* @param startVertex
* @param visited
*/
DFS(startVertex: string, visited: { [key: string]: boolean }) {
// Mark the current vertex as visited
visited[startVertex] = true;
// For each neighbor of the current vertex, if it hasn't been visited, recursively call DFS
this.adjacencyList[startVertex].map((neighbor) => {
if (!visited[Number(neighbor.vertex)]) {
this.DFS(neighbor.vertex, visited);
}
});
}
/**
* addVertex
* O(1) - Time Complexity
* O(1) - Space Complexity
* @param vertex
*/
addVertex(vertex: string) {
if (!this.adjacencyList[vertex]) {
this.adjacencyList[vertex] = [];
this.vertexList.push(vertex);
}
}
/**
* addEdge
* O(N) - Time Complexity - we must loop to prevent adding the same edge twice
* O(1) - Space Complexity
* @param vertex1
* @param vertex2
* @param weight
* @returns void - or console.log if the vertices don't exist
*/
addEdge(vertex1: string, vertex2: string, weight = 0, undirected?: boolean) {
if (vertex1 === vertex2)
return console.log("Cannot add an edge to the same vertex");
if (undirected) {
if (this.adjacencyList[vertex1] && this.adjacencyList[vertex2]) {
this.adjacencyList[vertex1].push({ vertex: vertex2, weight });
this.adjacencyList[vertex2].push({ vertex: vertex1, weight });
this.edgeList.push({ start: vertex1, end: vertex2, weight });
this.edgeList.push({ start: vertex2, end: vertex1, weight });
} else {
return console.log("One of the vertices does not exist");
}
} else if (
this.adjacencyList[vertex1] &&
this.adjacencyList[vertex1].map((edge) => edge.vertex !== vertex2)
) {
// If the vertex is in the adjaceny list, and we haven't already added the vertex2 to the vertex1's list
// O(1) Time complexity, preventing the possible N^2 worst case scenario
this.adjacencyList[vertex1].push({ vertex: vertex2, weight });
this.edgeList.push({ start: vertex1, end: vertex2, weight });
}
}
/**
* removeEdge
* O(N) - Time Complexity - looping to filter out the edge that the user wants to remove
* O(1) - Space Complexity
* @param vertex1
* @param vertex2
* @returns
*/
removeEdge(vertex1: string, vertex2: string) {
if (!this.adjacencyList[vertex1] || !this.adjacencyList[vertex2]) {
return console.log("One of the vertices does not exist");
}
this.adjacencyList[vertex1] = this.adjacencyList[vertex1].filter((v) => {
v.vertex !== vertex2;
});
this.adjacencyList[vertex2] = this.adjacencyList[vertex2].filter((v) => {
v.vertex !== vertex1;
});
this.edgeList = this.edgeList.filter((edge) => {
return edge.start !== vertex1 && edge.end !== vertex2;
});
}
/**
* Dijkstra - Shortest Path Algorithm, finding the shortest path from one vertex to all other vertices
* O((V+E) * log(V)) - Time Complexity
* O(V) - Space Complexity
* @param startVertex
*
*/
Dijkstra(startVertex: string, endVertex?: string) {
// Comparison function for the priority queue, to sort by priority
const comparison = (value: DjQueueType) => {
return value.priority;
};
let heap = new MinPriorityQueue(comparison); // Min Heap, for getting the smallest edge
let distance: { [key: string]: number } = {}; // Distance from the start vertex to all other vertices
heap.push({ priority: 0, element: startVertex });
distance[startVertex] = 0;
// Initialize the distance array
this.vertexList.map((vertex) => {
if (vertex !== startVertex) {
distance[vertex] = Infinity; // Set all other vertices to infinity
}
});
while (heap.size() > 0) {
// While we have elements in the heap
let currentVertex = heap.pop().element;
// pop the element with the smallest priority
// for each neighbor of the current vertex, calculate the distance
this.adjacencyList[currentVertex].map((neighbor) => {
let candidateDistance = distance[currentVertex] + neighbor.weight;
// we check if the current distance of the node combined with the neighbor
// is less than the current distance of the neighbor
if (candidateDistance < distance[neighbor.vertex]) {
// update if true
distance[neighbor.vertex] = candidateDistance;
heap.push({ priority: candidateDistance, element: neighbor.vertex });
}
});
}
// In the case you want to see the distance from the start vertex to the end vertex
if (endVertex) return distance[endVertex];
// Otherwise, return the distance array, with all nodes
return distance;
}
/**
* Kruskals Algorithm for finding the minimum spanning tree
* O(E * log(E)) - Time Complexity - Looping through all edges O(E), inserting and popping from the priority queue (log E)
* O(E) - Space Complexity
* @returns {MST: { start: string; end: string; weight: number }[], weight: number}
*/
Kruskal() {
const comparison = (value: {
startVertex: string;
vertex: string;
weight: number;
}) => {
return value.weight;
};
let heap = new MinPriorityQueue(comparison); // Min Heap, for getting the smallest edge
this.edgeList.map((edge) => {
heap.push({
startVertex: edge.start,
vertex: edge.end,
weight: edge.weight,
});
});
let minimumSpanningTree: { start: string; end: string; weight: number }[] =
[];
let visited: { [key: string]: boolean } = {};
this.vertexList.map((vertex) => {
visited[vertex] = false;
});
while (heap.size() > 0) {
let currentEdge = heap.pop();
if (!visited[currentEdge.startVertex] || !visited[currentEdge.vertex]) {
minimumSpanningTree.push({
start: currentEdge.startVertex,
end: currentEdge.vertex,
weight: currentEdge.weight,
});
visited[currentEdge.startVertex] = true;
visited[currentEdge.vertex] = true;
}
}
let totalWeight = minimumSpanningTree.reduce((acc, edge) => {
acc += edge.weight;
return acc;
}, 0);
return { MST: minimumSpanningTree, weight: totalWeight };
}
/**
* Prims Algorithm for finding the minimum spanning tree
* O(E + V * log(E)) - Time Complexity - Looping through all edges O(E + V) and Vertices, inserting and popping from the priority queue (log E)
* O(E + V) - Space Complexity
* @param startVertex
* @returns {MST: { start: string; end: string; weight: number }[], minimumWeight: number}
*/
Prims(startVertex: string) {
const comparison = (value: {
start: string;
end: string;
weight: number;
}) => {
return value.weight;
};
let heap = new MinPriorityQueue(comparison); // Min Heap, for getting the smallest edge connected to the current spanning tree
let minimumSpanningTree: { start: string; end: string; weight: number }[] =
[];
let visited: { [key: string]: boolean } = {}; // keep track of what vertices have been visited
this.vertexList.map((vertex) => {
// initialize the visited array to all false
visited[vertex] = false;
});
heap.push({ start: startVertex, end: startVertex, weight: 0 }); // push the start vertex to the heap
while (heap.size()) {
let { start, end, weight } = heap.pop();
// Pop the smallest edge from the heap, that is connected to the current spanning tree
if (!visited[end]) {
// if the end vertex hasn't been visited, we add it to the minimum spanning tree
minimumSpanningTree.push({ start, end: end, weight });
visited[end] = true;
// Then mark it as visited
this.adjacencyList[end].map((neighbor) => {
// Go through the neighbors of the end vertex, and add them to the heap
// This will allow us to find the smallest edge connected to the current spanning tree
if (!visited[neighbor.vertex]) {
// if the neighbor hasn't been visited, add it to the heap
heap.push({
start: end,
end: neighbor.vertex,
weight: neighbor.weight,
});
}
});
}
}
// Calculate the minimum weight of the minimum spanning tree
let minimumWeight = minimumSpanningTree.reduce((acc, edge) => {
acc += edge.weight;
return acc;
}, 0);
minimumSpanningTree.shift(); // remove the first element, as it is the start vertex
return { MST: minimumSpanningTree, minimumWeight: minimumWeight };
}
/**
* FindPath - Find the path from the start vertex to the end vertex
* O(V+E) - Time Complexity
* O(V) - Space Complexity
* @param startVertex
* @param endVertex
* @returns {path: string[], distance: number}
*/
FindPath(
startVertex: string,
endVertex: string,
path: string[],
visited: { [key: string]: boolean }
) {
// If the start vertex is the end vertex, we have found the path
if (startVertex === endVertex) {
path.push(startVertex);
visited[startVertex] = true;
return true;
}
visited[startVertex] = true;
// Mark the start vertex as visited
for (let neighbor of this.adjacencyList[startVertex]) {
// For each neighbor, if it hasn't been visited, recursively call FindPath
// Similar to a DFS, but we are looking for a specific vertex
if (!visited[neighbor.vertex]) {
let res = this.FindPath(neighbor.vertex, endVertex, path, visited);
if (res) {
// If the path is found, push the start vertex to the path and return true
path.push(startVertex);
return true;
}
}
}
// If no path is found, remove the vertex from the path and mark it as unvisited
// Utilizing backtracking to find the path
path.pop();
visited[startVertex] = false;
return false;
}
}
export default Graph;