Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.

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

CS138A Single Source Shortest Paths Peter Schröder.
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.
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.
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.
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?
Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.
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 & BFS.
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)
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 12 Graphs and basic search algorithms Motivation Definitions and properties Representation.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
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.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
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.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
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 Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
CSC 413/513: Intro to Algorithms Graph Algorithms.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
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.
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
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.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
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.
Elementary Graph Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
BFS,DFS Topological Sort
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Chapter 22: Elementary Graph Algorithms I
Graph Representation (23.1/22.1)
Basic Graph Algorithms
CS 583 Analysis of Algorithms
Elementary Graph Algorithms
Presentation transcript:

Introduction to Graphs And Breadth First Search

Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified relationship Representations of pairwise relationships Collections of objects under some specified relationship

Graphs: what are they mathematically? A graph G is a pair (V,E) V is a set of vertices (nodes) E is a set of pairs (a,b), a,b  V V is the set of relatable objects E is the set of relationships A graph G is a pair (V,E) V is a set of vertices (nodes) E is a set of pairs (a,b), a,b  V V is the set of relatable objects E is the set of relationships

A Visual Example G = ( {1,2,3,4,5}, {(1,2), (1,4), (2,3), (2,4), (1,5)} )

Directed Graphs In a directed graph (a,b)  E does not imply (b,a)  E Undirected graphs are a subset (a,b)  E if and only if (b,a)  E Visually, directed graphs are drawn with arrows In a directed graph (a,b)  E does not imply (b,a)  E Undirected graphs are a subset (a,b)  E if and only if (b,a)  E Visually, directed graphs are drawn with arrows

Directed Graph Example G = ( {1,2,3,4,5}, { (1,5), (2,1), (2,3), (2,4), (3,2), (4,1) } )

Weighted Graphs Have weights associated with edges Can be directed or undirected Can have pairs, in a directed graph, where the weights from (a,b) have no relationship on the weights from (b,a) Have weights associated with edges Can be directed or undirected Can have pairs, in a directed graph, where the weights from (a,b) have no relationship on the weights from (b,a)

Weighted Graph Example G = ( {1,2,3,4,5}, { (1,5,-5), (2,1,3.2), (2,3,42), (3,2, π ), (2,4,777), (4,1,666) } ) 3.2 π

Graph Representation How to represent in memory? Two common ways: Adjacency Lists Adjacency Matrix How to represent in memory? Two common ways: Adjacency Lists Adjacency Matrix

Adjacency Lists Compact usage in sparse graphs where |E| << |V| 2 Stores graph as array of |V| lists Each v has a list of adjacent v in G Compact usage in sparse graphs where |E| << |V| 2 Stores graph as array of |V| lists Each v has a list of adjacent v in G

Undirected Graph Example G = ( {1,2,3,4,5}, {(1,2), (1,4), (2,3), (2,4), (1,5)} )

Directed Graph Example G = ( {1,2,3,4,5}, { (1,5), (2,1), (2,3), (2,4), (3,2), (4,1) } )

Adjacency Lists Wrap Up Sum of list lengths for undirected 2|E| For some apps, could optimize to |E| Sum of list lengths for directed |E| Weighted graphs: left as exercise Sum of list lengths for undirected 2|E| For some apps, could optimize to |E| Sum of list lengths for directed |E| Weighted graphs: left as exercise

Adjacency Matrix Often less memory for dense graphs Faster check for edge existence Mathematically: M is a |V|*|V| matrix Dimensions represent vertices M(i,j)=1 if (i,j)  E, 0 otherwise Often less memory for dense graphs Faster check for edge existence Mathematically: M is a |V|*|V| matrix Dimensions represent vertices M(i,j)=1 if (i,j)  E, 0 otherwise

Undirected Graph Example G = ( {1,2,3,4,5}, {(1,2), (1,4), (2,3), (2,4), (1,5)} )

Directed Graph Example G = ( {1,2,3,4,5}, { (1,5), (2,1), (2,3), (2,4), (3,2), (4,1) } )

