data structures using c++ 2e1 topic of this lecture: topological order ou cs prerequisite bubble...
TRANSCRIPT
Data Structures Using C++ 2E 1
Topic of this lecture: Topological Order
• OU CS prerequisite bubble chart
http://www.ohio.edu/engineering/eecs/undergradtools/upload/CS-BUBBLE-CHART-October-2012.pdf
Data Structures Using C++ 2E 5
Topological Order
• Algorithm topological order– Outputs directed graph vertices in topological order– Assume graph has no cycles
• There exists a vertex v in G such that v has no successor
• There exists a vertex u in G such that u has no predecessor
Data Structures Using C++ 2E 6
Topological Order (cont’d.)
• Topological sort algorithm– Implemented with the depth first traversal or the
breadth first traversal
• Depth-first (stack) vs. Breadth-first (queue) • Depth-first: CS 240024013610 4040 …
EE1024…
• Breadth-first: CS 2400EE1024 Chem 1510 Math 2301 CS 2401 …
Data Structures Using C++ 2E 7
Breadth First Topological Ordering
• General algorithm: 1. Use an array predCount to record the number of predecessors
(prerequisites) for each node (course)
2. For a node (course) without any predecessor (prerequisite), it can be output (taken)
3. Use a queue for breadth-first traversal (maintain the prerequisite-cleared courses).
Data Structures Using C++ 2E 10
How to do Breadth First Topological Ordering?
• Similar to breadth-first traversal: using a queue, but only put prerequisite-cleared items (predCount == 0) into the queue.
• When an item is removed from the queue, update the predCount values for the successors
• predCount[CS3610] = 2• After taking CS 2400, it should be reduced by 1.• After taking CS 3000, it will be become 0.
Data Structures Using C++ 2E 12
Breadth First Topological Ordering (cont’d.)
• Breadth First Topological order– 0 9 1 7 2 5 4 6 3 8 10
FIGURE 12-18 Arrays predCount, topologicalOrder,and queue after Steps 1 and 2 execute
Data Structures Using C++ 2E 13
Breadth First Topological Ordering (cont’d.)
• Breadth First Topological order– 0 9 1 7 2 5 4 6 3 8 10
FIGURE 12-19 Arrays predCount, topologicalOrder,and queue after the first iteration of Step 3
Data Structures Using C++ 2E 14
FIGURE 12-20 Arrays predCount, topologicalOrder, and queue after the second iteration of Step 3
Data Structures Using C++ 2E 15
FIGURE 12-21 Arrays predCount, topologicalOrder, and queue after the third iteration of Step 3
Data Structures Using C++ 2E 16
Breadth First Topological Ordering (cont’d.)
• See code on pages 718-719– Function implementing breadth first topological
ordering algorithm
FIGURE 12-22 Arrays predCount, topologicalOrder,and queue after Step 3 executes
Data Structures Using C++ 2E 17
How to do Depth First Topological Ordering?
• Project 5 question. Use depth-first traversal with recursive calls.
• Find a course without successor (e.g, CS 4561) through depth-first traversal and assign it into topologicalOrder[n-1]. When you trace back, assign topologicalOrder[n-2], [n-3]… ..
Data Structures Using C++ 2E 18
How to do Depth First Topological Ordering?
• A course should NOT be output (taken) until the subtrees have been visited.
• Decrement topIndex each time when you output a course.
• Assign topologicalOrder[topIndex--] at the end of recursive calls. Pre-requisites are guaranteed to be taken first.