-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path49adjecency_list.js
More file actions
35 lines (30 loc) · 854 Bytes
/
49adjecency_list.js
File metadata and controls
35 lines (30 loc) · 854 Bytes
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
let arr = [[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 5], [3, 4], [4, 2]];
function solution(n, arr) {
let answer = 0;
let graph = Array.from(Array(n+1), () => Array());
let ch = Array.from({ length: n+1 }, () => 0);
let path = [];
for (let [a, b] of arr) {
graph[a].push(b);
}
function DFS(v) {
if (v === n) {
answer++;
console.log(path);
} else {
for (let i = 0; i <= graph[v].length; i++) {
if (ch[graph[v][i]]=== 0) {
ch[graph[v][i]] = 1;
path.push(graph[v][i]);
DFS(graph[v][i]);
ch[graph[v][i]] = 0;
path.pop();
}
}
}
}
ch[1] = 1;
DFS(1);
return answer;
}
console.log(solution(5, arr));