Skip to content

Commit 10529fe

Browse files
joaquingxtcNickolas
authored andcommitted
Topological Sort improve formatting + practice problem (#160)
1 parent a918550 commit 10529fe

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/graph/topological-sort.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<!--?title Topological Sorting -->
22
# Topological Sorting
33

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.
55

66
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.
77

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).
99

1010
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).
1111

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.
1313

1414
## The Algorithm
1515

1616
For the solution we will use a depth-first-search.
1717

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.
1919

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**.
2121

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.
2323

2424
## Implementation
2525

@@ -54,9 +54,9 @@ void topological_sort() {
5454
}
5555
```
5656
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.
5858
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}$.
6060
6161
## Practice Problems
6262
@@ -66,3 +66,5 @@ The task list in which you want to search topological sorting:
6666
- [UVA #10305 "Ordering Tasks" [difficulty: easy]](https://z5h64q92x9.net/proxy_u/ru-en.en/uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1246)
6767
- [UVA #124 "Following Orders" [difficulty: easy]](https://z5h64q92x9.net/proxy_u/ru-en.en/uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=60)
6868
- [UVA #200 "Rare Order" [difficulty: easy]](https://z5h64q92x9.net/proxy_u/ru-en.en/uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=136)
69+
- [Codeforces 510C "Fox and Names" [difficulty: easy]](http://codeforces.com/problemset/problem/510/C)
70+

0 commit comments

Comments
 (0)