Graph Search. Adnan Aziz

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

Graph Representation

Basic Graph Algorithms

Elementary Graph Algorithms

Graph implementations :

Design and Analysis of Algorithms

Representations of Graphs

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

Graph representation

Chapter 22. Elementary Graph Algorithms

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

Outlines: Graphs Part-2

Graph: representation and traversal

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

CSI 604 Elementary Graph Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

DFS & STRONGLY CONNECTED COMPONENTS

Graph Algorithms. Definition

COT 6405 Introduction to Theory of Algorithms

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

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

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

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

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

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

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

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

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

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

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Unit 2: Algorithmic Graph Theory

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

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

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

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

Graph: representation and traversal

Review: Graph Theory and Representation

Introduction to Algorithms. Lecture 11

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

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

Elementary Graph Algorithms

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Graph. Vertex. edge. Directed Graph. Undirected Graph

22.1 Representations of graphs

Announcements Problem Set 4 is out!

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

W4231: Analysis of Algorithms

22.3 Depth First Search

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

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

Sample Solutions to Homework #4

Topic 12: Elementary Graph Algorithms

Data Structures and Algorithms. Werner Nutt

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Chapter 22 Elementary Graph Algorithms

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

CSC263 Week 8. Larry Zhang.

Data Structures and Algorithms. Chapter 7. Graphs

TIE Graph algorithms

TIE Graph algorithms

Announcements. HW3 is graded. Average is 81%

Dijkstra s Shortest Path Algorithm

Algorithm Design and Analysis

csci 210: Data Structures Graph Traversals

CS 310 Advanced Data Structures and Algorithms

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

Design and Analysis of Algorithms

Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

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

Minimum Spanning Trees Ch 23 Traversing graphs

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

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

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

Introduction to Algorithms

Outline. 1 Introduction. 2 Algorithm. 3 Example. 4 Analysis. 5 References. Idea

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part A

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

Homework Assignment #3 Graph

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Figure 1: A directed graph.

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

csci 210: Data Structures Graph Traversals

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

Strongly Connected Components

Inf 2B: Graphs, BFS, DFS

Graph Representations and Traversal

CS2 Algorithms and Data Structures Note 9

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

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

Transcription:

Graph Search Adnan Aziz Based on CLRS, Ch 22. Recall encountered graphs several weeks ago (CLRS B.4) restricted our attention to definitions, terminology, properties Now we ll see how to perform basic operations on graphs. Implementation Assumption vertex set V = {1, 2, 3,..., n}. Two standard ways of implementing a graph G = (V, E): 1. collection of adjacency lists 2. adjacency matrix Usually, we prefer adjacency lists, since it uses considerably less memory when E V 2. A couple of situations where adjacency matrix is better: graph is dense, i.e., E V 2 frequently need to check whether an edge exists from u to v Adjacency list representation Given a graph G = (V, E), represent it by an array Adj of V lists, one for each vertex in V. For each u V the entry Adj[u] is a list of vertices v such that there is an edge from u to v. order within list can be arbitrary 1

Graph directed sum of all adjacency list lengths is E ; undirected sum of all adjacency list lengths is 2 E. Hence total memory needed to represent is O( V + E ). Weighted graph each edge has an associated weight; formally, there is a function w : E R. can easily store w(u, v) in an adjacency list representation Adjacency matrix representation Can t efficiently check if there is an edge from u to v use adjacency matrix representation (possibly takes more memory) Represent using a V V matrix A = (a ij ) such that a ij = 1 if (i, j) E = 0 otherwise needs Θ( V 2 ) memory, regardless of the number of edges Recall A T is the transpose of A, i.e., a T ij = a ji. For an undirected graph, we have a ij = a ji ; hence can save half the entries in the A matrix. Obvious can use adjacency matrix representation with weighted graphs. Breadth first search Formally, given a graph G = (V, E), and a designated source vertex s, what is the set of vertices that can be reached from s? Various applications geometric interpretation obivious (search maze), later will see less obvious applications (e.g., task scheduling). BFS a very simple algorithm for searching a graph prototype for many important graph algorithms Idea systematically explore the edges of G to find ( discover ) each vertex reachable from s works on both directed and undirected graphs 2

yields shortest paths, BFS tree Conceptually BFS uniformly expands the frontier between discovered and undiscovered vertices. Details: vertices are colored white, gray, or black; initially all are colored white a vertex is discovered the first time it s encountered in the search, at which point we change its color to gray the set of gray vertices represent the frontier process gray vertices: examine each outgoing edge, see if connected to any white vertex; if so, color that vertex gray crucial point: insert newly discovered vertices at end of queue ( BFS queue ) when done with all outgoing edges, remove vertex from the queue and change its color to black; process next vertex in queue // CLRS page 532 - not showing depths, parents BFS(G,s) for each vertex u in V[G] - {s} do color[u] <- WHITE color[s] <- GRAY enqueue(q,s) while Q!= 0 do u <- head[q] for each v in Adj[u] do if color[v] = WHITE then color[v] = GRAY enqueue(q,v) dequeue(q) color[u] = BLACK Properties of BFS Correctness after BFS terminates, all black vertices are reachable from s and all white vertices are colored white. 3

easy part black vertices are reachable slightly more involved white vertices are not reachable Runtime O( V + E ) each vertex enters Q at most once O( V ) scan adjacency list of each vertex at most once O( E ) Shortest distances define the shortest-path distance from s to v to be minimum number of edges in any path from s to v (set to if there is no path). Can compute the shortest distances as follows: keep an integer valued array d[x], x V initially, d[s] = 0, d[x] =, x s when discover a vertex v for the first time, say through edge (u, v), then set d[v] = d[u] + 1 Nontrivial fact when BFS terminates, the d array holds the shortest distances. BFS Tree initially, it s just the source vertex. whenever a white vertex v is discovered in the course of examining the outgoing edges of a vertex u, add the vertex v and edge (v, u) to the BFS tree. set u to be the parent of v in the BFS tree Since a vertex is discovered at most once, it has at most one parent. Can talk about ancestor, descendant relative to source vertex. Fact the BFS tree can be used to find shortest paths follow the parent links all the way up to s Depth First Search Another way of exploring a graph keep exploring from most recently discovered vertex nothing else left backtrack 4

More natural way of exploring a graph. // simplified version of algorithm in CLRS, page 541 DFS(G) for each vertex u in V[G] do color[u] <- WHITE p[u] <- NIL time <- 0 for each vertex u in V[G] do if color[u] = WHITE then DFS_visit(u) DFS_visit(u) color[u] = GRAY time <- time + 1 d[u] <- time for each v in Adj[u] do if color[v] = WHITE then p[v] <- u DFS_visit(v) color[u] <- BLACK time <- time + 1 f[u] <- time Keep track of additional data in search: timestamps and parents 1. first time vertex encountered, time when vertex finished processing 2. vertex from which first discovered Factoids: Can use DFS to topologically sort an acyclic graph G = (V, E) linear ordering of all vertices such that if (u, v) E, then u appears before v in the ordering compute the strongly connected components of a graph 5

strongly connected component maximal set of vertices U V having the property that for ever pair of vertices x, y such that x U and y U, there is a path from x to y and vice versa computation of articulation points, bridges, biconnected components widespread use in Artificial Intelligence 6