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-8Lines changed: 10 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -1,25 +1,25 @@
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
+
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.
5
5
6
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.
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 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).
9
9
10
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).
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** 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.
13
13
14
14
## The Algorithm
15
15
16
16
For the solution we will use a depth-first-search.
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
+
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.
19
19
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**.
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
22
-
These explanations can also be presented in a somewhat different light, through the concept of **"time out"** of depth. Output time for each vertex `v` is the point at which the finished challenge {\rm dfs}(v) the depth of it (the times, when you can zanemarivali from 1 before n). It is easy to understand that the depth-first-exit time of any vertex `v` is always greater than the exit time of all vertices reachable from it (since they were visited either before the call {\rm dfs}(v)or during it). Thus, the desired topological sort is sorting in descending order times out.
22
+
These explanations can also be presented in a somewhat different light, through the concept of **"time out"** of depth. Output time for each vertex $v$ is the point at which the finished challenge ${\rm dfs}(v)$ the depth of it (the times, when you can zanemarivali from $1$ before $n$). It is easy to understand that the depth-first-exit time of any vertex $v$ is always greater than the exit time of all vertices reachable from it (since they were visited either before the call ${\rm dfs}(v)$ or during it). Thus, the desired topological sort is sorting in descending order times out.
23
23
24
24
## Implementation
25
25
@@ -54,9 +54,9 @@ void topological_sort() {
54
54
}
55
55
```
56
56
57
-
Here the constant **MAXN** value must be set equal to the maximum possible number of vertices in the graph.
57
+
Here the constant $MAXN$ value must be set equal to the maximum possible number of vertices in the graph.
58
58
59
-
The main function of the solution is topological_sort, it initializes marking depth, starts it, and answer the result in the vector \rm ans.
59
+
The main function of the solution is $topological\\_sort$, it initializes marking depth, starts it, and answer the result in the vector ${\rm ans}$.
60
60
61
61
## Practice Problems
62
62
@@ -66,3 +66,5 @@ The task list in which you want to search topological sorting:
0 commit comments