Tirgul 11 DFS Properties of DFS Topological sort.

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 – Depth First Search ORD DFW SFO LAX
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
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:
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 8 Graph algorithms: Strongly connected components.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
David Luebke 1 5/20/2015 CS 332: Algorithms Graph Algorithms.
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.
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.
1 7/3/2015 ITCS 6114 Graph Algorithms. 2 7/3/2015 Depth-First Search ● Depth-first search is another strategy for exploring a graph ■ Explore “deeper”
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
David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
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.
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.
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.
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.
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/6/2016 CS 332: Algorithms Graph Algorithms.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Liaquat Majeed Sheikh 1 1/25/2016 Graph Algorithms.
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.
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.
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 ?
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.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
CS138A Elementary Graph Algorithms Peter Schröder.
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 Minimum Spanning Tree
CSC 413/513: Intro to Algorithms
CS200: Algorithm Analysis
Many slides here are based on E. Demaine , D. Luebke slides
CS 3343: Analysis of Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Advanced Algorithms Analysis and Design
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

Tirgul 11 DFS Properties of DFS Topological sort

Depth First Search (DFS) We will now see another approach to graph traversal – Depth First Search (DFS). The strategy that we use in DFS is to go as “deep” as we can in the graph. We check the edges that expands from the last vertex we checked, and that wasn’t checked yet.

DFS – cont. Like BFS, the DFS algorithm colors the vertices as it goes. In the beginning of the algorithm, all the vertices are white. In the first time that the algorithm sees a vertex, it is painted in gray. When the algorithms finishes handling a vertex, it is painted in black. In addition, each vertex v has two time stamps. The first, v.d, is the time when it was painted in gray (discovered). The second, v.f, is the time when it was painted in black (finished).

DFS – pseudo code DFS(G) //initializing. for each vertex u  V[G] { u.color = white; u.prev = nil; } time = 0; for each vertex u  V[G] { if (u.color == white) DFS-VISIT(u) }

DFS – pseudo code (cont.) DFS-VISIT(u) u.color = gray; u.d = ++time; for each vertex v  adj[u] { if (v.color == white) { v.prev = u; DFS-VISIT(v); } u.color = black; u.f = ++time;

A short example uv w uv w 1/ uv w 2/ uv w 1/2/ 3/

A short example (cont.) uv w 1/2/ 3/ uv w 1/2/ 3/4 uv w 1/2/5 3/4 uv w 1/62/5 3/4

Running time of DFS : What is the running time of DFS ? Both loops in the DFS procedure takes O(|V|) time, not including the calls to DFS-VISIT. The algorithm calls DFS-VISIT exactly once for each vertex, because it is only called on white vertices. Each DFS-VISIT takes |adj[v]| to finish. Thus, the running time of the second loop is:  v  V |adj[v]| = Θ(E). And the total running time is: Θ(E+V).

predecessor subgraph of DFS Definition: the predecessor subgraph of DFS is the graph G π (V,E π ) when E π ={(v.prev, v) | v  V} (v.prev is defined during the run of DFS). The predecessor subgraph of DFS creates a depth-first rooted forest, which consists of several depth-first rooted trees. The coloring of vertices and the fact that we update the prev field only when we reach a white vertex ensures that the trees in the first-depth forest are disjoint.

Properties of DFS : The parenthesis theorem: Let G be a graph (directed or undirected) then after DFS on the graph: For each two vertices u, v exactly one of the following is true: [u.d, u.f] and [v.d, v.f] are disjoint. [u.d, u.f]  [v.d, v.f] and u is a descendant of v. [v.d, v.f]  [u.d, u.f] and v is a descendant of u. Immediate conclusion: a vertex v is a descendant of a vertex u in the first-depth forest iff [v.d, v.f]  [u.d, u.f].

Proof of the parenthesis theorem : We will consider the case where u.d < v.d If u.f > v.d this means that we first encountered v when u was still gray. Therefore, v is a descendant of u. Furthermore, since v was discovered after u, all the edges that expand from v are checked and it is painted black before u is painted in black. Thus, v.f < u.f and [v.d, v.f]  [u.d, u.f]. If u.f < v.d then [u.d, u.f] and [v.d, v.f] are disjoint. The case which v.d < u.d is symmetrical, only switch u and v in the above argument.

The DFS results: After DFS on directed graph. Each vertex has a time stamp. The edges in gray are the edges in the depth-first forest. The first-depth forest of the above graph. There is a correspondence between the discover and finish times of each vertex and the parenthesis structure below.

The white path theorem: Theorem: in a depth-first forest of a graph G, a vertex v is a descendant of a vertex u iff in the time u.d, which the algorithm discovers u, there is a path from u to v which consists only of white vertices. Proof:  Assume that u is a descendant of v, let w be a vertex on the path from u to v in the depth-first tree. The conclusion from the parenthesis theorem implies that u.d<w.d and thus w is white in time u.d

The white path theorem (cont.) Proof:  w.l.o.g. each other vertex along the path becomes a descendant of u in the tree. Let w be the predecessor of u in the path. According to the parenthesis theorem: w.f  u.f (they might be the same vertex). v.d>u.d and v.d<w.f. Thus, u.d<v.d<w.f  u.f. According to the parenthesis theorem, [v.d, v.f]  [u.d, u.f], and v must be a descendant of u. u v w

edge classification: Another interesting property of depth-first search is that it can be used to classify the edges of the graph. This classification can give important information on the graph. We define four types of edges: 1. tree edges are edges in G π. 2. back edges are edges which connects a vertex to it’s ancestor in a first-depth tree (a cycle). 3. forward edges are edges which are not tree edges but connects a vertex u to a descendant v in a first-depth tree. 4. cross edges are all the other edges.

A-cyclic graphs and DFS: A directed a-cyclic graph is denoted DAG. Theorem: A graph is a DAG iff during a DFS run on the graph, there are no back edges. Proof:  Assume that there is a back edge, (u,v). So v is an ancestor of u in the depth first tree and the edge (u,v) completes a cycle.  Assume that G contains a cycle c. Let v be the first vertex that DFS discovers. Let u be the predecessor of v in the cycle. In time v.d, there is a white path from v to u. According to the white path theorem, u is a descendant of v in the depth first tree. Thus, the edge (u,v) is a back edge.

Topological Sort A topological sort of a DAG G is a linear ordering of the vertices in G, such that if G contains an edge (u,v), then u appears before v in the ordering. If G contains a cycle, no such ordering exists. Topological-Sort(G) call DFS on G. As each vertex is finished, insert it onto the front of a linked list. What is topological sort good for ? DAG’s are used in many applications to denote precedence order in a set of events. A Topological Sort of such a graph suggests an order to the events.

Topological Sort - example How to dress in the morning ? After using DFS in order to compute the finish times we have the vertices, ordered according to the finish times and now we can dress… socks --- shirt --- tie --- pants --- shoes --- belts pants belt shirt tie shoessocks1/6 2/3 4/5 7/10 8/9 11/12