-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (54 loc) · 1.4 KB
/
index.js
File metadata and controls
65 lines (54 loc) · 1.4 KB
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
const path = require('path');
const fs = require('fs');
const rawInputs = fs
.readFileSync(path.resolve(__dirname, '../TopologicalSort/input.txt'))
.toString()
.trim()
.split('\n')
.map((e) => e.split(' ').map((v) => +v));
// 입력 예시
// a -> b
// 1 2
// 1 5
// 2 3
// 3 4
// 4 6
// 5 6
// 6 7
const topologySort = (arr) => {
const answer = [];
const queue = []; // TODO: queue구현해서 수정하기
const n = arr.length;
const graph = {};
const indegreeTable = {};
// 그래프의 인접리스트와 진입차수 테이블 초기화
arr.forEach(([a, b]) => {
graph[a] = [];
graph[b] = [];
indegreeTable[a] = 0;
indegreeTable[b] = 0;
});
arr.forEach(([a, b]) => {
graph[a].push(b);
indegreeTable[b]++;
});
// 진입차수가 0인 정점 큐에 넣기
Object.entries(indegreeTable).forEach(([key, value]) => {
if (value === 0) queue.push(key);
});
while (queue.length > 0) {
const current = queue.shift();
answer.push(current);
// 해당 노드에서 연결된 노드의 진입차수를 명목상 1씩 줄이기
// 줄인 노드의 진입차수가 0이면 해당 노드를 큐에 넣기
for (const b of graph[current]) {
indegreeTable[b]--;
if (indegreeTable[b] === 0) {
queue.push(b);
}
}
}
return answer.join(' ');
};
console.log(topologySort(rawInputs));
module.exports = { topologySort };