UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs – Depth First Search ORD DFW SFO LAX
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 11 DFS Properties of DFS Topological sort.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Chapter 23: Graph Algorithms Chapter 24: Minimum Spanning Trees.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
Shortest Path Problems
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2008 Lecture 4 Tuesday, 9/30/08 Graph Algorithms: Part 1 Shortest.
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Topics Application of DFS Topological Sort
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Graph Algorithms: Part 1
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
1 7/3/2015 ITCS 6114 Graph Algorithms. 2 7/3/2015 Depth-First Search ● Depth-first search is another strategy for exploring a graph ■ Explore “deeper”
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Topological Sort (an application of DFS) CSC263 Tutorial 9.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Graph Algorithms – 2. graphs Parenthesis Theorem Theorem 22.7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or.
Introduction to Algorithms
Chapter 22 Elementary Graph Algorithms
CS200: Algorithm Analysis
Graph Algorithms – 2 DAGs Topological order
Intro to Graph Algorithms (Slides originally created by David Luebke)
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

UMass Lowell Computer Science 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]

Depth-First Search (DFS) & Breadth-First Search (BFS) Running Time Analysis Vertex Color Changes Edge Classification Using the Results of DFS & BFS Examples

Running Time Analysis ä Key ideas in the analysis are similar for DFS and BFS. In both cases we assume an Adjacency List representation. Let’s examine DFS. ä Let t be number of DFS trees generated by DFS search ä Outer loop in DFS(G) executes t times ä each execution contains call: DFS_Visit(G,u) ä each such call constructs a DFS tree by visiting (recursively) every node reachable from vertex u ä Time: ä Now, let r i be the number of vertices in DFS tree i ä Time to construct DFS tree i: continued on next slide...

