SlideShare a Scribd company logo
GRAPH SEARCH ALGORITHMS Dept Futures Studies  2010-111
Graph Search Algorithms Systematic search of every edge and every vertex of a graph.
 Graph G=(V,E) is either directed or undirected.
Applications
Compilers
Networks
Routing, Searching, Clustering, etc.Dept Futures Studies  2010-112
Graph Traversal  Breadth First Search (BFS)
Start several paths at a time, and advance in each one step at a time
 Depth First Search (DFS)
Once a possible path is found, continue the search until the end of the path Dept Futures Studies  2010-113
Dept Futures Studies  2010-114Breadth-First SearchBreadth-first search starts at a given vertex s, which is at level 0.
In the first stage, we visit all the vertices that are at the distance of one edge away. When we visit there, we paint as "visited," the vertices adjacent to the start vertex s - these vertices are placed into level 1.
 In the second stage, we visit all the new vertices we can reach at the distance of two edges away from the source vertex s. These new vertices, which are adjacent to level 1 vertices and not previously assigned to a level, are placed into level 2, and so on.
 The BFS traversal terminates when every vertex has been visited.Breadth-First SearchThe algorithm maintains a queue Q to manage the set of vertices and starts with s, the source vertexInitially, all vertices except s are colored white, and s is gray.BFS algorithm maintains the following information for each vertex u:	- color[u] (white, gray, or black) : indicates statuswhite 	= not discovered yetgray	= discovered, but not finishedblack	= finished	- d[u] : distance from s to u	- π[u] : predecessor of u in BF treeDept Futures Studies  2010-115
Each vertex is assigned a color.In general, a vertex is white before we start processing it,it is gray during the period the vertex is being processed, and it is black after the processing of the vertex is completed.Dept Futures Studies  2010-116
BFS AlgorithmInputs: Inputs are a graph(directed or undirected) G =(V, E) and a source vertex s, where s is an element of V. The adjacency list representation of a graph is used in this analysis.Dept Futures Studies  2010-117
Outputs: The outputs are a predecessor graph, which represents the paths travelled in the BFS traversal, and a collection of distances, which represent the distance of each of the vertices from the source vertex.Dept Futures Studies  2010-118
Dept Futures Studies  2010-1191.         for each u in V − {s}             2.             do color[u] ← WHITE3.                 d[u] ← infinity4.                 π[u] ← NIL5.         color[s] ← GRAY                 6.         d[s] ← 0                               7.         π[s] ← NIL                           8.         Q ← {}                              9.         ENQUEUE(Q, s)10        while Q is non-empty11.             dou ← DEQUEUE(Q)                    12.                 for each v adjacent to u                  13.                         do if color[v] ← WHITE       14.                                 then  color[v] ← GRAY15.                                          d[v] ← d[u] + 116.                                          π[v] ← u17.                                          ENQUEUE(Q, v)18.                 DEQUEUE(Q)19.         color[u] ← BLACKBFS Algorithm
1. Enqueue (Q,s)  2. while Q ≠  3.      do u Dequeue(Q) 4.      for each v adjacent to u 5.           doif color[v] = white 6.                then color[v]  gray 7.                       d[v]  d[u] + 1 8.                       [v]  u 9.                       Enqueue(Q,v)     10.     color[u]  blackNote:  If G is not connected, then BFS will not visit theentire graph (withoutsome extra provisionsin the algorithm)Dept Futures Studies  2010-1110
Graph G=(V, E)surtvwxyDept Futures Studies  2010-1111
surt0∞∞∞Q0∞∞∞∞vwxysurt10∞∞Q1        1∞∞1∞vwxyDept Futures Studies  2010-1112
1rsut1rsut120∞210∞QQ1       2       21       2       2∞2∞∞212vwxyvwxyDept Futures Studies  2010-1113
rsrsutut11003232QQ2       2      32      3      3∞2123212vwxyvwxyDept Futures Studies  2010-1114
rsut1032Q3      3    3212vwxy3rsut1302Q3          3212vwxyDept Futures Studies  2010-1115
3rsut1302QEmpty3212vwxyrsut1302Spanning Tree3212vwxyDept Futures Studies  2010-1116
Complexityeach node enqueued and dequeued once = O(V) time
each edge considered once (in each direction) = O(E) timeRunning Time of BFSBFS (G, s)   0. For all i ≠ s, d[i]=∞; d[s]=0  1. Enqueue (Q,s)  2. while Q ≠  3.      do u Dequeue(Q) 4.      for each v adjacent to u 5.           doif color[v] = white 6.                then color[v]  gray 7.                       d[v]  d[u] + 1 8.                       [v]  u 9.                       Enqueue(Q,v)     10.     color[u]  blackDept Futures Studies  2010-1117
Running Time of BFSWe will denote the running time by T(m, n)where m is the number of edges and n is the number of vertices.Let V ′ subset of V be the set of vertices enqueued on Q. Then,T(m, n) =  O(m+ n)Dept Futures Studies  2010-1118
Analysis of Breadth-First SearchShortest-path distance (s,v) :  minimum number of edges in any path from vertex s to v.  If no path exists from s to v, then (s,v) = ∞ The ultimate goal of the proof of correctness is to show that d[v] = (s,v) when the algorithm is done and that a path is found from s to all reachable vertices.Dept Futures Studies  2010-1119
Analysis of Breadth-First SearchLemma 1:Let G = (V, E) be a directed or undirected graph, and let s be an arbitrary vertex in G.  Then for any edge (u, v)  E,(s,v) (s,u) + 1	Case 1:  If u is reachable from s, then so is v.  In this case, the shortest path from s to v can't be longer than the shortest path from s to u followed by the edge (u,v).  Case 2: If u is not reachable from s, then (s,u) = ∞ In either case, the lemma holds. Dept Futures Studies  2010-1120
Show that d[v] = (s,v) for every vertex v.Lemma 2:Let G = (V, E) be a graph, and suppose that BFS is run from vertex s.  Then for each vertex v, the value d[v] ≥ (s,v).By induction on the number of enqueues (line 9).  Basis:  When s is enqueued, d[s] = 0 = (s,s) and d[v] = ∞  ≥  (s,v) for all other nodes.Ind.Step:  Consider a white vertex v discovered from vertex u.           implies that d[u] ≥ (s,u).  Then 	d[v] = d[u] + 1			       ≥ (s,u) + 1		                  ≥ (s,v).    		Therefore, d[v] bounds (s,v) from above.Dept Futures Studies  2010-1121