Adjacency Matrix Wrap Up Size is always |V| 2 If |E| close to |V|, can be more efficient because edge is 1 bit instead of a 4 bytes for a pointer Weighted graphs: use weight instead of 0’s and 1’s Size is always |V| 2 If |E| close to |V|, can be more efficient because edge is 1 bit instead of a 4 bytes for a pointer Weighted graphs: use weight instead of 0’s and 1’s

Breadth-first Search Problem: For a given graph G, and a specified s in the graph, find all vertices v that are reachable from s and determine the shortest path in G from s to v.

How BFS works Constructs a breadth first tree Root is s Path from s to v is shortest path from s to v in G Constructs a breadth first tree Root is s Path from s to v is shortest path from s to v in G

The BFS algorithm Assigns a color to each node white = vertex has not been reached gray = vertex is in the BFS frontier) black = vertex and ALL of its neighbors have been processed. Assigns a color to each node white = vertex has not been reached gray = vertex is in the BFS frontier) black = vertex and ALL of its neighbors have been processed.

BFS algorithm (cont.) Computes d[v] for each v Shortest distance from s to v in G Computes p[v] for each v Predecessor of v in the breadth-first tree Computes d[v] for each v Shortest distance from s to v in G Computes p[v] for each v Predecessor of v in the breadth-first tree

BFS Pseudo Code (initialization) 1.for each vertex v in V 2. color[v] = white 3. d[v] = INFINITY 4. p[v] = NULL 5.color[s] = gray 6.d[s] = 0 7.Queue.clear() 8.Queue.put(s) 1.for each vertex v in V 2. color[v] = white 3. d[v] = INFINITY 4. p[v] = NULL 5.color[s] = gray 6.d[s] = 0 7.Queue.clear() 8.Queue.put(s)

BFS Pseudo Code (tree construction) 9.while (!Queue.empty()) 10. u = Queue.get() 11. for each v adjacent to u 12. if (color[v] == white) 13. color[v] = gray 14. d[v] = d[u] p[v] = u 16. Queue.put(v) 17. color[u] = black 9.while (!Queue.empty()) 10. u = Queue.get() 11. for each v adjacent to u 12. if (color[v] == white) 13. color[v] = gray 14. d[v] = d[u] p[v] = u 16. Queue.put(v) 17. color[u] = black

Correctness of BFS Definition 1. b(s,v) is the min number of edges in any path from s to v. If there is no path from s to v then b(s,v) = INFINITY. b(s,v) is the shortest-path distance. Lemma 1. Let G=(V,E), v in V. For any edge (u,v) in E b(s,v) <= b(s,u) + 1 Definition 1. b(s,v) is the min number of edges in any path from s to v. If there is no path from s to v then b(s,v) = INFINITY. b(s,v) is the shortest-path distance. Lemma 1. Let G=(V,E), v in V. For any edge (u,v) in E b(s,v) <= b(s,u) + 1

Proof of Lemma 1 If u is reachable from s, so is v. The shortest path from s to v cannot be more than the shortest path from s to u plus the edge (u,v), thus the inequality holds. If u is not reachable then b(s,u) = INFINITY so the inequality holds

Lemma 2 Upon termination, the BFS algorithm computes d[v] for every vertex and d[v] >= b(s,v)

Proof of Lemma 2 By induction on the number i of enqueue operations. For i = 1 (s is enqueued), d[s]=[0]=b(s,s) d[v]=INFINITY>=b(s,v) for all v != s For i = n, consider white v discovered from u. By induction, d[u]>=b(s,u). Since d[v]=d[u]+1 >= b(s,u)+1 >= b(s,v) By induction on the number i of enqueue operations. For i = 1 (s is enqueued), d[s]=[0]=b(s,s) d[v]=INFINITY>=b(s,v) for all v != s For i = n, consider white v discovered from u. By induction, d[u]>=b(s,u). Since d[v]=d[u]+1 >= b(s,u)+1 >= b(s,v)