Running Time Analysis ä Total DFS time: ä Now, consider this expression for the extreme values of t: ä 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 expression simplifies to O(V) ä O(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 running time: ä 1 for each vertex that is its own (degenerate) DFS tree with no edges ä upper bound on this total is O(V) ä |AdjList[u]| for each vertex u that is the root of a non-degenerate DFS tree ä 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= Note that for an Adjacency Matrix representation, we would need to scan an entire matrix row (containing |V| entries) each time we examined the vertices adjacent to a vertex. This would make the running time O(V 2 ) instead of O(V+E).

Vertex Color Changes ä Vertex is WHITE if it has not yet been encountered during the search. ä Vertex is GRAY if it has been encountered but has not yet been fully explored. ä Vertex is if it has been fully explored. ä Vertex is BLACK if it has been fully explored.

Edge Classification ä Each edge of the original graph G is classified during the search ä produces information needed to: ä build DFS or BFS spanning forest of trees ä detect cycles (DFS) or find shortest paths (BFS) ä When vertex u is being explored, edge e = (u,v) is classified based on the color of v when the edge is first explored: ä e is a tree edge if v is WHITE [for DFS and BFS] ä e is a back edge if v is GRAY [for DFS only] ä for DFS this means v is an ancestor of u in the DFS tree ä e is a forward edge if v is and [for DFS only] v is a descendent of u in the DFS tree ä e is a forward edge if v is BLACK and [for DFS only] v is a descendent of u in the DFS tree ä e is a cross edge if v is and [for DFS only] there is no ancestor or descendent relationship between u and v in the DFS tree ä e is a cross edge if v is BLACK and [for DFS only] there is no ancestor or descendent relationship between u and v in the DFS tree ä Note that: ä For BFS we’ll only consider tree edges. ä 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.

Using the Results of DFS & BFS A directed graph G is acyclic if and only if a Depth-First Search of G yields no back edges. ä Using DFS to Detect Cycles: ä Using BFS for Shortest Paths: A Breadth-First Search of G yields shortest path information: 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. see p. 486 of text for proof Note: DFS can also be used to detect cycles in undirected graphs if notion of cycle is refined appropriately. see p of text for proof

Example: DFS of Directed Graph Source: Graph is from Computer Algorithms: Introduction to Design and Analysis by Baase and Gelder. A B C D E F GG=(V,E) Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Edge Classification Legend: T: tree edge B: back edge F: forward edge C: cross edge

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F T

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F T

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C T

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C T

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C T C

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C T C B

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C T C B

Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C T C B

Example: (continued) DFS of Directed Graph A D G B T T B C T F B F C C C C E T B A B C D E F G T T T B C F TB C C T C B T DFS Tree 1 DFS Tree 2

Example: DFS of Undirected Graph A B C D E F GG=(V,E) Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T B

Example: DFS of Undirected Graph Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E A B C D E F G T T B TBB T T B T B

Example: (continued) DFS of Undirected Graph DFS Tree A B C D E F G T T B TBB T T B T B A C D E F G B B T T T T T T B B B B

Example: BFS of Directed Graph Source: Graph is from Computer Algorithms: Introduction to Design and Analysis by Baase and Gelder. A B C D E F GG=(V,E) Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Edge Classification Legend: T: tree edge only tree edges are used

Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: A

Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: AB T

Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: ABC T T

Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: ABCF T T T

Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: BCF T T T

Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: BCFD T T T T

Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: CFD T T T T

Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: FD T T T T

Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: D T T T T

Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: - T T T T

Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: E T T T T

Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: EG T T T T T

Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: G T T T T T

Example: BFS of Directed Graph Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: - A B C D E F G T T T T T

Example: (continued) BFS of Directed Graph G E T A D B T T T F C T BFS Tree 1 BFS Tree 2 A B C D E F G T T T T T Shortest path distance from : A to B = 1 A to C = 1 A to F = 1 A to D = 2 Shortest path distance from : E to G = 1

Example: BFS of Undirected Graph A B C D E F GG=(V,E) Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Edge Classification Legend: T: tree edge only tree edges are used

Example: BFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: A

Example: BFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: AB T

Example: BFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: ABC T T

Example: BFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: ABCD T T T

Example: BFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: ABCDF T T T T

Example: BFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: BCDF T T T T

Example: BFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: CDF T T T T

Example: BFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: CDFE T T T T T

Example: BFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: DFE T T T T T

Example: BFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: DFEG T T T T T T

Example: BFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: FEG T T T T T T

Example: BFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: EG T T T T T T

Example: BFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: G T T T T T T

Example: BFS of Undirected Graph Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E Queue: - A B C D E F G T TTT T T

Example: (continued) BFS of Undirected Graph BFS Tree Shortest path distance from : A to B = 1 A to E = 2 A to C = 1A to G = 2 A to D = 1 A to F = 1 A B C D E F G T TTT T T A F B T T T D C T E G T T

Topological Sort Source: Previous instructors

Definition: DAG ä A Directed Acyclic Graph is often abbreviated by DAG ä As noted on slide 7, if DFS of a directed graph yields no back edges, then the graph contains no cycles [this is Lemma in text] A B C D E F G This graph has more than one cycle. Can you find them all? A B C D E F G This graph has no cycles, so it is a DAG.

Definition: Topological Sort ä A topological sort of a dag G = (V, E) is a linear ordering of all its vertices such that if G contains an edge (u, v), then u appears before v in the ordering. ä 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 its vertices along a horizontal line so that all directed edges go from left to right.  Topological sorting is thus different from the usual kind of "sorting".

Definition: Topological Sort ä Directed acyclic graphs are used in many applications to indicate precedences among events. ä Figure 23.7 gives an example that arises when Professor Bumstead gets dressed in the morning. The professor must don certain garments before others (e.g., socks before shoes). Other 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 garment u must be donned before garment v. A topological sort of this dag therefore gives an order for getting dressed. Figure 23.7(b) shows the topologically sorted dag as an ordering of vertices along a horizontal line such that all directed edges go from left to right.

Definition: Topological Sort ä The following simple algorithm topologically sorts a dag. ä TOPOLOGICAL-SORT(G) ä call DFS(G) to compute finishing times f[v] for each vertex v (this is equal to the order in which vertices change color from gray to black) ä as each vertex is finished (turns black), insert it onto the front of a linked list ä return the linked list of vertices

Definition: Topological Sort ä Figure 23.7(b) shows how the topologically sorted vertices appear in reverse order of their finishing times. ä We can perform a topological sort in time Q(V + E), since depth-first search takes Q(V + E) time and it takes 0(1) time to insert each of the |V| vertices onto the front of the linked list.

Definition: Topological Sort ä Theorem 23.11: TOPOLOGICAL-SORT(G) produces a topological sort of a directed acyclic graph G. ä Proof: Suppose that DFS is run on a given dag G = (V, E) to 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 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 v would be an ancestor of u and (u,v) would be a back edge, contradicting Lemma Therefore, v must be either white or black. If v is white, it becomes a descendant of u, and so f[v] < f[u]. If v is black, then f[v] < f[u] as well. Thus, for any edge (u,v) in the dag, we have f[v] < f[u], proving the theorem.

Example ä For the DAG of slide 97: ä DFS produces this result: ä this contains 2 DFS trees ä Vertices are blackened in the following order: ä C, B, F, A, D, E, G A B C D E F G A B C D E F G T T T T T F C C C C

Example ä Vertices are added to front of a linked list in the blackening order. ä Final result is shown below ä Note that all tree edges and non-tree edges point to the right A B C DE F G T F C T T T C T C C