umass lowell computer science 91.404 analysis of algorithms prof. karen daniels spring, 2001

104
UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Makeup Lecture Chapter 23: Graph Algorithms Chapter 23: Graph Algorithms Depth-First Search Depth-First Search Breadth-First Breadth-First Search Search Topological Sort Topological Sort Tuesday, 5/8/01 Tuesday, 5/8/01 [Source: Cormen et al. textbook except where noted] [Source: Cormen et al. textbook except where noted]

Upload: peta

Post on 24-Jan-2016

38 views

Category:

Documents


0 download

DESCRIPTION

UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001. Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First Search Topological Sort Tuesday, 5/8/01 [Source: Cormen et al. textbook except where noted]. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

UMass Lowell Computer Science 91.404

Analysis of Algorithms Prof. Karen Daniels

Spring, 2001

UMass Lowell Computer Science 91.404

Analysis of Algorithms Prof. Karen Daniels

Spring, 2001

Makeup LectureMakeup LectureChapter 23: Graph AlgorithmsChapter 23: Graph Algorithms

Depth-First SearchDepth-First Search Breadth-First SearchBreadth-First SearchTopological SortTopological Sort

Tuesday, 5/8/01Tuesday, 5/8/01

[Source: Cormen et al. textbook except where noted][Source: Cormen et al. textbook except where noted]

Page 2: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

Depth-First Search (DFS) &

Breadth-First Search (BFS)

Depth-First Search (DFS) &

Breadth-First Search (BFS)

Running Time AnalysisRunning Time AnalysisVertex Color ChangesVertex Color Changes

Edge ClassificationEdge ClassificationUsing the Results of DFS & BFS Using the Results of DFS & BFS

ExamplesExamples

Page 3: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

Running Time AnalysisRunning Time Analysis

Key ideas in the analysis are similar for DFS and BFS. In both Key ideas in the analysis are similar for DFS and BFS. In both cases cases we assume an Adjacency List representationwe assume an Adjacency List representation. Let’s examine . Let’s examine DFS.DFS.

Let Let tt be number of DFS trees generated by DFS search be number of DFS trees generated by DFS search Outer loop in DFS(G) executes Outer loop in DFS(G) executes tt times times

each execution contains call: DFS_Visit(G,u)each execution contains call: DFS_Visit(G,u) each such call constructs a DFS tree by visiting (recursively) each such call constructs a DFS tree by visiting (recursively)

every node reachable from vertex uevery node reachable from vertex u Time:Time:

Now, let rNow, let rii be the number of vertices in DFS tree i be the number of vertices in DFS tree i

Time to construct DFS tree i:Time to construct DFS tree i:

t

i

itreeDFSconstructtotime1

ir

j

