Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Similar documents
Elementary Graph Algorithms

22.3 Depth First Search

Graph representation

COT 6405 Introduction to Theory of Algorithms

Graph Representation

Elementary Graph Algorithms

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Design and Analysis of Algorithms

22.1 Representations of graphs

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai

Chapter 22. Elementary Graph Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Basic Graph Algorithms

Chapter 22: Elementary Graph Algorithms. Definitions:

Representations of Graphs

Graph: representation and traversal

CSI 604 Elementary Graph Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Figure 1: A directed graph.

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

DFS & STRONGLY CONNECTED COMPONENTS

Design and Analysis of Algorithms

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Unweighted directed graphs

Data Structures and Algorithms. Chapter 7. Graphs

Minimum Spanning Trees Ch 23 Traversing graphs

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Graph Search. Adnan Aziz

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Algorithm Design and Analysis

Outlines: Graphs Part-2

Chapter 22 Elementary Graph Algorithms

Data Structures and Algorithms. Werner Nutt

Announcements. HW3 is graded. Average is 81%

Data Structure and Algorithm, Spring 2017 Final Examination

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Graph Algorithms. Definition

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

Strongly Connected Components

Introduction to Algorithms. Lecture 11

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Cut vertices, Cut Edges and Biconnected components. MTL776 Graph algorithms

CS Yaxin.Huang.HWM10- Solution

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

W4231: Analysis of Algorithms

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

Graph Representations and Traversal

Graph implementations :

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

Graphs: basic concepts and algorithms

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part B

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Homework No. 4 Answers

Graph: representation and traversal


Review: Graph Theory and Representation

ECE250: Algorithms and Data Structures Single Source Shortest Paths Bellman-Ford Algorithm

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

CS 310 Advanced Data Structures and Algorithms

Topic 12: Elementary Graph Algorithms

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Lecture 9 Graph Traversal

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Unit 2: Algorithmic Graph Theory

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

Graph. Vertex. edge. Directed Graph. Undirected Graph

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Lecture 3: Graphs and flows

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Inf 2B: Graphs II - Applications of DFS

CSC263 Week 8. Larry Zhang.

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

Algorithm Design and Analysis

Announcements Problem Set 4 is out!

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search

MAL 376: Graph Algorithms I Semester Lecture 1: July 24

Graph Algorithms Using Depth First Search

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Definition Example Solution

Inf 2B: Graphs, BFS, DFS

Algorithm Design and Analysis

(Re)Introduction to Graphs and Some Algorithms

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

Transcription:

Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1

Graphs A graph, G = (V, E), consists of two sets: V is a finite non-empty set of vertices. E is a set of pairs of vertices, called edges. V={1, 2, 3,4} E = {(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)} 2

3

Graphs In an undirected graph The pair of vertices are unordered pairs. Thus, the pairs (v 1, v 2 ) and (v 2, v 1 ) are the same. No reflective (self) edges. In a directed graph The edges are represented by a directed pair (v 1, v 2 ). Therefore (v 2, v 1 ) and (v 1, v 2 ) are two different edges v 1 is the tail and v 2 the head of the edge. 4

Graphs G 1 G 2 G 1 is an undirected. G 2 is a directed graph. G 1 = ({1,2,3,4}, {(1,2),(1,3),(3,4),(2,3)} ) G 2 = ({a,b,c,d,e}, {<a,b>,<b,c>,<c,a>,<a,d>,<d,e>} ) 5

Graphs The maximum number edges in An undirected graph with n vertices is n(n - 1)/2 A directed graph with n vertices is n 2 An undirected graph with n vertices and exactly n(n-1)/2 edges is said to be complete 6

Graphs If (v 1,v 2 ) is an edge of a graph G, then We shall say that the vertices v 1 and v 2 are adjacent And that the edge (v 1,v 2 ) is incident on vertices v 1 and v 2 If (v 1,v 2 ) is a directed edge, then vertex v 1 will be said to be adjacent to v 2, while v 2 is adjacent from v 1. The degree of a vertex in undirected graph is the number of edges incident to that vertex. Example: In G 1, degree(2) = 2. In case G is a directed graph, we define The in-degree of a vertex v is the number of edges for which v is the head. The out-degree is the number of edges for which v is the tail. Example: In G 2, in-degree(e) = 1; out-degree(e) = 0. 7

