Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.

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.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
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.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
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?
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.
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:
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
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.
1 7/2/2015 ITCS 6114 Graph Algorithms. 2 7/2/2015 Graphs ● A graph G = (V, E) ■ V = set of vertices ■ E = set of edges = subset of V  V ■ Thus |E| =
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
David Luebke 1 8/7/2015 CS 332: Algorithms Graph Algorithms.
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
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.
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.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
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.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
Mudasser Naseer 1 1/9/2016 CS 201: Design and Analysis of Algorithms Lecture # 17 Elementary Graph Algorithms (CH # 22)
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.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
Liaquat Majeed Sheikh 1 1/25/2016 Graph Algorithms.
COSC 3101A - Design and Analysis of Algorithms 9 Knapsack Problem Huffman Codes Introduction to Graphs Many of these slides are taken from Monica Nicolescu,
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.
1 Algorithms CSCI 235, Fall 2015 Lecture 32 Graphs I.
Chapter 05 Introduction to Graph And Search Algorithms.
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.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Data Structures, Algorithms & Complexity
CSC317 Graph algorithms Why bother?
Graphs Breadth First Search & Depth First Search
Graph Representation, DFS and BFS
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
Graph Representation (23.1/22.1)
Basic Graph Algorithms
Algorithms Searching in a Graph.
Data structure for graph algorithms: Adjacent list, Adjacent matrix
Elementary Graph Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I
Presentation transcript:

Breadth First Search

Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency lists Graph G(V, E) is represented by array Adj of |V| lists For each u  V, the adjacency list Adj[u] consists of all the vertices adjacent to u in G The amount of memory required is: (V + E) Representations of Graphs

