Download presentation
Presentation is loading. Please wait.
Published byErin McCarthy Modified over 8 years ago
1
Topological Sort
2
Sorting technique over DAGs (Directed Acyclic Graphs) It creates a linear sequence (ordering) for the nodes such that: –If u has an outgoing edge to v then u must finish before v starts Very common in ordering jobs or tasks
3
Topological Sort Example A job consists of 10 tasks with the following precedence rules: Must start with 7, 5, 4 or 9. Task 1 must follow 7. Tasks 3 & 6 must follow both 7 & 5. 8 must follow 6 & 4. 2 must follow 4. 10 must follow 2. Make a directed graph and then do DFS.
4
9 1 7 3 5 4 6 8 2 10 Tasks shown as a directed graph.
5
Topological Sort using DFS To create a topological sort from a DAG 1- Final linked list is empty 2- Run DFS 3- When a node becomes black (finishes) insert it to the top of a linked list
6
9 1 7 3 5 4 6 8 2 10 Example
7
9 1 7 3 5 4 6 8 2 10 Example
8
9 1 7 3 5 4 6 8 2 10 Example
9
9 1 7 3 5 4 6 8 2 10 Example
10
9 1 7 3 5 4 6 8 2 10 Example 10 head
11
9 1 7 3 5 4 6 8 2 10 Example 2, 10 head
12
9 1 7 3 5 4 6 8 2 10 Example 2, 10 head
13
9 1 7 3 5 4 6 8 2 10 Example 8, 2, 10 head
14
9 1 7 3 5 4 6 8 2 10 Example 4, 8, 2, 10 head
15
9 1 7 3 5 4 6 8 2 10 Example 4, 8, 2, 10 head
16
9 1 7 3 5 4 6 8 2 10 Example 4, 8, 2, 10 head
17
9 1 7 3 5 4 6 8 2 10 Example 1, 4, 8, 2, 10 head
18
9 1 7 3 5 4 6 8 2 10 Example 1, 4, 8, 2, 10 head
19
9 1 7 3 5 4 6 8 2 10 Example 3, 1, 4, 8, 2, 10 head
20
9 1 7 3 5 4 6 8 2 10 Example 3, 1, 4, 8, 2, 10 head
21
9 1 7 3 5 4 6 8 2 10 Example 6, 3, 1, 4, 8, 2, 10 head
22
9 1 7 3 5 4 6 8 2 10 Example 7, 6, 3, 1, 4, 8, 2, 10 head
23
9 1 7 3 5 4 6 8 2 10 Example 7, 6, 3, 1, 4, 8, 2, 10 head
24
9 1 7 3 5 4 6 8 2 10 Example 9, 7, 6, 3, 1, 4, 8, 2, 10 head
25
9 1 7 3 5 4 6 8 2 10 Example 9, 7, 6, 3, 1, 4, 8, 2, 10 head
26
9 1 7 3 5 4 6 8 2 10 Example 5, 9, 7, 6, 3, 1, 4, 8, 2, 10 head
27
9 1 7 3 5 4 6 8 2 10 Topological Sort: Summary 5, 9, 7, 6, 3, 1, 4, 8, 2, 10 head The final order or jobs Time complexity = DFS complexity O(V + E) There can be many orders that meet the requirements
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.