View
14
Download
1
Category
Preview:
Citation preview
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
2
Plan For This Week• Graphs: Topological Sort (HW8)• Classes: Inheritance and Polymorphism (HW8)• Sorting Algorithms
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.
4
Plan for Today• Topological Sort
– Kahn’s Algorithm– Recursive DFS– Spreadsheets
• Announcements
5
Graphs So Far• Graphs as Data Structures
– Adjacency List– Adjacency Matrix– Edge List
• Searching– DFS– BFS– Dijkstra’s Algorithm– A* Search
6
Dependency Graphs
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?
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?
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?
10
Dependency Graphs: Games
What is an effective AI strategy for resource management games (like Civilization)?
11
Dependency Graphs: Compilers
LoginUI
Toolbar
MainUI
AppDB
Bundle
DataTypes
In what order should we recompile source files with dependencies?
12
Dependency Graphs: Web
LoginUI
Toolbar
MainUI
AppDB
Bundle
DataTypes
How can I efficiently package up and transmit web dependencies?
13
Topological SortToast Bread
Butter Toast Sauté Veggies
Chop Veggies
Add Eggs & Cook
Prepare Eggs
Plate FoodWhich tasks could we complete first?
14
Topological SortToast Bread
Butter Toast Sauté Veggies
Chop Veggies
Add Eggs & Cook
Prepare Eggs
Plate FoodWhich tasks could we complete first?
15
Topological SortToast Bread
Butter Toast Sauté Veggies
Chop Veggies
Add Eggs & Cook
Prepare Eggs
Plate Food
Toast Bread
16
Topological SortToast Bread
Butter Toast Sauté Veggies
Chop Veggies
Add Eggs & Cook
Prepare Eggs
Plate Food
Toast Bread
17
Topological SortToast Bread
Butter Toast Sauté Veggies
Chop Veggies
Add Eggs & Cook
Prepare Eggs
Plate Food
Toast Bread
Chop Veggies
18
Topological SortToast Bread
Butter Toast Sauté Veggies
Chop Veggies
Add Eggs & Cook
Prepare Eggs
Plate Food
Toast Bread
Chop Veggies
19
Topological Sort
Butter Toast Sauté Veggies
Add Eggs & Cook
Prepare Eggs
Plate Food
Toast Bread
Chop Veggies
Butter Toast
Toast Bread Chop Veggies
20
Topological Sort
Butter Toast Sauté Veggies
Add Eggs & Cook
Prepare Eggs
Plate Food
Toast Bread
Chop Veggies
Butter Toast
Toast Bread Chop Veggies
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
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
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
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
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
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
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
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
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
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
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
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.
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.
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
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
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?
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?
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
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: { }
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: { }
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: { }
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: { }
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” }
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” }
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” }
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” }
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?
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
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))
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)
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)
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)
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?
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
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.
56
Topological Sort: Take 2 !
A
B F
D
G
E
H
0-In-Degree Queue: { }In-Degree Map: { }
C
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
79
Plan for Today• Topological Sort
– Kahn’s Algorithm– Recursive DFS– Spreadsheets
• Announcements
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?
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?
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
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: { }
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: { }
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” }
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” }
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” }
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” }
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” }
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?
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
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)
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)
94
Plan for Today• Topological Sort
– Kahn’s Algorithm– Recursive DFS– Spreadsheets
• Announcements
95
Revisiting Recursive DFS
C
A
F
D
B E
G
H
96
Revisiting Recursive DFS
C
A
F
D
B E
G
H
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.
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?
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
100
Revisiting Recursive DFS
C
A
F
D
B E
G
H
A D F G EB C
H
101
Revisiting Recursive DFS
C
A
F
D
B E
G
H
H B C A D F G E
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
103
Revisiting Recursive DFS
C
A
F
D
B E
G
H
HGFEDCBA
104
Revisiting Recursive DFS
C
A
F
D
B E
G
H
A B C D E F G H
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
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
107
Plan for Today• Topological Sort
– Kahn’s Algorithm– Recursive DFS– Spreadsheets
• Announcements
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
126
Plan for Today• Topological Sort
– Kahn’s Algorithm– Recursive DFS– Spreadsheets
• Announcements
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!
128
Recap• Topological Sort
– Kahn’s Algorithm– Recursive DFS– Spreadsheets
• Announcements
Next time: Classes – Inheritance and Polymorphism
Recommended