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?

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Lecture 12 Prof. Constantinos Daskalakis CLRS
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.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
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.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
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?
Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:
Breadth-First and Depth-First Search
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:
1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 11 DFS Properties of DFS 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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Shortest Path Problems
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.
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.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 12 Graphs and basic search algorithms Motivation Definitions and properties Representation.
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.
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
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.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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.
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.
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.
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.
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.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Chapter 05 Introduction to Graph And Search Algorithms.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
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.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
CS138A Elementary Graph Algorithms Peter Schröder.
Graphs – Breadth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Elementary Graph Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Finding Shortest Paths
Basic Graph Algorithms
Data structure for graph algorithms: Adjacent list, Adjacent matrix
Presentation transcript:

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? Will removing a single edge disconnect G? If G is directed, what are the strongly connected components? If G is the WWW, follow links to locate information. Most graph algorithms need to examine or process each vertex and each edge.

Breadth-First Search Find the shortest path from a source node s to all other nodes. It returns the distance of each vertex from s. a tree of shortest paths called the breadth-first tree. Idea: Find nodes at distance 0, then at distance 1, then at distance 2, etc.

A BFS Example s d b g f e c a L =  s  0

s d b g f e c a Visualized as many simultaneous explorations starting from s and spreading out independently. L =  s  0 L =  a, c  1 frontier of exploration

s d b g f e c a L =  s  0 L =  a, c  1 L =  d, e, f  2 Dashed edges were explored but had previously been discovered.

s d b g f e c a L =  s  0 L =  a, c  1 L =  d, e, f  2 L =  b, g  3

s d b g f e c a L =  s  0 L =  a, c  1 L =  d, e, f  2 L =  b, g  3 The Finish

s d b g f e c a Breadth-First Tree d(b): shortest distance from s to b The number of visited vertices is one more than the number of tree edges and hence at most one more than the number of explored edges.

The BFS Algorithm BFS(G, s) for each v  V(G) – s do d(v)   // shortest distance from s d(s)  0 // initialization L   s  T   i  0 while L ≠  do // all nodes at distance i from s L    for each u  L do for each v  Adj(u) do if d(v) =  then // not yet visited d(v)  d(u) + 1 insert v into L T  T  {(u, v)} i  i i i+1 i

Correctness Lemma For each i, the set L includes all nodes at distance i from s. i Proof By induction on the distance k from s. s … uv k Basis: L =  s  includes the only node at distance 0 from s. 0 Inductive step: Suppose L consists of all nodes at distance k ≥ 0 from s. Every node v at distance k+1 from s must be adjacent to a node u at distance k. By hypothesis u  L. Thus v  L. k k k+1 Corollary At the finish of BFS, d(v) is the length of the shortest path from s to v.

Running Time O(|E|) if G is represented using adjacency lists. The total number of edge scans is ≤ |E| for a directed graph ≤ 2 |E| for an undirected graph u v e u v e O(|V| ) if represented as an adjacency matrix. 2 Every vertex is inserted at most once into some list L. i The number of inserted vertices is at most one more than the number of scanned edges; hence ≤ |E| + 1.

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 before exploring G and G From Computer Algorithms by S. Baase and A. van Gelder

The DFS Algorithm DFS(G) time  0 // global variable for each v  V(G) do disc(v)  unseen for each v  V(G) do if disc(v) = unseen then DFS-visit(v) time  time + 1 disc(v)  time for each u  Adj(v) do if disc(u) = unseen then DFS-visit(u)

A DFS Example a l g f b ced j k ih time = 1

a l g f b ced j k ih 1 2

a l g f b c e d j k ih

a l g f b ced j k ih

a l g f b ced j k ih

a l g f b ced j k ih