Graphs A subgraph of a graph G is a graph G' = (V, E ) such that V' V and E' E. 8

Graphs A path from vertex v p to vertex v q (v p v q ) in graph G is a sequence of vertices v p,v i1,v i2,...,v in,v q such that (v p,v i1 ),(v i1,v i2 ),...,(v in,v q ) are edges The length of a path is the number of edges on it A simple path is a path in which All vertices except possibly the first and last are distinct 1 2 4 5 (1,2,3,4) simple path (1,4,2,3,4,5) not simple 3 9

Graphs A cycle is a path in which The first and last vertices are the same A simple cycle same with simple path When the graph is directed, we add the prefix "directed" to the terms 1 2 4 5 3 10

Graphs In an undirected graph, G, two vertices v 1 and v 2 are said to be connected If there is a path in G from v 1 to v 2. Since G is undirected, this means there must also be a path from v 2 to v 1. An undirected graph is said to be connected If for every pair of distinct vertices v i, v j in there is a path from v i to v j in G 1 1 2 4 5 2 4 5 3 connected graph 3 not connected graph 11

Graphs A (connected) component of an undirected graph is a maximal connected subgraph A tree is a connected acyclic undirected graph 1 1 2 4 5 2 4 5 3 Two connected components 3 tree 12

Graphs A forest is a acyclic undirected graph (not have to be connected) 13

Graphs 14

Graph Representation Two common ways to represent a graph (either directed or undirected): Adjacency lists. Adjacency matrix. 15

Adjacency Lists Array Adj of V lists, One per vertex. The list Adj[u] contains all vertices v such that (u, v) E. Works for both directed and undirected graphs. Space: Θ(V + E). When expressing space/running time, we ll drop the cardinality. Time to list all vertices adjacent to u: Θ(degree(u)). Time to determine if (u, v) E: O(degree(u)). 16

Adjacency Matrix Space: Θ(V 2 ). Time to list all vertices adjacent to u: Θ(V). Time to determine if (u, v) E: O(1). 17

Breadth-First Search Given a graph G = (V, E) and a vertex s V Which vertices are reachable from s What is the shortest distance to each reachable vertex What are the shortest paths 18

Breadth-First Search Algorithm Idea: Send a wave out from s: First hits all vertices 1 edge from s. From there, hits all vertices 2 edges from s. Etc. Use FIFO queue Q to maintain wave front. v Q if and only if wave has hit v but has not come out of v yet. source 19

Breadth-First Search Input: Graph G = (V, E), either directed or undirected. Source vertex s V. Output: v.d = distance to all v V from s. If v V is not reachable from s, v.d =. v.π is the predecessor u of v on shortest path s v. If v V is not reachable from s, v.π will be null. The set of edges {(v.π, v) : v s} forms a tree, such that u is v s predecessor. Auxiliary Means: every vertex has a color: White - undiscovered Gray - discovered, but not finished (not done exploring from it) Black - finished (have found everything reachable from it) 20

Breadth-First Search r s t u v w x y s w w t r t w x v r x y x t u u s y 21

BFS(G, s) 1 for each vertex u G.V {s} 2 u.color WHITE 3 u.d 4 u.π NULL 5 s.color GRAY 6 s.d 0 7 s.π NULL 8 Q 9 ENQUEUE(Q, s) 10 while Q 11 u DEQUEUE(Q) 12 for each v G.Adj[u] 13 if v.color = WHITE 14 v.color GRAY 15 v.d u.d + 1 16 v.π u 17 ENQUEUE(Q, v) 18 u.color BLACK Complexity: O(V + E) 22

Printing The Shortest Path 1. PrintPath (G, s, v) 2. if (v = s) then 3. print s 4. else if (v.π = null) then 5. print "no path from" s "to" v "exists" 6. else 7. printpath (G, s, v.π) 8. print v 23

Depth-First Search Given a graph G = (V, E) Draw G as a forest of sub graphs Determine which vertex is a descendant of another in the forest Detect cycles Discover connected components Topological Sort 24

Depth-First Search Algorithm Idea: Search deeper in the graph whenever possible: Start from an arbitrary unvisited vertex. Explore out one of the undiscovered edge of the most recently discovered vertex v. When all of v's edges have been explored, backtracks, and continue. Start all over again. 25