A graph G(V, E) assuming the vertices are numbered 1, 2, 3, …, |V| in some arbitrary manner, then representation of G consists of: |V| × |V| matrix A = (a ij ) such that 1 if (i, j)  E 0 otherwise Preferred when graph is dense –|E| is close to |V| 2 a ij = { Adjacency Matrix

Adjacency matrix of undirected graph

The amount of memory required is T(V 2 ) For undirected graph to cut down needed memory only entries on and above diagonal are saved –In an undirected graph, (u, v) and (v, u) represents the same edge, adjacency matrix A of an undirected graph is its own transpose A = A T It can be adapted to represent weighted graphs. Adjacency Matrix

Breadth First Search

One of simplest algorithm searching graphs A vertex is discovered first time, encountered Let G (V, E) be a graph with source vertex s, BFS –discovers every vertex reachable from s. –gives distance from s to each reachable vertex –produces BF tree root with s to reachable vertices To keep track of progress, it colors each vertex –vertices start white, may later gray, then black –Adjacent to black vertices have been discovered –Gray vertices may have some adjacent white vertices Breadth First Search

It is assumed that input graph G (V, E) is represented using adjacency list. Additional structures maintained with each vertex v  V are –color[u] – stores color of each vertex –π[u] – stores predecessor of u –d[u] – stores distance from source s to vertex u Breadth First Search

BFS(G, s) 1 for each vertex u  V [G] – {s} 2 do color [u] ← WHITE 3 d [u] ← ∞ 4 π[u] ← NIL 5 color[s] ← GRAY 6 d [s] ← 0 7 π[s] ← NIL 8 Q ← Ø /* Q always contains the set of GRAY vertices */ 9 ENQUEUE (Q, s) 10 while Q ≠ Ø 11 do u ← DEQUEUE (Q) 12 for each v  Adj [u] 13 do if color [v] = WHITE /* For undiscovered vertex. */ 14 then color [v] ← GRAY 15 d [v] ← d [u] π[v] ← u 17 ENQUEUE(Q, v) 18 color [u] ← BLACK Breadth First Search

      rstu vwxy  Q Except root node, s For each vertex u  V(G) color [u]  WHITE d[u]  ∞ π [s]  NIL

  0      rstu vwxy s Q Breadth First Search Considering s as root node color[s]  GRAY d[s]  0 π [s]  NIL ENQUEUE (Q, s)

1  0 1     rstu vwxy Breadth First Search DEQUEUE s from Q Adj[s] = w, r color [w] = WHITE color [w] ← GRAY d [w] ← d [s] + 1 = = 1 π[w] ← s ENQUEUE (Q, w) color [r] = WHITE color [r] ← GRAY d [r] ← d [s] + 1 = = 1 π[r] ← s ENQUEUE (Q, r) color [s] ← BLACK w Q r

1    rstu vwxy Breadth First Search DEQUEUE w from Q Adj[w] = s, t, x color [s] ≠ WHITE color [t] = WHITE color [t] ← GRAY d [t] ← d [w] + 1 = = 2 π[t] ← w ENQUEUE (Q, t) color [x] = WHITE color [x] ← GRAY d [x] ← d [w] + 1 = = 2 π[x] ← w ENQUEUE (Q, x) color [w] ← BLACK r Q tx

  rstu vwxy Breadth First Search DEQUEUE r from Q Adj[r] = s, v color [s] ≠ WHITE color [v] = WHITE color [v] ← GRAY d [v] ← d [r] + 1 = = 2 π[v] ← r ENQUEUE (Q, v) color [r] ← BLACK Q txv

Breadth First Search DEQUEUE t from Q Adj[t] = u, w, x color [u] = WHITE color [u] ← GRAY d [u] ← d [t] + 1 = = 3 π[u] ← t ENQUEUE (Q, u) color [w] ≠ WHITE color [x] ≠ WHITE color [t] ← BLACK  rstu vwxy Q xvu

Breadth First Search DEQUEUE x from Q Adj[x] = t, u, w, y color [t] ≠ WHITE color [u] ≠ WHITE color [w] ≠ WHITE color [y] = WHITE color [y] ← GRAY d [y] ← d [x] + 1 = = 3 π[y] ← x ENQUEUE (Q, y) color [x] ← BLACK rstu vwxy Q vuy

Breadth First Search DEQUEUE v from Q Adj[v] = r color [r] ≠ WHITE color [v] ← BLACK rstu vwxy Q uy

Breadth First Search DEQUEUE u from Q Adj[u] = t, x, y color [t] ≠ WHITE color [x] ≠ WHITE color [y] ≠ WHITE color [u] ← BLACK rstu vwxy Q y

Breadth First Search DEQUEUE y from Q Adj[y] = u, x color [u] ≠ WHITE color [x] ≠ WHITE color [y] ← BLACK rstu vwxy Q 

Each vertex is enqueued and dequeued atmost once –Total time devoted to queue operation is O(V) The sum of lengths of all adjacency lists is  (E) –Total time spent in scanning adjacency lists is O(E) The overhead for initialization O(V) Total Running Time of BFS = O(V+E) Breadth First Search

The shortest-path-distance δ (s, v) from s to v as the minimum number of edges in any path from vertex s to vertex v. –if there is no path from s to v, then δ (s, v) = ∞ A path of length δ (s, v) from s to v is said to be a shortest path from s to v. Breadth First search finds the distance to each reachable vertex in the graph G (V, E) from a given source vertex s  V. The field d, for distance, of each vertex is used. Shortest Paths

BFS-Shortest-Paths (G, s) 1  v  V 2 d [v] ← ∞ 3 d [s] ← 0 4 ENQUEUE (Q, s) 5 while Q ≠ φ 6 do v ← DEQUEUE(Q) 7 for each w in Adj[v] 8 do if d [w] = ∞ 9 then d [w] ← d [v] ENQUEUE (Q, w) Shortest Paths

Print Path PRINT-PATH (G, s, v) 1 if v = s 2 then print s 3 else if π[v] = NIL 4 then print “no path from s to v exists 5 else PRINT-PATH (G, s, π[v]) 6 print v