Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
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.
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.
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 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 11 DFS Properties of DFS Topological sort.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
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.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
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
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.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
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.
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.
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.
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
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
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.
Graphs.
© 2004 Goodrich, Tamasia Recall: Digraphs A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights.
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.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Properties and Applications of Depth-First Search Trees and Forests
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
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.
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
Breadth-First Search (BFS)
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.
CMSC 341 Graphs.
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
Graph Representation (23.1/22.1)
Richard Anderson Autumn 2016 Lecture 5
Topological Sort (an application of DFS)
Lecture 11 Graph Algorithms
Presentation transcript:

Theory of Computing Lecture 6 MAS 714 Hartmut Klauck

Traversing Graphs We are given a graph G=(V,E) Starting vertex s The goal is to traverse the graph, i.e., to visit each vertex at least once – For example to find a marked vertex t or decide if t is reachable from s Two variants: – Breadth First Search (BFS) – Depth First Search (DFS)

Traversing Graphs Common to both procedures: – Use a datastructure with the following operations: Insert a vertex Remove a vertex – Maintain an active vertex (start with s) – Maintain an array of vertices already visited – Then: Insert all (unvisited) neighbors of the active vertex, mark it as visited Remove a vertex v and make it active

The Datastructure We distinguish by the rule that determines the next active vertex Alternative 1: queue – FIFO (first in first out) Alternative 2: stack – LIFO (last in first out)

Result Alternative 1: FIFO – Breadth First Search – Neighbors of s will be visited before their neighbors etc. Alternative 2: LIFO – Depth First Search – Insert neighbors, last neighbor becomes active, then insert his neighbors, last neighbor becomes active etc.

Traversing Graphs With both methods eventually all reachable vertices are visited Different applications: – BFS can be used to find shorted paths in unweighted graphs – DFS can be used to topologically sort a directed acyclic graph

Depth First Search If we use a stack as datastructure we get Depth First Search (DFS) Typically, DFS will maintain some extra information: – Time when v is put on the stack – Time, when all neighbors of v have been examined This information is useful for applications

Datastructure: Stack A stack is a linked list together with two operations – push(x,S): Insert element x at the front of the list S – pop(S): Remove the front element of the list S Implementation: – Need to maintain only the pointer to the front of the stack – Useful to also have peek(S): Find the front element but don’t remove

Digression: Recursion and Stacks Our model of Random Access Machines does not directly allow recursion – Neither does any real hardware Compilers will “roll out” recursive calls – Put all local variables of the calling procedure in a safe place – Execute the call – Return the result and restore the local variables

Recursion The best datastructure for this is a stack – Push all local variables to the stack – LIFO functionality is exactly the right thing Example: Recursion tree of Quicksort

DFS Procedure: 1.For all v: ¼ (v)=NIL, d(v)=0, f(v)=0 2.Enter s into the stack S, set TIME=1, d(s)=TIME 3.While S is not empty a)v=peek(S) b)Find the first neighbor w of v with d(w)=0: – push(w,S), ¼ (w)=v, TIME=TIME+1, d(w)=TIME c)If there is no such w: pop(S), TIME=TIME+1, f(v)=TIME

DFS The array d(v) holds the time we first visit a vertex. The array f(v) holds the time when all neighbors of v have been processed “discovery” and “finish” In particular, when d(v)=0 then v has not been found yet

Simple Observations Vertices are given d(v) numbers between 1 and 2n Each vertex is put on the stack once, and receives the f(v) number once all neighbors are visited Running time is O(n+m)

A recursive DFS Stacks are there to roll out recursion Consider the procedure in a recursive way! Furthermore we can start DFS from all unvisited vertices to traverse the whole graph, not just the vertices reachable from s We also want to label edges – The edges in ( ¼ (v),v) form trees: tree edges – We can label all other edges as back edges cross edges forward edges

Edge classification Lemma: the edges ( ¼ (v),v) form a tree Definition: – Edges going down along a path in a tree (but not tree edge) are forward edges – Edges going up along a path in a tree are back edges – Edges across paths/tree are cross edges A vertex v is a descendant of u if there is a path of tree edges from u to v Observation: descendants are discovered after their “ancestors” but finish before them

Example: edge labeling Tree edges, Back edges, Forward edges, Cross edges

Recursive DFS DFS(G): 1.TIME=1 (global variable) 2.For all v: ¼ (v)=NIL, d(v)=0, f(v)=0 3.For all v: if d(v)=0 then DFS(G,v) DFS(G,v) 1.TIME=TIME+1, d(v)=TIME 2.For all neighbors w of v: 1.If d(w)=0 then (v,w) is tree edge, DFS(G,w) 2.If d(w)  0 and f(w)  0 then cross edge or forward edge 3.If d(w)  0 and f(w)=0 then back edge 3.TIME=TIME+1, f(v)=TIME

Recursive DFS How to decide if forward or cross edge? – Assume, (v,w) is an edge and f(w) is not 0 – If d(w)>d(v) then forward edge – If d(v)>d(w) then cross edge

Application 1: Topological Sorting A DAG is a directed acyclic graph – A partial order on vertices A topological sorting of a DAG is a numbering of vertices s.t. all edges go from smaller to larger vertices – A total order that is consistent with the partial order

DAGs Lemma: G is a DAG iff there are no back edges – Proof: If there is a back edge, then there is a cycle The other direction: suppose there is a cycle c Let u be the first vertex discovered on c v is u’s predecessor on c Then v is a descendant of u, i.e., d(v)>d(u) and f(v)<f(u) When edge (v,u) is processed: f(u)=0, d(v)>d(u) Thus (v,u) is a back edge

Topological Sorting Algorithm: – Output the vertices in the reverse order of the f(v) as the topological sorting of G I.e., put the v into a list when they finish in DFS, so that the last finished vertex is first in list

Topological Sorting We need to prove correctness – Certainly we provide a total ordering of vertices – Now assume vertex i is smaller than j in the ordering – I.e., i finished after j – Need to show: there is no path from j to i – Proof: j finished means all descendants of j are finished Hence i is not a descendant of j (otherwise i finishes first) If j is a descendant of i then a path from j to i must contain a back edge (but those do not exist in a DAG) If j is not a descendant of i then a path from j to i contains a cross edge, but then f(i)< f(j)

Topological Sorting Hence we can compute a topological sorting in linear time

Application 2: Strongly connected components Definition: – A strongly connected component of a graph G is a maximal set of vertices V’ such that for each pair of vertices v,w in V’ there is a path from v to w Note: in undirected graphs this corresponds to connected components, but here we have one-way roads Strongly connected components (viewed as vertices) form a DAG inside a graph G

Strongly connected components Algorithm – Use DFS(G) to compute finish times f(v) for all v – Compute G T [Transposed graph: edges (u,v) replaced by (v,u)] – Run DFS(G T ), but in the DFS procedure go over vertices in order of decreasing f(v) from the first DFS – Vertices in a tree generated by DFS(G T ) form a strongly connected component

Strongly connected components Time: O(n+m) We skip the correctness proof Note the usefulness of the f(v) numbers computed in the first DFS