CSE 2331/5331 Topological Sort A topological sort of a DAG G = (V, E) A linear ordering A of all vertices from V If edge (u,v) E => A[u] < A[v] undershorts pants belt shirt tie jacket shoes socks watch
Another Example CSE 2331/5331 Is the sorting order unique? Why requires DAG?
A topological sorted order of graph G exists if and only if G is a directed acyclic graph (DAG). CSE 2331/5331
Question How to topologically sort a given DAG? Intuition: Which node can be the first node in the topological sort order? A node with in-degree 0 ! After we remove this, the process can be repeated. CSE 2331/5331
Example CSE 2331/5331 undershorts pants belt shirt tie jacket shoes socks watch
Time complexity O(V+E) Correctness: What if the algorithm terminates before we finish visiting all nodes? Procedure TopologicalSort(G) outputs a sorted list of all nodes if and only if the input graph G is a DAG If G is not DAG, the algorithm outputs only a partial list of vertices. CSE 2331/5331
Remarks Other topological sort algorithm by using properties of DFS CSE 2331/5331