UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Chapter 23: Graph Algorithms Chapter 24: Minimum Spanning Trees.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
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.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
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.
Chapter 23 Minimum Spanning Trees
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Wednesday, 9/26/01 Graph Basics.
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.
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 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
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.
Chapter 2 Graph Algorithms.
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.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
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.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
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
Lecture 10 Algorithm Analysis
Advanced Algorithms Analysis and Design
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Chapter 23: Graph Algorithms Chapter 24: Minimum Spanning Trees Chapter 25: Shortest Paths [Source: Cormen et al. textbook except where noted]

Overview: Graph Algorithms ä Chapter 23: Elementary Graph Algorithms ä Introductory Concepts ä Graph Traversals: ä Depth-First Search ä Breadth-First Search ä Topological Sort ä Chapter 24: Minimum Spanning Trees ä Kruskal ä Prim ä Chapter 25: Shortest Paths ä Dijkstra

Chapter 23 Graph Algorithms Introductory Concepts Depth-First SearchBreadth-First Search Topological Sort [Source: Cormen et al. textbook except where noted]

Introductory Graph Concepts ä G= (V,E) ä Vertex Degree ä Self-Loops B E C F D A B E C F D A ä Directed Graph (digraph) ä Degree: in/out ä Self-Loops allowed ä Undirected Graph ä No Self-Loops ä Adjacency is symmetric This treatment follows textbook Cormen et al. Some definitions differ slightly from other graph literature.

Introductory Graph Concepts: Representations B E C F D A B E C F D A ä Undirected Graph ä Directed Graph (digraph) A B C D E F ABCDEF ABCDEF A BC B ACEF C AB D E E BDF F BE A BC B CEF C D D E BD F E Adjacency Matrix Adjacency List Adjacency Matrix Adjacency List This treatment follows textbook Cormen et al. Some definitions differ slightly from other graph literature.

Introductory Graph Concepts: Paths, Cycles ä Path: ä length: number of edges ä simple: all vertices distinct ä Cycle: ä Directed Graph: ä forms cycle if v 0 =v k and k>=1 ä simple cycle: v 1,v 2..,v k also distinct ä self-loop is cycle of length 1 ä Undirected Graph: ä forms (simple) cycle if v 0 =v k and k>=3 ä simple cycle: v 1,v 2..,v k also distinct B E C F D A path path B E C F D A simple cycle simple cycle This treatment follows textbook Cormen et al. Some definitions differ slightly from other graph literature. B E C F D A simple cycle = simple cycle =

Introductory Graph Concepts: Connectivity ä Undirected Graph: connected ä every pair of vertices is connected by a path ä one connected component ä connected components: ä equivalence classes under “is reachable from” relation ä Directed Graph: strongly connected ä every pair of vertices is reachable from each other ä one strongly connected component ä strongly connected components: ä equivalence classes under “mutually reachable” relation B E C F D A B E C F D Aconnected 2 connected components not strongly connected strongly connected component B E C F D A B E C F D A This treatment follows textbook Cormen et al. Some definitions differ slightly from other graph literature.

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

Depth-First Search (DFS)

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

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.

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

Elementary Graph Algorithms: DFS ä Review problem: TRUE or FALSE? ä The tree shown below on the right can be a DFS tree for some adjacency list representation of the graph shown below on the left. B E C F D A A C B E D F Tree Edge Cross Edge Back Edge

Breadth-First Search (BFS)

BFS PseudoCode ä See handout on web

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

Depth-First Search (DFS) & Breadth-First Search (BFS) Using the Results of DFS & BFS Running Time Analysis

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 defined appropriately. see p of text for proof

SEARCHING Elementary Graph Algorithms: SEARCHING: DFS, BFS ä Breadth-First-Search (BFS): ä Shortest Path Distance ä From source to each reachable vertex ä Record during traversal ä Foundation of many “shortest path” algorithms See DFS, BFS Handout for PseudoCode ä Depth-First-Search (DFS): ä Encountering, finishing times ä “well-formed” nested (( )( ) ) structure ä DFS of undirected graph produces only back edges or tree edges ä Directed graph is acyclic if and only if DFS yields no back edges for unweighted directed or undirected graph G=(V,E) Time: O(|V| + |E|) adj listO(|V| 2 ) adj matrix predecessor subgraph = forest of spanning trees Vertex color shows status: not yet encountered encountered, but not yet finished finished

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|).