Depth-First Search Input: Graph G = (V, E), either directed or undirected. Output: v.d = discovery time & v.f= finishing time. A unique integer from 1 to 2 V such that 1 v.d < v.f 2 V. v.π = u such that u is the predecessor to v in the visit order. The predecessor subgraph Gπ = (V, Eπ), where Eπ={(v.π,v) : v.π null} forms a forest composed of several trees. Auxiliary Means: every vertex has a color: White - undiscovered Gray - discovered, but not finished (not done exploring from it) Black - finished (have found everything reachable from it) 26

27

DFS(G) 1 for each vertex u G.V 2 u.color WHITE 3 u.π NULL 4 time 0 5 for each vertex u G.V 6 if u.color = WHITE 7 DFS-VISIT(G,u) DFS-VISIT(G,u) // white vertex u has just been discovered 1 u.color GRAY 2 time time+1 3 u.d time 4 for each v G.Adj[u] // explore edge (u, v) 5 if v.color = WHITE 6 v.π u 7 DFS-VISIT(G, v) 8 u.color BLACK // blacken u; it is finished. 9 time time+1 10 u.f time 28

Depth-First Search When one vertex is a descendant of another in the forest that was constructed by DFS? Parenthesis Theorem White-path Theorem 29

Parenthesis Theorem Theorem (Parenthesis theorem): For all u, v, exactly one of the following holds: 1. u.d < u.f < v.d < v.f or v.d < v.f < u.d < u.f and neither of u and v is a descendant of the other. 2. u.d < v.d < v.f < u.f and v is a descendant of u. 3. v.d < u.d < u.f < v.f and u is a descendant of v. So u.d < v.d < u.f < v.f cannot happen. Like parentheses: OK: ( ) [ ] ( [ ] ) [ ( ) ] Not OK: ( [ ) ] [ ( ] ) Corollary (Nesting of descendants intervals): v is a proper descendant of u if and only if u.d < v.d < v.f < u.f. 30

White-path Theorem Theorem (White-path theorem): v is a descendant of u if and only if at time u.d, there is a path u v consisting of only white vertices (except for u, which was just colored gray) 31

Classification of Edges Tree edge: In the constructed forest. Found by exploring (u, v). Back edge: (u, v), where u is a descendant of v. Forward edge: (u, v), where v is a descendant of u, but not a tree edge. Cross edge: any other edge. Can go between vertices in same tree or in different trees. In an undirected graph, there may be some ambiguity since (u, v) & (v, u) are the same edge. Classify by the first type above that matches Theorem: In DFS of an undirected graph, we get only tree and back edges. No forward or cross edges. 32

Classification of Edges Edge (u, v) can be classified by the color of v when the edge is first explored: WHITE - indicates a tree edge GRAY - indicates a back edge BLACK indicates a forward or cross edge. (u, v) is a forward edge if u.d < v.d (u, v) is a cross edge if u.d > v.d. 33

Detection of Cycles Lemma: A directed graph G is acyclic if and only if a DFS of G yields no back edges. Proof: Back edge Cycle Suppose there is a back edge (u, v) Then v is ancestor of u in the constructed forest Therefore, there is a path v u, so v u v is a cycle 34

Detection of Cycles Cycle back edge. Suppose G contains cycle C. Let v be the first vertex discovered in C, and let (u, v) be the preceding edge in C. At time v.d, vertices of C form a white path v u since v is the first vertex discovered in c. By white-path theorem, u is descendant of v in depth-first forest. Therefore, (u, v) is a back edge. Similar for undirected graph. 35

Topological Sort Directed acyclic graph (DAG) is a directed graph with no cycles Good for modeling a partial order: a > b and b > c a > c. May have a and b such that neither a > b nor b > c. U V W X Y Z Topological sort of a DAG: a linear ordering of vertices such that if (u, v) E, then u appears somewhere before v. V Y X W U Z 36

Topological Sort topologicalsort (G) // Assume G is a DAG Complexity: Θ(V + E) Call DFS(G) to compute finishing times v.f for all v V as each vertex is finished, insert it onto the front of a linked list return the linked list of vertices 37

Topological Sort Correctness: Just need to show if (u, v) E, then v.f < u.f. When we explore (u, v), what are the colors of u and v? u is gray. v can t be gray. Because then (u, v) is a back edge, but G is a dag. If v is white. By parenthesis theorem, u.d < v.d < v.f < u.f. If v is black. Then v is already finished, but u doesn't, therefore, v.f < u.f. 38