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.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
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.
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.
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.
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:
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.
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.
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.
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.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
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 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.
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.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
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.
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/25/2016 CSE 207: Algorithms Graph Algorithms.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
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.
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.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
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.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
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.
Topological Sort (an application of DFS)
CS200: Algorithm Analysis
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XII
Basic Graph Algorithms
Algorithms and Data Structures Lecture XI
Topological Sort (an application of DFS)
Elementary Graph Algorithms
Presentation transcript:

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 but not all of its edges have been discovered A vertex is black after all of its adjacent vertices have been discovered (the adj. list was examined completely)

BFS Running Time Given a graph G = (V,E) – Vertices are enqueued if their color is white. Once enqueued their color is set to grey. – Assuming that en- and dequeuing takes O(1) time the total cost of this operation is O(V) – Adjacency list of a vertex is scanned when the vertex is dequeued – The sum of the lengths of all lists is  (E). Consequently, O(E) time is spent on scanning them – Initializing the algorithm takes O(V) Total running time O(V+E) (linear in the size of the adjacency list representation of G)

BFS Properties Given a graph G = (V,E), BFS discovers all vertices reachable from a source vertex s It computes the shortest distance to all reachable vertices It computes a breadth-first tree that contains all such reachable vertices For any vertex v reachable from s, the path in the breadth first tree from s to v, corresponds to a shortest path in G

Breadth First Tree G  is a breadth-first tree – V  consists of the vertices reachable from s – for all v in V , there is a unique simple path from s to v in G  that is also a shortest path from s to v in G The edges in G  are called tree edges

Init all vertices DFS For Directed Graphs Visit all children recursively

DFS Edge Classification Ancestor(u): Ancestor of a vertex ‘u’ is any vertex that lies above ‘u’ on the path between ‘u’ and the root (including the root). Descendant(u) : Descendant of a vertex ‘u’ is any vertex that lies below ‘u’ on a path from ‘u’ down to a leaf. Tree Edge : edges which belong to the spanning tree Forward Edge : edges which connect node to descendent Back Edge : edges which connect node to ancestor Cross Edge : (u,v) is a cross edge if neither ‘v’ is a descendant of ‘u’ nor ‘u’ is a descendant of ‘v’.

DFS Example u x vw yz 1/ u x vw yz 2/ u x vw yz 1/ 2/ 3/ u x vw yz 1/ 2/ 3/4/ u x vw yz 1/ 2/ 3/4/ B u x vw yz 1/ 2/ 3/4/5 B

DFS Example (2) u x vw yz 1/ 2/ 3/64/5 B u x vw yz 1/ 2/7 3/64/5 B u x vw yz 1/ 2/7 3/64/5 B F u x vw yz 1/8 2/7 3/64/5 B F u x vw yz 1/8 2/7 3/64/5 B F 9/ u x vw yz 1/8 2/7 3/64/5 B F 9/ C

DFS Example (3) u x vw yz 1/8 2/7 3/64/5 B F 9/ C 10/ u x vw yz 1/8 2/7 3/64/5 B F 9/ C 10/ B u x vw yz 1/8 2/7 3/64/5 B F 9/ C 10/11 B u x vw yz 1/8 2/7 3/64/5 B F 9/12 C 10/11 B

DFS Edge Classification Tree edge (gray to white) – encounter new vertices (white) Back edge (gray to gray) – from descendant to ancestor

DFS Edge Classification (2) Forward edge (gray to black) – from ancestor to descendant Cross edge (gray to black) – remainder – between trees or subtrees

DFS Edge Classification (3) Tree and back edges are important Most algorithms do not distinguish between forward and cross edges

DFS Algorithm When DFS returns, every vertex u is assigned – a discovery time d[u], and a finishing time f[u] Running time – DFS-Visit is called once for every vertex its only invoked on white vertices, and paints the vertex gray immediately – for each DFS-visit a loop interates over all Adj[v] – the loops in DFS take time  (V) each, excluding the time to execute DFS-Visit – the total cost for DFS-Visit is  (E) – the running time of DFS is  (V+E)

DFS Timestamping The DFS algorithm maintains a monotonically increasing global clock – discovery time d[u] and finishing time f[u] For every vertex u, the inequality d[u] < f[u] must hold

DFS Timestamping Vertex u is – white before time d[u] – gray between time d[u] and time f[u], and – black thereafter Notice the structure througout the algorithm. – gray vertices form a linear chain – correponds to a stack of vertices that have not been exhaustively explored (DFS-Visit started but not yet finished)

DFS Parenthesis Theorem Discovery and finish times have parenthesis structure – represent discovery of u with left parenthesis "(u" – represent finishin of u with right parenthesis "u)" – history of discoveries and finishings makes a well-formed expression (parenthesis are properly nested) Intuition for proof: any two intervals are either disjoint or enclosed – Overlaping intervals would mean finishing ancestor, before finishing descendant or starting descendant without starting ancestor

DFS Parenthesis Theorem (2)

Directed Acyclic Graphs A DAG is a directed graph with no cycles Often used to indicate precedences among events, i.e., event a must happen before b An example would be a parallel code execution Total order can be introduced using Topological Sorting

DAG Theorem A directed graph G is acyclic if and only if a DFS of G yields no back edges. Proof: – suppose there is a back edge (u,v); v is an ancestor of u. Thus, there is a path from v to u in G and (u,v) completes the cycle – suppose there is a cycle c; let v be the first vertex in c to be discovered and u is a predecessor of v in c. Upon discovering v the whole cycle from v to u is white We must visit all nodes reachable on this white path before return, i.e., vertex u becomes a descendant of v Thus, (u,v) is a back edge Thus, we can verify a DAG using DFS!

Recall : Topological Sort Precedence relations: an edge from x to y means one must be done with x before one can do y Intuition: can schedule task only when all of its subtasks have been scheduled A topological sort of a DAG is a linear ordering of all its vertices such that for any edge (u,v) in the DAG, u appears before v in the ordering

Topological Sort of DAG The following algorithm topologically sorts a DAG The linked lists comprises a total ordering Note, larger finishing time appears earlier Topological-Sort(G) 1) call DFS(G) to compute finishing times f[v] for each vertex v 2) as each vertex is finished, insert it onto the front of a linked list 3) return the linked list of vertices

Topological Sort Correctness Claim: for a DAG, an edge When (u,v) explored, u is gray. We can distinguish three cases – v = gray  (u,v) = back edge (cycle, contradiction) – v = white  v becomes descendant of u  v will be finished before u  f[v] < f[u] – v = black  v is already finished  f[v] < f[u] The definition of topological sort is satisfied

Topological Sort Running time – depth-first search: O(V+E) time – insert each of the |V| vertices to the front of the linked list: O(1) per insertion Thus the total running time is O(V+E)