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
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

1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
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.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
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.
© 2004 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Zhengjin Graphs: Adjacency Matrix ● Example: a d bc A ?? 4.
Breadth-First and Depth-First Search
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
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?
1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
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.
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.
Breadth-First Search1 Part-H3 Breadth-First Search CB A E D L0L0 L1L1 F L2L2.
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
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.
Shortest Path Algorithms
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 12 Graphs and basic search algorithms Motivation Definitions and properties Representation.
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.
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.
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.
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.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
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.
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.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
CSC 413/513: Intro to Algorithms Graph Algorithms.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
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.
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
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
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.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Graphs – Breadth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Data Structures, Algorithms & Complexity
Elementary Graph Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
Advanced Algorithms Analysis and Design
Breadth-First Search The Breadth-first search algorithm
Basic Graph Algorithms
Algorithms Searching in a Graph.
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. Outputs 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, …

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

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

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 resulted in the discovery of no new vertices.

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 L =  s  0 L =  a, c  1 L =  d, e, f  2 L =  b, g  3

Breadth-First Tree s d b g f e c a d(b): shortest distance from s to b # visited vertices = 1 + # tree edges  1 + # 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. k Corollary At the finish of BFS, d(v) is the length of the shortest path from s to v. k k+1 By hypothesis u  L. Thus v  L. Every node v at distance k+1 from s must be adjacent to a node u at distance k.

Running Time O(|E|) if G is represented using adjacency lists. # edge scans ≤ |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 # inserted vertices  1 + # scanned edges Courtesy of Dr. Fernandez-Baca  |E| + 1.