csci 210: Data Structures Graph Traversals

Similar documents
csci 210: Data Structures Graph Traversals

Basic Graph Algorithms (CLRS B.4-B.5, )

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Outlines: Graphs Part-2

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

CSI 604 Elementary Graph Algorithms

Graph Representation

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

Graph: representation and traversal

DFS & STRONGLY CONNECTED COMPONENTS

Graph representation

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Introduction to Algorithms. Lecture 11

Graph implementations :

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Mystery Algorithm! ALGORITHM MYSTERY( G = (V,E), start_v ) mark all vertices in V as unvisited mystery( start_v )

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Homework Assignment #3 Graph

Representations of Graphs

Elementary Graph Algorithms

Basic Graph Algorithms

811312A Data Structures and Algorithms, , Exercise 6, solution

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Graph Search. Adnan Aziz

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

Review: Graph Theory and Representation

Graph Algorithms. Definition

Three Graph Algorithms

Three Graph Algorithms

Graph: representation and traversal

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search

Graphs. Motivations: o Networks o Social networks o Program testing o Job Assignment Examples: o Code graph:

Chapter 22. Elementary Graph Algorithms

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

22.3 Depth First Search

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Algorithm Design and Analysis

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Graph Algorithms Using Depth First Search

Announcements. HW3 is graded. Average is 81%

W4231: Analysis of Algorithms

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

Fundamental Algorithms

22.1 Representations of graphs

Inf 2B: Graphs, BFS, DFS

TIE Graph algorithms

TIE Graph algorithms

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Design and Analysis of Algorithms

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

CS302 - Data Structures using C++

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

Graph Algorithms. Andreas Klappenecker

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Section authors: Hamilton Clower, David Scott, Ian Dundore.

Copyright 2000, Kevin Wayne 1

Minimum Spanning Trees Ch 23 Traversing graphs

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Outline. Graphs. Divide and Conquer.

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

CSE 100: GRAPH ALGORITHMS

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Algorithms: Lecture 10. Chalmers University of Technology

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

15 211: RECITATION7, SECTION M. Hui Han Chin,


Sample Solutions to Homework #4

Chapter 22 Elementary Graph Algorithms

Algorithm Design and Analysis

Basic Graph Definitions

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Homework No. 4 Answers

Graphs: basic concepts and algorithms

Suggested Study Strategy

Graph Algorithms. Textbook reading. Chapter 3 Chapter 4. CSci 3110 Graph Algorithms 1/41

CS 361 Data Structures & Algs Lecture 13. Prof. Tom Hayes University of New Mexico

Graduate Algorithms CS F-15 Graphs, BFS, & DFS

(Re)Introduction to Graphs and Some Algorithms

Connectivity. Use DFS or BFS. 03/11/04 Lecture 18 1

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

Graph Search Methods. Graph Search Methods

Chapter 28 Graphs and Applications. Objectives

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Cut vertices, Cut Edges and Biconnected components. MTL776 Graph algorithms

3.1 Basic Definitions and Applications

Info 2950, Lecture 16

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Data Structures and Algorithms. Chapter 7. Graphs

Final Exam. EECS 2011 Prof. J. Elder - 1 -

implementing the breadth-first search algorithm implementing the depth-first search algorithm

Transcription:

csci 210: Data Structures Graph Traversals

Graph traversal (BFS and DFS) G can be undirected or directed We think about coloring each vertex WHITE before we start GRAY after we visit a vertex but before we visited all its adjacent vertices BLACK after we visit a vertex and all its adjacent vertices We store all GRAY vertices---these are the vertices we have seen but we are not done with Depending on the structure (queue or list), we get BFS or DFS We remember from which vertex a given vertex w is colored GRAY ---- this is the vertex that discovered w, or the parent of w

BFS G can be undirected or directed Initialize: for each v in V color(v) = WHITE parent(v) = NULL Traverse(v) color(v) = GRAY create an empty set S insert v in S while S not empty delete node u from S for all adjacent edges (u,w) of e in E do if color(w) = WHITE» color(w) = GRAY» parent(w) = u» insert w in S color(u) = BLACK

Breadth-first search (BFS) How it works: BFS(v) start at v and visit first all vertices at distance =1 followed by all vertices at distance=2 followed by all vertices at distance=3... BFS corresponds to computing the shortest path (in terms of number of edges) from v to all other vertices

BFS G can be undirected or directed BFS-initialize: for each v in V color(v) = WHITE d[v] = infinity parent(v) = NULL BFS(v) color(v) = GRAY d[v] = 0 create an empty queue Q Q.enqueue(v) while Q not empty Q.dequeue(u) for all adjacent edges (u,w) of e in E do if color(w) = WHITE» color(w) = GRAY» d[w] = d[u] + 1» parent(w) = u» Q.enqueue(w) color(u) = BLACK 5

BFS We can classify edges as discovery (tree) edges: edges used to discover new vertices non-discovery (non-tree) edges: lead to already visited vertices The distance d(u) corresponds to its level For each vertex u, d(u) represents the shortest path from v to u justification: by contradiction. If d[u]=k, assume there exists a shorter path from v to u... Assume G is undirected (similar properties hold when G is directed). connected components are defined undirected graphs (note: on directed graphs: strong connectivity) As for DFS, the discovery edges form a tree, the BFS-tree BFS(v) visits all vertices in the connected component of v If (u,w) is a non-tree edges, then d(u) and d(w) differ by at most 1. If G is given by its adjacency-list, BFS(v) takes O( V + E ) time. 6

BFS Putting it all together: Proposition: Let G=(V,E) be an undirected graph represented by its adjacency-list. A BFS traversal of G can be performed in O( V + E ) time and can be used to solve the following problems: testing whether G is connected computing the connected components (CC) of G computing a spanning tree of the CC of v DFS computing a path between 2 vertices, if one exists computing a cycle, or reporting that there are no cycles in G computing the shortest paths from v to all vertices in the CC ov v 7

Depth-first search (DFS) G can be directed or undirected use Traverse(v) with S = stack or recursively DFS(v) mark v visited for all adjacent edges (v,w) of v do if w is not visited parent(w) = v (v,w) is a discovery (tree) edge DFS(w) else (v,w) is a non-discovery (non-tree) edge 8

DFS Assume G is undirected (similar properties hold when G is directed). DFS(v) visits all vertices in the connected component of v The discovery edges form a tree: the DFS-tree of v justification: never visit a vertex again==> no cycles we can keep track of the DFS tree by storing, for each vertex w, its parent The non-discovery (non-tree) edges always lead to a parent If G is given as an adjacency-list of edges, then DFS(v) takes O( V + E ) time. 9

DFS Putting it all together: Proposition: Let G=(V,E) be an undirected graph represented by its adjacency-list. A DFS traversal of G can be performed in O( V + E ) time and can be used to solve the following problems: testing whether G is connected computing the connected components (CC) of G computing a spanning tree of the CC of v computing a path between 2 vertices, if one exists computing a cycle, or reporting that there are no cycles in G 10