Post on 24-Jan-2016
description
UMass Lowell Computer Science 91.404
Analysis of Algorithms Prof. Karen Daniels
Spring, 2001
UMass Lowell Computer Science 91.404
Analysis of Algorithms Prof. Karen Daniels
Spring, 2001
Makeup LectureMakeup LectureChapter 23: Graph AlgorithmsChapter 23: Graph Algorithms
Depth-First SearchDepth-First Search Breadth-First SearchBreadth-First SearchTopological SortTopological Sort
Tuesday, 5/8/01Tuesday, 5/8/01
[Source: Cormen et al. textbook except where noted][Source: Cormen et al. textbook except where noted]
Depth-First Search (DFS) &
Breadth-First Search (BFS)
Depth-First Search (DFS) &
Breadth-First Search (BFS)
Running Time AnalysisRunning Time AnalysisVertex Color ChangesVertex Color Changes
Edge ClassificationEdge ClassificationUsing the Results of DFS & BFS Using the Results of DFS & BFS
ExamplesExamples
Running Time AnalysisRunning Time Analysis
Key ideas in the analysis are similar for DFS and BFS. In both Key ideas in the analysis are similar for DFS and BFS. In both cases cases we assume an Adjacency List representationwe assume an Adjacency List representation. Let’s examine . Let’s examine DFS.DFS.
Let Let tt be number of DFS trees generated by DFS search be number of DFS trees generated by DFS search Outer loop in DFS(G) executes Outer loop in DFS(G) executes tt times times
each execution contains call: DFS_Visit(G,u)each execution contains call: DFS_Visit(G,u) each such call constructs a DFS tree by visiting (recursively) each such call constructs a DFS tree by visiting (recursively)
every node reachable from vertex uevery node reachable from vertex u Time:Time:
Now, let rNow, let rii be the number of vertices in DFS tree i be the number of vertices in DFS tree i
Time to construct DFS tree i:Time to construct DFS tree i:
t
i
itreeDFSconstructtotime1
ir
j
itreeDFSinvertexjthAdjList1
][
continued on next slide...continued on next slide...
Running Time AnalysisRunning Time Analysis Total DFS time:Total DFS time: Now, consider this expression for the extreme values of Now, consider this expression for the extreme values of tt::
if t=1, all edges are in one DFS tree and the expression simplifies to O(E)if t=1, all edges are in one DFS tree and the expression simplifies to O(E) if t=|V|, each vertex is its own (degenerate) DFS tree with no edges so the if t=|V|, each vertex is its own (degenerate) DFS tree with no edges so the
expression simplifies to O(V)expression simplifies to O(V) O(V+E) is therefore an upper bound on the time for the extreme casesO(V+E) is therefore an upper bound on the time for the extreme cases
For values of t in between 1 and |V| we have these contributions to For values of t in between 1 and |V| we have these contributions to running time:running time:
1 for each vertex that is its own (degenerate) DFS tree with no edges 1 for each vertex that is its own (degenerate) DFS tree with no edges upper bound on this total is O(V)upper bound on this total is O(V)
|AdjList[u]| for each vertex u that is the root of a non-degenerate DFS tree|AdjList[u]| for each vertex u that is the root of a non-degenerate DFS tree upper bound on this total is O(E)upper bound on this total is O(E)
Total time for values of t in between 1 and |V| is therefore also O(V+E)Total time for values of t in between 1 and |V| is therefore also O(V+E)
t
i
r
j
i
itreeDFSinvertexjthAdjList1
1
][
)( EVO Total time=Total time=
Note that for an Note that for an Adjacency MatrixAdjacency Matrix representation, we would need to scan an entire representation, we would need to scan an entire matrix row (containing |V| entries) each time we examined the vertices adjacent to a matrix row (containing |V| entries) each time we examined the vertices adjacent to a vertex. This would make the running time O(Vvertex. This would make the running time O(V22) instead of O(V+E).) instead of O(V+E).
Vertex Color ChangesVertex Color Changes
Vertex is Vertex is WHITEWHITE if it has not yet been if it has not yet been encountered during the search.encountered during the search.
Vertex is Vertex is GRAYGRAY if it has been encountered if it has been encountered but has not yet been fully explored.but has not yet been fully explored.
Vertex is Vertex is BLACK if it has been fully if it has been fully explored.explored.
Edge ClassificationEdge Classification
Each edge of the original graph G is classified during the searchEach edge of the original graph G is classified during the search produces information needed to:produces information needed to:
build DFS or BFS spanning forest of treesbuild DFS or BFS spanning forest of trees detect cycles (DFS) or find shortest paths (BFS)detect cycles (DFS) or find shortest paths (BFS)
When vertex u is being explored, edge e = (u,v) is classified based on When vertex u is being explored, edge e = (u,v) is classified based on the color of v when the edge is the color of v when the edge is first exploredfirst explored:: e is a e is a tree edgetree edge if v is if v is WHITEWHITE [for DFS and BFS] [for DFS and BFS] e is a e is a back edgeback edge if v is if v is GRAYGRAY [for DFS only] [for DFS only]
for DFS this means v is an ancestor of u in the DFS treefor DFS this means v is an ancestor of u in the DFS tree e is a e is a forward edgeforward edge if v is if v is BLACK and [for DFS only] v is a descendent of u in and [for DFS only] v is a descendent of u in
the DFS treethe DFS tree e is a e is a cross edgecross edge if v is if v is BLACK and [for DFS only] there is no ancestor or and [for DFS only] there is no ancestor or
descendent relationship between u and v in the DFS treedescendent relationship between u and v in the DFS tree Note that:Note that:
For BFS we’ll only consider tree edges. For BFS we’ll only consider tree edges. For DFS we consider all 4 edge types.For DFS we consider all 4 edge types. In DFS of an undirected graph, every edge is either a tree edge or a back edge.In DFS of an undirected graph, every edge is either a tree edge or a back edge.
Using the Results of DFS & BFSUsing the Results of DFS & BFS
A directed graph G is acyclic if and only if a A directed graph G is acyclic if and only if a Depth-First Search of G yields no back edges.Depth-First Search of G yields no back edges.
Using DFS to Detect Cycles:Using DFS to Detect Cycles:
Using BFS for Shortest Paths:Using BFS for Shortest Paths:
A Breadth-First Search of G yields shortest path information: A Breadth-First Search of G yields shortest path information:
For each Breadth-First Search tree, the path from its For each Breadth-First Search tree, the path from its root u to a vertex v yields the shortest path from u to v in G.root u to a vertex v yields the shortest path from u to v in G.
see p. 486 of text for proofsee p. 486 of text for proof
Note: DFS can also be used to detect cycles in undirected graphs if Note: DFS can also be used to detect cycles in undirected graphs if notion of cycle is refined appropriately.notion of cycle is refined appropriately.
see p. 472-475 of text for proofsee p. 472-475 of text for proof
Example: DFS of Directed GraphExample: DFS of Directed Graph
SourceSource: Graph is from : Graph is from Computer Computer Algorithms: Introduction to Design and Algorithms: Introduction to Design and AnalysisAnalysis by Baase and Gelder. by Baase and Gelder.
AA
BB
CC
DD
EEFF
GG
G=(V,E)G=(V,E)Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,EEdge Classification LegendEdge Classification Legend::
T: tree edge T: tree edge
B: back edgeB: back edge
F: forward edgeF: forward edge
C: cross edgeC: cross edge
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CC
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CC
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CC
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFF
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTT
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTT
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTTBB
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTTBB
CC
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTTBB
CC
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTTBB
CC
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTTBB
CC
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTTBB
CC CC
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTTBB
CC CC
TT
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTTBB
CC CC
TT
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTTBB
CC CC
TT
CC
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTTBB
CC CC
TT
CC
BB
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTTBB
CC CC
TT
CC
BB
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
TT
BB
CCFFTTBB
CC CC
TT
CC
BB
Example: (continued)
DFS of Directed GraphExample: (continued)
DFS of Directed Graph
AA
DD
GGBB
TT
TT
BB
CC
TT
FFBBFF
CCCC
CC CC
EE
TTBB
AA
BB
CC
DD
EEFF
GG
TT
TT
TT
BB
CCFFTTBB
CC CC
TT
CC
BBTT
DFS Tree 1DFS Tree 1
DFS DFS
Tree 2Tree 2
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
G=(V,E)G=(V,E)Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBB
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TT
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TT
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBB
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBB
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBB
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBB
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBBTT
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBBTT
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBBTT
BB
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBBTT
BB
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBBTT
BB
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBBTT
BB
Example: DFS of Undirected GraphExample: DFS of Undirected Graph
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
AA
BB
CC
DD
EEFF
GG
TT
TTBBTT
BB
BB TT
TTBBTT
BB
Example: (continued)
DFS of Undirected GraphExample: (continued)
DFS of Undirected Graph
DFS TreeDFS Tree
AA
BB
CC
DD
EEFF
GG
TT
TTBBTT
BB
BB TT
TTBBTT
BB
AA
CC
DD
EE
FF
GG
BB
BB
TT
TT
TT
TT
TT
TT
BB
BB
BB
BB
Example: BFS of Directed GraphExample: BFS of Directed Graph
SourceSource: Graph is from : Graph is from Computer Computer Algorithms: Introduction to Design and Algorithms: Introduction to Design and AnalysisAnalysis by Baase and Gelder. by Baase and Gelder.
AA
BB
CC
DD
EEFF
GG
G=(V,E)G=(V,E)Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,EEdge Classification LegendEdge Classification Legend::
T: tree edge T: tree edge
only tree edges are usedonly tree edges are used
Example: BFS of Directed GraphExample: BFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: AQueue: A
Example: BFS of Directed GraphExample: BFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: ABQueue: AB
TT
Example: BFS of Directed GraphExample: BFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: ABCQueue: ABC
TT
TT
Example: BFS of Directed GraphExample: BFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: ABCFQueue: ABCF
TT
TTTT
Example: BFS of Directed GraphExample: BFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: BCFQueue: BCF
TT
TTTT
Example: BFS of Directed GraphExample: BFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: BCFDQueue: BCFD
TT
TTTT
TT
Example: BFS of Directed GraphExample: BFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: CFDQueue: CFD
TT
TTTT
TT
Example: BFS of Directed GraphExample: BFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: FDQueue: FD
TT
TTTT
TT
Example: BFS of Directed GraphExample: BFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: DQueue: D
TT
TTTT
TT
Example: BFS of Directed GraphExample: BFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: -Queue: -
TT
TTTT
TT
Example: BFS of Directed GraphExample: BFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: EQueue: E
TT
TTTT
TT
Example: BFS of Directed GraphExample: BFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: EGQueue: EG
TT
TTTT
TT
TT
Example: BFS of Directed GraphExample: BFS of Directed Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: GQueue: G
TT
TTTT
TT
TT
Example: BFS of Directed GraphExample: BFS of Directed Graph
Adjacency ListAdjacency List::A: B,C,FA: B,C,FB: C,DB: C,DC: -C: -D: A,CD: A,CE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: -Queue: -
AA
BB
CC
DD
EEFF
GG
TT
TTTT
TT
TT
Example: (continued)
BFS of Directed GraphExample: (continued)
BFS of Directed Graph
GG
EE
TT
AA
DD
BB
TTTT TT
FFCCTT
BFS Tree 1BFS Tree 1
BFS BFS
Tree 2Tree 2
AA
BB
CC
DD
EEFF
GG
TT
TTTT
TT
TTShortest path Shortest path
distance from :distance from :
A to B = 1A to B = 1
A to C = 1A to C = 1
A to F = 1A to F = 1
A to D = 2A to D = 2
Shortest path Shortest path
distance from :distance from :
E to G = 1E to G = 1
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
G=(V,E)G=(V,E)Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Edge Classification LegendEdge Classification Legend::
T: tree edge T: tree edge
only tree edges are usedonly tree edges are used
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: AQueue: A
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: ABQueue: AB
TT
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: ABCQueue: ABC
TT
TT
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: ABCDQueue: ABCD
TT
TT
TT
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: ABCDFQueue: ABCDF
TT
TT
TT
TT
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: BCDFQueue: BCDF
TT
TT
TT
TT
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: CDFQueue: CDF
TT
TT
TT
TT
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: CDFEQueue: CDFE
TT
TT
TT
TT
TT
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: DFEQueue: DFE
TT
TT
TT
TT
TT
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: DFEGQueue: DFEG
TT
TT
TT
TT
TT
TT
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: FEGQueue: FEG
TT
TT
TT
TT
TT
TT
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: EGQueue: EG
TT
TT
TT
TT
TT
TT
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: GQueue: G
TT
TT
TT
TT
TT
TT
Example: BFS of Undirected GraphExample: BFS of Undirected Graph
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Queue: -Queue: -
AA
BB
CC
DD
EEFF
GG
TT
TT
TT
TT
TT
TT
Example: (continued)
BFS of Undirected GraphExample: (continued)
BFS of Undirected Graph
BFS Tree BFS Tree
Shortest path distance from :Shortest path distance from :
A to B = 1A to B = 1 A to E = 2 A to E = 2
A to C = 1A to C = 1 A to G = 2A to G = 2
A to D = 1A to D = 1
A to F = 1A to F = 1
AA
BB
CC
DD
EEFF
GG
TT
TT
TT
TT
TT
TT
AA
FFBB
TTTT TT
DDCC
TT
EE GG
TT TT
Topological SortTopological Sort
Source: Previous 91.404 instructorsSource: Previous 91.404 instructors
Definition: DAGDefinition: DAG
A A Directed Acyclic Graph is often abbreviated Directed Acyclic Graph is often abbreviated by DAGby DAG
As noted on slide 7, if DFS of a directed graph As noted on slide 7, if DFS of a directed graph yields no back edges, then the graph contains yields no back edges, then the graph contains no cycles no cycles [this is Lemma 23.10 in text][this is Lemma 23.10 in text]
AA
BB
CC
DD
EEFF
GG
This graph has more than one cycle.This graph has more than one cycle.
Can you find them all?Can you find them all?
AA
BB
CC
DD
EEFF
GG
This graph has no cycles, This graph has no cycles, so it is a DAG.so it is a DAG.
Definition: Topological SortDefinition: Topological Sort
A A topological sorttopological sort of a dag Gof a dag G = (V, E) is a = (V, E) is a linear ordering of all its vertices such that if G linear ordering of all its vertices such that if G contains an edge (u, v), then u appears before v contains an edge (u, v), then u appears before v in the ordering. in the ordering. If the graph is not acyclic, then no linear ordering is possible. If the graph is not acyclic, then no linear ordering is possible. A topological sort of a graph can be viewed as an ordering of A topological sort of a graph can be viewed as an ordering of
its vertices along a horizontal line so that all directed edges its vertices along a horizontal line so that all directed edges go from left to right. go from left to right.
Topological sorting is thus different from the usual kind of Topological sorting is thus different from the usual kind of "sorting" ."sorting" .
Definition: Topological SortDefinition: Topological Sort
Directed acyclic graphs are used in many applications to indicate Directed acyclic graphs are used in many applications to indicate precedences among events. precedences among events.
Figure 23.7 gives an example that arises when Professor Figure 23.7 gives an example that arises when Professor Bumstead gets dressed in the morning. The professor must don Bumstead gets dressed in the morning. The professor must don certain garments before others (e.g., socks before shoes). Other certain garments before others (e.g., socks before shoes). Other items may be put on in any order (e.g., socks and pants). A items may be put on in any order (e.g., socks and pants). A directed edge (u,v) in the dag of Figure 23.7(a) indicates that directed edge (u,v) in the dag of Figure 23.7(a) indicates that garment u must be donned before garment v. A topological sort garment u must be donned before garment v. A topological sort of this dag therefore gives an order for getting dressed. Figure of this dag therefore gives an order for getting dressed. Figure 23.7(b) shows the topologically sorted dag as an ordering of 23.7(b) shows the topologically sorted dag as an ordering of vertices along a horizontal line such that all directed edges go vertices along a horizontal line such that all directed edges go from left to right.from left to right.
Definition: Topological SortDefinition: Topological Sort
The following simple algorithm topologically The following simple algorithm topologically sorts a dag.sorts a dag.
TOPOLOGICAL-SORT(G)TOPOLOGICAL-SORT(G) call DFS(G) to compute finishing times f[v] for call DFS(G) to compute finishing times f[v] for
each vertex v (this is equal to the order in which each vertex v (this is equal to the order in which vertices change color from gray to black)vertices change color from gray to black)
as each vertex is finished (turns black), insert it as each vertex is finished (turns black), insert it onto the front of a linked listonto the front of a linked list
return the linked list of verticesreturn the linked list of vertices
Definition: Topological SortDefinition: Topological Sort
Figure 23.7(b) shows how the topologically Figure 23.7(b) shows how the topologically sorted vertices appear in reverse order of sorted vertices appear in reverse order of their finishing times.their finishing times.
We can perform a topological sort in time We can perform a topological sort in time Q(V + E), since depth-first search takes Q(V + E), since depth-first search takes Q(V + E) time and it takes 0(1) time to Q(V + E) time and it takes 0(1) time to insert each of the |V| vertices onto the front insert each of the |V| vertices onto the front of the linked list.of the linked list.
Definition: Topological SortDefinition: Topological Sort
Theorem 23.11: Theorem 23.11: TOPOLOGICAL-SORT(G) produces a TOPOLOGICAL-SORT(G) produces a topological sort of a directed acyclic graph G.topological sort of a directed acyclic graph G.
Proof: Proof: Suppose that DFS is run on a given dag G = (V, E) to Suppose that DFS is run on a given dag G = (V, E) to determine finishing times for its vertices. It suffices to show that determine finishing times for its vertices. It suffices to show that for any pair of distinct vertices u,v Î V, if there is an edge in G for any pair of distinct vertices u,v Î V, if there is an edge in G from u to v, then f[v] < f[u]. Consider any edge (u,v) explored by from u to v, then f[v] < f[u]. Consider any edge (u,v) explored by DFS(G). When this edge is explored, v cannot be gray, since then DFS(G). When this edge is explored, v cannot be gray, since then v would be an ancestor of u and (u,v) would be a back edge, v would be an ancestor of u and (u,v) would be a back edge, contradicting Lemma 23.10. Therefore, v must be either white or contradicting Lemma 23.10. Therefore, v must be either white or black. If v is white, it becomes a descendant of u, and so f[v] < black. If v is white, it becomes a descendant of u, and so f[v] < f[u]. If v is black, then f[v] < f[u] as well. Thus, for any edge (u,v) f[u]. If v is black, then f[v] < f[u] as well. Thus, for any edge (u,v) in the dag, we have f[v] < f[u], proving the theorem.in the dag, we have f[v] < f[u], proving the theorem.
ExampleExample
For the DAG of slide 97:For the DAG of slide 97:
DFS produces this result:DFS produces this result: this contains 2 DFS treesthis contains 2 DFS trees
Vertices are blackened in the Vertices are blackened in the following order:following order: C, B, F, A, D, E , GC, B, F, A, D, E , G
AA
BB
CC
DD
EEFF
GG
AA
BB
CC
DD
EEFF
GGTT
TT
TT
TT
TT
FF
CC CC
CC
CC
ExampleExample
Vertices are added to Vertices are added to frontfront of a linked list in the of a linked list in the blackening order.blackening order.
Final result is shown belowFinal result is shown below Note that all tree edges and non-tree edges point Note that all tree edges and non-tree edges point
to the right to the right
AA BB CCDDEE FFGG TT
FF
CC TT TT
TTCC
TT CC
CC