Graphs-Part II Depth First Search (DFS)

Slides:



Advertisements
Similar presentations
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Advertisements

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
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 8 Graph algorithms: Strongly connected components.
Tirgul 11 DFS Properties of DFS Topological sort.
Graphs-Part II Depth First Search (DFS). We Already Covered Breadth First Search(BFS) Traverses the graph one level at a time – Visit all outgoing edges.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
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.
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 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
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.
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.
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.
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
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.
CS138A Elementary Graph Algorithms Peter Schröder.
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Introduction to Algorithms
Breadth-First Search (BFS)
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.
Topological Sort Minimum Spanning Tree
Depth-First Search Depth-first search is a strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the.
CS200: Algorithm Analysis
Graphs Breadth First Search & Depth First Search
Graph: representation and traversal CISC4080, Computer Algorithms
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill.
Binhai Zhu Computer Science Department, Montana State University
Many slides here are based on E. Demaine , D. Luebke slides
Elementary Graph Algorithms
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
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
Graphs Chapter 15 explain graph-based algorithms Graph definitions
BFS,DFS Topological Sort
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Graph Representation (23.1/22.1)
Basic Graph Algorithms
Decrease and Conquer Decrease and conquer technique Insertion sort
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms Searching in a Graph.
Text Book: Introduction to algorithms By C L R S
Algorithms CSCI 235, Spring 2019 Lecture 34 Graphs III
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Algorithms CSCI 235, Spring 2019 Lecture 33 Graphs II
Premaster Course Algorithms 1 Chapter 5: Basic Graph Algorithms
Presentation transcript:

Graphs-Part II Depth First Search (DFS)

We Already Covered Breadth First Search(BFS) Traverses the graph one level at a time Visit all outgoing edges from a node before you go deeper Needs a queue BFS creates a tree called BFS-Tree Time Complexity O(V+E)

BFS & Shortest Path BFS can be used to compute the shortest path (minimum number of edges) from source s and any reachable nodes v Maintain a counter for each node When a node x is first visited from parent y  x.counter = y.counter + 1

Connected Undirected Graph G = (V, E) is called connected iff From any node u, we can read all other nodes Connected graph Un-Connected graph What is the time complexity to decide if G is a connected graph? Take any node from G and apply BFS If you reached all nodes  G is connected Otherwise  G is not connected Time Complexity O(V+E)

Depth First Search (DFS) Traverse the graph by going deeper whenever possible DFS uses a stack, hence can be implemented using recursion While traversing keep some useful information u.color: White  u has not been visited yet Gray  u is visited but its descendent are not completed yet Black  u and its all descendent are visited u.startTime (u.d)= the first time u is visited u.endTime (u.f) = the last time u will be seen (after all descendent of u are processed)

Notice: We maintain 4 arrays during the traversal DFS: Pseudocode U is just discovered…make it gray Time is a global veriable = U start time in If v is not seen before, recursively visit v = Notice: We maintain 4 arrays during the traversal d[u] First time u is seen f[u] Last time u is seen color[u]  {white, Gray, Black} π[u]  parent of node u

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

Depth First Search (DFS): Example

DFS: Forest The DFS may create multiple disconnected trees called forest

DFS: Time Complexity = = Each node is recursively called once  O(|V|) in = Each node is recursively called once  O(|V|) The For Loop (Step 4) visits the outgoing edges of each node  Total O(|E|) Total Complexity O( |V| + |E|)

Analyzing The Collected Info. The algorithm computes for each node u u.startTime (u.d) u.endTime (u.f) These intervals form a nested structure Parenthesis Theorem If u has (u.d, u.f) , and v has (v.d, v.f), then either: Case 1: u descendant of v in DFS Case 2: v descendant of u in DFS Case 3: u and v are disjoint (different trees in the forest) v.d < u.d < u.f < v.f u.d < v.d < v.f < u.f (u.d, u.f) and (v.d, v.f) are not overlapping

Based on the numbers you can draw the DFS trees Example Based on the numbers you can draw the DFS trees v, y, x are all descendants of u In other words: u is an ancestor to v, y, x w and u are disjoint

Classification of Graph Edges While doing DFS, we can label the edges Tree edges: Included in the DFS trees Forward edges: From ancestor to already-visited descendant Backward edges: From descendant to already-visited ancestor Cross edges: Any other edges

Depth First Search (DFS): Example Tree edge Backward edge Cross edge Forward edge Backward edge

Cycles in Graphs Cyclic graph (Graph containing cycles) Acyclic graph (Graph without cycles) Called: Directed Acyclic Graph DAG How to know if a graph has a cycle or not? What is the time complexity?

Cycles in Graphs (Cont’d) Answer Perform DFS on the graph If there are backward edges  there is a cycle Otherwise  there are no cycles