128
This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, Marty Stepp, Ashley Taylor and others. CS 106X, Lecture 25 Topological Sort reading: Programming Abstractions in C++, Chapter 18

CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

  • Upload
    others

  • View
    10

  • Download
    1

Embed Size (px)

Citation preview

Page 1: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2.5 License. All rights reserved.Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, Marty Stepp, Ashley Taylor and others.

CS 106X, Lecture 25Topological Sort

reading:Programming Abstractions in C++, Chapter 18

Page 2: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

2

Plan For This Week• Graphs: Topological Sort (HW8)• Classes: Inheritance and Polymorphism (HW8)• Sorting Algorithms

Page 3: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

3

Plan For Today• Topological Sort

– Kahn’s Algorithm– Recursive DFS– Spreadsheets

• Announcements

• Learning Goal: understand how to implement topological sort, and why it and dependency graphs are useful.

Page 4: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

4

Plan for Today• Topological Sort

– Kahn’s Algorithm– Recursive DFS– Spreadsheets

• Announcements

Page 5: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

5

Graphs So Far• Graphs as Data Structures

– Adjacency List– Adjacency Matrix– Edge List

• Searching– DFS– BFS– Dijkstra’s Algorithm– A* Search

Page 6: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

6

Dependency Graphs

Page 7: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

7

Dependency Graphs: TasksToast Bread

Butter Toast Sauté Veggies

Chop Veggies

Add Eggs & Cook

Prepare Egg Mixture

Plate Omelette and Toast

How can I parallelize these tasks to finish more quickly?

Page 8: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

8

Dependency Graphs: TasksToast Bread

Butter Toast Sauté Veggies

Chop Veggies

Add Eggs & Cook

Prepare Egg Mixture

Plate Omelette and Toast

What step is best to optimize in this production process?

Page 9: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

9

Dependency Graphs: Orderings

CS 106A

CS 106B/X CS 103

CS 109

CS 161

CS 107

CS 110

CS 221

What are valid orderings of these tasks?

Page 10: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

10

Dependency Graphs: Games

What is an effective AI strategy for resource management games (like Civilization)?

Page 11: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

11

Dependency Graphs: Compilers

LoginUI

Toolbar

MainUI

AppDB

Bundle

DataTypes

In what order should we recompile source files with dependencies?

Page 12: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

12

Dependency Graphs: Web

LoginUI

Toolbar

MainUI

AppDB

Bundle

DataTypes

How can I efficiently package up and transmit web dependencies?

Page 13: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

13

Topological SortToast Bread

Butter Toast Sauté Veggies

Chop Veggies

Add Eggs & Cook

Prepare Eggs

Plate FoodWhich tasks could we complete first?

Page 14: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

14

Topological SortToast Bread

Butter Toast Sauté Veggies

Chop Veggies

Add Eggs & Cook

Prepare Eggs

Plate FoodWhich tasks could we complete first?

Page 15: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

15

Topological SortToast Bread

Butter Toast Sauté Veggies

Chop Veggies

Add Eggs & Cook

Prepare Eggs

Plate Food

Toast Bread

Page 16: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

16

Topological SortToast Bread

Butter Toast Sauté Veggies

Chop Veggies

Add Eggs & Cook

Prepare Eggs

Plate Food

Toast Bread

Page 17: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

17

Topological SortToast Bread

Butter Toast Sauté Veggies

Chop Veggies

Add Eggs & Cook

Prepare Eggs

Plate Food

Toast Bread

Chop Veggies

Page 18: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

18

Topological SortToast Bread

Butter Toast Sauté Veggies

Chop Veggies

Add Eggs & Cook

Prepare Eggs

Plate Food

Toast Bread

Chop Veggies

Page 19: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

19

Topological Sort

Butter Toast Sauté Veggies

Add Eggs & Cook

Prepare Eggs

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Toast Bread Chop Veggies

Page 20: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

20

Topological Sort

Butter Toast Sauté Veggies

Add Eggs & Cook

Prepare Eggs

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Toast Bread Chop Veggies

Page 21: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

21

Topological Sort

Sauté Veggies

Add Eggs & Cook

Prepare Eggs

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Prepare Eggs

Butter Toast

Toast Bread Chop Veggies

Page 22: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

22

Topological Sort

Sauté Veggies

Add Eggs & Cook

Prepare Eggs

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Prepare Eggs

Butter Toast

Toast Bread Chop Veggies

Page 23: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

23

Topological Sort

Sauté Veggies

Add Eggs & Cook

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Prepare Eggs

Sauté Veggies

Prepare Eggs

Butter Toast

Toast Bread Chop Veggies

Page 24: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

24

Topological Sort

Sauté Veggies

Add Eggs & Cook

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Prepare Eggs

Sauté Veggies

Prepare Eggs

Butter Toast

Toast Bread Chop Veggies

Page 25: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

25

Topological Sort

Add Eggs & Cook

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Prepare Eggs

Sauté Veggies

Add Eggs & Cook

Sauté Veggies

Prepare Eggs

Butter Toast

Toast Bread Chop Veggies

Page 26: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

26

Topological Sort

Add Eggs & Cook

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Prepare Eggs

Sauté Veggies

Add Eggs & Cook

Sauté Veggies

Prepare Eggs

Butter Toast

Toast Bread Chop Veggies

Page 27: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

27

Topological Sort

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Prepare Eggs

Sauté Veggies

Add Eggs & Cook

Plate Food

Add Eggs & Cook

Sauté Veggies

Prepare Eggs

Butter Toast

Toast Bread Chop Veggies

Page 28: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

28

Topological Sort

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Prepare Eggs

Sauté Veggies

Add Eggs & Cook

Plate Food

Add Eggs & Cook

Sauté Veggies

Prepare Eggs

Butter Toast

Toast Bread Chop Veggies

Page 29: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

29

Topological Sort

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Prepare Eggs

Sauté Veggies

Add Eggs & Cook

Plate Food

Add Eggs & Cook

Sauté Veggies

Prepare Eggs

Butter Toast

Toast Bread Chop Veggies

Page 30: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

30

Topological SortGiven a directed (acyclic!) graph G = (V, E), a topological sort is a total ordering of G's vertices such that for every edge (v, w) in E, vertex vprecedes w in the ordering.

CS 106A

CS 106B/X CS 103

CS 109

CS 161

CS 107

CS 110

CS 221

Page 31: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

31

Topological Sort

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Prepare Eggs

Sauté Veggies

Add Eggs & Cook

Plate Food

Add Eggs & Cook

Sauté Veggies

Prepare Eggs

Butter Toast

Toast Bread Chop Veggies

Page 32: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

32

Topological SortToast Bread

Butter Toast Sauté Veggies

Chop Veggies

Add Eggs & Cook

Prepare Eggs

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Prepare Eggs

Sauté Veggies

Add Eggs & Cook

Plate Food

Given a directed graph G = (V, E), a topological sort is a total ordering of G's vertices such that for every edge (v, w) in E, vertex v precedes w in the ordering.

Page 33: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

33

Topological SortToast Bread

Butter Toast Sauté Veggies

Chop Veggies

Add Eggs & Cook

Prepare Eggs

Plate Food

Toast Bread

Chop Veggies

Butter Toast

Prepare Eggs

Sauté Veggies

Add Eggs & Cook

Plate Food

Given a directed graph G = (V, E), a topological sort is a total ordering of G's vertices such that for every edge (v, w) in E, vertex v precedes w in the ordering.

Page 34: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

34

Topological SortToast Bread

Butter Toast Sauté Veggies

Chop Veggies

Add Eggs & Cook

Prepare Eggs

Plate Food

Make Lunch

Toast Bread

Chop Veggies

Butter Toast

Prepare Eggs

Sauté Veggies

Add Eggs & Cook

Plate Food

Page 35: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

35

Topological Sort: Idea1. Find node with in-degree of 02. Add it at the end of our topological ordering so far, and remove it

and its edges from the graph3. Repeat until no nodes left

Page 36: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

36

Topological Sort Algorithmordering := { }. // (empty list)Repeat until graph is empty:

Find a vertex v with in-degree of 0Delete v and its outgoing edges from graphordering += v

• This algorithm modifies the passed-in graph L• Have we handled all edge cases?• What is the runtime of this algorithm?

Page 37: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

37

Topological Sort Algorithmordering := { }. // (empty list)Repeat until graph is empty:

Find a vertex v with in-degree of 0Delete v and its outgoing edges from graphordering += v

• This algorithm modifies the passed-in graph L• Have we handled all edge cases?• What is the runtime of this algorithm?

Page 38: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

38

Topological Sort Algorithm: Cycles

ordering := { }.Repeat until graph is empty:

Find a vertex v with in-degree of 0Delete v and its outgoing edges from graphordering += v

Task 1 Task 2

Task 3

Page 39: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

39

Topological Sort Algorithm: Cycles

ordering := { }.Repeat until graph is empty:

Find a vertex v with in-degree of 0Delete v and its outgoing edges from graphordering += v

Task 1 Task 2

Task 3

Ordering: { }

Page 40: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

40

Topological Sort Algorithm: Cycles

ordering := { }.Repeat until graph is empty:

Find a vertex v with in-degree of 0Delete v and its outgoing edges from graphordering += v

Task 1 Task 2

Task 3

Ordering: { }

Page 41: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

41

Topological Sort Algorithm: Cycles

ordering := { }.Repeat until graph is empty:

Find a vertex v with in-degree of 0Delete v and its outgoing edges from graphordering += v

Task 1 Task 2

Task 3

Ordering: { }

Page 42: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

42

Topological Sort Algorithm: Cycles

ordering := { }.Repeat until graph is empty:

Find a vertex v with in-degree of 0Delete v and its outgoing edges from graphordering += v

Task 1 Task 2

Task 3

Ordering: { }

Page 43: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

43

Topological Sort Algorithm: Cycles

ordering := { }.Repeat until graph is empty:

Find a vertex v with in-degree of 0Delete v and its outgoing edges from graphordering += v

Task 1 Task 2

Task 3

Ordering: { “Task 2” }

Page 44: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

44

Topological Sort Algorithm: Cycles

ordering := { }.Repeat until graph is empty:

Find a vertex v with in-degree of 0Delete v and its outgoing edges from graphordering += v

Task 1 Task 2

Task 3

Ordering: { “Task 2” }

Page 45: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

45

Topological Sort Algorithm: Cycles

ordering := { }.Repeat until graph is empty:

Find a vertex v with in-degree of 0Delete v and its outgoing edges from graphordering += v

Task 1 Task 2

Task 3

Ordering: { “Task 2” }

Page 46: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

46

Topological Sort Algorithm: Cycles

ordering := { }.Repeat until graph is empty:

Find a vertex v with in-degree of 0- if none, no valid ordering possible

Delete v and its outgoing edges from graphordering += v

Task 1 Task 2

Task 3

Ordering: { “Task 2” }

Page 47: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

47

Topological Sort Algorithm: Cycles

ordering := { }.Repeat until graph is empty:

Find a vertex v with in-degree of 0- if none, no valid ordering possible

Delete v and its outgoing edges from graphordering += v

• This algorithm modifies the passed-in graph L• Have we handled all edge cases?• What is the runtime of this algorithm?

Page 48: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

48

Topological Sort Algorithm: Runtime

For graph with V vertexes and E edges:ordering := { }.Repeat until graph is empty:

Find a vertex v with in-degree of 0- if none, no valid ordering possible

Delete v and its outgoing edges from graphordering += v

Page 49: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

49

Topological Sort Algorithm: Runtime

For graph with V vertexes and E edges:

ordering := { }.

Repeat until graph is empty:

Find a vertex v with in-degree of 0- if none, no valid ordering possible

Delete v and its outgoing edges from graph

ordering += v

O(1)

O(V)

O(V)

O(E)

O(1) O(V(V+E))

Page 50: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

50

Topological Sort Algorithm: Runtime

For graph with V vertexes and E edges:

ordering := { }.

Repeat until graph is empty:

Find a vertex v with in-degree of 0- if none, no valid ordering possible

Delete v and its outgoing edges from graph

ordering += v

O(V)

O(E)

O(1) O(V(V+E))Is the worst case here really O(E) every time? For example, if one node has all the outgoing edges, then we’ll have an O(E) operation that time, but a no-op every other time.

O(1)

O(V)

Page 51: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

51

Topological Sort Algorithm: Runtime

For graph with V vertexes and E edges:

ordering := { }.

Repeat until graph is empty:

Find a vertex v with in-degree of 0- if none, no valid ordering possible

Delete v and its outgoing edges from graph

ordering += v

O(V)

O(E)

O(1) O(V(V+E))Key Idea: every edge can be deleted at most once. Therefore, the total runtime of this operation is O(E), not O(V*E).

