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

topological sort
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Topological Sort

Shivam Patel Viktor Zenkov

slide-2
SLIDE 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?
slide-3
SLIDE 3

Outline

  • About Us
  • History
  • Algorithm
  • Example
  • Applications
slide-4
SLIDE 4

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

Shivam

slide-5
SLIDE 5

Food

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

slide-6
SLIDE 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

slide-7
SLIDE 7

Friends

slide-8
SLIDE 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.

slide-9
SLIDE 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.

slide-10
SLIDE 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.

slide-11
SLIDE 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

slide-12
SLIDE 12

Example

1 2 3 4 5 6 7 8 9 10

S:

slide-13
SLIDE 13

Example - Add vertices

1 2 3 4 5 6 7 8 9 10

S:

1 7 9

slide-14
SLIDE 14

Example - Remove 1 & edges

1 2 3 4 5 6 7 8 9 10

S:

1 7 9 2 3

T:

1

slide-15
SLIDE 15

Example - Remove 7 & edges

1 2 3 4 5 6 7 8 9 10

S:

7 9 2 3

T:

1 7

slide-16
SLIDE 16

Example - Remove 9 & edges

1 2 3 4 5 6 7 8 9 10

S:

9 2 3

T:

1 7 9

slide-17
SLIDE 17

Example - Remove 2 & edges

1 2 3 4 5 6 7 8 9 10

S:

2 3

T:

1 7 9 2

slide-18
SLIDE 18

Example - Remove 3 & edges

1 2 3 4 5 6 7 8 9 10

S:

3 4

T:

1 7 9 2 5 3

slide-19
SLIDE 19

Example - Remove 4 & edges

1 2 3 4 5 6 7 8 9 10

S:

4 5

T:

1 7 9 2 6 3 4

slide-20
SLIDE 20

Example - Remove 5 & edges

1 2 3 4 5 6 7 8 9 10

S:

5 6

T:

1 7 9 2 3 4 5

slide-21
SLIDE 21

Example - Remove 6 & edges

1 2 3 4 5 6 7 8 9 10

S:

6 8

T:

1 7 9 2 3 4 5 6

slide-22
SLIDE 22

Example - Remove 8 & edges

1 2 3 4 5 6 7 8 9 10

S:

8 10

T:

1 7 9 2 3 4 5 6 8

slide-23
SLIDE 23

Example - Remove 10 & edges

1 2 3 4 5 6 7 8 9 10

S:

10

T:

1 7 9 2 3 4 5 6 8 10

slide-24
SLIDE 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

slide-25
SLIDE 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.

slide-26
SLIDE 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.

slide-27
SLIDE 27

Application - Classes

slide-28
SLIDE 28

Miscellaneous Applications

  • Family tree
  • Figures/equations in papers
  • Photoshop “layers”
  • Scheduling
  • Compiling
slide-29
SLIDE 29

Application - Shared Universe

slide-30
SLIDE 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
  • rder while we’re in quarantine.
slide-31
SLIDE 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/

slide-32
SLIDE 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?
slide-33
SLIDE 33

Discussion

Thank you for your attention! Stay safe.