Download - UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001
![Page 1: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/1.jpg)
UMass Lowell Computer Science 91.404
Analysis of Algorithms Prof. Karen Daniels
Fall, 2001
UMass Lowell Computer Science 91.404
Analysis of Algorithms Prof. Karen Daniels
Fall, 2001
Chapter 23: Graph AlgorithmsChapter 23: Graph AlgorithmsChapter 24: Minimum Spanning TreesChapter 24: Minimum Spanning Trees
Chapter 25: Shortest PathsChapter 25: Shortest Paths
[Source: Cormen et al. textbook except where noted][Source: Cormen et al. textbook except where noted]
![Page 2: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/2.jpg)
Overview: Graph AlgorithmsOverview: Graph Algorithms
Chapter 23: Elementary Graph AlgorithmsChapter 23: Elementary Graph Algorithms Introductory ConceptsIntroductory Concepts Graph Traversals:Graph Traversals:
Depth-First SearchDepth-First Search Breadth-First SearchBreadth-First Search
Topological SortTopological Sort Chapter 24: Minimum Spanning TreesChapter 24: Minimum Spanning Trees
KruskalKruskal PrimPrim
Chapter 25: Shortest PathsChapter 25: Shortest Paths DijkstraDijkstra
![Page 3: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/3.jpg)
Chapter 23 Graph Algorithms
Chapter 23 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]
![Page 4: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/4.jpg)
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.
![Page 5: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/5.jpg)
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.
![Page 6: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/6.jpg)
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>
![Page 7: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/7.jpg)
Introductory Graph Concepts:ConnectivityIntroductory Graph Concepts:Connectivity
Undirected GraphUndirected Graph: : connectedconnected every pair of vertices is connected by a pathevery pair of vertices is connected by a path 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 each every pair of vertices is reachable from each
otherother one one stronglystrongly connected componentconnected component strongly connected components:strongly connected components:
equivalence classes under “mutually reachable” equivalence classes under “mutually reachable” relation 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.
![Page 8: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/8.jpg)
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
![Page 9: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/9.jpg)
Depth-First Search (DFS) Depth-First Search (DFS)
![Page 10: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/10.jpg)
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
![Page 11: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/11.jpg)
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.
![Page 12: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/12.jpg)
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.
![Page 13: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/13.jpg)
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
![Page 14: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/14.jpg)
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
![Page 15: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/15.jpg)
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
![Page 16: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/16.jpg)
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
![Page 17: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/17.jpg)
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
![Page 18: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/18.jpg)
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
![Page 19: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/19.jpg)
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
![Page 20: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/20.jpg)
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
![Page 21: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/21.jpg)
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
![Page 22: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/22.jpg)
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
![Page 23: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/23.jpg)
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
![Page 24: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/24.jpg)
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
![Page 25: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/25.jpg)
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
![Page 26: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/26.jpg)
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
![Page 27: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/27.jpg)
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
![Page 28: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/28.jpg)
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
![Page 29: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/29.jpg)
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
![Page 30: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/30.jpg)
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
![Page 31: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/31.jpg)
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
![Page 32: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/32.jpg)
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
![Page 33: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/33.jpg)
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
![Page 34: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/34.jpg)
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
![Page 35: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/35.jpg)
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
![Page 36: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/36.jpg)
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
![Page 37: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/37.jpg)
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
![Page 38: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/38.jpg)
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
![Page 39: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/39.jpg)
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
![Page 40: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/40.jpg)
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
![Page 41: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/41.jpg)
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
![Page 42: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/42.jpg)
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
![Page 43: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/43.jpg)
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
![Page 44: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/44.jpg)
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
![Page 45: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/45.jpg)
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
![Page 46: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/46.jpg)
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
![Page 47: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/47.jpg)
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
![Page 48: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/48.jpg)
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
![Page 49: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/49.jpg)
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
![Page 50: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/50.jpg)
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
![Page 51: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/51.jpg)
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
![Page 52: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/52.jpg)
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
![Page 53: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/53.jpg)
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
![Page 54: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/54.jpg)
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
![Page 55: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/55.jpg)
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
![Page 56: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/56.jpg)
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
![Page 57: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/57.jpg)
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
![Page 58: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/58.jpg)
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
![Page 59: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/59.jpg)
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
![Page 60: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/60.jpg)
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
![Page 61: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/61.jpg)
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
![Page 62: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/62.jpg)
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
![Page 63: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/63.jpg)
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
![Page 64: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/64.jpg)
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
![Page 65: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/65.jpg)
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
![Page 66: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/66.jpg)
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
![Page 67: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/67.jpg)
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
![Page 68: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/68.jpg)
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
![Page 69: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/69.jpg)
Breadth-First Search (BFS)Breadth-First Search (BFS)
![Page 70: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/70.jpg)
BFS PseudoCodeBFS PseudoCode
See handout on webSee handout on web
![Page 71: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/71.jpg)
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
![Page 72: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/72.jpg)
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
![Page 73: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/73.jpg)
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
![Page 74: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/74.jpg)
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
![Page 75: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/75.jpg)
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
![Page 76: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/76.jpg)
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
![Page 77: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/77.jpg)
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
![Page 78: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/78.jpg)
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
![Page 79: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/79.jpg)
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
![Page 80: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/80.jpg)
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
![Page 81: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/81.jpg)
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
![Page 82: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/82.jpg)
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
![Page 83: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/83.jpg)
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
![Page 84: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/84.jpg)
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
![Page 85: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/85.jpg)
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
![Page 86: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/86.jpg)
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
![Page 87: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/87.jpg)
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
![Page 88: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/88.jpg)
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
![Page 89: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/89.jpg)
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
![Page 90: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/90.jpg)
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
![Page 91: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/91.jpg)
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
![Page 92: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/92.jpg)
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
![Page 93: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/93.jpg)
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
![Page 94: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/94.jpg)
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
![Page 95: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/95.jpg)
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
![Page 96: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/96.jpg)
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
![Page 97: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/97.jpg)
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
![Page 98: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/98.jpg)
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
![Page 99: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/99.jpg)
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
![Page 100: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/100.jpg)
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
![Page 101: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/101.jpg)
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
![Page 102: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/102.jpg)
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
![Page 103: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/103.jpg)
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
![Page 104: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/104.jpg)
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 defined appropriately.notion of cycle is defined appropriately.
see p. 472-475 of text for proofsee p. 472-475 of text for proof
![Page 105: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/105.jpg)
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
![Page 106: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/106.jpg)
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...
![Page 107: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/107.jpg)
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(|V|vertex. This would make the running time O(|V|22) instead of O(|V|+|E|).) instead of O(|V|+|E|).
![Page 108: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/108.jpg)
Topological SortTopological Sort
Source: Previous 91.404 instructorsSource: Previous 91.404 instructors
![Page 109: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/109.jpg)
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
precedences among events. precedences 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 23.10 in text][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.
![Page 110: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/110.jpg)
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" .
![Page 111: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/111.jpg)
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.
See also 91.404 DFS/BFS slide showSee also 91.404 DFS/BFS slide show
![Page 112: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/112.jpg)
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
![Page 113: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/113.jpg)
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
![Page 114: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/114.jpg)
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
![Page 115: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/115.jpg)
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.
![Page 116: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/116.jpg)
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 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.
![Page 117: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/117.jpg)
Chapter 24Minimum Spanning Trees
Chapter 24Minimum Spanning Trees
Kruskal Kruskal PrimPrim
[Source: Cormen et al. textbook except where noted][Source: Cormen et al. textbook except where noted]
![Page 118: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/118.jpg)
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
![Page 119: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/119.jpg)
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
![Page 120: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/120.jpg)
Chapter 25Shortest Paths
Chapter 25Shortest Paths
DijkstraDijkstra
[Source: Cormen et al. textbook except where noted][Source: Cormen et al. textbook except where noted]
![Page 121: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/121.jpg)
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 for unweighted,unweighted, undirected undirected 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
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.
![Page 122: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/122.jpg)
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)
![Page 123: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/123.jpg)
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
![Page 124: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/124.jpg)
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
![Page 125: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/125.jpg)
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)
![Page 126: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/126.jpg)
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)
![Page 127: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/127.jpg)
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?
![Page 128: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/128.jpg)
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)
![Page 129: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001](https://reader035.vdocuments.net/reader035/viewer/2022062322/568150be550346895dbedd2e/html5/thumbnails/129.jpg)
Shortest Path AlgorithmsShortest Path Algorithms
source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms