CS138A 1999 1 Elementary Graph Algorithms Peter Schröder.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
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.
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.
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 II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 11 DFS Properties of DFS Topological sort.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Shortest Path Problems
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 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
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 Comp 122, Fall 2004.
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.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graph. Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices.
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.
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
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.
Introduction to Algorithms
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
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.
Lecture 16 Strongly Connected Components
CSC317 Graph algorithms Why bother?
CS200: Algorithm Analysis
Graphs Breadth First Search & Depth First Search
Graph: representation and traversal CISC4080, Computer Algorithms
Binhai Zhu Computer Science Department, Montana State University
Elementary Graph Algorithms
CS 3343: Analysis of Algorithms
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
Finding Shortest Paths
BFS,DFS Topological Sort
Graph Algorithms – 2.
BFS,DFS Topological Sort
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Graph Representation (23.1/22.1)
Basic Graph Algorithms
Algorithms and Data Structures Lecture XI
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Text Book: Introduction to algorithms By C L R S
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Premaster Course Algorithms 1 Chapter 5: Basic Graph Algorithms
Presentation transcript:

CS138A Elementary Graph Algorithms Peter Schröder

CS138A Representations For graphs G=(V,E) adjacency list for every vertex v list Adj[v] better for sparse graphs: size of O(max(E,V)) adjacency matrix A: a ij =1 if (v i,v j ) in E single bit entry for each edge in |V|x|V| matrix better for dense graphs: size is O(|V| 2 ) meaning of A 2 ?

CS138A Breadth First Search Given a graph G=(V,E) and a distinguished vertex s in V discover every vertex reachable from s find shortest path from s the name reflects the fact that all vertices at distance k-1 are discovered before any at distance k are discovered 3 colors white: undiscovered; gray: discovered; black: all neighbors have been discovered

CS138A Breadth First Tree Incremental buildup of a tree with a predecessor relationship 1. BFS(G,s) 2. for( u in V[G]-{s} ) 3. color[u] = white; d[u] = infinity; p[u] = nil; 4. color[s] = gray; d[s] = 0; p[s] = nil; Q = {s} 5. while( Q != {} ) 6. u = head[Q] 7. for( v in Adj[u] ) 8. if( color[v] == white ) 9. color[v] = gray; d[v] = d[u]+1; p[v] = u; 10. Enqueue(Q,v) 11. Dequeue(Q) 12. color[u] = black

CS138A Analysis Running time? Shortest paths Lemma 1: Let G=(V,E) be (un-)directed, s in V arbitrary. Then for any (u,v) in E: ∂(s,v)≤ ∂(s,u)+1 Lemma 2: Let G=(V,E) be (un-)directed, suppose BFS(G,s) is run. Then upon termination d[v]≥ ∂(s,v) for all v in V

CS138A Analysis Shortest paths Lemma 3: Suppose during execution of BFS(G,s) Q contains (v 1,v 2,…v r ). Then d[v r ] ≤d[v 1 ]+1 and d[v i ] ≤d[v i+1 ] for i=1,2,…r- 1 Theorem: Correctness of BFS. Suppose BFS(G,s) is run then every v in V reachable from s is discovered and d[v]= ∂(s,v) upon termination. One of the shortest paths from s to v is given by the shortest path from s to p[v] and the edge (p[v],v)

CS138A Breadth First Trees BFS builds a breadth first tree in the p[] field predecessor subgraph of G=(V,E) G p (V p,E p ) where V p contains all v in V with non-nil p[] field plus {s} and E p contains all predecessor edges for all v in V p this is a tree. Why? Lemma 5: BFS(G,s) produces a predecessor subgraph which is a breadth first tree

CS138A Depth First Search Different strategy always go as deep as possible before going on not just a single tree, but a forest results G p =(V,E p ) where E p consists of all edges (p[v],v) for any v in V with predecessor field not empty contrast with BFS tree: G p (V p,E p ), where E p depends on V p not V! there will be no distinguished start vertex

CS138A Depth First Search 1. DFS(G) 2. for( u in V[G] ) color[u] = white; p[u] = nil; 3. time = 0 4. for( u in V[G] ) if( color[u] == white ) DFS-visit(u) 5. DFS-visit(u) 6. color[u] = gray; d[u] = time = time+1; 7. for( v in adj[u] ) 8. if( color[v] == white ) 9. p[v] = u; DFS-visit(v); 10. color[u] = black; f[u] = time = time + 1;

CS138A Analysis Running time Correctness Theorem 6: (Parenthesis theorem) In any DFS of a (un-)directed graph G=(V,E) for any pair u,v one of the following holds [d[u],f[u]] and [d[v],f[v]] are entirely disjoint [d[u],f[u]] is contained in [d[v],f[v]] and u is a descencent of v vice versa Corollary 7: descendent intervals are nested

CS138A Analysis Correctness Theorem 8: (White path theorem) In a DFF of a (un-)directed G=(V,E) vertex v is a descendent of u iff at the time d[u] v can be reached from u along a path of white vertices

CS138A Classification DFS admits classification of edges Tree edges: members of DFF of G p. (u,v) is a tree edge if v was discovered exploring u Back edges: edges (u,v) connecting a vertex u to an ancestor v (self loops are back edges) Forward edges: non-tree edges (u,v) connecting u to a descendent v Cross edges: all others (c0uld be same tree, could be different trees)

CS138A Edge Types Undirected graphs Theorem 9: in a DFS of an undirected graph G=(V,E) every edge of G is either a tree edge or a back edge

CS138A Topological Sort For a directed acyclic graph (DAG) a topological sort is a linear ordering of all its vertices such that if G contains (u,v) then u appears before v in the ordering 1. Topological-Sort(G) 2. call DFS(G) to compute finish times 3. enter vertices onto front of list according to their finish times 1. return linked list of vertices

CS138A Analysis Running time Correctness Lemma 10: a directed graph is acyclic iff DFS(G) yields no back edges Theorem 11: Topological-Sort(G) produces a topological sort of a DAG G

CS138A A Classic Problem Strongly connected components a strongly connected component of a directed graph G=(V,E) is a maximal subset U of V such that for any u,v in U there is a path from u to v and a path from v to u form the transpose G T (runtime?) u is reachable from v in G iff v is reachable from u in G T

CS138A Algorithm 1. Strongly-Connected-Components(G) 2. call DFS(G) to compute finish times 3. compute G T 4. call DFS(G T ) and consider vertices 5. decreasing finish time order 6. output vertices of each tree in DFS(G T ) 7. as separate strongly connected component

CS138A Analysis Running time Correctness Lemma 12: if two vertices are in the same SCC then no path between them ever leaves the SCC Theorem 13: in any DFS all vertices in the same SCC are placed in the same DFT

CS138A Analysis Correctness Theorem 14: in a directed graph G=(V,E) the forefather phi(u) of any u in V in any DFS of G is an ancestor of u Corollary 15: in any DFS of a directed graph G=(V,E) vertices u and phi(u) for all u in V lie in the same SCC Theorem 16: in a directed graph G=(V,E) two vertices u,v in V lies in the same SCC iff they have the same forefather in DFS(G)