Outlines: Graphs Part-2

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

Representations of Graphs

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

Elementary Graph Algorithms

COT 6405 Introduction to Theory of Algorithms

Graph Representation

Graph representation

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

Graph Search. Adnan Aziz

Design and Analysis of Algorithms

Introduction to Algorithms A graph G =(V, E) V = set of vertices E = set of edges

Graph implementations :

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Basic Graph Algorithms

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Graph: representation and traversal

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

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

Chapter 22. Elementary Graph Algorithms

22.1 Representations of graphs

Minimum Spanning Trees Ch 23 Traversing graphs

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Introduction to Algorithms. Lecture 11

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

DFS & STRONGLY CONNECTED COMPONENTS

Graph Algorithms. Definition

Unit 2: Algorithmic Graph Theory

csci 210: Data Structures Graph Traversals

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

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

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

Graph Search Methods. Graph Search Methods

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Graph Search Methods. Graph Search Methods

CSI 604 Elementary Graph Algorithms

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

Announcements Problem Set 4 is out!

CSC263 Week 8. Larry Zhang.

Graph Algorithms: Chapters Part 1: Introductory graph concepts

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

Sample Solutions to Homework #4

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

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

Chapter 22 Elementary Graph Algorithms

Topic 12: Elementary Graph Algorithms

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

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

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

csci 210: Data Structures Graph Traversals

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part A

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

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

Graph: representation and traversal

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

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

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

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

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

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

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

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

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

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

Graph Algorithms. Andreas Klappenecker

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

Review: Graph Theory and Representation

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Elementary Graph Algorithms

Figure 1: A directed graph.

Three Graph Algorithms

Three Graph Algorithms

Data Structures and Algorithms. Werner Nutt

W4231: Analysis of Algorithms

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

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

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

Unweighted directed graphs

TIE Graph algorithms

Single Source Shortest Paths

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

Homework Assignment #3 Graph

3.1 Basic Definitions and Applications

CS 206 Introduction to Computer Science II

Minimum Spanning Trees Ch 23 Traversing graphs

Trees Algorhyme by Radia Perlman

Breadth First Search. cse2011 section 13.3 of textbook

Dijkstra s Shortest Path Algorithm

TIE Graph algorithms

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

Week 12: Minimum Spanning trees and Shortest Paths

Suggested Study Strategy

Data Structures and Algorithms. Chapter 7. Graphs

Announcements. HW3 is graded. Average is 81%

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

Algorithm Design and Analysis

Homework No. 4 Answers

Transcription:

Elementary Graph Algorithms PART-2 1 Outlines: Graphs Part-2 Graph Search Methods Breadth-First Search (BFS): BFS Algorithm BFS Example BFS Time Complexity Output of BFS: Shortest Path Breath-First Tree Is the Graph Connected? 2 1

Graph Search Methods Many graph problems solved using a search method Path from one vertex to another Is the graph connected? etc. Commonly used search methods: Breadth-First Search (BFS) Depth-First Search (DFS) 3 Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method starts at a given vertex v and visits every vertex that is reachable from v. 2 3 8 1 4 5 9 10 6 7 11 4 2

Graph Search Methods Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately: build a tree on the graph Pick a vertex as the root Choose certain edges to produce a tree Note: might also build a forest if graph is not connected 5 Breadth First Search Example: Binary Tree (This is a special case of a graph). The order of search is across levels. The root is examined first; then children of the root; then the children of those nodes, and so on. 1 2 3 4 5 6 7 8 6 3

Breadth First Search Example: Directed Graph Pick a source vertex S to start. Find (or discover) the vertices that are adjacent to S. Pick each child of S in turn and discover their vertices adjacent to that child. Done when all children have been discovered and examined. This results in a tree that is rooted at the source vertex S. The idea is to find the distance from the selected Source vertex to all reachable vertices (Destinations). 7 Breadth-First Search Visit start vertex (s) and put into a FIFO queue. Repeatedly remove a vertex from the queue, visit its unvisited adjacent vertices, put newly visited vertices into the queue. All vertices reachable from the start vertex (s) (including the start vertex) are visited. 8 4

Breadth-First Search Again will associate vertex colors to guide the algorithm White vertices have not been discovered All vertices start out white Gray vertices are discovered but not fully explored They may be adjacent to white vertices Black vertices are discovered and fully explored They are adjacent only to black and gray vertices Explore vertices by scanning adjacency list of gray vertices 9 Breadth-First Search BFS(G, s) { // initialize vertices; 1 for each u V(G) {s{ 2 do color[u] = WHITE 3 d[u] = // distance from s to u 4 p[u] = NIL // predecessor or parent of u 5 color[s] = GRAY 6 d[s] = 0 7 p[s] = NIL 8 Q = Empty; 9 Enqueue (Q,s); // Q is a queue; initialize to s 10 while (Q not empty) { 11 u = Dequeue(Q); 12 for each v adj[u] { 13 if (color[v] == WHITE) 14 color[v] = GRAY; 15 d[v] = d[u] + 1; 16 p[v] = u; 17 Enqueue(Q, v); 18 color[u] = BLACK; W hat does d[v] represent? W hat does p[v] represent? 10 5

Breadth-First Search Lines 1-4 paint every vertex white, set d[u] to be infinity for each vertex (u), and set p[u] the parent of every vertex to be NIL. Line 5 paints the source vertex (s) gray. Line 6 initializes d[s] to 0. Line 7 sets the parent of the source to be NIL. Lines 8-9 initialize Q to the queue containing just the vertex (s). The while loop of lines 10-18 iterates as long as there remain gray vertices, which are discovered vertices that have not yet had their adjacency lists fully examined. This while loop maintains the test in line 10, the queue Q consists of the set of the gray vertices. 11 Breadth-First Search Prior to the first iteration in line 10, the only gray vertex, and the only vertex in Q, is the source vertex (s). Line 11 determines the gray vertex (u) at the head of the queue Q and removes it from Q. The for loop of lines 12-17 considers each vertex (v) in the adjacency list of (u). If (v) is white, then it has not yet been discovered, and the algorithm discovers it by executing lines 14-17. It is first grayed, and its distance d[v] is set to d[u]+1. Then, u is recorded as its parent. Finally, it is placed at the tail of the queue Q. When all the vertices on (u s) adjacency list have been examined, u is blackened in line 18. 12 6

Breadth-First Search: Example 13 Breadth-First Search: Example 0 Q: s 14 7

Breadth-First Search: Example 1 0 1 Q: w r 15 Breadth-First Search: Example 1 0 2 1 2 Q: r t x 16 8

Breadth-First Search: Example 1 0 2 2 1 2 Q: t x v 17 Breadth-First Search: Example 1 0 2 3 2 1 2 Q: x v u 18 9

Breadth-First Search: Example 1 0 2 3 2 1 2 3 Q: v u y 19 Breadth-First Search: Example 1 0 2 3 2 1 2 3 Q: u y 20 10

Breadth-First Search: Example 1 0 2 3 2 1 2 3 Q: y 21 Breadth-First Search: Example 1 0 2 3 2 1 2 3 Q: Ø 22 11

BFS: The Code Again other vert s BFS(G, s) { // initialize vertices for each u V(G) {s{ do color[u] = WHITE d[u] = p[u] = NIL color[s] = GRAY; d[s] = 0; p[s] = NIL; Q = Empty; Enqueue (Q,s); while (Q not empty) { u = Dequeue(Q); for each v adj[u] { if (color[v] == WHITE) So v = every vertex that appears in some adjacency list color[v] = GRAY; d[v] = d[u] + 1; p[v] = u; Enqueue(Q, v); color[u] = BLACK; W hat will be the running time? Total running time: O(V+E) 23 Touch every vertex: O(V) u = every vertex, but only once BFS: Time Complexity Given a graph G = (V, E) Vertices are enqueued if there color is white Assuming that en- and dequeuing takes O(1) time the total cost of this operation is O(V) Adjacency list of a vertex is scanned when the vertex is dequeued (and at most once) The sum of the lengths of all lists is Ο(E). Consequently, O(E) time is spent on scanning them Initializing the algorithm takes O(V) Total running time O(V+E) linear in the size of the adjacency list representation of G 24 12

BFS: The Code Again BFS(G, s) { // initialize vertices for each u V(G) {s{ do color[u] = WHITE d[u] = p[u] = NIL color[s] = GRAY d[s] = 0 p[s] = NIL Q = Empty; Enqueue (Q,s); while (Q not empty) { u = Dequeue(Q); for each v adj[u] { if (color[v] == WHITE) color[v] = GREY; d[v] = d[u] + 1; p[v] = u; Enqueue(Q, v); color[u] = BLACK; 25 Each vertex will be enqueue once. What will be the storage cost in addition to storing the tree? Queue Size Total space used: at least = O(max(degree(v))) at most = O(V) Shortest Paths Given a graph G = (V, E), BFS discovers all vertices reachable from a source vertex s It computes the shortest-path distance to all reachable vertices It computes a breadth-first tree that contains all such reachable vertices For any vertex v reachable from s, the path in the breadth first tree from s to v, corresponds to a shortest path in G. BFS calculates the shortest-path distance to the source node shortest-path distance = minimum number of edges from s to v = δ(s,v) = d[v] or δ(s,v) = if v not reachable from s 26 13

Breadth-First Tree BFS builds breadth-first tree, in which paths to root represent shortest paths in G Given a graph G = (V, E) with source s V, we define the prdecessor subgraph of G as G p = (V p, E p ), where V p = {v V : p[v] NIL {s E p = {(p[v], v) : v V p - {s G p is a breadth-first tree V p consists of the vertices reachable from s, and for all v V p, there is a unique simple path from s to v in G p that is also a shortest path from s to v in G. The edges in G p are called tree edges 27 Path From Vertex v To Vertex u Start a breadth-first search at vertex v. Terminate when vertex u is visited or when Q becomes empty (whichever occurs first). Time O(V+E) 28 14

Is The Graph Connected? Start a breadth-first search at any vertex of the graph. Graph is connected iff all n vertices get visited. Time O(V+E) 29 15