Lemma 3 At all times during execution of BFS the queue contains vertices (v1, v2, … vr) such that d[v1] <= d[v2]…<=d[vr] d[vr] <= d[v1] + 1 At all times during execution of BFS the queue contains vertices (v1, v2, … vr) such that d[v1] <= d[v2]…<=d[vr] d[vr] <= d[v1] + 1

Proof of Lemma 3 By induction on number i of queue op’s. For i=1, queue only has s, hypothesis holds For i=n After dequeueing v1: d[vr]<=d[v1]+1 and d[v1]<=d[v2], then d[vr]<=d[v2]+1, so hypothesis holds After enqueueing v r+1 : D[v r+1 ] = d[v1]+1 >= d[vr] D[v r+1 ] = d[v1]+1 <= d[v2]+1, since d[v1]<=d[v2] Since v2 is the new head of queue, hypothesis holds By induction on number i of queue op’s. For i=1, queue only has s, hypothesis holds For i=n After dequeueing v1: d[vr]<=d[v1]+1 and d[v1]<=d[v2], then d[vr]<=d[v2]+1, so hypothesis holds After enqueueing v r+1 : D[v r+1 ] = d[v1]+1 >= d[vr] D[v r+1 ] = d[v1]+1 <= d[v2]+1, since d[v1]<=d[v2] Since v2 is the new head of queue, hypothesis holds

Corollary (4) to Lemma 3 If vertices u and v are enqueued during execution of BFS and u is enqueued before v, then d[u] <= d[v]

Theorem 5 Given G=(V,E) and s BFS discovers every v reachable from s Upon termination, d[v]=b(s,v) Moreover, for v reachable from s One of the shortests paths from s to v is a path followed from s to p[v], followed by edge (p[v],v). Given G=(V,E) and s BFS discovers every v reachable from s Upon termination, d[v]=b(s,v) Moreover, for v reachable from s One of the shortests paths from s to v is a path followed from s to p[v], followed by edge (p[v],v).

Proof of Theorem 5 By contradiction. Assume v assigned d[v] != b(s,v). By lemma 2, d[v]>=b(s,v), so d[v] > b(s,v). v must be reachable, else b(s,v)>=d[v] Let u be predecessor on path to v b(s,v) = b(s,u)+1 = d[u]+1 This would mean d[v] > d[u]+1 By contradiction. Assume v assigned d[v] != b(s,v). By lemma 2, d[v]>=b(s,v), so d[v] > b(s,v). v must be reachable, else b(s,v)>=d[v] Let u be predecessor on path to v b(s,v) = b(s,u)+1 = d[u]+1 This would mean d[v] > d[u]+1

Proof completion d[v] > d[u]+1 cannot happen! Look at when BFS dequeues u v is either white, black, or gray If v is black, already removed from queue, and by corollary 4, d[v]<=d[u] If v is gray, it was made gray when other vertex w was dequeued, so d[v]=d[w]+1 <= d[u]+1 (by corollary 4) If v is white, then the code sets d[v] d[v] = d[u] + 1 d[v] > d[u]+1 cannot happen! Look at when BFS dequeues u v is either white, black, or gray If v is black, already removed from queue, and by corollary 4, d[v]<=d[u] If v is gray, it was made gray when other vertex w was dequeued, so d[v]=d[w]+1 <= d[u]+1 (by corollary 4) If v is white, then the code sets d[v] d[v] = d[u] + 1

BFS Wrap Up So, d[v]=b(s,v) for all v in V All reachable vertices discovered, else d = INFINITY If p[v]=u, then d[v]=d[u]+1, so one of the shortest paths from s to v takes path from s to u then (u,v) So, d[v]=b(s,v) for all v in V All reachable vertices discovered, else d = INFINITY If p[v]=u, then d[v]=d[u]+1, so one of the shortest paths from s to v takes path from s to u then (u,v)

Applications for Graphs Link structure of a website Problems in travel, biology, etc. Network representation Solution space: EXAMPLE: Sudoku Link structure of a website Problems in travel, biology, etc. Network representation Solution space: EXAMPLE: Sudoku