Arian Soleimanzadeh
  • 홈
  • 블로그
  • 팟캐스트
  • 비디오
  • 라이브러리
  • 문의
العربيةArabic
DeutschGerman
EnglishEnglish
فارسیPersian
한국어Korean
中文Chinese
패널•빠른 문의

Languages

Choose your interface locale

ar

العربية

Arabic

de

Deutsch

German

en

English

English

fa

فارسی

Persian

ko

한국어

Korean

zh

中文

Chinese

상담 예약

짧은 메시지를 남겨주세요 — 가능한 한 빠르게 답변드리겠습니다.

LinkedIn빠른 응답
홈/아티클/Topological Sorting: Kahn, DFS와 DAG 의존성 정렬
AlgorithmsGraph Algorithms아티클

Topological Sorting: Kahn, DFS와 DAG 의존성 정렬

Kahn, DFS, Cycle Detection, Dependency Level과 실제 DAG 활용을 통해 Topological Sorting을 설명합니다.

2026년 8월 21일9 분 읽기22 조회수
#Graph#Algorithms#Topological Sorting#DAG#Kahn Algorithm#DFS#JavaScript#TypeScript

Arian Soleimanzadeh

Software Engineer & Researcher

Topological Sorting과 DAG 시각 가이드

Arian Soleimanzadeh

AI · 코드 · 제품

연구 + 엔지니어링
이 페이지에서
Kahn 알고리즘DFS 방식여러 정답활용복잡도정리

Topological Sorting은 방향 그래프에서 모든 간선:

Code
1
u → v

에 대해 u가 v보다 먼저 나오도록 정점을 선형 순서로 배치합니다.

이 순서는 DAG(Directed Acyclic Graph) 에서만 존재합니다.

Kahn 알고리즘

Kahn은 in-degree를 사용합니다.

  1. 모든 정점의 in-degree를 계산합니다.
  2. in-degree가 0인 정점을 Queue에 넣습니다.
  3. 하나를 꺼내 결과에 추가합니다.
  4. 이 정점의 outgoing edge를 제거한 것처럼 이웃의 in-degree를 줄입니다.
  5. 새로 0이 된 정점을 Queue에 넣습니다.
  6. 모든 정점을 처리하지 못하면 Cycle이 존재합니다.
JavaScript
1234567891011121314151617181920212223242526272829303132333435363738394041
function topoSort(graph) {
  const nodes = new Set();
  const indegree = new Map();

  for (const [u, list] of Object.entries(graph)) {
    nodes.add(u);
    for (const v of list) nodes.add(v);
  }

  for (const n of nodes) indegree.set(n, 0);

  for (const list of Object.values(graph)) {
    for (const v of list) {
      indegree.set(v, indegree.get(v) + 1);
    }
  }

  const queue = [...nodes].filter(
    n => indegree.get(n) === 0
  );

  let head = 0;
  const order = [];

  while (head < queue.length) {
    const u = queue[head++];
    order.push(u);

    for (const v of graph[u] ?? []) {
      indegree.set(v, indegree.get(v) - 1);

      if (indegree.get(v) === 0) {
        queue.push(v);
      }
    }
  }

  return order.length === nodes.size
    ? order
    : null;
}

DFS 방식

DFS에서 모든 이웃 처리가 끝난 뒤 정점을 결과에 넣고 Finish Order를 뒤집어도 Topological Order를 얻을 수 있습니다.

Cycle Detection을 위해 unvisited, visiting, finished 상태가 필요합니다.

여러 정답

서로 의존하지 않는 Task가 있으면 Topological Order는 여러 개일 수 있습니다.

활용

  • Build Order
  • Course Prerequisite
  • Package Dependency
  • CI/CD
  • Workflow
  • ETL Pipeline
  • Database Migration
  • DAG Dynamic Programming

복잡도

Code
1
O(V + E)

정리

Topological Sorting은 «A가 B보다 먼저 실행되어야 한다»와 같은 의존성을 정렬하는 핵심 알고리즘입니다.

이 페이지에서
Kahn 알고리즘DFS 방식여러 정답활용복잡도정리

아티클 정보

게시 정보, 읽기 시간 및 조회 데이터입니다.

게시일

2026년 8월 21일

업데이트

2026년 11월 2일

읽기 시간

9 분 읽기

조회수

22

작성자

Arian Soleimanzadeh

이전 아티클

그래프 Connected Components: DFS, BFS와 Union-Find

다음 아티클

Articulation Points: Tarjan과 Low-Link로 핵심 정점 찾기

Arian Soleimanzadeh

현대적인 웹 엔지니어링, UI 시스템, 실용적인 AI 제품에 집중한 개인 포트폴리오 — 깨끗한 코드, 명확한 디자인.

빠른 링크

  • 소개
  • 블로그
  • 라이브러리
  • 문의

문의

  • soleimanzadeh.a.work@gmail.com
  • soleimanzadeh.uni@gmail.com

가능 시간: 평일

보통 다음 시간 내에 답변합니다 24시간.

뉴스레터

글, 프로젝트, 새 릴리스에 대한 업데이트를 받아보세요.

© 2026 ariansoleimanzadeh.dev — 모든 권리 보유.

LinkedIn