Presentation is loading. Please wait.

Presentation is loading. Please wait.

"Learning how to learn is life's most important skill. " - Tony Buzan

Similar presentations


Presentation on theme: ""Learning how to learn is life's most important skill. " - Tony Buzan"— Presentation transcript:

1 "Learning how to learn is life's most important skill. " - Tony Buzan
Topological Sort "Learning how to learn is life's most important skill. " - Tony Buzan

2 Class Prerequisites Consider part-time student planning schedule
Must take the following 10 required courses with the given prerequisites: Can take classes in any order, if prerequisite met. Can only take one class a semester. What order should the classes be taken? 142 143 321 322 326 341 370 378 401 421 none CS Data Structures

3 Graph Modelling Model problem as a directed, acyclic graph (DAG):
nodes represent courses. contains an edge (u, v) if course u is a prerequisite of course v. Use topological sort to determine order to take classes. CS Data Structures

4 Topological Sort Topological sorting problem:
Given directed acyclic graph (DAG) G = (V, E) , find a linear ordering of vertices such that for any edge (u, v) in E, u precedes v in the ordering may be more than one ordering Topological Sort CS Data Structures

5 Simple Topological Sort Algorithm
Identify vertices with no incoming edges. The in-degree of these vertices is zero. If no vertices with no incoming edges, graph is not acyclic. doesn’t have a topological ordering. Remove one of these vertices and its edges from the graph. Return this vertex. Repeat until graph is empty. Order vertices are returned is topological ordering. CS Data Structures

6 Example: Simple Topological Sort
322 143 321 326 142 341 370 401 378 421 Identify vertices with no incoming edges. Only vertex with no incoming edges CS Data Structures

7 Example: Simple Topological Sort
322 143 321 326 341 370 401 378 421 Remove vertex and its edges. Output: 142 CS Data Structures

8 Example: Simple Topological Sort
322 143 321 326 341 370 401 378 421 Identify vertices with no incoming edges. Only vertex with no incoming edges Output: 142 CS Data Structures

9 Example: Simple Topological Sort
322 321 326 341 370 401 378 421 Remove vertex and its edges. Output: CS Data Structures

10 Example: Simple Topological Sort
322 321 326 341 370 401 378 421 Identify vertices with no incoming edges. Four vertices with no incoming edges. Select one. Output: CS Data Structures

11 Example: Simple Topological Sort
322 326 341 370 401 378 421 Remove vertex and its edges. Output: CS Data Structures

12 Example: Simple Topological Sort
322 326 341 370 401 378 421 Identify vertices with no incoming edges. Five vertices with no incoming edges. Select one. Output: CS Data Structures

13 Example: Simple Topological Sort
322 326 370 401 378 421 Remove vertex and its edges. Output: CS Data Structures

14 Example: Simple Topological Sort
322 326 370 401 378 421 Identify vertices with no incoming edges. Four vertices with no incoming edges. Select one. Output: CS Data Structures

15 Example: Simple Topological Sort
322 326 401 378 421 Remove vertex and its edges. Output: CS Data Structures

16 Example: Simple Topological Sort
322 326 401 378 421 Identify vertices with no incoming edges. Three vertices with no incoming edges. Select one. Output: CS Data Structures

17 Example: Simple Topological Sort
322 326 401 421 Remove vertex and its edges. Output: CS Data Structures

18 Example: Simple Topological Sort
322 326 401 421 Identify vertices with no incoming edges. Two vertices with no incoming edges. Select one. Output: CS Data Structures

19 Example: Simple Topological Sort
Remove vertex and its edges. 326 401 421 Output: CS Data Structures

20 Example: Simple Topological Sort
Identify vertices with no incoming edges. 326 401 421 Only vertex with no incoming edge. Output: CS Data Structures

21 Example: Simple Topological Sort
Remove vertex and its edges. 401 421 Output: CS Data Structures

22 Example: Simple Topological Sort
Identify vertices with no incoming edges. 401 421 Two vertices with no incoming edges. Select one. Output: CS Data Structures

23 Example: Simple Topological Sort
Remove vertex and its edges. 401 Output: CS Data Structures

24 Example: Simple Topological Sort
Identify vertices with no incoming edges. Only vertex with no incoming edges. 401 Output: CS Data Structures

