|
| 1 | +# Topological Sorting |
| 2 | + |
| 3 | +In the field of computer science, a topological sort or |
| 4 | +topological ordering of a directed graph is a linear ordering |
| 5 | +of its vertices such that for every directed edge `uv` from |
| 6 | +vertex `u` to vertex `v`, `u` comes before `v` in the ordering. |
| 7 | + |
| 8 | +For instance, the vertices of the graph may represent tasks to |
| 9 | +be performed, and the edges may represent constraints that one |
| 10 | +task must be performed before another; in this application, a |
| 11 | +topological ordering is just a valid sequence for the tasks. |
| 12 | + |
| 13 | +A topological ordering is possible if and only if the graph has |
| 14 | +no directed cycles, that is, if it is a [directed acyclic graph](https://en.wikipedia.org/wiki/Directed_acyclic_graph) |
| 15 | +(DAG). Any DAG has at least one topological ordering, and algorithms are |
| 16 | +known for constructing a topological ordering of any DAG in linear time. |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +A topological ordering of a directed acyclic graph: every edge goes from |
| 21 | +earlier in the ordering (upper left) to later in the ordering (lower right). |
| 22 | +A directed graph is acyclic if and only if it has a topological ordering. |
| 23 | + |
| 24 | +## Example |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +The graph shown above has many valid topological sorts, including: |
| 29 | + |
| 30 | +- `5, 7, 3, 11, 8, 2, 9, 10` (visual left-to-right, top-to-bottom) |
| 31 | +- `3, 5, 7, 8, 11, 2, 9, 10` (smallest-numbered available vertex first) |
| 32 | +- `5, 7, 3, 8, 11, 10, 9, 2` (fewest edges first) |
| 33 | +- `7, 5, 11, 3, 10, 8, 9, 2` (largest-numbered available vertex first) |
| 34 | +- `5, 7, 11, 2, 3, 8, 9, 10` (attempting top-to-bottom, left-to-right) |
| 35 | +- `3, 7, 8, 5, 11, 10, 2, 9` (arbitrary) |
| 36 | + |
| 37 | +## Application |
| 38 | + |
| 39 | +The canonical application of topological sorting is in |
| 40 | +**scheduling a sequence of jobs** or tasks based on their dependencies. The jobs |
| 41 | +are represented by vertices, and there is an edge from `x` to `y` if |
| 42 | +job `x` must be completed before job `y` can be started (for |
| 43 | +example, when washing clothes, the washing machine must finish |
| 44 | +before we put the clothes in the dryer). Then, a topological sort |
| 45 | +gives an order in which to perform the jobs. |
| 46 | + |
| 47 | +Other application is **dependency resolution**. Each vertex is a package |
| 48 | +and each edge is a dependency of package `a` on package 'b'. Then topological |
| 49 | +sorting will provide a sequence of installing dependencies in a way that every |
| 50 | +next dependency has its dependent packages to be installed in prior. |
| 51 | + |
| 52 | +## References |
| 53 | + |
| 54 | +- [Wikipedia](https://en.wikipedia.org/wiki/Topological_sorting) |
| 55 | +- [Topological Sorting on YouTube by Tushar Roy](https://www.youtube.com/watch?v=ddTC4Zovtbc) |
0 commit comments