1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.

Slides:



Advertisements
Similar presentations
Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Advertisements

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.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
1 Chapter 22: Elementary Graph Algorithms IV. 2 About this lecture Review of Strongly Connected Components (SCC) in a directed graph Finding all SCC (i.e.,
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
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 Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
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?
Tirgul 8 Graph algorithms: Strongly connected components.
Tirgul 11 DFS Properties of DFS Topological sort.
Graphs-Part II Depth First Search (DFS). We Already Covered Breadth First Search(BFS) Traverses the graph one level at a time – Visit all outgoing edges.
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 Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
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.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
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.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
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.
 2004 SDU Lecture 7- Minimum Spanning Tree-- Extension 1.Properties of Minimum Spanning Tree 2.Secondary Minimum Spanning Tree 3.Bottleneck.
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.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
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.
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.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
 2004 SDU 1 Lecture5-Strongly Connected Components.
November 19, Algorithms and Data Structures Lecture XI 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
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Graphs-Part II Depth First Search (DFS)
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Topological Sort (an application of DFS)
CS200: Algorithm Analysis
Graph Algorithms Using Depth First Search
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Chapter 22: Elementary Graph Algorithms I
Depth-First Search Graph Traversals Depth-First Search DFS.
Topological Sort (an application of DFS)
Elementary Graph Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 33 Graphs II
Chapter 24: Single-Source Shortest-Path
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

1 Chapter 22: Elementary Graph Algorithms II

2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very important) White-path theorem (very useful)

3 Depth First Search (DFS) An alternative algorithm to find all vertices reachable from a particular source vertex s Idea: Explore a branch as far as possible before exploring another branch Easily done by recursion or stack

4 The DFS Algorithm DFS(u) { Mark u as discovered ; while (u has unvisited neighbor v) DFS(v); Mark u as finished ; } The while-loop explores a branch as far as possible before the next branch

5 Example (s = source) v rs wx tu y v rs wx tu y v rs wx tu y finished discovered direction of edge when new node is discovered

6 Example (s = source) v rs wx tu y v rs wx tu y v rs wx tu y finished discovered direction of edge when new node is discovered

7 Example (s = source) v rs wx tu y v rs wx tu y v rs wx tu y finished discovered direction of edge when new node is discovered

8 Example (s = source) v rs wx tu y v rs wx tu y v rs wx tu y finished discovered direction of edge when new node is discovered

9 Example (s = source) v rs wx tu y v rs wx tu y v rs wx tu y finished discovered direction of edge when new node is discovered

10 Example (s = source) Done when s is discovered The directed edges form a tree that contains all nodes reachable from s Called DFS tree of s v rs wx tu y v rs wx tu y

11 Generalization Just like BFS, DFS may not visit all the vertices of the input graph G, because : G may be disconnected G may be directed, and there is no directed path from s to some vertex In most application of DFS (as a subroutine), once DFS tree of s is obtained, we will continue to apply DFS algorithm on any unvisited vertices …

12 Generalization (Example) v rs wx tu y Suppose the input graph is directed

13 Generalization (Example) v rs wx tu y 1. After applying DFS on s

14 Generalization (Example) v rs wx tu y 2. Then, after applying DFS on t

15 Generalization (Example) v rs wx tu y 3. Then, after applying DFS on y

16 Generalization (Example) v rs wx tu y 4. Then, after applying DFS on r

17 Generalization (Example) v rs wx tu y 5. Then, after applying DFS on v

18 Generalization (Example) Result : a collection of rooted trees called DFS forest v rs wx tu y

19 Performance Since no vertex is discovered twice, and each edge is visited at most twice (why?)  Total time: O(|V|+|E|) As mentioned, apart from recursion, we can also perform DFS using a LIFO stack (Do you know how?)

20 Who will be in the same tree ? Because we can only explore branches in an unvisited node  DFS(u) may not contain all nodes reachable by u in its DFS tree v rs w x tu y E.g, in the previous run, v can reach r, s, w, x but v ’s tree does not contain any of them Can we determine who will be in the same tree ?

21 Who will be in the same tree ? Yes, we will soon show that by white-path theorem, we can determine who will be in the same tree as v at the time when DFS is performed on v Before that, we will define the discovery time and finishing time for each node, and show interesting properties of them

22 Discovery and Finishing Times When the DFS algorithm is run, let us consider a global time such that the time increases one unit : when a node is discovered, or when a node is finished (i.e., finished exploring all unvisited neighbors) Each node u records : d(u) = the time when u is discovered, and f(u) = the time when u is finished