O(1)

O(V)

Page 52: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

52

Topological Sort Algorithm: Runtime

For graph with V vertexes and E edges:

ordering := { }.

Repeat until graph is empty:

Find a vertex v with in-degree of 0- if none, no valid ordering possible

Delete v and its outgoing edges from graph

ordering += v

O(V)

O(E) *total*

O(1) Tighter bound: O(V2 + E)

O(1)

O(V)

Page 53: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

53

Topological Sort Algorithm: Runtime

For graph with V vertexes and E edges:

ordering := { }.

Repeat until graph is empty:

Find a vertex v with in-degree of 0- if none, no valid ordering possible

Delete v and its outgoing edges from graph

ordering += v

O(V)

O(E) *total*

O(1)

O(1)

O(V)

Can we make this more efficient?

Page 54: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

54

Topological Sort: Take 2 !Toast Bread

Butter Toast Sauté Veggies

Chop Veggies

Add Eggs & Cook

Prepare Eggs

Plate Food

ordering := { }. Repeat until graph is empty:

Find a vertex v with in-degree of 0- if none, no valid ordering possible

Delete v and its outgoing edges from graphordering += v

Page 55: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

55

3 Key Ideas for Improvement• Keep a queue of nodes with in-degree 0 so we don’t have to search

for nodes multiple times.• A node’s in-degree only changes when one of its prerequisites is

completed. Therefore, when completing a task, check if any of its neighbors now has in-degree 0.

• Keep a map of nodes’ in-degrees so we don’t need to modify the graph.

Page 56: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

56

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { }In-Degree Map: { }

C

Page 57: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

57

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “A”, “D”, “E”, “G” }In-Degree Map: { “A”:0, “B”:1, “C”:1, “D”:0, “E”: 0, “F”: 2, “G”:0, “H”:3 }

C

Page 58: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

58

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “A”, “D”, “E”, “G” }In-Degree Map: { “A”:0, “B”:1, “C”:1, “D”:0, “E”: 0, “F”: 2, “G”:0, “H”:3 }

C

A

Page 59: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

59

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “D”, “E”, “G” }In-Degree Map: { “A”:0, “B”:1, “C”:1, “D”:0, “E”: 0, “F”: 2, “G”:0, “H”:3 }

C

A

Page 60: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

60

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “D”, “E”, “G”, “B” }In-Degree Map: { “A”:0, “B”:0, “C”:1, “D”:0, “E”: 0, “F”: 2, “G”:0, “H”:3 }

C

A

Page 61: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

61

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “D”, “E”, “G”, “B” }In-Degree Map: { “A”:0, “B”:0, “C”:1, “D”:0, “E”: 0, “F”: 2, “G”:0, “H”:3 }

C

A D

Page 62: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

62

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “E”, “G”, “B” }In-Degree Map: { “A”:0, “B”:0, “C”:1, “D”:0, “E”: 0, “F”: 2, “G”:0, “H”:3 }

C

A D

Page 63: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

63

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “E”, “G”, “B” }In-Degree Map: { “A”:0, “B”:0, “C”:1, “D”:0, “E”: 0, “F”: 1, “G”:0, “H”:3 }

C

A D

Page 64: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

64

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “E”, “G”, “B” }In-Degree Map: { “A”:0, “B”:0, “C”:1, “D”:0, “E”: 0, “F”: 1, “G”:0, “H”:3 }

C

A D E

Page 65: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

65

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “G”, “B” }In-Degree Map: { “A”:0, “B”:0, “C”:1, “D”:0, “E”: 0, “F”: 1, “G”:0, “H”:3 }

C

A D E

Page 66: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

66

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “G”, “B”, “F” }In-Degree Map: { “A”:0, “B”:0, “C”:1, “D”:0, “E”: 0, “F”: 0, “G”:0, “H”:2 }

C

A D E

Page 67: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

67

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “G”, “B”, “F” }In-Degree Map: { “A”:0, “B”:0, “C”:1, “D”:0, “E”: 0, “F”: 0, “G”:0, “H”:2 }

C

A D E G

Page 68: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

68

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “B”, “F” }In-Degree Map: { “A”:0, “B”:0, “C”:1, “D”:0, “E”: 0, “F”: 0, “G”:0, “H”:2 }

C

A D E G

Page 69: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

