Skip to content

Commit 774c87d

Browse files
committed
✨ [1971] bfs
1 parent 97a4b30 commit 774c87d

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

1971/gpt_solution.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
function validPath(n, edges, source, destination) {
2+
// Step 1: Build adjacency list
3+
let graph = buildGraph(n, edges);
4+
console.log("graph")
5+
console.log(graph)
6+
7+
8+
// Step 2: Initialize BFS
9+
let queue = [];
10+
queue.push(source);
11+
12+
// Step 3: Initialize visited array
13+
let visited = Array(n).fill(false);
14+
15+
// console.log("visited")
16+
// console.log(visited)
17+
18+
visited[source] = true; // this is how the duplicated was prevented.
19+
20+
// Step 4: Perform BFS
21+
while (queue.length > 0) {
22+
let currentVertex = queue.shift();
23+
console.log("currentVertex: " + currentVertex)
24+
25+
26+
// Step 4.1: Check if destination is reached
27+
if (currentVertex === destination) {
28+
console.log(`> Found ${currentVertex} === ${destination}`)
29+
return true;
30+
}
31+
32+
console.log("graph[currentVertex]: " + graph[currentVertex])
33+
34+
// Step 4.2: Iterate through adjacent vertices
35+
for (let neighbor of graph[currentVertex]) {
36+
if (!visited[neighbor]) {
37+
visited[neighbor] = true;
38+
queue.push(neighbor);
39+
}
40+
}
41+
}
42+
43+
// Step 5: No valid path found
44+
return false;
45+
}
46+
47+
function buildGraph(n, edges) {
48+
// const graph = new Array(n).fill(null).map(() => []);
49+
const graph = Array.from({ length: n }, () => []);
50+
// console.log("init")
51+
// console.log(graph)
52+
53+
for (const [u, v] of edges) {
54+
graph[u].push(v);
55+
graph[v].push(u);
56+
}
57+
58+
return graph;
59+
}
60+
61+
// let x = validPath(3, [[0, 1], [1, 2], [2, 0]], 0, 2)
62+
// let x = validPath(6, [[0, 1], [0, 2], [3, 5], [5, 4], [4, 3]], 0, 5)
63+
// let x = validPath(1, [], 0, 0)
64+
let x = validPath(10, [[4, 3], [1, 4], [4, 8], [1, 7], [6, 4], [4, 2], [7, 4], [4, 0], [0, 9], [5, 4]], 5, 9)
65+
console.log("x")
66+
console.log(x)

1971/my_solution.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// 1971. Find if Path Exists in Graph
2+
/**
3+
* @param {number} n
4+
* @param {number[][]} edges
5+
* @param {number} source
6+
* @param {number} destination
7+
* @return {boolean}
8+
*/
9+
const validPath = (n, e, s, d) => {
10+
let map = new Map();
11+
12+
for (let i = 0; i < e.length; i++) {
13+
// console.log("e[i]")
14+
// console.log(e[i])
15+
// console.log(map.get(e[i][0]) || []);
16+
// console.log((map.get(e[i][1]) || []).push(e[i][0]));
17+
18+
19+
map.set(e[i][0], (map.get(e[i][0]) || []).concat(e[i][1]));
20+
map.set(e[i][1], (map.get(e[i][1]) || []).concat(e[i][0]));
21+
// console.log(map)
22+
}
23+
console.log(map)
24+
25+
const findInLayer = (list, count) => {
26+
console.log("> Proc")
27+
console.log(list, count)
28+
if (count === n || 0 === list.length) return false;
29+
30+
if (list.includes(d)) return true;
31+
32+
for (let i = 0; i < list.length; i++) {
33+
console.log()
34+
count++;
35+
return findInLayer(map.get(list[i]), 0);
36+
}
37+
}
38+
39+
return (undefined === map.get(s)) ? true : findInLayer(map.get(s), 0);
40+
};
41+
42+
// let x = validPath(3, [[0, 1], [1, 2], [2, 0]], 0, 2)
43+
// let x = validPath(6, [[0, 1], [0, 2], [3, 5], [5, 4], [4, 3]], 0, 5)
44+
// let x = validPath(1, [], 0, 0)
45+
let x = validPath(10, [[4, 3], [1, 4], [4, 8], [1, 7], [6, 4], [4, 2], [7, 4], [4, 0], [0, 9], [5, 4]], 5, 9)
46+
console.log("x")
47+
console.log(x)

1971/solution.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 1971. Find if Path Exists in Graph
2+
/**
3+
* @param {number} n
4+
* @param {number[][]} edges
5+
* @param {number} source
6+
* @param {number} destination
7+
* @return {boolean}
8+
*/
9+
const validPath = (n, e, s, d) => {
10+
let map = new Map();
11+
12+
for (let i = 0; i < e.length; i++) {
13+
map.set(e[i][0], (map.get(e[i][0]) || []).concat(e[i][1]));
14+
map.set(e[i][1], (map.get(e[i][1]) || []).concat(e[i][0]));
15+
}
16+
17+
const findInLayer = (list, count) => {
18+
if (count === n || 0 === list.length) return false;
19+
if (list.includes(d)) return true;
20+
21+
for (let i = 0; i < list.length; i++) {
22+
return findInLayer(map.get(list[i]), count + 1);
23+
}
24+
}
25+
26+
return (undefined === map.get(s)) ? true : findInLayer(map.get(s), 0);
27+
};

0 commit comments

Comments
 (0)