-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindPathGraph.js
79 lines (63 loc) · 1.74 KB
/
findPathGraph.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
/*
Route between Nodes: Given a directed graph, design an algorithm to find out whether there is a route between two nodes
Graph is represented via an adjacency list:
let example =
[ [1, 6, 8],
[0, 4, 6, 9],
[4, 6],
[4, 5, 8],
[1, 2, 3, 5, 9],
[3, 4],
[0, 1, 2],
[8, 9],
[0, 3, 7],
[1, 4, 7] ]
Open to comments and suggestions
*/
// Using BFS
function findPathBFS(graph, nodeA, nodeB) {
let queue = [];
let visited = new Set();
queue.push(nodeA);
visited.add(nodeA);
while (queue.length !== 0) {
let currNode = queue.shift(); // shift off first element
if (currNode === nodeB) return true; // check if current node equals target
for (let i = 0; i < graph[currNode].length; i++) {
if (!visited.has(graph[currNode][i])) { // check if child has been visited
queue.add(graph[currNode][i]); // add child to queue
visited.add(graph[currNode][i]); // mark child as visited
}
}
}
return false;
}
// Using DFS
function findPathDFSRecursive(graph, visited, nodeA, nodeB) {
visited.add(nodeA);
if (nodeA === nodeB) return true;
for (let i = 0; i < graph[nodeA].length; i++) {
if (!visited.has(graph[nodeA][i])) {
findPathDFSRecursive(graph, visited, graph[nodeA][i], nodeB);
}
}
return false;
}
// Using DFS Stack
function findPathDFSIterative(graph, nodeA, nodeB) {
let stack = [];
let visited = new Set();
stack.push(nodeA);
visited.add(nodeA);
while (stack.length !== 0) {
let currNode = stack.pop();
if (currNode === nodeB) return true;
for (let i = 0; i < graph[currNode][i]; i++) {
if (!visited.has(graph[currNode][i])) {
stack.push(graph[currNode][i]);
visited.add(graph[currNode][i]);
}
}
}
return false;
}