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: Topological Sort/README.markdown
+31-13
Original file line number
Diff line number
Diff line change
@@ -2,31 +2,39 @@
2
2
3
3
Topological sort is an algorithm aimed at ordering a directed graph such that for each directed edge *uv* from vertex *u* to vertex *v*, *u* comes before *v*.
4
4
5
+
In other words, a topological sort places the vertices of a directed acyclic graph on a line so that all directed edges go from left to right.
6
+
7
+
The graph in the following example has two possible topological sorts:
8
+
9
+

10
+
5
11
## Where is this used?
6
12
7
-
Let's consider that you want to learn all the algorithms present in the swift-algorithm-club, this might seem daunting at first but we can use topological sort to get things organized.
13
+
Let's consider that you want to learn all the algorithms present in the Swift Algorithm Club. This might seem daunting at first but we can use topological sort to get things organized.
8
14
9
-
For e.g. To learn the depth first search algorithm you need to know how a graph is represented. Similarly, to understand how to calculate the length of a binary tree you need to know details of how a tree is traversed. If we were to represent these in the form of a graph it would be as follows:
15
+
For example, to learn the [depth-first search](../Depth-First Search/) algorithm you need to know how a [graph](../Graph/) is represented. Similarly, to understand how to calculate the length of a [binary tree](../Binary Tree/) you need to know details of how a [tree is traversed](../Tree/).
16
+
17
+
If we were to represent these objectives in the form of a graph it would be as follows:
10
18
11
19

12
20
13
-
If we consider each algorithm to be a node in the graph you'll see dependancies among them i.e. to learn something you might have to know something else first. This is exactly what topological sort is used for, it will sort things out such that you know what to learn first.
21
+
If we consider each algorithm to be a node in the graph you'll see dependancies among them, i.e. to learn something you might have to know something else first. This is exactly what topological sort is used for -- it will sort things out such that you know what to learn first.
14
22
15
23
## How does it work?
16
24
17
-
### Step 1: Find all nodes that have in-degree of 0
25
+
**Step 1: Find all nodes that have in-degree of 0**
18
26
19
-
The in-degree of a node is the number of incoming edges to that node. All nodes that have no incoming edges have an in-degree of 0. These nodes are the starting points for the sort.
27
+
The *in-degree* of a node is the number of incoming edges to that node. Nodes that have no incoming edges have an in-degree of 0. These nodes are the starting points for the sort.
20
28
21
-
If you think about it in the context of the previous example these nodes represent algorithms that don't need anything else to be learnt, hence the sort starts with them.
29
+
If you think about it in the context of the previous example, these nodes represent algorithms that don't have any prerequisites; you don't need to learn anything else first, hence the sort starts with them.
22
30
23
-
### Step 2: Depthfirst search for traversal
31
+
**Step 2: Depth-first search for traversal**
24
32
25
-
Depthfirst search is an algorithm that is used to traverse a graph. This algorithm traverses all the child nodes recursively and uses backtracking.
33
+
Depth-first search is an algorithm that is used to traverse a graph. This algorithm traverses all the child nodes recursively and uses backtracking.
26
34
27
-
To know more about this algorithm please take a look at the explanation [here](https://github.com/hollance/swift-algorithm-club/tree/master/Depth-First%20Search).
35
+
To find out more about depth-first search, please take a look at the [detailed explanation](../Depth-First%20Search/).
28
36
29
-
### Step 3: Remember all visited nodes
37
+
**Step 3: Remember all visited nodes**
30
38
31
39
The last step of the sort is to maintain a list of all the nodes that have been visited. This is to avoid visiting the same node twice.
32
40
@@ -36,16 +44,17 @@ Consider the following graph:
36
44
37
45

38
46
39
-
*Step 1* Nodes with 0 in-degree: **5, 7, 3**
40
-
*Step 2* Depth first search for each without remembering nodes that have already been visited:
47
+
**Step 1:** The nodes with 0 in-degree are: **5, 7, 3**
48
+
49
+
**Step 2:** Depth-first search for each starting node, without remembering nodes that have already been visited:
41
50
42
51
```
43
52
Node 3: 3, 8, 9, 10
44
53
Node 7: 7, 11, 2, 8, 9
45
54
Node 5: 5, 11, 2, 9, 10
46
55
```
47
56
48
-
*Step 3* Remember nodes already visited in each DFS.
57
+
**Step 3:** Remember nodes already visited in each DFS:
49
58
50
59
```
51
60
Node 3: 3, 8, 9, 10
@@ -54,3 +63,12 @@ Node 5: 5
54
63
```
55
64
56
65
The final sorted order is a concatenation of the above three traversals: **3, 8, 9, 10, 7, 11, 2, 5**
66
+
67
+
The result of the topological sort looks like this:
68
+
69
+

70
+
71
+
TODO: I don't think this is correct! There should be no arrows going from right to left! (A valid topological order would be 3, 7, 5, 10, 8, 11, 9, 2 -- or 3, 7, 5, 8, 11, 2, 9, 10.)
0 commit comments