Topological Sort Example This 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 must follow 2. Make a directed graph and then a list of ordered pairs that represent these relationships.
Tasks listed as ordered pairs: 7,1 7,3 7,6 5,3 5,6 6,8 4,8 4,2 2,10 Tasks shown as a directed graph.
Predecessor CountsSuccessors
Place all tasks that have zero predecessors in a “bag”. BAGBAG Step 1. Remove any task from the bag and place in sorted list. BAGBAG Step 2. Update Predecessor and Successor arrays as needed. Sorted List: 7,
Predecessor CountsSuccessors 136
Step 3. Add to bag any tasks that now have zero predecessors. BAGBAG Step. 4. Repeat steps 1, 2, and 3 until all predecessor counts are -1. (We are performing a “loop” or repetition.)