You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/graph/topological-sort.md
+10-12Lines changed: 10 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,21 @@
1
1
<!--?title Topological Sorting -->
2
2
# Topological Sorting
3
3
4
-
Given a directed graph with $n$ vertices and $m$ edges. You want to **renumber the vertices** so that every edge led from the top with a smaller number in the top with a large.
4
+
You are given a directed graph with $n$ vertices and $m$ edges. You have to **number the vertices** so that every edge leads from the vertex with a smaller number assigned to the vertex with a larger one.
5
5
6
-
In other words, you want to find a permutation of the vertices **(topological order)** corresponding to the order defined by all edges of the graph.
6
+
In other words, you want to find a permutation of the vertices (**topological order**) which corresponds to the order defined by all edges of the graph.
7
7
8
-
Topological sort can be **not only** (for example, if the graph is empty; or if there are three such vertices $a$, $b$, $c$, what $a$ is the way in $b$ and in $c$, but neither $b$ in $c$ nor out $c$ to $b$ get them).
8
+
Topological order can be **non-unique** (for example, if the graph is empty; or if there exist three vertices $a$, $b$, $c$ for which there exist paths from $a$ to $b$ and from $a$ to $c$ but not paths from $b$ to $c$ or from $c$ to $b$).
9
9
10
-
Topological sorting **may not** exist at all - if the graph contains cycles (because there is a contradiction: there is a path from one vertex to another, and Vice versa).
10
+
Topological order may **not exist** at all if the graph contains cycles (because there is a contradiction: there is a path from $a$ to $b$ and vice versa).
11
11
12
-
A **common problem** for topological sorting next: There are $n$ variables whose values are unknown to us. We know only about some pairs of variables, one variable less than the other. You want to check, is not controversial whether these inequalities, and if not, to give the variables in ascending order (if the answer to any issue). It is easy to notice that this is exactly the task is about finding a topological sort of the graph of $n$ vertices.
12
+
A common problem in which topological sorting occurs is the following. There are $n$ variables with unknown values. For some variables we know that one of them is less than the other. You have to checkwhether these constraints are contradictory, and if not, output the variables in ascending order (if several answers are possible, output any of them). It is easy to notice that this is exactly the problem of finding topological order of a graph with $n$ vertices.
13
13
14
14
## The Algorithm
15
15
16
-
For the solution we will use a depth-first-search.
16
+
To solve this problem we will use [depth-first search](./graph/depth-first-search.html).
17
17
18
-
Assume that the graph is acyclic, i.e. there is a solution. What makes the depth? When you run out of some of the peaks $v$ he tries to run along all edges emanating from $v$. Along those edges, the ends of which have already been previously visited, it doesn't pass, and along the rest goes and causes themselves from their ends.
18
+
Let's assume that the graph is acyclic, i.e. there is a solution. What does the depth-first search do? When you run out of some of the peaks $v$ he tries to run along all edges emanating from $v$. Along those edges, the ends of which have already been previously visited, it doesn't pass, and along the rest goes and causes themselves from their ends.
19
19
20
20
Thus, by the time of the call, ${\rm dfs}(v)$ all vertices that are reachable from $v$ either directly (one edge), and indirectly (by the way) - all such vertices already visited. Therefore, if we at the time of exit from ${\rm dfs}(v)$ add our peak in the beginning of a list, in the end, this list will get a **topological sort**.
21
21
@@ -25,16 +25,16 @@ These explanations can also be presented in a somewhat different light, through
25
25
26
26
Here is an implementation, assuming that the graph is acyclic, i.e. the desired topological sort exists. If necessary, check the graph on acyclicity easy to insert into the depth, as described in the article on depth.
0 commit comments