Presentation on theme: "Data Structures Using C++ 2E1 Topic of this lecture: Topological Order OU CS prerequisite bubble chart"— Presentation transcript:
Data Structures Using C++ 2E1 Topic of this lecture: Topological Order OU CS prerequisite bubble chart upload/CS-BUBBLE-CHART-October-2012.pdf
Data Structures Using C++ 2E
Data Structures Using C++ 2E5 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++ 2E6 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 2400 2401 3610 4040 … EE1024… Breadth-first: CS 2400 EE1024 Chem 1510 Math 2301 CS 2401 …
Data Structures Using C++ 2E7 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).
8 How to initialize preCount?
9 How to do breadth-first traversal?
Data Structures Using C++ 2E10 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 Breadth First Topological Ordering
Data Structures Using C++ 2E12 Breadth First Topological Ordering (cont’d.) Breadth First Topological order – FIGURE Arrays predCount, topologicalOrder, and queue after Steps 1 and 2 execute
Data Structures Using C++ 2E13 Breadth First Topological Ordering (cont’d.) Breadth First Topological order – FIGURE Arrays predCount, topologicalOrder, and queue after the first iteration of Step 3
Data Structures Using C++ 2E14 FIGURE Arrays predCount, topologicalOrder, and queue after the second iteration of Step 3
Data Structures Using C++ 2E15 FIGURE Arrays predCount, topologicalOrder, and queue after the third iteration of Step 3
Data Structures Using C++ 2E16 Breadth First Topological Ordering (cont’d.) See code on pages –Function implementing breadth first topological ordering algorithm FIGURE Arrays predCount, topologicalOrder, and queue after Step 3 executes
Data Structures Using C++ 2E17 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++ 2E18 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.
Data Structures Using C++ 2E19 Depth First Topological Ordering If start at 0, dft to 10, then 10 will be put as the last element in toplogicalOrder; 8 will be the second last. Output: