What is topological sort and when should you use it?
LeetCode Patterns Flashcards: Graphs, BFS, DFS, Topological Sort, Union Find
Аудио-карточка · 0:31Nortren·
What is topological sort and when should you use it?
0:31
Topological sort produces a linear ordering of nodes in a directed acyclic graph, or DAG, such that for every directed edge from A to B, A appears before B. It is used for dependency resolution: course prerequisites, build systems, task scheduling, and compilation order. Two approaches exist. Kahn's algorithm uses BFS: start with nodes having zero incoming edges, remove them and reduce neighbors' incoming counts, repeating until empty. DFS-based approach adds nodes to the result in reverse postorder. If the result does not include all nodes, the graph has a cycle.
neetcode.io