25 Example: Simple Topological Sort
Remove vertex and its edges. Output: CS Data Structures

26 Example: Simple Topological Sort
Therefore, the student can take the classes in this order: There are many other possible orderings, depending how choose which vertex to remove if there’s more than one with no incoming edges. CS Data Structures

27 Analysis of Simple Topological Sort
Runtime of each step: Initialize in-degree array: Find vertex with in-degree zero: 𝑉 vertices, takes 𝑂( 𝑉 ) to search in-degree array. Reduce degree of vertices adjacent to removed vertex: Output and mark removed vertex: Total time: CS Data Structures

28 Analysis of Simple Topological Sort
Runtime of each step: Initialize in-degree array: Find vertex with in-degree zero: 𝑉 vertices, takes 𝑂( 𝑉 ) to search in-degree array. Reduce degree of vertices adjacent to removed vertex: Output and mark removed vertex: Total time: 𝑂( 𝐸 ) CS Data Structures

29 Analysis of Simple Topological Sort
Runtime of each step: Initialize in-degree array: Find vertex with in-degree zero: 𝑉 vertices, takes 𝑂( 𝑉 ) to search in-degree array. Reduce degree of vertices adjacent to removed vertex: Output and mark removed vertex: Total time: 𝑂( 𝐸 ) 𝑂( 𝑉 2 ) CS Data Structures

30 Analysis of Simple Topological Sort
Runtime of each step: Initialize in-degree array: Find vertex with in-degree zero: 𝑉 vertices, takes 𝑂( 𝑉 ) to search in-degree array. Reduce degree of vertices adjacent to removed vertex: Output and mark removed vertex: Total time: 𝑂( 𝐸 ) 𝑂( 𝑉 2 ) CS Data Structures

31 Analysis of Simple Topological Sort
Runtime of each step: Initialize in-degree array: Find vertex with in-degree zero: 𝑉 vertices, takes 𝑂( 𝑉 ) to search in-degree array. Reduce degree of vertices adjacent to removed vertex: Output and mark removed vertex: Total time: 𝑂( 𝐸 ) 𝑂( 𝑉 2 ) 𝑂( 𝑉 ) CS Data Structures

32 Analysis of Simple Topological Sort
Runtime of each step: Initialize in-degree array: Find vertex with in-degree zero: 𝑉 vertices, takes 𝑂( 𝑉 ) to search in-degree array. Reduce degree of vertices adjacent to removed vertex: Output and mark removed vertex: Total time: 𝑂( 𝐸 ) 𝑂( 𝑉 2 ) 𝑂( 𝑉 ) 𝑂( 𝑉 2 + 𝐸 ) CS Data Structures

33 Topological Sort with DFS
The topological sort of a DAG G can by computed by leveraging the DFS algorithm CS Data Structures

34 Example: TS with DFS Modelling the order a person can dress:
CS Data Structures

35 Example: TS with DFS The v.d and v.f times for the items after complete DFS. CS Data Structures

36 Example: TS with DFS Add items to list as finish each one.
Topological Sort CS Data Structures

37 Analysis of Topological Sort with DFS
Runtime of each step: DFS(G): Add vertex to front of list: Return list: Total time: CS Data Structures

38 Analysis of Topological Sort with DFS
Runtime of each step: DFS(G): Add vertex to front of list: Return list: Total time: 𝑂( 𝑉 + 𝐸 ) CS Data Structures

39 Analysis of Topological Sort with DFS
Runtime of each step: DFS(G): Add vertex to front of list: Return list: Total time: 𝑂( 𝑉 + 𝐸 ) 𝑂(1) CS Data Structures

40 Analysis of Topological Sort with DFS
Runtime of each step: DFS(G): Add vertex to front of list: Return list: Total time: 𝑂( 𝑉 + 𝐸 ) 𝑂(1) CS Data Structures

41 Analysis of Topological Sort with DFS
Runtime of each step: DFS(G): Add vertex to front of list: Return list: Total time: 𝑂( 𝑉 + 𝐸 ) 𝑂(1) CS Data Structures

42 CS Data Structures


Download ppt ""Learning how to learn is life's most important skill. " - Tony Buzan"

Similar presentations


Ads by Google