Skip to content

Commit d868a05

Browse files
committed
Update README.markdown
1 parent 4a512ea commit d868a05

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Topological Sort/README.markdown

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Let's consider that you want to learn all the algorithms present in the swift-al
88

99
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:
1010

11-
//add image
11+
![Example](Images/algorithm_example.png)
1212

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

@@ -34,17 +34,23 @@ The last step of the sort is to maintain a list of all the nodes that have alrea
3434

3535
Consider the following graph:
3636

37+
![Graph Example](Images/graph_example.png)
38+
3739
*Step 1* Nodes with 0 in-degree: **5, 7, 3**
3840
*Step 2* Depth first search for each without remembering nodes that have already been visited:
3941

42+
```
4043
Node 3: 3, 8, 9, 10
4144
Node 7: 7, 11, 2, 8, 9
4245
Node 5: 5, 11, 2, 9, 10
46+
```
4347

4448
*Step 3* Remember nodes already visited in each DFS.
4549

50+
```
4651
Node 3: 3, 8, 9, 10
4752
Node 7: 7, 11, 2
4853
Node 5: 5
54+
```
4955

5056
The final sorted order is a concatenation of the above three traversals: **3, 8, 9, 10, 7, 11, 2, 5**

0 commit comments

Comments
 (0)