-
Notifications
You must be signed in to change notification settings - Fork 0
/
1260_dfsBfs.js
88 lines (82 loc) · 1.61 KB
/
1260_dfsBfs.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
// 1260, dfs와 bfs
let fs = require("fs");
let [info, ...list] = fs.readFileSync("input.txt").toString().trim().split("\n");
class Queue {
constructor() {
this.list = {};
this.head = 0;
this.tail = 0;
}
enqueue(item) {
this.list[this.tail] = item;
this.tail++;
}
dequeue() {
let item = this.list[this.head];
delete this.list[this.head];
this.head++;
return item;
}
getLength() {
return this.tail - this.head;
}
}
const [n, m, start] = info.split(" ").map(Number);
let visited = new Array(n).fill().map((v) => 0);
let DFSpath = [];
let obj = {};
for (let item of list) {
let [a, b] = item.split(" ");
if (!obj[a]) {
obj[a] = [];
}
if (!obj[b]) {
obj[b] = [];
}
obj[a].push(Number(b));
obj[b].push(Number(a));
}
console.log(obj);
for (let key in obj) {
obj[key].sort((a, b) => a - b);
}
function dfs(num) {
for (let nxt of obj[num]) {
if (!visited[nxt]) {
visited[nxt] = 1;
DFSpath.push(nxt);
dfs(nxt);
}
}
}
if (obj[start]) {
DFSpath.push(start);
visited[start] = 1;
dfs(start);
console.log(DFSpath.join(" "));
} else {
console.log(start);
}
visited = new Array(n).fill().map((v) => 0);
let BFSpath = [];
function bfs(start) {
let queue = new Queue();
queue.enqueue(start);
while (queue.getLength() !== 0) {
let cur = queue.dequeue();
BFSpath.push(cur);
for (let nxt of obj[cur]) {
if (!visited[nxt]) {
visited[nxt] = 1;
queue.enqueue(nxt);
}
}
}
}
if (obj[start]) {
visited[start] = 1;
bfs(start);
console.log(BFSpath.join(" "));
} else {
console.log(start);
}