basic graph search - itu.dkitu.dk/people/pagh/ads11/08-graphsearch.pdf · circuit gate, register,...
TRANSCRIPT
Algorithms and Data Structures, Fall 2011
Basic Graph Search
Rasmus Pagh
Based on slides by Kevin Wayne, PrincetonAlgorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011
Monday, October 31, 11
Mid-term evaluation
Thanks to those of you who provided feedback
- Will try to choose case studies for lecture on November 14 that allow us to rehearse algorithm analysis basics.
- Will start posing previous exam problems for exercises (from next week). Also, there will be a trial exam on November 28.
2
Monday, October 31, 11
Graph. Set of vertices connected pairwise by edges.
Why study graph algorithms?
• Interesting and broadly useful abstraction.
• Challenging branch of computer science and discrete math.
• Hundreds of graph algorithms known.
• Thousands of practical applications.
3
Undirected graphs
Monday, October 31, 11
4
Protein-protein interaction network
Reference: Jeong et al, Nature Review | Genetics
Monday, October 31, 11
5
The Internet as mapped by the Opte Project
http://en.wikipedia.org/wiki/Internet
Monday, October 31, 11
6
10 million Facebook friends
"Visualizing Friendships" by Paul Butler
Monday, October 31, 11
7
Relationship graph
Relationship graph at "Jefferson High"
Monday, October 31, 11
8
Graph applications
graph vertex edge
communication telephone, computer fiber optic cable
circuit gate, register, processor wire
mechanical joint rod, beam, spring
financial stock, currency transactions
transportation street intersection, airport highway, airway route
internet class C network connection
game board position legal move
social relationship person, actor friendship, movie cast
neural network neuron synapse
protein network protein protein-protein interaction
chemical compound molecule bond
Monday, October 31, 11
9
Graph terminology
Path. Sequence of vertices connected by edges.Cycle. Path whose first and last vertices are the same.
Two vertices are connected if there is a path between them.
Anatomy of a graph
cycle oflength 5
vertex
vertex ofdegree 3
edge
path oflength 4
connectedcomponents
Monday, October 31, 11
10
Some graph-processing problems
Path. Is there a path between s and t?Shortest path. What is the shortest path between s and t?
Cycle. Is there a cycle in the graph?Euler tour. Is there a cycle that uses each edge exactly once?Hamilton tour. Is there a cycle that uses each vertex exactly once?
Connectivity. Is there a way to connect all of the vertices?MST. What is the best way to connect all of the vertices?Biconnectivity. Is there a vertex whose removal disconnects the graph?
Planarity. Can you draw the graph in the plane with no crossing edges?Graph isomorphism. Do two adjacency lists represent the same graph?
Challenge. Which of these problems are easy? difficult? intractable?
Monday, October 31, 11
Graph drawing. Provides intuition about the structure of the graph.Caveat. Intuition can be misleading.
11
Graph representation
Two drawings of the same graph
Monday, October 31, 11
Vertex representation.
• This lecture: use integers between 0 and V-1.
• Applications: convert between names and integers with symbol table.
Anomalies.
A
G
E
CB
F
D
12
Graph representation
symbol table
0
6
4
21
5
3
Anomalies
paralleledgesself-loop
Monday, October 31, 11
13
Graph API
public class Graph public class Graph
Graph(int V) create an empty graph with V vertices
Graph(In in) create a graph from input stream
void addEdge(int v, int w) add an edge v-w
Iterable<Integer> adj(int v) vertices adjacent to v
int V() number of vertices
int E() number of edges
String toString() string representation
Monday, October 31, 11
Maintain a two-dimensional V-by-V boolean array;for each edge v-w in graph: adj[v][w] = adj[w][v] = true.
0 1 2 3 4 5 6 7 8 9 10 11 12
0
1
2
3
4
5
6
7
8
9
10
11
12
0 1 1 0 0 1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0 0 0 0
0 0 0 1 0 1 1 0 0 0 0 0 0
1 0 0 1 1 0 0 0 0 0 0 0 0
1 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 1
0 0 0 0 0 0 0 0 0 1 0 1 0
14
Adjacency-matrix graph representation
two entries
for each edge
109
1211
0
6
4
21
5
3
87
Monday, October 31, 11
Maintain vertex-indexed array of lists.(use Bag abstraction)
15
Adjacency-list graph representation
109
1211
0
6
4
21
5
3
adj[]0
1
2
3
4
5
6
7
8
9
10
11
12
5 4
0 4
9 12
11 9
0
0
8
7
9
5 6 3
3 4 0
11 10 12
6 2 1 5
Adjacency-lists representation (undirected graph)
Bag objects
representationsof the same edge
87
(alt: HashSet)
Monday, October 31, 11
In practice. Use adjacency-lists representation.
• Algorithms based on iterating over vertices adjacent to v.
• Real-world graphs tend to be “sparse.”
16
Graph representations
representation space add edgeedge between
v and w?
iterate over vertices
adjacent to v?
adjacency matrix V 2 1 * 1 V
adjacency lists E + V 1 degree(v) degree(v)
huge number of vertices,
small average vertex degree
* disallows parallel edges
Monday, October 31, 11
In practice. Use adjacency-lists representation.
• Algorithms based on iterating over vertices adjacent to v.
• Real-world graphs tend to be “sparse.”
17
Graph representations
huge number of vertices,
small average vertex degree
sparse (E = 200) dense (E = 1000)
Two graphs (V = 50)
Monday, October 31, 11
18
Maze exploration: A graph search problem
Maze graphs.
• Vertex = intersection.
• Edge = passage.
Goal. Explore every intersection in the maze.
intersection passage
Monday, October 31, 11
Algorithm.
• Unroll a ball of string behind you.
• Mark each visited intersection and each visited passage.
• Retrace steps when no unvisited options.
19
Trémaux maze exploration
Tremaux exploration
Theseus and
the Minotaur
Monday, October 31, 11
20
Maze exploration
Monday, October 31, 11
20
Maze exploration
Monday, October 31, 11
Goal. Systematically search through a graph.Idea. Mimic maze exploration.
Typical applications. [ahead]
• Find all vertices connected to a given source vertex.
• Find a path between two vertices.
Depth-first search
Mark v as visited.Recursively visit all unmarked vertices w adjacent to v.
DFS (to visit a vertex v)
Monday, October 31, 11
22
Design pattern. Decouple graph data type from graph processing.
Typical client program.
• Create a Graph.
• Pass the Graph to a graph-processing routine, e.g., Search.
• Query the graph-processing routine for information.
Design pattern for graph processing
Search search = new Search(G, s); for (int v = 0; v < G.V(); v++) if (search.marked(v)) StdOut.println(v);
public class Search public class Search
Search(Graph G, int s) find vertices connected to s
boolean marked(int v) is vertex v connected to s?
int count() how many vertices connected to s?
print all vertices
connected to s
Monday, October 31, 11
23
Depth-first search (reachability)
public class DepthFirstSearch{ private boolean[] marked;
public DepthFirstSearch(Graph G, int s) { marked = new boolean[G.V()]; dfs(G, s); }
private void dfs(Graph G, int v) { marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); }
public boolean marked(int v) { return marked[v]; }}
true if connected to s
constructor marks
vertices connected to s
recursive DFS does the work
client can ask whether
vertex v is connected to s
Monday, October 31, 11
Depth-first search properties
Proposition. DFS marks all vertices connected to s in time proportional to the sum of their degrees.
Pf.
• Correctness:
- if w marked, then w connected to s (why?)
- if w connected to s, then w marked(if w unmarked, then consider last edgeon a path from s to w that goes from amarked vertex to an unmarked one)
• Running time: each vertexconnected to s is visited once.
24
set ofunmarked
vertices
no such edgecan exist
source
v
s
set of markedvertices
w
x
Monday, October 31, 11
Depth-first search application: preparing for a date
25
http://xkcd.com/761/
Monday, October 31, 11
26
Depth-first search application: flood fill
Change color of entire blob of neighboring red pixels to blue.
Build a grid graph.
• Vertex: pixel.
• Edge: between two adjacent red pixels.
• Blob: all pixels connected to given pixel.
recolor red blob to blue
Monday, October 31, 11
27
Depth-first search application: flood fill
Change color of entire blob of neighboring red pixels to blue.
Build a grid graph.
• Vertex: pixel.
• Edge: between two adjacent red pixels.
• Blob: all pixels connected to given pixel.
recolor red blob to blue
Monday, October 31, 11
Goal. Does there exist a path from s to t ? If yes, find any such path.
28
Pathfinding in graphs
† amortizedMonday, October 31, 11
29
Pathfinding in graphs
Goal. Does there exist a path from s to t ? If yes, find any such path.
Depth-first search. After linear-time preprocessing, can recover path itself in time proportional to its length.
Idea: Mark each visited vertex by keeping track of edge taken to visit it.
public class Paths public class Paths
Paths(Graph G, int s) find paths in G from source s
boolean hasPathTo(int v) is there a path from s to v?
Iterable<Integer> pathTo(int v) path from s to v; null if no such path
Monday, October 31, 11
30
Depth-first search (pathfinding)
public class DepthFirstPaths{ private boolean[] marked; private int[] edgeTo; private final int s; public DepthFirstPaths(Graph G, int s) { marked = new boolean[G.V()]; edgeTo = new int[G.V()]; this.s = s; dfs(G, s); } private void dfs(Graph G, int v) { marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) { edgeTo[w] = v; dfs(G, w); } }
public boolean hasPathTo(int v) public Iterable<Integer> pathTo(int v)}
parent-link representation
of DFS tree
set parent link
ahead
Monday, October 31, 11
edgeTo[] is a parent-link representation of a tree rooted at s.
31
Depth-first search (pathfinding iterator)
public boolean hasPathTo(int v) { return marked[v]; }
public Iterable<Integer> pathTo(int v) { if (!hasPathTo(v)) return null; Stack<Integer> path = new Stack<Integer>(); for (int x = v; x != s; x = edgeTo[x]) path.push(x); path.push(s); return path; }
Trace of pathTo() computation
edgeTo[] 0 1 2 2 0 3 2 4 3 5 3
5 53 3 52 2 3 50 0 2 3 5
x path
Monday, October 31, 11
Depth-first search. Put unvisited vertices on a stack.Breadth-first search. Put unvisited vertices on a queue.
Shortest path. Find path from s to t that uses fewest number of edges.
Intuition. BFS examines vertices in increasing distance from s.32
Breadth-first search
Put s onto a FIFO queue, and mark s as visited.Repeat until the queue is empty: - remove the least recently added vertex v - add each of v's unvisited neighbors to the queue, and mark them as visited.
BFS (from source vertex s)
Breadth-!rstmaze exploration
Monday, October 31, 11
33
Breadth-first search (pathfinding)
private void bfs(Graph G, int s) { Queue<Integer> q = new Queue<Integer>(); q.enqueue(s); marked[s] = true; while (!q.isEmpty()) { int v = q.dequeue(); for (int w : G.adj(v)) if (!marked[w]) { q.enqueue(w); marked[w] = true; edgeTo[w] = v; } } }
Trace of breadth-!rst search to !nd all paths from 0
marked[]
0 T 1 2 3 4 5
0 T 1 T 2 T 3 4 5 T
0 T 1 T 2 T 3 T 4 T 5 T
q
0
1534
215
534
34
4
edgeTo[]
0 1 2 3 4 5
0 1 0 2 0 3 4 5 0
0 1 0 2 0 3 2 4 2 5 0
0 2 1 5 1 0 2 2 0 1 3 4 3 5 4 2 4 3 2 5 3 0
0 2 1 5 1 0 2 2 0 1 3 4 3 5 4 2 4 3 2 5 3 0
0 T 1 T 2 T 3 T 4 T 5 T
0 1 0 2 0 3 2 4 2 5 0
0 2 1 5 1 0 2 2 0 1 3 4 3 5 4 2 4 3 2 5 3 0
adj[]
0 2 1 5 1 0 2 2 0 1 3 4 3 5 4 2 4 3 2 5 3 0
0 T 1 T 2 T 3 T 4 T 5 T
0 1 0 2 0 3 2 4 2 5 0
0 2 1 5 1 0 2 2 0 1 3 4 3 5 4 2 4 3 2 5 3 0
0 T 1 T 2 T 3 T 4 T 5 T
0 1 0 2 0 3 2 4 2 5 0
0 2 1 5 1 0 2 2 0 1 3 4 3 5 4 2 4 3 2 5 3 0
Monday, October 31, 11
Proposition. BFS computes shortest path (number of edges) from sin a connected graph in time proportional to E + V.
Pf.
• Correctness: queue always consists of zero or more vertices of distance k from s, followed by zero or more vertices of distance k + 1.
• Running time: each vertex connected to s is visited once.
Breadth-first search properties
34
0
4
2
1
53
standard drawing
0
4
2
1
5
3
dist = 0 dist = 1 dist = 2
Monday, October 31, 11
DFS and BFS space
In the worst case, need to store V vertices on the stack/queue.
Possible to do this with less space?
In 2004, Omer Reingold showed that it is possible to determine the existence of a path using just a constant number of integers (in addition to the input).
However, the running time of Reingold’s algorithm is high, between V10 and V18.
35
Monday, October 31, 11
36
Breadth-first search application: routing
Fewest number of hops in a communication network.
ARPANET, July 1977
Monday, October 31, 11
37
Kevin Bacon graph
• Include a vertex for each performer and for each movie.
• Connect a movie to all performers that appear in that movie.
• Compute shortest path from s = Kevin Bacon.
KevinBacon
KathleenQuinlan
MerylStreep
NicoleKidman
JohnGielgud
KateWinslet
BillPaxton
DonaldSutherland
The StepfordWives
Portraitof a Lady
Dial Mfor Murder
Apollo 13
To Catcha Thief
The EagleHas Landed
ColdMountain
Murder on theOrient Express
VernonDobtcheff
An AmericanHaunting
Jude
Enigma
Eternal Sunshineof the Spotless
Mind
TheWoodsman
WildThings
Hamlet
Titanic
AnimalHouse
GraceKellyCaligola
The RiverWild
LloydBridges
HighNoon
The DaVinci Code
Joe Versusthe Volcano
PatrickAllen
TomHanks
SerrettaWilson
GlennClose
JohnBelushi
YvesAubert Shane
Zaza
PaulHerbert
performervertex
movievertex
Symbol graph example (adjacency lists)
...Tin Men (1987)/DeBoy, David/Blumenfeld, Alan/... /Geppi, Cindy/Hershey, Barbara...Tirez sur le pianiste (1960)/Heymann, Claude/.../Berger, Nicole (I)...Titanic (1997)/Mazin, Stan/...DiCaprio, Leonardo/.../Winslet, Kate/...Titus (1999)/Weisskopf, Hermann/Rhys, Matthew/.../McEwan, GeraldineTo Be or Not to Be (1942)/Verebes, Ernö (I)/.../Lombard, Carole (I)...To Be or Not to Be (1983)/.../Brooks, Mel (I)/.../Bancroft, Anne/...To Catch a Thief (1955)/París, Manuel/.../Grant, Cary/.../Kelly, Grace/...To Die For (1995)/Smith, Kurtwood/.../Kidman, Nicole/.../ Tucci, Maria......
movies.txt
V and E not explicitly
specified
"/"delimiter
http://oracleofbacon.org
Monday, October 31, 11
Def. Vertices v and w are connected if there is a path between them.
Goal. Preprocess graph to answer queries: is v connected to w ? in constant time.
38
Connectivity queries
public class CC public class CC
CC(Graph G) find connected components in G
boolean connected(int v, int w)connected(int v, int w) are v and w connected?
int count() number of connected components
int id(int v) component identifier for v
Monday, October 31, 11
Def. A connected component is a maximal set of connected vertices.
39
Connected components
Monday, October 31, 11
Def. A connected component is a maximal set of connected vertices.
39
Connected components
63 connected components
Monday, October 31, 11
Goal. Partition vertices into connected components.
40
Connected components
Initialize all vertices v as unmarked.
For each unmarked vertex v, run DFS to identify all vertices discovered as part of the same component.
Connected components
13130 54 30 19 126 45 40 211 129 100 67 89 115 3
tinyG.txt
Input format for Graph constructor (two examples)
2501273244 246239 240238 245235 238233 240232 248231 248229 249228 241226 231...(1261 additional lines)
mediumG.txtV
EV
E
13130 54 30 19 126 45 40 211 129 100 67 89 115 3
tinyG.txt
Input format for Graph constructor (two examples)
2501273244 246239 240238 245235 238233 240232 248231 248229 249228 241226 231...(1261 additional lines)
mediumG.txtV
EV
E
Monday, October 31, 11
Graph-processing challenge 1
Problem. Is a graph bipartite?
How difficult?
• Any COS 126 student could do it.
• Need to be a typical diligent COS 226 student.
• Hire an expert.
• Intractable.
• No one knows.
• Impossible.
41
0-10-20-50-61-32-32-44-54-6
0
6
4
21
5
3
0-10-20-50-61-32-32-44-54-6
0
6
4
21
5
3
Monday, October 31, 11
Graph-processing challenge 2
Problem. Find a cycle.
How difficult?
• Any COS 126 student could do it.
• Need to be a typical diligent COS 226 student.
• Hire an expert.
• Intractable.
• No one knows.
• Impossible.
42
0-10-20-50-61-32-32-44-54-6
0
6
4
21
5
3
0
6
4
21
5
3
Monday, October 31, 11
Graph-processing challenge 4
Problem. Find a cycle that visits every vertex.Assumption. Need to visit each vertex exactly once.
How difficult?
• Any COS 126 student could do it.
• Need to be a typical diligent COS 226 student.
• Hire an expert.
• Intractable.
• No one knows.
• Impossible.
43
0-10-20-50-61-22-63-43-54-54-60-5-3-4-6-2-1-0
0
6
4
21
5
3
Monday, October 31, 11
Graph-processing challenge 4
Problem. Find a cycle that visits every vertex.Assumption. Need to visit each vertex exactly once.
How difficult?
• Any COS 126 student could do it.
• Need to be a typical diligent COS 226 student.
• Hire an expert.
• Intractable.
• No one knows.
• Impossible.
44
Hamiltonian cycle
(classical NP-complete problem)
0-10-20-50-61-22-63-43-54-54-60-5-3-4-6-2-1-0
0
6
4
21
5
3
✓
Andreas Björklund
Monday, October 31, 11
Graph-processing challenge 5
Problem. Are two graphs identical except for vertex names?
How difficult?
• Any COS 126 student could do it.
• Need to be a typical diligent COS 226 student.
• Hire an expert.
• Intractable.
• No one knows.
• Impossible.
45
0-10-20-50-63-43-54-54-6
0
6
4
21
5
3
3
1
5
2
4
0
6
0↔4, 1↔3, 2↔2, 3↔6, 4↔5, 5↔0, 6↔1
0-40-50-61-41-52-43-45-6
Monday, October 31, 11
Graph-processing challenge 5
Problem. Are two graphs identical except for vertex names?
How difficult?
• Any COS 126 student could do it.
• Need to be a typical diligent COS 226 student.
• Hire an expert.
• Intractable.
• No one knows.
• Impossible.
46
graph isomorphism is
longstanding open problem
0-10-20-50-63-43-54-54-6
0
6
4
21
5
3
✓3
1
5
2
4
0
6
0-40-50-61-41-52-43-45-6
0↔4, 1↔3, 2↔2, 3↔6, 4↔5, 5↔0, 6↔1
Monday, October 31, 11
Digraph. Set of vertices connected pairwise by directed edges.
47
Directed graphs
A directed graph (digraph)
directedcycle
directed pathfrom 0 to 2
vertex of outdegree 3
and indegree 1
Monday, October 31, 11
48
Road network
Vertex = intersection; edge = one-way street.
©2008 Google - Map data ©2008 Sanborn, NAVTEQ™ - Terms of Use
Monday, October 31, 11
Vertex = synset; edge = hypernym relationship.
49
WordNet graph
http://wordnet.princeton.edu
ĞǀĞŶƚ
ŚĂƉƉĞŶŝŶŐ�ŽĐĐƵƌƌĞŶĐĞ�ŽĐĐƵƌƌĞŶ��ŶĂƚƵƌĂůͺĞǀĞŶƚ
ĐŚĂŶŐĞ�ĂůƚĞƌĂƚŝŽŶ�ŵŽĚŝĨŝĐĂƚŝŽŶ
ĚĂŵĂŐĞ�ŚĂƌŵ�͘͘ŝŵƉĂŝƌŵĞŶƚ ƚƌĂŶƐŝƚŝŽŶ
ůĞĂƉ�ũƵŵƉ�ƐĂůƚĂƚŝŽŶ ũƵŵƉ�ůĞĂƉ
ĂĐƚ�ŚƵŵĂŶͺĂĐƚŝŽŶ�ŚƵŵĂŶͺĂĐƚŝǀŝƚLJ
ŐƌŽƵƉͺĂĐƚŝŽŶ
ĨŽƌĨĞŝƚ�ĨŽƌĨĞŝƚƵƌĞ�ƐĂĐƌŝĨŝĐĞ ĂĐƚŝŽŶ
ĐŚĂŶŐĞ
ƌĞƐŝƐƚĂŶĐĞ�ŽƉƉŽƐŝƚŝŽŶ ƚƌĂŶƐŐƌĞƐƐŝŽŶ
ĚĞŵŽƚŝŽŶ ǀĂƌŝĂƚŝŽŶ
ŵŽƚŝŽŶ�ŵŽǀĞŵĞŶƚ�ŵŽǀĞ
ůŽĐŽŵŽƚŝŽŶ�ƚƌĂǀĞů
ƌƵŶ�ƌƵŶŶŝŶŐ
ĚĂƐŚ�ƐƉƌŝŶƚ
ĚĞƐĐĞŶƚ
ũƵŵƉ�ƉĂƌĂĐŚƵƚŝŶŐ
ŝŶĐƌĞĂƐĞ
ŵŝƌĂĐůĞ
ŵŝƌĂĐůĞ
��� ���
Monday, October 31, 11
50
Digraph applications
digraph vertex directed edge
transportation street intersection one-way street
web web page hyperlink
food web species predator-prey relationship
WordNet synset hypernym
scheduling task precedence constraint
financial bank transaction
cell phone person placed call
infectious disease person infection
game board position legal move
citation journal article citation
object graph object pointer
inheritance hierarchy class inherits from
control flow code block jump
Monday, October 31, 11
51
Some digraph problems
Path. Is there a directed path from s to t ?
Shortest path. What is the shortest directed path from s to t ?
Topological sort. Can you draw the digraph so that all edges point upwards?
Strong connectivity. Is there a directed path between all pairs of vertices?
Transitive closure. For which vertices v and w is there a path from v to w ?
PageRank. What is the importance of a web page?
NEW
NEW
NEW
NEW
Monday, October 31, 11
Same as for undirected graphs.
In practice. Use adjacency-list representation.
• Algorithms based on iterating over vertices adjacent from v.
• Real-world digraphs tend to be sparse.
52
Digraph representations
representation spaceinsert edge
from v to w
edge from
v to w?
iterate over vertices
adjacent from v?
adjacency matrix V 2 1 † 1 V
adjacency list E + V 1 outdegree(v) outdegree(v)
† disallows parallel edges
Monday, October 31, 11
Same method as for undirected graphs.
• Every undirected graph is a digraph (with edges in both directions).
• DFS is a digraph algorithm.
53
Depth-first search in digraphs
Mark v as visited.Recursively visit all unmarked vertices w adjacent from v.
DFS (to visit a vertex v)
Monday, October 31, 11
Same method as for undirected graphs.
• Every undirected graph is a digraph (with edges in both directions).
• BFS is a digraph algorithm.
Proposition. BFS computes shortest paths (fewest number of edges).
54
Breadth-first search in digraphs
Is w reachable from v in this digraph?
v
w
s
Put s onto a FIFO queue, and mark s as visited.Repeat until the queue is empty: - remove the least recently added vertex v - for each unmarked vertex adjacent from v: add to queue and mark as visited..
BFS (from source vertex s)
Monday, October 31, 11
Every data structure is a digraph.
• Vertex = object.
• Edge = reference.Roots. Objects known to be directly accessible by program (e.g., stack).
Reachable objects. Objects indirectly accessible by program(starting at a root and following a chain of pointers).
55
Reachability application: mark-sweep garbage collector
PROBLEM SESSION
Another approach to garbage collection is to
maintain a count of the number of references
to each object, and remove objects with
count 0.
- Argue that this will not always get rid of all
unused objects.
- How can we find cycles in a directed graph?
Monday, October 31, 11
Goal. Given a set of tasks to be completed with precedence constraints,in which order should we schedule the tasks?
Graph model. vertex = task; edge = precedence constraint.
56
Precedence scheduling
tasksprecedence constraint graph
0
1
4
52
6
3
feasible schedule
0. Algorithms
1. Complexity Theory
2. Artificial Intelligence
3. Intro to CS
4. Cryptography
5. Scientific Computing
6. Advanced Programming
Monday, October 31, 11
Goal. Given a set of tasks to be completed with precedence constraints,in which order should we schedule the tasks?
Graph model. vertex = task; edge = precedence constraint.
56
Precedence scheduling
tasksprecedence constraint graph
0
1
4
52
6
3
feasible schedule
0. Algorithms
1. Complexity Theory
2. Artificial Intelligence
3. Intro to CS
4. Cryptography
5. Scientific Computing
6. Advanced Programming
Monday, October 31, 11
57
Topological sort
DAG. Directed acyclic graph.
Topological sort. Redraw DAG so all edges point up.
Solution. DFS. What else? topological order
directed edges DAG
0→5 0→2
0→1 3→6
3→5 3→4
5→4 6→4
6→0 3→2
1→4
0
1
4
52
6
3
Monday, October 31, 11
58
Depth-first search order
public class DepthFirstOrder{ private boolean[] marked; private Stack<Integer> reversePost;
public DepthFirstOrder(Digraph G) { reversePost = new Stack<Integer>(); marked = new boolean[G.V()]; for (int v = 0; v < G.V(); v++) if (!marked[v]) dfs(G, v); }
private void dfs(Digraph G, int v) { marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); reversePost.push(v); } public Iterable<Integer> reversePost() { return reversePost; }}
returns all vertices in
“reverse DFS postorder”
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 -
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 -
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 -
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 5
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0check 2 1 1 1 0 1 1 0 4 1 2 5 0
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0check 2 1 1 1 0 1 1 0 4 1 2 5 0dfs(3) 1 1 1 1 1 1 0 4 1 2 5 0
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0check 2 1 1 1 0 1 1 0 4 1 2 5 0dfs(3) 1 1 1 1 1 1 0 4 1 2 5 0 check 2 1 1 1 1 1 1 0 4 1 2 5 0
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0check 2 1 1 1 0 1 1 0 4 1 2 5 0dfs(3) 1 1 1 1 1 1 0 4 1 2 5 0 check 2 1 1 1 1 1 1 0 4 1 2 5 0 check 4 1 1 1 1 1 1 0 4 1 2 5 0
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0check 2 1 1 1 0 1 1 0 4 1 2 5 0dfs(3) 1 1 1 1 1 1 0 4 1 2 5 0 check 2 1 1 1 1 1 1 0 4 1 2 5 0 check 4 1 1 1 1 1 1 0 4 1 2 5 0 check 5 1 1 1 1 1 1 0 4 1 2 5 0
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0check 2 1 1 1 0 1 1 0 4 1 2 5 0dfs(3) 1 1 1 1 1 1 0 4 1 2 5 0 check 2 1 1 1 1 1 1 0 4 1 2 5 0 check 4 1 1 1 1 1 1 0 4 1 2 5 0 check 5 1 1 1 1 1 1 0 4 1 2 5 0 dfs(6) 1 1 1 1 1 1 1 4 1 2 5 0
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0check 2 1 1 1 0 1 1 0 4 1 2 5 0dfs(3) 1 1 1 1 1 1 0 4 1 2 5 0 check 2 1 1 1 1 1 1 0 4 1 2 5 0 check 4 1 1 1 1 1 1 0 4 1 2 5 0 check 5 1 1 1 1 1 1 0 4 1 2 5 0 dfs(6) 1 1 1 1 1 1 1 4 1 2 5 0 6 done 1 1 1 1 1 1 1 4 1 2 5 0 6
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0check 2 1 1 1 0 1 1 0 4 1 2 5 0dfs(3) 1 1 1 1 1 1 0 4 1 2 5 0 check 2 1 1 1 1 1 1 0 4 1 2 5 0 check 4 1 1 1 1 1 1 0 4 1 2 5 0 check 5 1 1 1 1 1 1 0 4 1 2 5 0 dfs(6) 1 1 1 1 1 1 1 4 1 2 5 0 6 done 1 1 1 1 1 1 1 4 1 2 5 0 63 done 1 1 1 1 1 1 1 4 1 2 5 0 6 3
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0check 2 1 1 1 0 1 1 0 4 1 2 5 0dfs(3) 1 1 1 1 1 1 0 4 1 2 5 0 check 2 1 1 1 1 1 1 0 4 1 2 5 0 check 4 1 1 1 1 1 1 0 4 1 2 5 0 check 5 1 1 1 1 1 1 0 4 1 2 5 0 dfs(6) 1 1 1 1 1 1 1 4 1 2 5 0 6 done 1 1 1 1 1 1 1 4 1 2 5 0 63 done 1 1 1 1 1 1 1 4 1 2 5 0 6 3check 4 1 1 1 1 1 1 0 4 1 2 5 0 6 3
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0check 2 1 1 1 0 1 1 0 4 1 2 5 0dfs(3) 1 1 1 1 1 1 0 4 1 2 5 0 check 2 1 1 1 1 1 1 0 4 1 2 5 0 check 4 1 1 1 1 1 1 0 4 1 2 5 0 check 5 1 1 1 1 1 1 0 4 1 2 5 0 dfs(6) 1 1 1 1 1 1 1 4 1 2 5 0 6 done 1 1 1 1 1 1 1 4 1 2 5 0 63 done 1 1 1 1 1 1 1 4 1 2 5 0 6 3check 4 1 1 1 1 1 1 0 4 1 2 5 0 6 3check 5 1 1 1 1 1 1 0 4 1 2 5 0 6 3
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0check 2 1 1 1 0 1 1 0 4 1 2 5 0dfs(3) 1 1 1 1 1 1 0 4 1 2 5 0 check 2 1 1 1 1 1 1 0 4 1 2 5 0 check 4 1 1 1 1 1 1 0 4 1 2 5 0 check 5 1 1 1 1 1 1 0 4 1 2 5 0 dfs(6) 1 1 1 1 1 1 1 4 1 2 5 0 6 done 1 1 1 1 1 1 1 4 1 2 5 0 63 done 1 1 1 1 1 1 1 4 1 2 5 0 6 3check 4 1 1 1 1 1 1 0 4 1 2 5 0 6 3check 5 1 1 1 1 1 1 0 4 1 2 5 0 6 3check 6 1 1 1 1 1 1 0 4 1 2 5 0 6 3
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0check 2 1 1 1 0 1 1 0 4 1 2 5 0dfs(3) 1 1 1 1 1 1 0 4 1 2 5 0 check 2 1 1 1 1 1 1 0 4 1 2 5 0 check 4 1 1 1 1 1 1 0 4 1 2 5 0 check 5 1 1 1 1 1 1 0 4 1 2 5 0 dfs(6) 1 1 1 1 1 1 1 4 1 2 5 0 6 done 1 1 1 1 1 1 1 4 1 2 5 0 63 done 1 1 1 1 1 1 1 4 1 2 5 0 6 3check 4 1 1 1 1 1 1 0 4 1 2 5 0 6 3check 5 1 1 1 1 1 1 0 4 1 2 5 0 6 3check 6 1 1 1 1 1 1 0 4 1 2 5 0 6 3done 1 1 1 1 1 1 1 4 1 2 5 0 6 3
marked[] reversePost
0
1
4
52
6
3
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Reverse DFS postorder in a DAG
59
dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 50 done 1 1 1 0 1 1 0 4 1 2 5 0check 1 1 1 1 0 1 1 0 4 1 2 5 0check 2 1 1 1 0 1 1 0 4 1 2 5 0dfs(3) 1 1 1 1 1 1 0 4 1 2 5 0 check 2 1 1 1 1 1 1 0 4 1 2 5 0 check 4 1 1 1 1 1 1 0 4 1 2 5 0 check 5 1 1 1 1 1 1 0 4 1 2 5 0 dfs(6) 1 1 1 1 1 1 1 4 1 2 5 0 6 done 1 1 1 1 1 1 1 4 1 2 5 0 63 done 1 1 1 1 1 1 1 4 1 2 5 0 6 3check 4 1 1 1 1 1 1 0 4 1 2 5 0 6 3check 5 1 1 1 1 1 1 0 4 1 2 5 0 6 3check 6 1 1 1 1 1 1 0 4 1 2 5 0 6 3done 1 1 1 1 1 1 1 4 1 2 5 0 6 3
marked[] reversePost
0
1
4
52
6
3
reverse DFS
postorder is a
topological
order!
0→5
0→2
0→1
3→6
3→5
3→4
5→4
6→4
6→0
3→2
1→4
Monday, October 31, 11
Proposition. Reverse DFS postorder of a DAG is a topological order.Pf. Consider any edge v→w. When dfs(G, v) is called:
• Case 1: dfs(G, w) has already been called and returned.Thus, w was put on the stack before v.
• Case 2: dfs(G, w) has not yet been called.It will get called directly or indirectlyby dfs(G, v) and will finish before dfs(G, v).Thus, w will be put on the stack before v.
• Case 3: dfs(G, w) has already been called, but has not returned. Can’t happen in a DAG: function call stack containspath from w to v, so v→w would complete a cycle.
dfs(0) dfs(1) dfs(4) 4 done 1 done dfs(2) 2 done dfs(5) check 2 5 done 0 done check 1 check 2 dfs(3) check 2 check 4 check 5 dfs(6) 6 done 3 done check 4 check 5 check 6 done
60
Topological sort in a DAG: correctness proof
all vertices adjacent from 3 are done before 3 is done,
so they appear after 3 in topological order
Ex:
case 1
case 2
Monday, October 31, 11
Microsoft Excel does cycle detection (and has a circular reference toolbar!)
61
Directed cycle detection application: spreadsheet recalculation
Monday, October 31, 11
Conclusion
Graphs can be used to model a large variety of computational problems.
Efficient graph algorithms (more to come!) can often be used as building blocks for solving given problems.
Releted course goals:• Choose among and make use of the most important algorithms and data
structures in libraries, based on knowledge of their complexity.• Design algorithms for ad hoc problems by using and combining known
algorithms and data structures.
62
Monday, October 31, 11