SlideShare a Scribd company logo
1 of 32
GRAPH SEARCH ALGORITHMS  Dept Futures Studies  2010-11 1
Graph Search Algorithms ,[object Object]
 Graph G=(V,E) is either directed or undirected.
Applications
Compilers
Networks
Routing, Searching, Clustering, etc.Dept Futures Studies  2010-11 2
Graph Traversal  ,[object Object]
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-11 3
Dept Futures Studies  2010-11 4 Breadth-First Search ,[object Object]
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.,[object Object]
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-11 6
BFS Algorithm Inputs: 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-11 7
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-11 8
Dept Futures Studies  2010-11 9 1.         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] ← BLACK BFS 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]  black Note:  If G is not  connected, then  BFS will not visit the entire graph (without some extra provisions in the algorithm) Dept Futures Studies  2010-11 10
Graph G=(V, E) s u r t v w x y Dept Futures Studies  2010-11 11
s u r t 0 ∞ ∞ ∞ Q 0 ∞ ∞ ∞ ∞ v w x y s u r t 1 0 ∞ ∞ Q 1        1 ∞ ∞ 1 ∞ v w x y Dept Futures Studies  2010-11 12
1 r s u t 1 r s u t 1 2 0 ∞ 2 1 0 ∞ Q Q 1       2       2 1       2       2 ∞ 2 ∞ ∞ 2 1 2 v w x y v w x y Dept Futures Studies  2010-11 13
r s r s u t u t 1 1 0 0 3 2 3 2 Q Q 2       2      3 2      3      3 ∞ 2 1 2 3 2 1 2 v w x y v w x y Dept Futures Studies  2010-11 14
r s u t 1 0 3 2 Q 3      3     3 2 1 2 v w x y 3 r s u t 1 3 0 2 Q 3           3 2 1 2 v w x y Dept Futures Studies  2010-11 15
3 r s u t 1 3 0 2 Q Empty 3 2 1 2 v w x y r s u t 1 3 0 2 Spanning Tree 3 2 1 2 v w x y Dept Futures Studies  2010-11 16
Complexity ,[object Object]
each edge considered once (in each direction) = O(E) timeRunning Time of BFS BFS (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]  black Dept Futures Studies  2010-11 17
Running Time of BFS We 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-11 18
Analysis of Breadth-First Search Shortest-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-11 19
Analysis of Breadth-First Search Lemma 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-11 20
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-11 21

More Related Content

What's hot

What's hot (20)

Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
N queen problem
N queen problemN queen problem
N queen problem
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
N queens using backtracking
N queens using backtrackingN queens using backtracking
N queens using backtracking
 
Artificial Intelligence -- Search Algorithms
Artificial Intelligence-- Search Algorithms Artificial Intelligence-- Search Algorithms
Artificial Intelligence -- Search Algorithms
 
Dfs
DfsDfs
Dfs
 
Np complete
Np completeNp complete
Np complete
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
Backtracking Algorithm.ppt
Backtracking Algorithm.pptBacktracking Algorithm.ppt
Backtracking Algorithm.ppt
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
 
DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
 
Backtracking
BacktrackingBacktracking
Backtracking
 

Viewers also liked

Depth first search and breadth first searching
Depth first search and breadth first searchingDepth first search and breadth first searching
Depth first search and breadth first searchingKawsar Hamid Sumon
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalAmrinder Arora
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationAmrinder Arora
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAmrinder Arora
 
