educative.io

Educative

Topological Sort from DFS

Isn’t topological sort typically done with DFS (depth first search)? Is there a reason that we are using a BFS here for topological sorting?

Topological Sort can be done using both DFS and BFS.

When using BFS we consider In Degree of node and for DFS we consider the nodes when we backtrack.

Thanks for the response. Is there a reason that the authors have chosen BFS for topological sort than the more conventional DFS?

I’m also wondering why BFS was chosen instead of DFS which, in my opinion, allows to implement TopSort much easier :thinking: