Search code examples
algorithmgraphquicksortdirected-acyclic-graphstopological-sort

Could you topologically sort a complete acyclic directed graph through classical sorting algorithms like quicksort?


If you have a complete directed acyclic graph (i.e each vertex has either an ongoing or outgoing edge with any other vertex), you can topologically sort it with the classic topological sort algorithm in O(V+E) = O(V^2) since E = O(V^2).

But could we sort it in O(V log V) through a classic O(n log n) algorithm like quicksort? Since we can always compare two vertices I have the impression this would work. Any counterexample?

Also I guess this could only work if the graph is complete. Even if the graph is almost complete then you can end up having two compare two vertices that don't have an edge.


Solution

  • Yes, if the input graph is acyclic, then it defines a strict weak ordering where a < b if and only if there is an arc from a to b, and quicksort will give you the unique topological order.

    In general, however, you can't test whether a tournament is acyclic without probing all of the arc directions.