(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 ai(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 aiRadhika Srinivasan
 
LEXBFS on Chordal Graphs with more Example
LEXBFS on Chordal Graphs with more ExampleLEXBFS on Chordal Graphs with more Example
LEXBFS on Chordal Graphs with more Examplenazlitemu
 
Review And Evaluations Of Shortest Path Algorithms
Review And Evaluations Of Shortest Path AlgorithmsReview And Evaluations Of Shortest Path Algorithms
Review And Evaluations Of Shortest Path AlgorithmsPawan Kumar Tiwari
 
5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back trackingKrish_ver2
 
BFS & Interval Graph Introduction
BFS & Interval Graph IntroductionBFS & Interval Graph Introduction
BFS & Interval Graph Introductionnazlitemu
 
lecture 26
lecture 26lecture 26
lecture 26sajinsc
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problemfika sweety
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problemSumita Das
 

Viewers also liked (20)

Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Depth first search and breadth first searching
Depth first search and breadth first searchingDepth first search and breadth first searching
Depth first search and breadth first searching
 
Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
2.5 graph dfs
2.5 graph dfs2.5 graph dfs
2.5 graph dfs
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
chapter22.ppt
chapter22.pptchapter22.ppt
chapter22.ppt
 
(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 ai(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 ai
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
LEXBFS on Chordal Graphs with more Example
LEXBFS on Chordal Graphs with more ExampleLEXBFS on Chordal Graphs with more Example
LEXBFS on Chordal Graphs with more Example
 
Review And Evaluations Of Shortest Path Algorithms
Review And Evaluations Of Shortest Path AlgorithmsReview And Evaluations Of Shortest Path Algorithms
Review And Evaluations Of Shortest Path Algorithms
 
5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back tracking
 
2.5 dfs & bfs
2.5 dfs & bfs2.5 dfs & bfs
2.5 dfs & bfs
 
BFS & Interval Graph Introduction
BFS & Interval Graph IntroductionBFS & Interval Graph Introduction
BFS & Interval Graph Introduction
 
lecture 26
lecture 26lecture 26
lecture 26
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
 
Stack and Hash Table
Stack and Hash TableStack and Hash Table
Stack and Hash Table
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
 

Similar to BFS

BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmMSA Technosoft
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in indiaEdhole.com
 
topological_sort_strongly Connected Components
topological_sort_strongly Connected Componentstopological_sort_strongly Connected Components
topological_sort_strongly Connected ComponentsJahidulIslam47153
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.pptSazidHossain9
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithmRuchika Sinha
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsYi-Lung Tsai
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxbashirabdullah789
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Traian Rebedea
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfChristianKapsales1
 
bellman-ford Theorem.ppt
bellman-ford Theorem.pptbellman-ford Theorem.ppt
bellman-ford Theorem.pptSaimaShaheen14
 

Similar to BFS (20)

BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal Algorithm
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
19-graph1 (1).ppt
19-graph1 (1).ppt19-graph1 (1).ppt
19-graph1 (1).ppt
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
topological_sort_strongly Connected Components
topological_sort_strongly Connected Componentstopological_sort_strongly Connected Components
topological_sort_strongly Connected Components
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.ppt
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Graps 2
Graps 2Graps 2
Graps 2
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Dijksatra
DijksatraDijksatra
Dijksatra
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
 
kumattt).pptx
kumattt).pptxkumattt).pptx
kumattt).pptx
 
bellman-ford Theorem.ppt
bellman-ford Theorem.pptbellman-ford Theorem.ppt
bellman-ford Theorem.ppt
 
Graph
GraphGraph
Graph
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 

Recently uploaded (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

BFS

  • 1. GRAPH SEARCH ALGORITHMS Dept Futures Studies 2010-11 1
  • 2.
  • 3. Graph G=(V,E) is either directed or undirected.
  • 7. Routing, Searching, Clustering, etc.Dept Futures Studies 2010-11 2
  • 8.
  • 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-11 3
  • 12.
  • 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.
  • 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-11 6
  • 17. BFS Algorithm Inputs: 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-11 7
  • 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-11 8
  • 19. Dept Futures Studies 2010-11 9 1.         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] ← BLACK BFS 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]  black Note: If G is not connected, then BFS will not visit the entire graph (without some extra provisions in the algorithm) Dept Futures Studies 2010-11 10
  • 21. Graph G=(V, E) s u r t v w x y Dept Futures Studies 2010-11 11
  • 22. s u r t 0 ∞ ∞ ∞ Q 0 ∞ ∞ ∞ ∞ v w x y s u r t 1 0 ∞ ∞ Q 1 1 ∞ ∞ 1 ∞ v w x y Dept Futures Studies 2010-11 12
  • 23. 1 r s u t 1 r s u t 1 2 0 ∞ 2 1 0 ∞ Q Q 1 2 2 1 2 2 ∞ 2 ∞ ∞ 2 1 2 v w x y v w x y Dept Futures Studies 2010-11 13
  • 24. r s r s u t u t 1 1 0 0 3 2 3 2 Q Q 2 2 3 2 3 3 ∞ 2 1 2 3 2 1 2 v w x y v w x y Dept Futures Studies 2010-11 14
  • 25. r s u t 1 0 3 2 Q 3 3 3 2 1 2 v w x y 3 r s u t 1 3 0 2 Q 3 3 2 1 2 v w x y Dept Futures Studies 2010-11 15
  • 26. 3 r s u t 1 3 0 2 Q Empty 3 2 1 2 v w x y r s u t 1 3 0 2 Spanning Tree 3 2 1 2 v w x y Dept Futures Studies 2010-11 16
  • 27.
  • 28. each edge considered once (in each direction) = O(E) timeRunning Time of BFS BFS (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]  black Dept Futures Studies 2010-11 17
  • 29. Running Time of BFS We 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-11 18
  • 30. Analysis of Breadth-First Search Shortest-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-11 19
  • 31. Analysis of Breadth-First Search Lemma 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-11 20
  • 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-11 21
  • 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-11 22
  • 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-1 Case 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-11 23
  • 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-11 24
  • 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-11 25
  • 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-11 26
  • 38.
  • 39. BipartiteDept Futures Studies 2010-11 27
  • 40. Find the number of connected components of a graph. Describe with diagram. Dept Futures Studies 2010-11 28
  • 41. Bipartite Graph A 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-11 29
  • 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-11 30
  • 43. 31 Level 0 a c b Level 1 Odd cycle Level 2 d e f Level 3 i h g j Queue E f g 2 2 3 Dept Futures Studies 2010-11
  • 44. Dept Futures Studies 2010-11 32 Jyothimon C Dept Futures Studies University of KERALA jyothimon86@gmail.com Thank You