23 Discovery and Finishing Times In our first example (undirected graph) v rs wx t u y 1/1612/15 13/14 2/11 4/95/8 3/10 6/7

24 Discovery and Finishing Times v rs wx t u y 1/613/14 15/16 2/5 7/108/9 3/4 11/12 In our second example (directed graph)

25 Nice Properties Lemma: For any node u, d(u)  f(u) Theorem (Parenthesis Theorem): Let u and v be two nodes with d(u)  d(v). Then, either 1. d(u)  d(v)  f(v)  f(u) [contain], or 2. d(u)  f(u)  d(v)  f(v) [disjoint] Lemma: For nodes u and v, d(u), d(v), f(u), f(v) are all distinct

26 Proof of Parenthesis Theorem Consider the time when v is discovered Since u is discovered before v, there are two cases concerning the status of u : Case 1: (u is not finished) This implies v is a descendant of u  f(v)  f(u) (why?) Case 2: (u is finished)  f(u)  d(v)

27 Corollary Corollary: v is a (proper) descendant of u if and only if d(u)  d(v)  f(v)  f(u) Proof: v is a (proper) descendant of u  d(u)  d(v) and f(v)  f(u)  d(u)  d(v)  f(v)  f(u)

28 White-Path Theorem Theorem: In a DF forest of a graph G = (V, E), vertex v is a descendant of vertex u if and only if at the time d(u) that the search discovers u, there is a path from u to v consisting entirely of white nodes only E.g., If we perform DFS(w) now, will the descendant of w always be the same set of nodes? v rs wx tu y

29 Proof (Part 1) => Suppose that v is a descendant of u Let P = (u, w 1, w 2, …, w k, v) be the directed path from u to v in DFS tree of u Then, apart from u, each node on P must be discovered after u  They are all unvisited by the time we perform DFS on u  Thus, at this time, there exists a path from u to v with unvisited nodes only

30 Proof (Part 2) So, every descendant of u is reachable from u with unvisited nodes only To complete the proof, it remains to show the converse : Any node reachable from u with unvisited nodes only becomes u ’s descendant is also true (We shall prove this by contradiction)

31 Proof (Part 2) Suppose on contrary the converse is false Then, there exists some v, reachable from u with unvisited nodes only, does not become u ’s descendant If more than one choice of v, let v be one such vertex closest to u  d(u)  f(u)  d(v)  f(v) … EQ.1

32 Proof (Part 2) Let P = (u, w 1, w 2, …, w k, v) be any path from u to v using unvisited nodes only By our choice of v (closest one), all w 1, w 2, …, w k become u ’s descendants This implies: d(u)  d(w k )  f(w k )  f(u) Handle special case: when u = w k Combining with EQ.1, we have d(w k )  f(w k )  d(v)  f(v)

33 Proof (Part 2) However, since there is an edge (no matter undirected or directed) from w k to v, if d(w k )  d(v), then we must have d(v)  f(w k ) … (why??) Consequently, it contradicts with : d(w k )  f(w k )  d(v)  f(v)  Proof completes

34 Classification of Tree Edges After a DFS process, we can classify the edges of a graph into four types : 1. Tree : Edges in the DFS forest 2. Back : From descendant to ancestor when explored (include self loop) 3. Forward : From ancestor to descendant when explored (exclude tree edge) 4. Cross : Others (no ancestor-descendant relation)

35 Example v rs wx tu y Suppose the input graph is directed

36 Example v rs wx tu y Suppose this is the DFS forest obtained Can you classify the type of each edge ? B F C C C

37 Example v rs wx tu y Suppose the DFS forest is different now … Can you classify the type of each edge ? B C C C C

38 Example v rs wx tu y Suppose the input graph is undirected

39 Example v rs wx tu y Suppose this is the DFS forest obtained Can you classify the type of each edge ? B B

40 Edges in Undirected Graph Theorem: After DFS of an undirected graph, every edge is either a tree edge or a back edge Proof: Let (u,v) be an edge. Suppose u is discovered first. Then, v will become u’s descendent (white-path) so that f(v)  f(u) If u discovers v  (u,v) is tree edge Else, (u,v) is explored after v discovered Then, (u,v) must be explored from v because f(v)  f(u)  (u,v) is back edge

41 Homework Exercise: , (Due Dec. 18) Practice at home: , ,