Download - UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2003
UMass Lowell Computer Science 91.404
Analysis of Algorithms Prof. Karen Daniels
Fall, 2003
UMass Lowell Computer Science 91.404
Analysis of Algorithms Prof. Karen Daniels
Fall, 2003
Chapter 22: Graph AlgorithmsChapter 22: Graph AlgorithmsChapter 23: Minimum Spanning TreesChapter 23: Minimum Spanning Trees
Chapter 24: Shortest PathsChapter 24: Shortest Paths
[Source: Cormen et al. textbook except where noted][Source: Cormen et al. textbook except where noted]
Overview: Graph AlgorithmsOverview: Graph Algorithms
Chapter 22: Elementary Graph AlgorithmsChapter 22: Elementary Graph Algorithms Introductory ConceptsIntroductory Concepts Graph Traversals:Graph Traversals:
Depth-First SearchDepth-First Search Breadth-First SearchBreadth-First Search
Topological SortTopological Sort Chapter 23: Minimum Spanning TreesChapter 23: Minimum Spanning Trees
KruskalKruskal PrimPrim
Chapter 24: Shortest PathsChapter 24: Shortest Paths DijkstraDijkstra
Chapter 22 Graph Algorithms
Chapter 22 Graph Algorithms
Introductory ConceptsIntroductory ConceptsDepth-First SearchDepth-First Search Breadth-First SearchBreadth-First Search
Topological SortTopological Sort
[Source: Cormen et al. textbook except where noted][Source: Cormen et al. textbook except where noted]
Introductory Graph ConceptsIntroductory Graph Concepts
G= (V,E)G= (V,E) Vertex DegreeVertex Degree Self-LoopsSelf-Loops
B
E
C
FD
A B
E
C
FD
A
Directed Graph (digraph)Directed Graph (digraph) Degree: in/outDegree: in/out Self-Loops allowedSelf-Loops allowed
Undirected GraphUndirected Graph No Self-LoopsNo Self-Loops Adjacency is symmetricAdjacency is symmetric
This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.
Introductory Graph Concepts:RepresentationsIntroductory Graph Concepts:Representations
B
E
C
FD
A B
E
C
FD
A
Undirected GraphUndirected Graph Directed Graph (digraph)Directed Graph (digraph)
0 1 1 0 0 0
0 0 1 0 1 1
0 0 0 0 0 0
0 0 0 1 0 0
0 1 0 1 0 0
0 0 0 0 1 0
A B C D E FA B C D E FA A B B C C D D E E FF
0 1 1 0 0 0
1 0 1 0 1 1
1 1 0 0 0 0
0 0 0 0 1 0
0 1 0 1 0 1
0 1 0 0 1 0
A B C D E FA B C D E FA A B B C C D D E E FF
A BC A BC B ACEFB ACEFC ABC ABD ED EE BDFE BDFF BEF BE
A BC A BC B CEFB CEFC C D DD DE BDE BDF EF E
Adjacency Adjacency MatrixMatrix
Adjacency ListAdjacency List Adjacency Adjacency MatrixMatrix
Adjacency ListAdjacency ListThis treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.
Introductory Graph Concepts:Paths, CyclesIntroductory Graph Concepts:Paths, Cycles
Path:Path: length: number of edgeslength: number of edges simple: all vertices distinctsimple: all vertices distinct
Cycle: Cycle: Directed Graph: Directed Graph:
<v<v00,v,v11,...,v,...,vkk > forms cycle if v > forms cycle if v00=v=vkk and and k>=1k>=1
simple cycle: vsimple cycle: v11,v,v22..,v..,vk k also distinctalso distinct self-loop is cycle of length 1self-loop is cycle of length 1
Undirected Graph: Undirected Graph: <v<v00,v,v11,...,v,...,vkk > forms (simple) cycle if > forms (simple) cycle if
vv00=v=vkk and k>=3 and k>=3 simple cycle: vsimple cycle: v11,v,v22..,v..,vk k also distinctalso distinct
B
E
C
FD
A path <A,B,F>path <A,B,F>
B
E
C
FD
A simple cycle simple cycle <E,B,F,E><E,B,F,E>
This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.
B
E
C
FD
Asimple cycle <A,B,C,A>= <B,C,A,B>simple cycle <A,B,C,A>= <B,C,A,B>
Introductory Graph Concepts:ConnectivityIntroductory Graph Concepts:Connectivity
Undirected GraphUndirected Graph: : connectedconnected every pair of vertices is connected by a every pair of vertices is connected by a
pathpath one one connected componentconnected component connected components:connected components:
equivalence classes under “is reachable from” equivalence classes under “is reachable from” relation relation
Directed GraphDirected Graph: : strongly connectedstrongly connected every pair of vertices is reachable from every pair of vertices is reachable from
each othereach other one one stronglystrongly connected componentconnected component strongly connected components:strongly connected components:
equivalence classes under “mutually equivalence classes under “mutually reachable” relation reachable” relation
B
E
C
FD
A
B
E
C
FD
A
connectedconnected
2 connected 2 connected componentscomponents
not not strongly strongly connectedconnected
strongly strongly connected connected componentcomponent
B
E
C
FD
A
B
E
C
FD
A
This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.
Depth-First Search (DFS) &
Breadth-First Search (BFS)
Depth-First Search (DFS) &
Breadth-First Search (BFS)
ExamplesExamplesVertex Color ChangesVertex Color Changes
Edge ClassificationEdge ClassificationUsing the Results of DFS & BFS Using the Results of DFS & BFS
Running Time AnalysisRunning Time Analysis
Depth-First Search (DFS) Depth-First Search (DFS)
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
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.
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
AA
BB
CC
DD
EEFF
GG
TT
TTBBTT
BB
BB TT
TTBBTT
BB
DFS TreeDFS Tree
AA
CC
DD
EE
FF
GG
BB
BB
TT
TT
TT
TT
TT
TT
BB
BB
BB
BB
Elementary Graph Algorithms:DFSElementary Graph Algorithms:DFS
Review problem: TRUE or FALSE?Review problem: TRUE or FALSE? The tree shown below on the right can be a DFS tree for some The tree shown below on the right can be a DFS tree for some
adjacency list representation of the graph shown below on the adjacency list representation of the graph shown below on the
left.left. B
E
C
F
D
A
A
C
B
E
D
F
Tree Edge
Tree Edge
Tree Edge
Tree Edge
Tree Edge
Cross Edge
Back Edge
Breadth-First Search (BFS)Breadth-First Search (BFS)
BFS PseudoCodeBFS PseudoCode
See handout on webSee handout on web
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
Depth-First Search (DFS) &
Breadth-First Search (BFS)
Depth-First Search (DFS) &
Breadth-First Search (BFS)
Using the Results of DFS & BFSUsing the Results of DFS & BFSRunning Time AnalysisRunning Time Analysis
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. 550 of text for proofsee p. 550 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 defined appropriately.notion of cycle is defined appropriately.
see p. 534-537 of text for proofsee p. 534-537 of text for proof
Elementary Graph Algorithms:SEARCHINGSEARCHING: DFS, BFSElementary Graph Algorithms:SEARCHINGSEARCHING: DFS, BFS
Breadth-First-Search (BFS):Breadth-First-Search (BFS): Shortest Path DistanceShortest Path Distance
From From sourcesource to each reachable vertex to each reachable vertex Record during traversalRecord during traversal
Foundation of many “Foundation of many “shortest pathshortest path” ” algorithmsalgorithms
See DFS, BFS Handout for PseudoCodeSee DFS, BFS Handout for PseudoCode
Depth-First-Search (DFS):Depth-First-Search (DFS): Encountering, finishing times Encountering, finishing times
“ “well-formed” nested (( )( ) ) structurewell-formed” nested (( )( ) ) structure DFS of undirected graph produces only back DFS of undirected graph produces only back
edges or tree edgesedges or tree edges Directed graph is acyclic if and only if DFS Directed graph is acyclic if and only if DFS
yields no back edges yields no back edges
for for unweighted unweighted directeddirected or or undirectedundirected graph G=(V,E) graph G=(V,E)TimeTime: O(|V| + |E|) adj list: O(|V| + |E|) adj list O(|V|O(|V|22) adj matrix) adj matrix
predecessor subgraph = forest of spanning treespredecessor subgraph = forest of spanning trees
Vertex color shows status:Vertex color shows status:not yet encounterednot yet encountered
encountered, but not yet finishedencountered, but not yet finished
finishedfinished
Running Time AnalysisRunning Time Analysis
Key ideas in the analysis are similar for DFS and BFS. In Key ideas in the analysis are similar for DFS and BFS. In both cases both cases we assume an Adjacency List representationwe assume an Adjacency List representation. . Let’s examine DFS.Let’s examine 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 each such call constructs a DFS tree by visiting
(recursively) every node reachable from vertex u(recursively) every 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 a node of a non-degenerate DFS tree|AdjList[u]| for each vertex u that is a node 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(|V|vertex. This would make the running time O(|V|22) instead of O(|V|+|E|).) instead of O(|V|+|E|).
Topological SortTopological Sort
Source: Previous 91.404 instructorsSource: Previous 91.404 instructors
Definition: DAGDefinition: DAG
A A Directed Acyclic Graph often abbreviated DAGDirected Acyclic Graph often abbreviated DAG DAGs used in many applications to indicate DAGs used in many applications to indicate
precedence among events. precedence among events. If DFS of a directed graph yields no back edges, If DFS of a directed graph yields no back edges,
then the graph contains no cycles then the graph contains no cycles [Lemma 22.11 in text][Lemma 22.11 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 linear ordering of all its vertices such that if G contains an edge (u, v), then u appears G contains an edge (u, v), then u appears before v in the ordering. before v in the ordering. If the graph is not acyclic, then no linear ordering is If the graph is not acyclic, then no linear ordering is
possible. possible. A topological sort of a graph can be viewed as an A topological sort of a graph can be viewed as an
ordering of its vertices along a horizontal line so that all ordering of its vertices along a horizontal line so that all directed edges go from left to right. directed edges go from left to right.
Topological sorting is thus different from the usual kind Topological sorting is thus different from the usual kind of "sorting" .of "sorting" .
Elementary Graph Algorithms:Topological SortElementary Graph Algorithms:Topological Sort
source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.
TOPOLOGICAL-SORT(G)TOPOLOGICAL-SORT(G)1 DFS(G) computes “finishing times” for each vertex1 DFS(G) computes “finishing times” for each vertex2 as each vertex is finished, insert it onto front of list2 as each vertex is finished, insert it onto front of list3 return list3 return list
for Directed, Acyclic Graph (DAG) G=(V,E)
Produces linear ordering of vertices.Produces linear ordering of vertices.For edge (u,v), u is ordered before v.For edge (u,v), u is ordered before v.
Topological SortTopological Sort
The following algorithm topologically sorts The following algorithm topologically sorts a DAG: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 each vertex v (this is equal to the order in which vertices change color from gray to black)which 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
ExampleExample
For this DAG:For this DAG:
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
Topological SortTopological Sort
We can perform a topological sort in time We can perform a topological sort in time (V + E), since depth-first search takes (V + E), since depth-first search takes (V + E) time and it takes 0(1) time to (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.
Topological SortTopological 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 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 that 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) G 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 explored by 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 gray, since then v would be an ancestor of u and (u,v) would be a back edge, contradicting Lemma 22.11. Therefore, v must be a back edge, contradicting Lemma 22.11. Therefore, v must be either white or black. If v is white, it becomes a descendant of either white or 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, u, and so f[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 for any edge (u,v) in the dag, we have f[v] < f[u], proving the theorem.theorem.
Chapter 23Minimum Spanning Trees
Chapter 23Minimum Spanning Trees
Kruskal Kruskal PrimPrim
[Source: Cormen et al. textbook except where noted][Source: Cormen et al. textbook except where noted]
Minimum Spanning Tree:Greedy Algorithms Minimum Spanning Tree:Greedy Algorithms
A B
CD
E
F
G
2
2
1 1
3
4
4
5 6
68
7
source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.
for Undirected, Connected, Weighted Graph
G=(V,E)
Produces minimum weight tree of Produces minimum weight tree of edges that includes every vertex.edges that includes every vertex.
Invariant: Minimum weight spanning forest
Becomes single tree at end
Invariant: Minimum weight tree
Spans all vertices at end
TimeTime: O(|E|: O(|E|lg|E|) given lg|E|) given fast FIND-fast FIND-SET, SET, UNIONUNION
TimeTime: O(|E|: O(|E|lg|V|) = O(|lg|V|) = O(|E|lg|E|) E|lg|E|) slightly slightly faster with faster with fast priority fast priority queuequeue
Minimum Spanning TreesMinimum Spanning Trees
Review problem:Review problem: For the undirected, weighted graph below, show 2 different For the undirected, weighted graph below, show 2 different
Minimum Spanning Trees. Draw each using one of the 2 graph Minimum Spanning Trees. Draw each using one of the 2 graph copies below. Thicken an edge to make it part of a spanning copies below. Thicken an edge to make it part of a spanning tree. What is the sum of the edge weights for each of your tree. What is the sum of the edge weights for each of your Minimum Spanning Trees?Minimum Spanning Trees?
A B
CD
E
F
G
2
2
1 1
3
4
4
5 6
68
7
Chapter 24Shortest Paths
Chapter 24Shortest Paths
DijkstraDijkstra
[Source: Cormen et al. textbook except where noted][Source: Cormen et al. textbook except where noted]
BFS as a Basis for Shortest Path AlgorithmsBFS as a Basis for Shortest Path Algorithms
Source/Sink Source/Sink Shortest Path Shortest Path
ProblemProblem: Given 2 vertices : Given 2 vertices u, v, find the shortest path u, v, find the shortest path in G from u to v.in G from u to v.
SolutionSolution: BFS : BFS starting at u. starting at u. Stop at v.Stop at v.
Single-Source Single-Source Shortest Paths Shortest Paths
ProblemProblem: Given a vertex : Given a vertex u, find the shortest path in u, find the shortest path in G from u to each vertex.G from u to each vertex.
SolutionSolution: BFS : BFS starting at u. Full starting at u. Full BFS tree.BFS tree.
source: based on Sedgewick, Graph Algorithmssource: based on Sedgewick, Graph Algorithms
BFSBFSfor unweighted, for unweighted, undirected graph G=(V,E)undirected graph G=(V,E)
TimeTime: O(|V| + |E|) adj list: O(|V| + |E|) adj list
O(|V|O(|V|22) adj matrix) adj matrix
TimeTime: O(|V|(|V| + |E|)) adj list: O(|V|(|V| + |E|)) adj list
O(|V|O(|V|33) adj matrix) adj matrix
All-Pairs All-Pairs Shortest Paths Shortest Paths
ProblemProblem: Find the shortest : Find the shortest path in G from each vertex u path in G from each vertex u to each vertex v.to each vertex v.
SolutionSolution: : For each uFor each u: : BFS starting at u; full BFS starting at u; full BFS tree.BFS tree.
Shortest Path ApplicationsShortest Path Applications
Road mapsRoad maps Airline routesAirline routes Telecommunications network routingTelecommunications network routing VLSI design routingVLSI design routing
Weight ~ Cost ~ DistanceWeight ~ Cost ~ Distance
source: based on Sedgewick, Graph Algorithmssource: based on Sedgewick, Graph Algorithms
for for weighted,weighted, directed directed graph G=(V,E)graph G=(V,E)
Shortest Path Trees
source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms
Shortest Path Tree gives shortest path from root to each other vertexShortest Path Tree gives shortest path from root to each other vertex
Shortest Path Principles: RelaxationShortest Path Principles: Relaxation
““Relax” a constraint to try to improve solutionRelax” a constraint to try to improve solution ““Rubber band” analogy Rubber band” analogy [Sedgewick][Sedgewick]
Relaxation of an Edge (u,v):Relaxation of an Edge (u,v): test if shortest path to v test if shortest path to v [found so far][found so far] can be improved can be improved
by going through uby going through u A B
C D
E
F
G
2
2
1 1
3
4
4
5 6
68
7
Single Source Shortest Paths Dijkstra’s AlgorithmSingle Source Shortest Paths Dijkstra’s Algorithm
source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.
for for (nonnegative)(nonnegative) weighted,weighted, directed directed graph G=(V,E)graph G=(V,E)
Single Source Shortest Paths Dijkstra’s AlgorithmSingle Source Shortest Paths Dijkstra’s Algorithm
See separate ShortestPath 91.404 slide See separate ShortestPath 91.404 slide showshow
A B
C D
E
F
G
2
2
1 1
3
4
4
5 6
68
7
source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.
for for (nonnegative)(nonnegative) weighted,weighted, directed directed graph G=(V,E)graph G=(V,E)
Single Source Shortest Paths Dijkstra’s AlgorithmSingle Source Shortest Paths Dijkstra’s Algorithm
Review problem:Review problem: For the directed, weighted graph below, find the shortest
path that begins at vertex A and ends at vertex F. List the vertices in the order that they appear on that path. What is the sum of the edge weights of that path?
A B
C D
E
F
G
2
2
1 1
3
4
4
5 6
68
7
Why can’t Dijkstra’s algorithm handle negative-weight edges?Why can’t Dijkstra’s algorithm handle negative-weight edges?
Single Source Shortest Paths Dijkstra’s AlgorithmSingle Source Shortest Paths Dijkstra’s Algorithm
source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms
for for (nonnegative)(nonnegative) weighted,weighted, directed directed graph G=(V,E)graph G=(V,E)
Shortest Path AlgorithmsShortest Path Algorithms
source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms