topological sort
play

Topological Sort Shivam Patel Viktor Zenkov Questions 1. Who - PowerPoint PPT Presentation

Topological Sort Shivam Patel Viktor Zenkov Questions 1. Who first described topological sort? 2. What kind of graph is required to perform a topological sort? 3. What is the running time of topological sort? Outline About Us


  1. Topological Sort Shivam Patel Viktor Zenkov

  2. Questions 1. Who first described topological sort? 2. What kind of graph is required to perform a topological sort? 3. What is the running time of topological sort?

  3. Outline • About Us • History • Algorithm • Example • Applications

  4. Shivam BS in Computer Science May 2020 MS in CS May 2021 Casual gamer Pro memer No pet ¯\_( ツ )_/¯

  5. Food I like spicy food. My favorite food is Mexican food. Chaiyo’s vs Jai Dee? I like Chaiyo’s better.

  6. Viktor BS in Computer Science May 2019 MS in CS August 2020 PhD in CS in the future Makes food that tastes better than it looks Owns lizard, owned by cat

  7. Friends

  8. History • First described by Arthur B. Kahn in 1962. • It was studied in the early 60’s for use in PERT (Program evaluation and review technique). • Topological sort would be used for finding the critical path of a project. • The critical path would be used to control the length of the project.

  9. Introduction • A graph is defined by a set of vertices and a set of edges. • A directed graph has a direction associated with each edge. • There is possible interest in sorting either set. • We consider a method for sorting the vertex set in a directed graph: topological sort. Sort all vertices such that for every edge in the graph, the first point in the edge is before the second point in the edge.

  10. Comments • The ordering may not be unique. • This sort can only be done for directed acyclic graphs (DAG). • What is a DAG? A DAG is a directed graph with no cycles, i.e. you can’t get back to a vertex from itself. • Any path traversing part of the graph will only move forward through the graph.

  11. Algorithm We have a graph G(V, E). 1. Place all vertices with no incoming edges in a set S and all edges in a set F. 2. While S is not empty, [O(|V|) runtime] a. Remove a node u from S and add u to a list T. b. For each vertex v such that ( u,v ) is an edge in F, remove that edge from F. [O(|E|) runtime] c. If v has no remaining incoming edges, append v to S. 3. Return T. O(|V| + |E|) runtime

  12. Example 1 2 4 5 6 8 9 7 10 3 S:

  13. Example - Add vertices 1 2 4 5 6 8 9 7 10 3 S: 1 7 9

  14. Example - Remove 1 & edges 1 2 4 5 6 8 9 7 10 3 S: T: 1 7 9 2 3 1

  15. Example - Remove 7 & edges 1 2 4 5 6 8 9 7 10 3 S: T: 7 9 2 3 1 7

  16. Example - Remove 9 & edges 1 2 4 5 6 8 9 7 10 3 S: T: 9 2 3 1 7 9

  17. Example - Remove 2 & edges 1 2 4 5 6 8 9 7 10 3 S: T: 2 3 1 7 9 2

  18. Example - Remove 3 & edges 1 2 4 5 6 8 9 7 10 3 S: T: 3 4 5 1 7 9 2 3

  19. Example - Remove 4 & edges 1 2 4 5 6 8 9 7 10 3 S: T: 4 5 6 1 7 9 2 3 4

  20. Example - Remove 5 & edges 1 2 4 5 6 8 9 7 10 3 S: T: 5 6 1 7 9 2 3 4 5

  21. Example - Remove 6 & edges 1 2 4 5 6 8 9 7 10 3 S: T: 6 8 1 7 9 2 3 4 5 6

  22. Example - Remove 8 & edges 1 2 4 5 6 8 9 7 10 3 S: T: 8 10 1 7 9 2 3 4 5 6 8

  23. Example - Remove 10 & edges 1 2 4 5 6 8 9 7 10 3 S: T: 10 1 7 9 2 3 4 5 6 8 10

  24. Example - Final List T: 1 7 9 2 3 4 5 6 8 10 1 7 9 2 3 4 5 6 8 10

  25. Application - Shortest Path • Set the distance to each vertex to infinity. • Set the source’s distance to 0. • Perform a topological sort. • When an edge ( u,v ) is removed, the distance to v is updated if it is improved by going through u . • This works because any vertex is processed after vertices before it in the graph.

  26. Application - Distinct Paths • Set the number of paths to each vertex to 0. • Set the number of paths to the source to 1. • Perform a topological sort. • When edge ( u,v ) is removed, add the number of paths to u to the number of paths to v . • This works because there can only be one path from u to v .

  27. Application - Classes

  28. Miscellaneous Applications • Family tree • Figures/equations in papers • Photoshop “layers” • Scheduling • Compiling

  29. Application - Shared Universe

  30. Conclusion • Topological sort is a sorting method for graph vertices. • It can be performed in O(|V| + |E|) time. • Now we can binge watch all 23 movies in a reasonable order while we’re in quarantine.

  31. References • https://en.wikipedia.org/wiki/Topological_sorting • https://dl.acm.org/doi/10.1145/368996.369025 • The Art of Computer Programming - Fundamental Algorithms (Donald Knuth) • http://web.eecs.utk.edu/~jplank/plank/classes/cs302/Not es/Topological/

  32. Questions 1. Who first described topological sort? 2. What kind of graph is required to perform a topological sort? 3. What is the running time of topological sort?

  33. Discussion Thank you for your attention! Stay safe.

Recommend


More recommend