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.

Slides:



Advertisements
Similar presentations
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
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.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
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.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Graph Searching CSE 373 Data Structures Lecture 20.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
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?
Algorithms and Data Structures
Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:
Zhengjin Graphs: Adjacency Matrix ● Example: a d bc A ?? 4.
Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.
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:
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?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
1 Data Structures DFS, Topological Sort Dana Shapira.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
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)
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
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.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
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.
1 Graphs - Definitions - Breadth First Search - Depth First Search - Connectivity - Topological Ordering.
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.
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.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
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.
Topological Sort: Definition
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.
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
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.
Graphs – Breadth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Three Graph Algorithms
Binhai Zhu Computer Science Department, Montana State University
Elementary Graph Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Finding Shortest Paths
BFS,DFS Topological Sort
Breadth-First Search The Breadth-first search algorithm
Basic Graph Algorithms
Graphs Definitions Breadth First Search Depth First Search
Algorithms Searching in a Graph.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

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 –In-order Traversal –Post-order Traversal

2 Graph Traversals 2 graph traversal methods –Breadth-First Search (BFS): Starting at a node visit ALL of your neighbors Then visit the neighbors of your neighbors and so on Like a wave-front expanding outwards from a source node –Depth-First Search (DFS) Starting at a node, follow a path all the way until you cannot move any further Then backtrack and try another branch Do this until all nodes have been visited

3 Breadth-First Search - Idea Given a graph G = (V, E), start at the source vertex “s” and discover which vertices are reachable from s –At any time there is a “frontier” of vertices that have been discovered, but not yet processed (gray vertices) Next pick the nodes in the frontier in sequence and discover their neighbors, forming a new “frontier” –Breadth-first search is so named because it visits vertices across the entire breadth of this frontier before moving on s

4 BFS - Continued Represent the final result as follows: –For each vertex v  V, we will store d[v] which is the distance (length of shortest path) from s to v Distance between a vertex “v” and “s” is defined to be the minimum number of edges on a path from “s” to “v” Note that d[s] = 0 –We will also store a predecessor (or parent) pointer pred[v], which indicates the first vertex along the shortest path if we walk from v backwards to s We will let pred[s] = 0 Notice that these predecessor pointers are sufficient to reconstruct the shortest path to any vertex

5 BFS – Implementation Initially all vertices (except the source) is colored white, meaning they have not been discovered just yet When a vertex is first discovered, it is colored gray (and is part of the frontier) When a gray vertex is processed, it becomes black s s s

6 BFS - Implementation The search makes use of a FIFO queue, Q We also maintain arrays –color[u], which holds the color of vertex u either white, gray, black –pred[u], which points to the predecessor of u The vertex that discovered u –d[u], the distance from s to u s s s

7 BFS – Implementation BFS(G, s){ for each u in V- {s} { // Initialization color[u] = white; d[u] = INFINITY; pred[u] = NULL; } //end-for color[s] = GRAY; // initialize source s d[s] = 0; pred[s] = NULL; Q = {s}; // Put s in the queue while (Q is nonempty){ u = Dequeue(Q); // u is the next vertex to visit for each v in Adj[u] { if (color[v] == white){ // if neighbor v undiscovered color[v] = gray; // … mark is discovered d[v] = d[u] + 1; // … set its distance pred[v] = u; // … set its predecessor Enqueue(v); //… put it in the queue } //end-if } //end-for color[u] = black; // we are done with u } //end-while } //end-BFS Running Time? O(n + e) O(1) O(n) O(e) n times

8 BFS - Example t s x w v u ∞ ∞ ∞ ∞ 0 Q: s t s x w vu ∞ 1 ∞ ∞ 1 0 Q: v, x t s x w vu ∞ Q: x, u, w t s x w vu ∞ Q: u, w t s x w vu Q: w, t t s x w vu Q: ∞

9 BFS Tree t s x w vu v x u t w The predecessor pointers of the BFS define an inverted tree If we reverse these edges, we get a rooted, unordered tree called a BFS tree for G –There are many potential BFS trees for a graph depending on where the search starts and in what order vertices are placed on the queue These edges of G are called the tree edges, and the remaining edges are called the cross edges

10 BFS Tree t s x w vu v x u t w It is not hard to prove that if G is an undirected graph, then cross edges always go between two nodes that are at most ONE level apart in the BFS tree –The reason is that if any cross edge spanned two or more levels, then when the vertex at the higher level (closer to the root) was being processed, it would have discovered the other vertex, implying that the other vertex would appear on the next level of the tree, a contradiction In a directed graph, cross edges may come up at an arbitrary number of levels