More Related Content

PPTX
Shortest path algorithm
sana younas
 
PPT
Breadth first search
Sazzad Hossain
 
PPT
Prim's Algorithm on minimum spanning tree
oneous
 
PPT
Hash tables
Rajendran
 
PPT
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
PDF
Red black tree
Dr Sandeep Kumar Poonia
 
PPTX
Bellman ford algorithm
AnuragChaudhary70
 
Shortest path algorithm
sana younas
 
Breadth first search
Sazzad Hossain
 
Prim's Algorithm on minimum spanning tree
oneous
 
Hash tables
Rajendran
 
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
Red black tree
Dr Sandeep Kumar Poonia
 
Bellman ford algorithm
AnuragChaudhary70
 

What's hot (20)

PPT
Graphs bfs dfs
Jaya Gautam
 
PPTX
asymptotic notation
SangeethaSasi1
 
PPTX
Graph theory
AparnaKumari31
 
PPTX
Trees and graphs
Lokesh Singrol
 
PPTX
Hashing
Amar Jukuntla
 
PDF
Algorithms Lecture 7: Graph Algorithms
Mohamed Loey
 
PPTX
DFS & BFS Graph
Mahfuzul Yamin
 
PPTX
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 
PPT
Breadth first search and depth first search
Hossain Md Shakhawat
 
PPT
Bfs and dfs in data structure
Ankit Kumar Singh
 
PDF
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 
PPTX
graph theory
ganith2k13
 
PDF
A* Search Algorithm
vikas dhakane
 
PPTX
Introduction to Graph Theory
Premsankar Chakkingal
 
PPTX
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Mrunal Patil
 
PPT
Asymptotic Notation and Complexity
Rajandeep Gill
 
PDF
Parallel sorting Algorithms
GARIMA SHAKYA
 
PPTX
Merge Sort
Nikhil Sonkamble
 
PPT
Binary Search
kunj desai
 
Graphs bfs dfs
Jaya Gautam
 
asymptotic notation
SangeethaSasi1
 
Graph theory
AparnaKumari31
 
Trees and graphs
Lokesh Singrol
 