69

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “B”, “F” }In-Degree Map: { “A”:0, “B”:0, “C”:1, “D”:0, “E”: 0, “F”: 0, “G”:0, “H”:2 }

C

A D E G B

Page 70: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

70

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “F” }In-Degree Map: { “A”:0, “B”:0, “C”:1, “D”:0, “E”: 0, “F”: 0, “G”:0, “H”:2 }

C

A D E G B

Page 71: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

71

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “F”, “C” }In-Degree Map: { “A”:0, “B”:0, “C”:0, “D”:0, “E”: 0, “F”: 0, “G”:0, “H”:1 }

C

A D E G B

Page 72: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

72

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “F”, “C” }In-Degree Map: { “A”:0, “B”:0, “C”:0, “D”:0, “E”: 0, “F”: 0, “G”:0, “H”:1 }

C

A D E G B F

Page 73: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

73

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “C” }In-Degree Map: { “A”:0, “B”:0, “C”:0, “D”:0, “E”: 0, “F”: 0, “G”:0, “H”:1 }

C

A D E G B F

Page 74: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

74

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “C”, “H” }In-Degree Map: { “A”:0, “B”:0, “C”:0, “D”:0, “E”: 0, “F”: 0, “G”:0, “H”:0 }

C

A D E G B F

Page 75: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

75

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “C”, “H” }In-Degree Map: { “A”:0, “B”:0, “C”:0, “D”:0, “E”: 0, “F”: 0, “G”:0, “H”:0 }

C

A D E G B F C

Page 76: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

76

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “H” }In-Degree Map: { “A”:0, “B”:0, “C”:0, “D”:0, “E”: 0, “F”: 0, “G”:0, “H”:0 }

C

A D E G B F C

Page 77: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

77

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { “H” }In-Degree Map: { “A”:0, “B”:0, “C”:0, “D”:0, “E”: 0, “F”: 0, “G”:0, “H”:0 }

C

A D E G B F C H

Page 78: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

78

Topological Sort: Take 2 !

A

B F

D

G

E

H

0-In-Degree Queue: { }In-Degree Map: { “A”:0, “B”:0, “C”:0, “D”:0, “E”: 0, “F”: 0, “G”:0, “H”:0 }

C

A D E G B F C H

Page 79: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

79

Plan for Today• Topological Sort

– Kahn’s Algorithm– Recursive DFS– Spreadsheets

• Announcements

Page 80: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

80

Kahn’s Algorithmmap := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

• This algorithm doesn’t modify the passed-in graph! J• Have we handled all edge cases?• What is the runtime of this algorithm?

Page 81: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

81

Kahn’s Algorithmmap := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

• This algorithm doesn’t modify the passed-in graph! J• Have we handled all edge cases?• What is the runtime of this algorithm?

Page 82: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

82

Kahn’s Algorithm: Cyclesmap := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

A B

C

Page 83: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

83

Kahn’s Algorithm: Cyclesmap := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

A B

C

0-In-Degree Queue: { “B” }In-Degree Map: { A”:1, “B”:0, “C”: 2 }

Ordering: { }

Page 84: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

84

Kahn’s Algorithm: Cyclesmap := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

A B

C

0-In-Degree Queue: { “B” }In-Degree Map: { A”:1, “B”:0, “C”: 2 }

Ordering: { }

Page 85: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

85

Kahn’s Algorithm: Cyclesmap := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

A B

C

0-In-Degree Queue: { “B” }In-Degree Map: { A”:1, “B”:0, “C”: 2 }

Ordering: { “B” }

Page 86: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

86

Kahn’s Algorithm: Cyclesmap := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

A B

C

0-In-Degree Queue: { }In-Degree Map: { A”:1, “B”:0, “C”: 1 }

Ordering: { “B” }

Page 87: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

87

Kahn’s Algorithm: Cyclesmap := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

A B

C

0-In-Degree Queue: { }In-Degree Map: { A”:1, “B”:0, “C”: 1 }

Ordering: { “B” }

Page 88: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

88

Kahn’s Algorithm: Cyclesmap := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

A B

C

0-In-Degree Queue: { }In-Degree Map: { A”:1, “B”:0, “C”: 1 }

Ordering: { “B” }

Page 89: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

89

Kahn’s Algorithm: Cyclesmap := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

A B

C