itreeDFSinvertexjthAdjList1

][

continued on next slide...continued on next slide...

Page 4: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

Running Time AnalysisRunning Time Analysis Total DFS time:Total DFS time: Now, consider this expression for the extreme values of Now, consider this expression for the extreme values of tt::

if t=1, all edges are in one DFS tree and the expression simplifies to O(E)if t=1, all edges are in one DFS tree and the expression simplifies to O(E) if t=|V|, each vertex is its own (degenerate) DFS tree with no edges so the if t=|V|, each vertex is its own (degenerate) DFS tree with no edges so the

expression simplifies to O(V)expression simplifies to O(V) O(V+E) is therefore an upper bound on the time for the extreme casesO(V+E) is therefore an upper bound on the time for the extreme cases

For values of t in between 1 and |V| we have these contributions to For values of t in between 1 and |V| we have these contributions to running time:running time:

1 for each vertex that is its own (degenerate) DFS tree with no edges 1 for each vertex that is its own (degenerate) DFS tree with no edges upper bound on this total is O(V)upper bound on this total is O(V)

|AdjList[u]| for each vertex u that is the root of a non-degenerate DFS tree|AdjList[u]| for each vertex u that is the root of a non-degenerate DFS tree upper bound on this total is O(E)upper bound on this total is O(E)

Total time for values of t in between 1 and |V| is therefore also O(V+E)Total time for values of t in between 1 and |V| is therefore also O(V+E)

t

i

r

j

i

itreeDFSinvertexjthAdjList1

1

][

)( EVO Total time=Total time=

Note that for an Note that for an Adjacency MatrixAdjacency Matrix representation, we would need to scan an entire representation, we would need to scan an entire matrix row (containing |V| entries) each time we examined the vertices adjacent to a matrix row (containing |V| entries) each time we examined the vertices adjacent to a vertex. This would make the running time O(Vvertex. This would make the running time O(V22) instead of O(V+E).) instead of O(V+E).

Page 5: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 6: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 7: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

Using the Results of DFS & BFSUsing the Results of DFS & BFS

A directed graph G is acyclic if and only if a A directed graph G is acyclic if and only if a Depth-First Search of G yields no back edges.Depth-First Search of G yields no back edges.

Using DFS to Detect Cycles:Using DFS to Detect Cycles:

Using BFS for Shortest Paths:Using BFS for Shortest Paths:

A Breadth-First Search of G yields shortest path information: A Breadth-First Search of G yields shortest path information:

For each Breadth-First Search tree, the path from its For each Breadth-First Search tree, the path from its root u to a vertex v yields the shortest path from u to v in G.root u to a vertex v yields the shortest path from u to v in G.

see p. 486 of text for proofsee p. 486 of text for proof

Note: DFS can also be used to detect cycles in undirected graphs if Note: DFS can also be used to detect cycles in undirected graphs if notion of cycle is refined appropriately.notion of cycle is refined appropriately.

see p. 472-475 of text for proofsee p. 472-475 of text for proof

Page 8: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 9: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 10: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 11: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 12: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 13: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 14: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 15: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 16: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 17: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 18: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 19: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 20: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 21: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 22: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 23: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 24: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 25: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 26: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 27: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 28: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 29: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 30: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 31: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 32: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 33: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 34: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 35: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 36: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 37: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 38: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 39: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 40: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 41: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 42: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 43: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 44: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 45: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 46: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 47: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 48: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 49: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 50: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 51: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 52: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 53: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 54: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 55: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 56: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 57: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 58: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 59: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 60: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 61: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 62: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 63: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 64: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 65: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 66: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 67: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 68: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 69: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 70: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 71: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 72: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 73: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 74: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 75: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 76: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 77: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 78: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 79: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 80: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 81: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 82: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 83: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 84: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 85: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 86: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 87: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 88: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 89: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 90: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 91: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 92: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 93: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 94: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 95: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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 96: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

Topological SortTopological Sort

Source: Previous 91.404 instructorsSource: Previous 91.404 instructors

Page 97: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

Definition: DAGDefinition: DAG

A A Directed Acyclic Graph is often abbreviated Directed Acyclic Graph is often abbreviated by DAGby DAG

As noted on slide 7, if DFS of a directed graph As noted on slide 7, if DFS of a directed graph yields no back edges, then the graph contains yields no back edges, then the graph contains no cycles no cycles [this is Lemma 23.10 in text][this is Lemma 23.10 in text]

AA

BB

CC

DD

EEFF

GG

This graph has more than one cycle.This graph has more than one cycle.

Can you find them all?Can you find them all?

AA

BB

CC

DD

EEFF

GG

This graph has no cycles, This graph has no cycles, so it is a DAG.so it is a DAG.

Page 98: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

Definition: Topological SortDefinition: Topological Sort

A A topological sorttopological sort of a dag Gof a dag G = (V, E) is a = (V, E) is a linear ordering of all its vertices such that if G linear ordering of all its vertices such that if G contains an edge (u, v), then u appears before v contains an edge (u, v), then u appears before v in the ordering. in the ordering. If the graph is not acyclic, then no linear ordering is possible. If the graph is not acyclic, then no linear ordering is possible. A topological sort of a graph can be viewed as an ordering of A topological sort of a graph can be viewed as an ordering of

its vertices along a horizontal line so that all directed edges its vertices along a horizontal line so that all directed edges go from left to right. go from left to right.

Topological sorting is thus different from the usual kind of Topological sorting is thus different from the usual kind of "sorting" ."sorting" .

Page 99: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

Definition: Topological SortDefinition: Topological Sort

Directed acyclic graphs are used in many applications to indicate Directed acyclic graphs are used in many applications to indicate precedences among events. precedences among events.

Figure 23.7 gives an example that arises when Professor Figure 23.7 gives an example that arises when Professor Bumstead gets dressed in the morning. The professor must don Bumstead gets dressed in the morning. The professor must don certain garments before others (e.g., socks before shoes). Other certain garments before others (e.g., socks before shoes). Other items may be put on in any order (e.g., socks and pants). A items may be put on in any order (e.g., socks and pants). A directed edge (u,v) in the dag of Figure 23.7(a) indicates that directed edge (u,v) in the dag of Figure 23.7(a) indicates that garment u must be donned before garment v. A topological sort garment u must be donned before garment v. A topological sort of this dag therefore gives an order for getting dressed. Figure of this dag therefore gives an order for getting dressed. Figure 23.7(b) shows the topologically sorted dag as an ordering of 23.7(b) shows the topologically sorted dag as an ordering of vertices along a horizontal line such that all directed edges go vertices along a horizontal line such that all directed edges go from left to right.from left to right.

Page 100: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

Definition: Topological SortDefinition: Topological Sort

The following simple algorithm topologically The following simple algorithm topologically sorts a dag.sorts a dag.

TOPOLOGICAL-SORT(G)TOPOLOGICAL-SORT(G) call DFS(G) to compute finishing times f[v] for call DFS(G) to compute finishing times f[v] for

each vertex v (this is equal to the order in which each vertex v (this is equal to the order in which vertices change color from gray to black)vertices change color from gray to black)

as each vertex is finished (turns black), insert it as each vertex is finished (turns black), insert it onto the front of a linked listonto the front of a linked list

return the linked list of verticesreturn the linked list of vertices

Page 101: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

Definition: Topological SortDefinition: Topological Sort

Figure 23.7(b) shows how the topologically Figure 23.7(b) shows how the topologically sorted vertices appear in reverse order of sorted vertices appear in reverse order of their finishing times.their finishing times.

We can perform a topological sort in time We can perform a topological sort in time Q(V + E), since depth-first search takes Q(V + E), since depth-first search takes Q(V + E) time and it takes 0(1) time to Q(V + E) time and it takes 0(1) time to insert each of the |V| vertices onto the front insert each of the |V| vertices onto the front of the linked list.of the linked list.

Page 102: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

Definition: Topological SortDefinition: Topological Sort

Theorem 23.11: Theorem 23.11: TOPOLOGICAL-SORT(G) produces a TOPOLOGICAL-SORT(G) produces a topological sort of a directed acyclic graph G.topological sort of a directed acyclic graph G.

Proof: Proof: Suppose that DFS is run on a given dag G = (V, E) to Suppose that DFS is run on a given dag G = (V, E) to determine finishing times for its vertices. It suffices to show that determine finishing times for its vertices. It suffices to show that for any pair of distinct vertices u,v Î V, if there is an edge in G for any pair of distinct vertices u,v Î V, if there is an edge in G from u to v, then f[v] < f[u]. Consider any edge (u,v) explored by from u to v, then f[v] < f[u]. Consider any edge (u,v) explored by DFS(G). When this edge is explored, v cannot be gray, since then DFS(G). When this edge is explored, v cannot be gray, since then v would be an ancestor of u and (u,v) would be a back edge, v would be an ancestor of u and (u,v) would be a back edge, contradicting Lemma 23.10. Therefore, v must be either white or contradicting Lemma 23.10. Therefore, v must be either white or black. If v is white, it becomes a descendant of u, and so f[v] < black. If v is white, it becomes a descendant of u, and so f[v] < f[u]. If v is black, then f[v] < f[u] as well. Thus, for any edge (u,v) f[u]. If v is black, then f[v] < f[u] as well. Thus, for any edge (u,v) in the dag, we have f[v] < f[u], proving the theorem.in the dag, we have f[v] < f[u], proving the theorem.

Page 103: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

ExampleExample

For the DAG of slide 97:For the DAG of slide 97:

DFS produces this result:DFS produces this result: this contains 2 DFS treesthis contains 2 DFS trees

Vertices are blackened in the Vertices are blackened in the following order:following order: C, B, F, A, D, E , GC, B, F, A, D, E , G

AA

BB

CC

DD

EEFF

GG

AA

BB

CC

DD

EEFF

GGTT

TT

TT

TT

TT

FF

CC CC

CC

CC

Page 104: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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