Hashing
Amar Jukuntla
 
Algorithms Lecture 7: Graph Algorithms
Mohamed Loey
 
DFS & BFS Graph
Mahfuzul Yamin
 
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 
Breadth first search and depth first search
Hossain Md Shakhawat
 
Bfs and dfs in data structure
Ankit Kumar Singh
 
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 
graph theory
ganith2k13
 
A* Search Algorithm
vikas dhakane
 
Introduction to Graph Theory
Premsankar Chakkingal
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Mrunal Patil
 
Asymptotic Notation and Complexity
Rajandeep Gill
 
Parallel sorting Algorithms
GARIMA SHAKYA
 
Merge Sort
Nikhil Sonkamble
 
Binary Search
kunj desai
 
Ad

Viewers also liked (20)

PPTX
Graph Traversal Algorithms - Depth First Search Traversal
Amrinder Arora
 
PPTX
Graphs Algorithms
Kasun Ranga Wijeweera
 
PDF
Graph applications chapter
Savit Chandra
 
PPT
2.5 dfs & bfs
Krish_ver2
 
PPT
Graph theory
Thirunavukarasu Mani
 
PDF
Distributed Graph Algorithms
Saurav Kumar
 
PPTX
Dynamic programming1
debolina13
 
PPT
1535 graph algorithms
Dr Fereidoun Dejahang
 
PDF
18 Basic Graph Algorithms
Andres Mendez-Vazquez
 
PDF
Topological Sort
Dr Sandeep Kumar Poonia
 
PPT
Graphs
Mohd Arif
 
PPTX
Fano algorithm
Hrudya Balachandran
 
PPTX
Stressen's matrix multiplication
Kumar
 
PPT
Minimum spanning tree
Hinal Lunagariya
 
PPTX
Minimum Spanning Tree
zhaokatherine
 
PPTX
strassen matrix multiplication algorithm
evil eye
 
PPTX
Shannon Fano
anithabalaprabhu
 
PPT
2.2 topological sort 02
Krish_ver2
 
PDF
The Graph Traversal Programming Pattern
Marko Rodriguez
 
PPTX
DFS and BFS
satya parsana
 
Graph Traversal Algorithms - Depth First Search Traversal
Amrinder Arora
 
Graphs Algorithms
Kasun Ranga Wijeweera
 
Graph applications chapter
Savit Chandra
 
2.5 dfs & bfs
Krish_ver2
 
Graph theory
Thirunavukarasu Mani
 
Distributed Graph Algorithms
Saurav Kumar
 
Dynamic programming1
debolina13
 
1535 graph algorithms
Dr Fereidoun Dejahang
 
18 Basic Graph Algorithms
Andres Mendez-Vazquez
 
Topological Sort
Dr Sandeep Kumar Poonia
 
Graphs
Mohd Arif
 
Fano algorithm
Hrudya Balachandran
 
Stressen's matrix multiplication
Kumar
 
Minimum spanning tree
Hinal Lunagariya
 
Minimum Spanning Tree
zhaokatherine
 
strassen matrix multiplication algorithm
evil eye
 
Shannon Fano
anithabalaprabhu
 
2.2 topological sort 02
Krish_ver2
 
The Graph Traversal Programming Pattern
Marko Rodriguez
 
DFS and BFS
satya parsana
 
Ad

Similar to Graph Traversal Algorithm (20)

PPTX
Breadth first search (Bfs)
Ishucs
 
DOC
BFS, Breadth first search | Search Traversal Algorithm
MSA Technosoft
 
PPT
Chapter 23 aoa
Hanif Durad
 
PDF
Bfs dfs
Praveen Yadav
 
PPTX
Technical_Seminar .pptx
ForJunks1
 
PPTX
Algorithm Design and Complexity - Course 7
Traian Rebedea
 
PPTX
Lecture 5 - Graph Algorithms BFS and DFS.pptx
mtahanasir65
 
PDF
Skiena algorithm 2007 lecture11 breadth deapth first search
zukun
 
PPT
COSC 3101A - Design and Analysis of Algorithms 10
arif151901
 
PPTX
kumattt).pptx
gurukhade1
 
PPTX
Breadth First Search (BFS)
Dhrumil Panchal
 
PPT
artificial intelligence
ilias ahmed
 
PDF
ada-191015145338.pdf
Ashwin180668
 
PPTX
Breath first Search and Depth first search
Kirti Verma
 
PPT
B.tech admission in india
Edhole.com
 
PPTX
130210107039 2130702
Ketaki_Pattani
 
PPTX
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
AzwanAlghifari
 
PPTX
Sec B Graph traversal.pptx
tabusam1
 
PPT
Graph traversal-BFS & DFS
Rajandeep Gill
 
PDF
Analysis and design of algorithms part 3
Deepak John
 
Breadth first search (Bfs)
Ishucs
 
BFS, Breadth first search | Search Traversal Algorithm
MSA Technosoft
 
Chapter 23 aoa
Hanif Durad
 
Bfs dfs
Praveen Yadav
 
Technical_Seminar .pptx
ForJunks1
 
Algorithm Design and Complexity - Course 7
Traian Rebedea
 
Lecture 5 - Graph Algorithms BFS and DFS.pptx
mtahanasir65
 
Skiena algorithm 2007 lecture11 breadth deapth first search
zukun
 
COSC 3101A - Design and Analysis of Algorithms 10
arif151901
 
kumattt).pptx
gurukhade1
 
Breadth First Search (BFS)
Dhrumil Panchal
 
artificial intelligence
ilias ahmed
 
ada-191015145338.pdf
Ashwin180668
 
Breath first Search and Depth first search
Kirti Verma
 
B.tech admission in india
Edhole.com
 
130210107039 2130702
Ketaki_Pattani
 
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
AzwanAlghifari
 
Sec B Graph traversal.pptx
tabusam1
 
Graph traversal-BFS & DFS
Rajandeep Gill
 
Analysis and design of algorithms part 3
Deepak John
 

Recently uploaded (20)

PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
Basics and rules of probability with real-life uses
ravatkaran694
 

Graph Traversal Algorithm

  • 1. GRAPH SEARCH ALGORITHMS Dept Futures Studies 2010-111
  • 2. Graph Search Algorithms Systematic search of every edge and every vertex of a graph.
  • 3. Graph G=(V,E) is either directed or undirected.
  • 7. Routing, Searching, Clustering, etc.Dept Futures Studies 2010-112
  • 8. Graph Traversal Breadth First Search (BFS)
  • 9. Start several paths at a time, and advance in each one step at a time
  • 10. Depth First Search (DFS)
  • 11. Once a possible path is found, continue the search until the end of the path Dept Futures Studies 2010-113
  • 12. Dept Futures Studies 2010-114Breadth-First SearchBreadth-first search starts at a given vertex s, which is at level 0.
  • 13. In the first stage, we visit all the vertices that are at the distance of one edge away. When we visit there, we paint as "visited," the vertices adjacent to the start vertex s - these vertices are placed into level 1.
  • 14. In the second stage, we visit all the new vertices we can reach at the distance of two edges away from the source vertex s. These new vertices, which are adjacent to level 1 vertices and not previously assigned to a level, are placed into level 2, and so on.
  • 15. The BFS traversal terminates when every vertex has been visited.Breadth-First SearchThe algorithm maintains a queue Q to manage the set of vertices and starts with s, the source vertexInitially, all vertices except s are colored white, and s is gray.BFS algorithm maintains the following information for each vertex u: - color[u] (white, gray, or black) : indicates statuswhite = not discovered yetgray = discovered, but not finishedblack = finished - d[u] : distance from s to u - π[u] : predecessor of u in BF treeDept Futures Studies 2010-115
  • 16. Each vertex is assigned a color.In general, a vertex is white before we start processing it,it is gray during the period the vertex is being processed, and it is black after the processing of the vertex is completed.Dept Futures Studies 2010-116
  • 17. BFS AlgorithmInputs: Inputs are a graph(directed or undirected) G =(V, E) and a source vertex s, where s is an element of V. The adjacency list representation of a graph is used in this analysis.Dept Futures Studies 2010-117
  • 18. Outputs: The outputs are a predecessor graph, which represents the paths travelled in the BFS traversal, and a collection of distances, which represent the distance of each of the vertices from the source vertex.Dept Futures Studies 2010-118
  • 19. Dept Futures Studies 2010-1191.         for each u in V − {s}             2.             do color[u] ← WHITE3.                 d[u] ← infinity4.                 π[u] ← NIL5.         color[s] ← GRAY                 6.         d[s] ← 0                               7.         π[s] ← NIL                           8.         Q ← {}                              9.         ENQUEUE(Q, s)10        while Q is non-empty11.             dou ← DEQUEUE(Q)                    12.                 for each v adjacent to u                  13.                         do if color[v] ← WHITE       14.                                 then  color[v] ← GRAY15.                                          d[v] ← d[u] + 116.                                          π[v] ← u17.                                          ENQUEUE(Q, v)18.                 DEQUEUE(Q)19.         color[u] ← BLACKBFS Algorithm
  • 20. 1. Enqueue (Q,s) 2. while Q ≠  3. do u Dequeue(Q) 4. for each v adjacent to u 5. doif color[v] = white 6. then color[v]  gray 7. d[v]  d[u] + 1 8. [v]  u 9. Enqueue(Q,v) 10. color[u]  blackNote: If G is not connected, then BFS will not visit theentire graph (withoutsome extra provisionsin the algorithm)Dept Futures Studies 2010-1110
  • 21. Graph G=(V, E)surtvwxyDept Futures Studies 2010-1111
  • 22. surt0∞∞∞Q0∞∞∞∞vwxysurt10∞∞Q1 1∞∞1∞vwxyDept Futures Studies 2010-1112
  • 23. 1rsut1rsut120∞210∞QQ1 2 21 2 2∞2∞∞212vwxyvwxyDept Futures Studies 2010-1113
  • 24. rsrsutut11003232QQ2 2 32 3 3∞2123212vwxyvwxyDept Futures Studies 2010-1114
  • 25. rsut1032Q3 3 3212vwxy3rsut1302Q3 3212vwxyDept Futures Studies 2010-1115
  • 27. Complexityeach node enqueued and dequeued once = O(V) time
  • 28. each edge considered once (in each direction) = O(E) timeRunning Time of BFSBFS (G, s) 0. For all i ≠ s, d[i]=∞; d[s]=0 1. Enqueue (Q,s) 2. while Q ≠  3. do u Dequeue(Q) 4. for each v adjacent to u 5. doif color[v] = white 6. then color[v]  gray 7. d[v]  d[u] + 1 8. [v]  u 9. Enqueue(Q,v) 10. color[u]  blackDept Futures Studies 2010-1117
  • 29. Running Time of BFSWe will denote the running time by T(m, n)where m is the number of edges and n is the number of vertices.Let V ′ subset of V be the set of vertices enqueued on Q. Then,T(m, n) = O(m+ n)Dept Futures Studies 2010-1118
  • 30. Analysis of Breadth-First SearchShortest-path distance (s,v) : minimum number of edges in any path from vertex s to v. If no path exists from s to v, then (s,v) = ∞ The ultimate goal of the proof of correctness is to show that d[v] = (s,v) when the algorithm is done and that a path is found from s to all reachable vertices.Dept Futures Studies 2010-1119
  • 31. Analysis of Breadth-First SearchLemma 1:Let G = (V, E) be a directed or undirected graph, and let s be an arbitrary vertex in G. Then for any edge (u, v)  E,(s,v) (s,u) + 1 Case 1: If u is reachable from s, then so is v. In this case, the shortest path from s to v can't be longer than the shortest path from s to u followed by the edge (u,v). Case 2: If u is not reachable from s, then (s,u) = ∞ In either case, the lemma holds. Dept Futures Studies 2010-1120
  • 32. Show that d[v] = (s,v) for every vertex v.Lemma 2:Let G = (V, E) be a graph, and suppose that BFS is run from vertex s. Then for each vertex v, the value d[v] ≥ (s,v).By induction on the number of enqueues (line 9). Basis: When s is enqueued, d[s] = 0 = (s,s) and d[v] = ∞ ≥ (s,v) for all other nodes.Ind.Step: Consider a white vertex v discovered from vertex u. implies that d[u] ≥ (s,u). Then d[v] = d[u] + 1 ≥ (s,u) + 1 ≥ (s,v). Therefore, d[v] bounds (s,v) from above.Dept Futures Studies 2010-1121
  • 33. Lemma 3:For every vertex (v1,v2,...,vr) on Q during BFS (such that v1 is Q head and vr is Q rear), d[vr] ≤ d[v1] + 1 and d[vi] ≤ d[vi+1] for i = 1,2,..., r-1.By induction on the number of queue operations. Basis: Q contains only s, so lemma trivially holds.Ind.Step:Case 1: Show lemma holds after every dequeue. Suppose v1 is dequeued and v2 becomes new head. Then d[v1] ≤ d[v2] d[vr] ≤ d[v1] + 1 ≤ d[v2] + 1, so lemma holds after every dequeue. Dept Futures Studies 2010-1122
  • 34. Lemma 3:For every vertex (v1,v2,...,vr) on Q during BFS (such that v1 is Q head and vr is Q rear), d[vr] ≤ d[v1] + 1 and d[vi] ≤ d[vi+1] for i = 1,2,..., r-1Case 2: Show lemma holds after every enqueue. Suppose v is enqueued, becoming vr+1 after vertex u is dequeued (i.e., v is adjacent to u). Then for the new head v1 d[u] ≤ d[v1] d[vr] ≤ d[u] + 1 = d[v] = d[vr+1]So after the enqueue, we have that d[vr] = d[v] = d[u] + 1 ≤ d[v1] + 1. Also, d[vr] ≤ d[vr+1] and for all other nodes on Q, d[vi] ≤ d[vi+1] . As a consequence of Lemma 3, if vi is enqueued before vj, then d[vi] ≤ d[vj] (also, if vi is enqueued before vj, then it is also dequeuedbefore vjby definition of a queue). Dept Futures Studies 2010-1123
  • 35. Theorem 4: (Correctness of BFS)Let G = (V, E) be a directed or undirected graph, and suppose that BFS is run from a given source vertex s  V. Then, during execution, BFS discovers every vertex v ≠ s that is reachable from the source s, and upon termination, d[v] = (s,v) for every reachable or unreachable vertex v.Proof by contradiction. Assume that for some vertex v that d[v] (s,v). Also, assume that v is the vertex with minimum(s,v) that receives an incorrect d value. By Lemma 2, it must be that d[v] > (s,v). Dept Futures Studies 2010-1124
  • 36. Case 1: v is not reachable from s. Then (s,v) = ∞ = d[v]. This is a contradiction, and the Thm holds. Case 2: v is reachable from s. Let u be the vertex immediately preceding v on a shortest path from s to v, so that (s,v) = (s,u) + 1. Because (s,u) < (s,v) and because v is the vertex with the minimum(s,v) that receives an incorrect d value, d[u] = (s,u). So we have d[v] > (s,v) = (s,u) + 1 = d[u] + 1 (by L.1 and how we chose v as minimum(s,v) that receives an incorrect d value).Dept Futures Studies 2010-1125
  • 37. Consider the time t when u is dequeued. At time t, v is either white, gray, or black. We can derive a contradiction in each of these cases.Case 1: v is white. Then in line 7, d[v] = d[u]+1.Case 2: v is black. Then v was already dequeued, and therefore d[v] ≤ d[u] (by L. 3).Case 3: v is gray. Then v turned gray when it was visited from some vertex w, which was dequeued before u. Then d[v] = d[w] + 1. Since d[w] ≤ d[u] (by L 3), d[v] ≤ d[u] + 1. Each of these cases is a contradiction to d[v] > (s,v), so we conclude that d[v] = (s,v).Dept Futures Studies 2010-1126
  • 38. Applications of BFS Connected components
  • 39. BipartiteDept Futures Studies 2010-1127
  • 40. Find the number of connected components of a graph. Describe with diagram.Dept Futures Studies 2010-1128
  • 41. Bipartite GraphA bipartite graph is an undirected graph G = (V, E) in which V can be partitioned into two sets V1 and V2 such that (u, v) E implies either u in V1 and v in V2 or u in V2 and v in V1. That is, all edges go between the two sets V1 and V2.Dept Futures Studies 2010-1129
  • 42. whenever the BFS is at a vertex u and encounters a vertex v that is already 'gray' our modified BSF should check to see if the depth of both u and v are even, or if they are both odd. If either of these conditions holds which implies d[u] and d[v] have the same parity, then the graph is not bipartite. Dept Futures Studies 2010-1130
  • 43. 31Level 0acbLevel 1Odd cycle Level 2defLevel 3ihgjQueue E f g 2 2 3Dept Futures Studies 2010-11
  • 44. Dept Futures Studies 2010-1132Jyothimon CDept Futures Studies University of [email protected] You