0-In-Degree Queue: { }In-Degree Map: { A”:1, “B”:0, “C”: 1 }

Ordering: { “B” }

Page 90: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

90

Kahn’s Algorithm: Cyclesmap := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

If all vertices processed, success! Otherwise, there is a cycle.

• This algorithm doesn’t modify the passed-in graph! J• Have we handled all edge cases?• What is the runtime of this algorithm?

Page 91: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

91

Kahn’s Algorithm: Runtime

map := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

For graph with V vertexes and E edges

Page 92: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

92

Kahn’s Algorithm: Runtime

map := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.

Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.

Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

For graph with V vertexes and E edgesO(V)

O(V)

O(1)

O(E)

O(VE)

Page 93: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

93

Kahn’s Algorithm: Runtime

map := {each vertex ® its in-degree}.queue := {all vertices with in-degree = 0}.ordering := { }.

Repeat until queue is empty:

Dequeue the first vertex v from the queue.ordering += v.

Decrease the in-degree of all v's neighbors by 1 in the map.queue += {any neighbors whose in-degree is now 0}.

For graph with V vertexes and E edgesO(V)

O(V)

O(1)

O(E) *total*

O(V + E)

Page 94: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

94

Plan for Today• Topological Sort

– Kahn’s Algorithm– Recursive DFS– Spreadsheets

• Announcements

Page 95: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

95

Revisiting Recursive DFS

C

A

F

D

B E

G

H

Page 96: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

96

Revisiting Recursive DFS

C

A

F

D

B E

G

H

Page 97: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

97

Revisiting Recursive DFS

C

A

F

D

B E

G

H

A-D-F-G-E

• Key idea: visiting node ameans we must have visited all nodes that have a as a prerequisite.

Page 98: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

98

Revisiting Recursive DFS

C

A

F

D

B E

G

H

B-C-F-G-E-D

• Key idea: visiting node ameans we must have visited all nodes that have a as a prerequisite.

• But different starting points may yield different subsets. Is there a way we can combine them into one complete topological ordering?

Page 99: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

99

Revisiting Recursive DFS

C

A

F

D

B E

G

H

A D F G EB C F G E DC F G ED F G EEF GGH

Page 100: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

100

Revisiting Recursive DFS

C

A

F

D

B E

G

H

A D F G EB C

H

Page 101: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

101

Revisiting Recursive DFS

C

A

F

D

B E

G

H

H B C A D F G E

Page 102: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

102

Revisiting Recursive DFS

C

A

F

D

B E

G

H

HGF GED F G EC F G EB C F G E DA D F G E

Page 103: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

103

Revisiting Recursive DFS

C

A

F

D

B E

G

H

HGFEDCBA

Page 104: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

104

Revisiting Recursive DFS

C

A

F

D

B E

G

H

A B C D E F G H

Page 105: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

105

• function topologicalSortDFS():L = { }. // to store the sorted vertexeswhile graph contains any unmarked vertexes:

• select any unmarked vertex v.• visit(L, v).

• function visit(L, v):if v is unmarked:

• for each neighbor n of v:visit(L, n).

• mark v.• add v to front of L.

Topological Sort: DFS

C

A

F

D

B E

Page 106: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

106

• function topologicalSortDFS():L = { }. // to store the sorted vertexeswhile graph contains any unmarked vertexes:

• select any unmarked vertex v.• visit(L, v).

• function visit(L, v):if v has a temp.mark, error.if v is unmarked:

• temp.mark v.• for each neighbor n of v:

visit(L, n).• mark v.• add v to front of L.

Topological Sort: DFS

C

A

F

D

B E

Page 107: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

107

Plan for Today• Topological Sort

– Kahn’s Algorithm– Recursive DFS– Spreadsheets

• Announcements

Page 108: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

108

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies.

A B1 20

=B1*210

2 30=A1+B1

25=A1+5

3 50=SUM(A1:A2)

0.833=B2/A2 B1

A3

B2A1 B3

A2

Page 109: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

109

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies.

A B1 20

=B1*215

2 30=A1+B1

25=A1+5

3 50=SUM(A1:A2)

0.833=B2/A2 B1

A3

B2A1 B3

A2

Page 110: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

110

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies.

A B1 30

=B1*215

2 30=A1+B1

25=A1+5

3 50=SUM(A1:A2)

