Skip to content

Commit

Permalink
12.13 : topological sort(문제집)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed Dec 13, 2023
1 parent e44a07e commit d315a1b
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 3 deletions.
121 changes: 121 additions & 0 deletions baekjoon/1766.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// 문제집
const input = require("fs")
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n")
.map((v) => v.split(" ").map((v) => parseInt(v, 10)));
const [n, m] = input.shift();
const edges = input;
const graph = {};
const answer = [];
const indegrees = new Array(n + 1).fill(0);

class MinHeap {
constructor() {
this.heap = [];
}

push(value) {
this.heap.push(value);
this.heapifyUp();
}

pop() {
if (this.isEmpty()) return null;

const root = this.heap[0];
const lastNode = this.heap.pop();

if (!this.isEmpty()) {
this.heap[0] = lastNode;
this.heapifyDown();
}

return root;
}

isEmpty() {
return this.heap.length === 0;
}

heapifyUp() {
let index = this.heap.length - 1;
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
if (this.heap[parentIndex] <= this.heap[index]) break;
[this.heap[parentIndex], this.heap[index]] = [
this.heap[index],
this.heap[parentIndex],
];
index = parentIndex;
}
}

heapifyDown() {
let index = 0;
const length = this.heap.length;

while (true) {
let smallest = index;
const leftChildIndex = 2 * index + 1;
const rightChildIndex = 2 * index + 2;

if (
leftChildIndex < length &&
this.heap[leftChildIndex] < this.heap[smallest]
) {
smallest = leftChildIndex;
}

if (
rightChildIndex < length &&
this.heap[rightChildIndex] < this.heap[smallest]
) {
smallest = rightChildIndex;
}

if (smallest === index) break;

[this.heap[index], this.heap[smallest]] = [
this.heap[smallest],
this.heap[index],
];
index = smallest;
}
}
}

const heap = new MinHeap();

for (let i = 1; i <= n; i++) graph[i] = [];

for (let [from, to] of edges) {
graph[from].push(to);
indegrees[to]++;
}

for (let i = 1; i <= n; i++) {
if (indegrees[i] === 0) heap.push(i);
}

while (!heap.isEmpty()) {
const top = heap.pop();
answer.push(top);
for (let node of graph[top]) {
indegrees[node]--;
if (indegrees[node] === 0) heap.push(node);
}
}

console.log(answer.join(" "));

// https://jason9319.tistory.com/93

// 문제를 푼 상황에서 다음에 가장 빨리 올 수 있는 문제를 풀어야 하기에 우선순위 큐를 사용
// 4 2
// 4 2
// 3 1
// 3을 하고 4가 아닌 1을 해야 함

// 우선순위 큐 구현의 문제였다
33 changes: 33 additions & 0 deletions baekjoon/1766.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sys
import heapq

n, m = map(int, sys.stdin.readline().rstrip().split())

answer = []
graph = [[] for _ in range(n + 1)]
inDegree = [0 for _ in range(n+1)]
queue = []


for i in range(m):
first, second = map(int, sys.stdin.readline().rstrip().split())
graph[first].append(second)
inDegree[second] += 1


for i in range(1, n + 1):
if inDegree[i] == 0:
heapq.heappush(queue, i)

print(graph)

while queue:
tmp = heapq.heappop(queue)
answer.append(tmp)
for i in graph[tmp]:
inDegree[i] -= 1
if inDegree[i] == 0:
heapq.heappush(queue, i)


print(" ".join(map(str, answer)))
6 changes: 3 additions & 3 deletions baekjoon/minHeap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class MinHeap {
this.size = 0;
}

insert = (v) => {
push = (v) => {
this.#heap.push(v);
this.size++;
let parent = Math.floor(this.size / 2);
Expand All @@ -26,7 +26,7 @@ class MinHeap {
}
};

delete = () => {
pop = () => {
if (this.size === 0) return;
if (this.size === 1) this.#heap.pop();
else this.#heap[ROOT] = this.#heap.pop();
Expand Down Expand Up @@ -56,7 +56,7 @@ class MinHeap {
}
};

min = () => this.#heap[this.size === 0 ? 0 : 1];
top = () => this.#heap[this.size === 0 ? 0 : 1];

print = () => {
console.log(this.#heap);
Expand Down

0 comments on commit d315a1b

Please sign in to comment.