Topological Sort | Data Structures Interview | Skill-Lync Resources
Medium Data Structures Graphs

What is topological sorting and how do you implement it?

Answer

Topological sort orders vertices of a directed acyclic graph (DAG) such that for every edge u->v, u comes before v. Two approaches: Kahn's algorithm uses BFS with in-degree tracking, repeatedly removing zero in-degree vertices. DFS-based approach recursively visits all descendants before adding current node to result (then reverse). Both are O(V+E). Used for task scheduling, build systems, and course prerequisites.

Master These Concepts with IIT Certification
IIT Certified

Master These Concepts with IIT Certification

175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.

Relevant for Roles

Software Engineer Backend Developer Systems Developer