0.833=B2/A2 B1

A3

B2A1 B3

A2

Page 111: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

111

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies.

A B1 30

=B1*215

2 30=A1+B1

35=A1+5

3 50=SUM(A1:A2)

0.833=B2/A2 B1

A3

B2A1 B3

A2

Page 112: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

112

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies.

A B1 30

=B1*215

2 30=A1+B1

35=A1+5

3 50=SUM(A1:A2)

1.166=B2/A2 B1

A3

B2A1 B3

A2

Page 113: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

113

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies.

A B1 30

=B1*215

2 45=A1+B1

35=A1+5

3 50=SUM(A1:A2)

1.166=B2/A2 B1

A3

B2A1 B3

A2

Page 114: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

114

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies.

A B1 30

=B1*215

2 45=A1+B1

35=A1+5

3 75=SUM(A1:A2)

1.166=B2/A2 B1

A3

B2A1 B3

A2

Page 115: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

115

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies.

A B1 30

=B1*215

2 45=A1+B1

35=A1+5

3 75=SUM(A1:A2)

1.166=B2/A2 B1

A3

B2A1 B3

A2

Page 116: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

116

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies.

A B1 30

=B1*215

2 45=A1+B1

35=A1+5

3 75=SUM(A1:A2)

1.166=B2/A2 B1

A3

B2A1 B3

A2

Page 117: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

117

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies.

A B1 30

=B1*215

2 45=A1+B1

35=A1+5

3 75=SUM(A1:A2)

0.777=B2/A2 B1

A3

B2A1 B3

A2

Page 118: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

118

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies.

A B1 20

=B1*210

2 30=A1+B1

25=A1+5

3 50=SUM(A1:A2)

0.833=B2/A2 B1

A3

B2A1 B3

A2

Page 119: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

119

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies. We can use topological sort to efficiently update cells!

A B1 20

=B1*210

2 30=A1+B1

25=A1+5

3 50=SUM(A1:A2)

0.833=B2/A2

B1 A3B2A1 B3A2

Page 120: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

120

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies. We can use topological sort to efficiently update cells!

A B1 20

=B1*215

2 30=A1+B1

25=A1+5

3 50=SUM(A1:A2)

0.833=B2/A2

B1 A3B2A1 B3A2

Page 121: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

121

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies. We can use topological sort to efficiently update cells!

A B1 30

=B1*215

2 30=A1+B1

25=A1+5

3 50=SUM(A1:A2)

0.833=B2/A2

B1 A3B2A1 B3A2

Page 122: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

122

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies. We can use topological sort to efficiently update cells!

A B1 30

=B1*215

2 30=A1+B1

35=A1+5

3 50=SUM(A1:A2)

0.833=B2/A2

B1 A3B2A1 B3A2

Page 123: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

123

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies. We can use topological sort to efficiently update cells!

A B1 30

=B1*215

2 45=A1+B1

35=A1+5

3 50=SUM(A1:A2)

0.833=B2/A2

B1 A3B2A1 B3A2

Page 124: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

124

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies. We can use topological sort to efficiently update cells!

A B1 30

=B1*215

2 45=A1+B1

35=A1+5

3 75=SUM(A1:A2)

0.833=B2/A2

B1 A3B2A1 B3A2

Page 125: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

125

Topological Sort: SpreadsheetsCells with formulas referencing other cells have dependencies. We can use topological sort to efficiently update cells!

A B1 30

=B1*215

2 45=A1+B1

35=A1+5

3 75=SUM(A1:A2)

0.777=B2/A2

B1 A3B2A1 B3A2

Page 126: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

126

Plan for Today• Topological Sort

– Kahn’s Algorithm– Recursive DFS– Spreadsheets

• Announcements

Page 127: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

127

Announcements• Homework 8 (Excel) goes out on Wednesday 11/28, is due Friday

12/7. No late submissions will be accepted.• Guest lecture on Hashing next Monday: Zach!

Page 128: CS 106X, Lecture 25 Topological Sort - Stanford University€¦ · Topological Sort Given a directed (acyclic!) graph G= (V, E), a topological sort is a total ordering of G's vertices

128

Recap• Topological Sort

– Kahn’s Algorithm– Recursive DFS– Spreadsheets

• Announcements

Next time: Classes – Inheritance and Polymorphism