Topological Sort: Introduction
Let’s go over the Topological Sort pattern, its real-world applications and some problems we can solve with it.
We'll cover the following
Overview#
The topological sort pattern is used to find valid orderings of elements that have dependencies on, or priority over each other. Scheduling and grouping problems that have prerequisites or dependencies generally fall under this pattern.
In simple terms, topological order can be described as a partial order. In comparison to a total order, it offers more flexibility since there can be multiple correct answers. Consider sorting an array in ascending order. This gives a unique result , which is the only correct answer—a complete order. On the other hand, consider an example of course prerequisites where English I has to be taken before English II, and Mathematics can be taken whenever. Since there’s no obligation on when to take Mathematics, we can take it in any order as long as the dependency is respected—that is, as long as English I is taken before English II. This is an example of partial order, or topological order, since there are three valid orders in which a student may take these courses:
- English I, English II, Mathematics
- Mathematics, English I, English II
- English I, Mathematics, English II
With the topological sort pattern, converting a problem to a directed graph is the first step to finding a solution. The elements to consider correspond to graph vertices and the dependency relationships form the edges. A valid topological order exists if the graph is acyclic. For example, if is dependent on , there is an edge from to , hence, comes before in a linear ordering. However, if an edge exists from to as well, that is, there is a cycle, a linear ordering is not possible.
Consider another example where an edge exists from vertex to vertex and vertex . In this case, after adding to our ordering, there are two possible options, or . Both of them are valid, so we get two orders: or .
The following illustration shows a simple example of finding the topological order from a graph:
1 of 6
2 of 6
3 of 6
4 of 6
5 of 6
6 of 6
Examples#
The following examples illustrate some problems that can be solved with this approach:
1 of 2
2 of 2
Does my problem match this pattern?#
-
Yes, if either of these conditions is fulfilled:
- A partial ordering of the given elements is required.
- The problem asks you to find an ordering of elements based on some dependency rules between them.
-
No, if either of these conditions is fulfilled:
- A total ordering of the given elements is required.
- The problem relationships cannot be converted to a graph.
- The corresponding graph is not a tree.
Real-world problems#
Many problems in the real world use the topological sort pattern. Let’s look at an example.
Topological sort can be used for process scheduling in a computer system. When a system is booted, the operating system needs to run a number of processes. Some processes have dependencies, which are specified using ordered pairs of the form . This means that process must be run before process . Some processes don’t have any dependencies, meaning they don’t have to wait for any processes to finish. Additionally, there cannot be any circular dependencies between the processes like . In order to successfully start the system, the operating system needs to select an order to run the processes in. The processes should be ordered in such a way that whenever a process is scheduled to run, all of its dependencies are already met.
These processes can be represented in the form of a graph where the vertices are the processes and the edges represent their dependency relationships. This makes scheduling a perfect example of the topological sort pattern.
Strategy time!#
Match the problems that can be solved using the topological sort pattern.
Note: Select a problem in the left-hand column by clicking it, and then click one of the two options in the right-hand column.
Given a sample of words in an alien language, deduce the order of the letters in their alphabet.
Topological Sort
Find the number of connected components in a directed acyclic graph.
Some other pattern
Schedule courses such that their prerequisites are met.
Find the next greatest element after in an unsorted array.
Solution: Min Stack
Course Schedule