CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.

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
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
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?
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?
Tirgul 8 Graph algorithms: Strongly connected components.
Tirgul 11 DFS Properties of DFS Topological sort.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
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.
Lecture 10 Topics Application of 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.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
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.
Topological Sorting and Least-cost Path Algorithms.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
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.
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.
Graph Algorithms.
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.
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.
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.
 2004 SDU 1 Lecture5-Strongly Connected Components.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?
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.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Chapter 22 Elementary Graph Algorithms
CSE 2331/5331 Topic 9: Basic Graph Alg.
Topological Sort Minimum Spanning Tree
CSC 413/513: Intro to Algorithms
CS200: Algorithm Analysis
Many slides here are based on E. Demaine , D. Luebke slides
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Applications of DFS Topological sort (for directed acyclic graph)
Analysis of Algorithms CS 477/677
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort

CSE 780 Algorithms Objectives On completion of this lecture, students should be able to: 1. Write a dfs algorithm 2. Apply dfs in topological sort and finding strongly- connected components.

CSE 780 Algorithms Breadth-first Search Works for both directed and undirected graph Starting from source node s, visits remaining nodes of graph from small distance to large distance Produce a BF-tree Return distance between s to any reachable node in time O(|V| + |E|)

CSE 780 Algorithms Depth-first Search Breadth-first search: Go as broad as possible at each node Depth-first search (a different strategy): Go as deep as possible first

CSE 780 Algorithms More Formally Again, a node white: unvisited gray: discovered but not finished black: finished (explored) For every node v  V d[v]: time v is discovered f[v]: time v is finished (all edges in v’s adjacency list are explored) f[v] - d[v] = time from grey to black

CSE 780 Algorithms Pseudo-code Time complexity

CSE 780 Algorithms Example

CSE 780 Algorithms Depth-first Forest Consider all edges (p(u), u) When it happens: p(u) is grey, u is white A forest, called Depth-first forest Depends on the order of vertices Example from previous page Property: Start (finish) time for each tree: same order as if we visit nodes in pre-order (post-order) tree walk

CSE 780 Algorithms Properties of DFS Parenthesis Theorem: Any two nodes u and v, one of the following 3 cases: (1) u is descendant of v, and d[v] < d[u] < f[u] < f[v] (2) v is descendant of u, and d[u] < d[v] < f[v] < f[u] (3) u is not descendant of v, neither is v a descendant of u, and d[u] < f[u] < d[v] < f[v] or d[v] < f[v] < d[u] < f[u] u is descendant of v iff d[v] < d[u] < f[u] < f[v] White-path Theorem u is descendant of v iff at the time of d[v], there is a all white path from v to u.

CSE 780 Algorithms Classification of Edges Four types of an edge (u,v) Tree edge Non-tree edges: Back edge: u is descendant of v Forward edge: v is descendent of u Cross edge: others Distinguish by the color of v Example b dc e f g h j a k

CSE 780 Algorithms Theorem In a DFS of an undirected graph G, every edge of G is either a tree edge or a back edge. b dc e f g h j a k

CSE 780 Algorithms Connected Components Undirected graph DF forests represents the set of connected components Directed graph Does the tree rooted at u include all nodes accessible from u ?

CSE 780 Algorithms Directed Acyclic Graph DAG: directed acyclic graph Determine whether a directed graph is DAG or not How? A directed graph G is a dag iff a DFS of G yields no back edges. How about an undirected acyclic graph?

CSE 780 Algorithms Topological Sort A topological sort of a dag G = (V, E) A linear ordering A of all vertices from V If edge (u,v)  E => A[u] < A[v] undershort s pants belt shirt tie jacke t shoes socks watch

CSE 780 Algorithms Topological Sort Using DFS Topological-Sort(G) Call DFS(G) to compute finishing times f[v] for each v Output vertices in descreasing order of finishing time Time complexity O (|V| + |E|)

CSE 780 Algorithms Example undershort s pants belt shirt tie jacke t shoes socks watch 11, 16 12, 15 6, 7 3, 4 2, 5 1, 8 13, 14 17, 18 9, 10 socks, undershots, pants, shoes, watch, shirt, belt, tie, jacket

CSE 780 Algorithms Correctness Theorem Topological-Sort(G) produces a topological sort of a directed acyclic graph G Proof: Consider any edge (u, v) Goal: f[v] < f[u] Three possible types of edges: Tree edge, forward edge, cross edge

CSE 780 Algorithms Connected Components An undirected graph is connected if every pair of vertices is connected by a path A graph that is not connected is naturally decomposed into several connected components. A connected components is a maximal set of vertices, which are all connected.

CSE 780 Algorithms Connected Components (cont.) The graph below has 3 connected components {1,2,5}, {3,6} and {4}. An undirected graph is connected if it has exactly one connected component.

CSE 780 Algorithms Strongly-connected components

CSE 780 Algorithms Strongly-connected components The above graph has 3 strongly connected components {1,2,4,5}, {3} and {6} A directed graph is strongly connected if it has only one strongly connected component.

CSE 780 Algorithms Transpose of a Graph The transpose of a directed graph G = (V,E) is the graph G T = (V, E T ) where E T = {(u,v): (v,u) Є E}. E T consists of the edges of G with their directions reversed. G and G T have exactly the same strongly connected components.

CSE 780 Algorithms Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

CSE 780 Algorithms

Corollary Let C and C’ be distinct strongly connected components in directed graph G = (V,E). Suppose that there is an edge (u,v)  E T, where u  C and v  C’. Then f(C) < f(C’) Note: f(C) = max u  C {f[u]}

CSE 780 Algorithms Theorem STRONGLY-CONNECTED-COMPONENTS(G) correctly computes the strongly connected components of a directed graph G. Proof. We argue by induction that the vertices of each tree form a strongly connected component. The inductive hypothesis: The first k trees produced in line 3 are strongly connected.

CSE 780 Algorithms The basis for the induction, when k=0, is trivial. The inductive step: assume that each of the first trees produced is a strongly connected component. Consider the (k+1)st tree. Let the root of this tree be vertex u, and let u be in a strongly connected component C. Because of how we choose roots in line 3, f[u] = f(C) > f(C’) for any strongly connected component C’ that has yet to be visited.

CSE 780 Algorithms By inductive hypothesis, at the time that the search visits u, all other vertices of C are white. Therefore, all other vertices of C are descendants of u in its tree. Moreover, by inductive hypothesis and corollary 22.15, any edge in G T that leave C must be to strongly connected components that have already visited. Thus, no vertex in any strongly connected component other than C will be a descendant of u during the depth-first search of G T. Thus, the vertices of the depth-first tree in G T that is rooted at u form exactly one strongly connected component.

CSE 780 Algorithms Summary Graph search/traversal method Breadth-first search Discover nodes in shortest distance (# links) e.g, chess Depth-first search Parenthesis theorem e.g, topological sort