Topological Sort Source: Previous instructors

Definition: DAG ä A Directed Acyclic Graph often abbreviated DAG ä DAGs used in many applications to indicate precedences among events. ä If DFS of a directed graph yields no back edges, then the graph contains no cycles [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".

Elementary Graph Algorithms: Topological Sort source: textbook Cormen et al. TOPOLOGICAL-SORT(G) 1 DFS(G) computes “finishing times” for each vertex 2 as each vertex is finished, insert it onto front of list 3 return list for Directed, Acyclic Graph (DAG) G=(V,E) Produces linear ordering of vertices. For edge (u,v), u is ordered before v. See also DFS/BFS slide show

Topological Sort ä The following 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

Example ä For this DAG: ä 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

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

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.

Chapter 24 Minimum Spanning Trees KruskalPrim [Source: Cormen et al. textbook except where noted]

Minimum Spanning Tree: Greedy Algorithms A B C D E F G source: textbook Cormen et al. for Undirected, Connected, Weighted Graph G=(V,E) Produces minimum weight tree of edges that includes every vertex. Invariant: Minimum weight spanning forest Becomes single tree at end Invariant: Minimum weight tree Spans all vertices at end Time: O(|E|lg|E|) given fast FIND-SET, UNION Time: O(|E|lg|V|) = O(|E|lg|E|) slightly faster with fast priority queue

Minimum Spanning Trees ä Review problem: ä For the undirected, weighted graph below, show 2 different Minimum Spanning Trees. Draw each using one of the 2 graph 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 Minimum Spanning Trees? A B C D E F G

Chapter 25 Shortest Paths Dijkstra [Source: Cormen et al. textbook except where noted]

BFS as a Basis for Shortest Path Algorithms Source/Sink Shortest Path Problem: Given 2 vertices u, v, find the shortest path in G from u to v. Solution: BFS starting at u. Stop at v. Single-Source Shortest Paths Problem: Given a vertex u, find the shortest path in G from u to each vertex. Solution: BFS starting at u. Full BFS tree. source: based on Sedgewick, Graph Algorithms BFS for unweighted, undirected graph G=(V,E) Time: O(|V| + |E|) adj list O(|V| 2 ) adj matrix O(|V| 2 ) adj matrix Time: O(|V|(|V| + |E|)) adj list O(|V| 3 ) adj matrix O(|V| 3 ) adj matrix All-Pairs Shortest Paths Problem: Find the shortest path in G from each vertex u to each vertex v. Solution: For each u: BFS starting at u; full BFS tree.

Shortest Path Applications ä Road maps ä Airline routes ä Telecommunications network routing ä VLSI design routing Weight ~ Cost ~ Distance source: based on Sedgewick, Graph Algorithms for weighted, directed graph G=(V,E)

Shortest Path Trees source: Sedgewick, Graph Algorithms Shortest Path Tree gives shortest path from root to each other vertex

Shortest Path Principles: Relaxation ä “Relax” a constraint to try to improve solution ä “Rubber band” analogy [Sedgewick] ä Relaxation of an Edge (u,v): ä test if shortest path to v [found so far] can be improved by going through u A B C D E F G

Single Source Shortest Paths Dijkstra’s Algorithm source: textbook Cormen et al. for (nonnegative) weighted, directed graph G=(V,E)

Single Source Shortest Paths Dijkstra’s Algorithm ä See separate ShortestPath slide show A B C D E F G source: textbook Cormen et al. for (nonnegative) weighted, directed graph G=(V,E)

Single Source Shortest Paths Dijkstra’s Algorithm ä 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 Why can’t Dijkstra’s algorithm handle negative-weight edges?

Single Source Shortest Paths Dijkstra’s Algorithm source: Sedgewick, Graph Algorithms for (nonnegative) weighted, directed graph G=(V,E)

Shortest Path Algorithms source: Sedgewick, Graph Algorithms