Topological sort

This article shows how we can use depth-first search to perform a topological sort of a directed acyclic graph, or a “dag” as it is sometimes called. A topological sort of a dag $G = (V, E)$ is a linear ordering of all its vertices such that if $G$ contains an edge $(u, v)$, then $u$ appears before $v$ in the ordering. (If the graph contains a cycle, then no linear ordering is possible.) We can view a topological sort of a graph as an ordering of its vertices along a horizontal line so that all directed edges go from left to right. Topological sorting is thus different from the usual kind of “sorting”. [1]

The following simple algorithm topologically sorts a dag:


Now let’s take a look at a real world application of the Topological sort algorithm.

Course Schedule

There are a total of $n$ courses you have to take, labeled from $0$ to $n-1$.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: $\left[
\begin{array}{cc}
  0,&1
\end{array}
\right]
$

Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

Example 1:

Input: $2$, $\left[\left[
\begin{array}{cc}
  1,&0
\end{array}
\right]
\right]$
Output: $\left[
\begin{array}{cc}
  0,&1
\end{array}
\right]
$
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is $\left[
\begin{array}{cc}
  0,&1
\end{array}
\right]
$

Example 2:

Input: $4$, $\left[\left[
\begin{array}{cc}
  1,&0
\end{array}
\right],\; \left[
\begin{array}{ccc}
  2,&0
\end{array}
\right],\; \left[
\begin{array}{ccc}
  3,&1
\end{array}
\right]
,\; \left[
\begin{array}{ccc}
  3,&2
\end{array}
\right]
\right]$
Output: $\left[
\begin{array}{cc}
  0,&1,&2,&3
\end{array}
\right]
$ or $\left[
\begin{array}{cc}
  0,&2,&1,&3
\end{array}
\right]
$
Explanation: There are a total of $4$ courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is $\left[
\begin{array}{cc}
  0,&1,&2,&3
\end{array}
\right]
$. Another correct ordering is $\left[
\begin{array}{cc}
  0,&2,&1,&3
\end{array}
\right]
$ .


Example 3:

Input: 4, $\left[\left[
\begin{array}{cc}
  1,&0
\end{array}
\right],\; \left[
\begin{array}{ccc}
  2,&0
\end{array}
\right],\; \left[
\begin{array}{ccc}
  0,&1
\end{array}
\right]
\right]$
Output: $\left[\;
\right]$
Explanation: There exists a cycle between the vertices $0$ and $1$, hence  no linear ordering is possible. Return an empty array.

Note:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented. [1]
  2. You may assume that there are no duplicate edges in the input prerequisites.
And, here’s the implementation of the algorithm.


Analysis

We can perform a topological sort in time $Θ(V + E)$, since depth-first search takes $Θ(V + E)$ time and it takes $Θ(1)$ time to insert each of the $\lvert V \rvert$ vertices onto the front of the linked list.


References

[1] https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press/dp/0262033844

Comments

Popular posts from this blog

Introducing Java Reactive Extentions in to a SpringBoot Micro Service

Optimal binary search trees

Edit distance