SlideShare a Scribd company logo
1 of 8
Breadth-First Search Traversal Algorithm
Breadth-first search is a way to find all the vertices reachable from the a given source vertex, s.
Like depth first search, BFS traverse a connected component of a given graph and defines a
spanning tree. Intuitively, the basic idea of the breath-first search is this: send a wave out from
source s. The wave hits all vertices 1 edge from s. From there, the wave hits all vertices 2 edges
from s. Etc. We use FIFO queue Q to maintain the wavefront: v is in Q if and only if wave has
hit v but has not come out of v yet.
Overall Strategy of BFS Algorithm
Breadth-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.
To keep track of progress, breadth-first-search colors each vertex. Each vertex of the graph is in
one of three states:
1. Undiscovered;
2. Discovered but not fully explored; and
3. Fully explored.
The state of a vertex, u, is stored in a color variable as follows:
1. color[u] = White - for the "undiscovered" state,
2. color [u] = Gray - for the "discovered but not fully explored" state, and
3. color [u] = Black - for the "fully explored" state.
The BFS(G, s) algorithm develops a breadth-first search tree with the source vertex, s, as its root.
The parent or predecessor of any other vertex in the tree is the vertex from which it was first
discovered. For each vertex, v, the parent of v is placed in the variable π[v]. Another variable,
d[v], computed by BFS contains the number of tree edges on the path from s to v. The breadth-
first search uses a FIFO queue, Q, to store gray vertices.
Algorithm: Breadth-First Search Traversal
BFS(V, E, s)
1. for each u in V − {s} ▷ for each vertex u in V[G] except s.
2. do color[u] ← WHITE
3. d[u] ← infinity
4. π[u] ← NIL
5. color[s] ← GRAY ▷ Source vertex discovered
6. d[s] ← 0 ▷ initialize
7. π[s] ← NIL ▷ initialize
8. Q ← {} ▷ Clear queue Q
9. ENQUEUE(Q, s)
10 while Q is non-empty
11. do u ← DEQUEUE(Q) ▷ That is, u = head[Q]
12. for each v adjacent to u ▷ for loop for every node along with edge.
13. do if color[v] ← WHITE ▷ if color is white you've never seen it before
14. then color[v] ← GRAY
15. d[v] ← d[u] + 1
16. π[v] ← u
17. ENQUEUE(Q, v)
18. DEQUEUE(Q)
19. color[u] ← BLACK
Example: The following figure (from CLRS) illustrates the progress of breadth-first search on
the undirected sample graph.
a. After initialization (paint every vertex white, set d[u] to infinity for each vertex u, and set the
parent of every vertex to be NIL), the source vertex is discovered in line 5. Lines 8-9 initialize Q
to contain just the source vertex s.
b. The algorithm discovers all vertices 1 edge from s i.e., discovered all vertices (w and r) at
level 1.
c.
d. The algorithm discovers all vertices 2 edges from s i.e., discovered all vertices (t, x, and v) at
level 2.
e.
f.
g. The algorithm discovers all vertices 3 edges from s i.e., discovered all vertices (u and y) at
level 3.
h.
i. The algorithm terminates when every vertex has been fully explored.
Analysis
• The while-loop in breadth-first search is executed at most |V| times. The reason is that
every vertex enqueued at most once. So, we have O(V).
• The for-loop inside the while-loop is executed at most |E| times if G is a directed graph or
2|E| times if G is undirected. The reason is that every vertex dequeued at most once and
we examine (u, v) only when u is dequeued. Therefore, every edge examined at most
once if directed, at most twice if undirected. So, we have O(E).
Therefore, the total running time for breadth-first search traversal is O(V + E).
Lemma 22.3 (CLRS) At any time during the execution of BFS suppose that Q contains the
vertices {v1, v2, ..., vr} with v1 at the head and vr at the tail. Then d[v1] ≤ d[v2] ≤ ... ≤ d[vr] ≤ d[v1]
+ 1.
Let v be any vertex in V[G]. If v is reachable from s then let δ(s, v) be the minimum number of
edges in E[G] that must be traversed to go from vertex s to vertex v. If v is not reachable from s
then let δ(s, v) = ∞.
Theorem 22.5 (CLRS) If BFS is run on graph G from a source vertex s in V[G] then for all v
in V[G], d[v] = δ(s, v) and if v ≠ s is reachable from s then one of the shortest paths from s to v is
a shortest path from s to π[v] followed by the edge from π[v] to v.
BFS builds a tree called a breadth-first-tree containing all vertices reachable from s. The set of
edges in the tree (called tree edges) contain (π[v], v) for all v where π[v] ≠ NIL.
If v is reachable from s then there is a unique path of tree edges from s to v. Print-Path(G, s, v)
prints the vertices along that path in O(|V|) time.
Print-Path(G, s, v)
if v = s
then print s
else if π[v] ← NIL
then print "no path exists from " s "to" v"
else Print-Path(G, s, π[v])
print v
Algorithms based on BFS
Based upon the BFS, there are O(V + E)-time algorithms for the following problems:
• Testing whether graph is connected.
• Computing a spanning forest of graph.
• Computing, for every vertex in graph, a path with the minimum number of edges between
start vertex and current vertex or reporting that no such path exists.
• Computing a cycle in graph or reporting that no such cycle exists.
In our course, we will use BFS in the following:
• Prim's MST algorithm. (CLRS, Chapter 23.)
• Dijkstra's single source shortest path algorithm. (CLRS, Chapter 24.)
Some Applications of BFS
1. Bipartite Graph
We define bipartite graph as follows: 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.
In other to determine if a graph G = (V, E) is bipartite, we perform a BFS on it with a little
modification such that 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. Note that this modification does not change the running time of
BFS and remains O(V + E).
Formally, to check if the given graph is bipartite, the algorithm traverse the graph labeling the
vertices 0, 1, or 2 corresponding to unvisited, partition 1 and partition 2 nodes. If an edge is
detected between two vertices in the same partition, the algorithm returns.
ALGORITHM: BIPARTITE (G, S)
For each vertex u in V[G] − {s}
do color[u] ← WHITE
d[u] ← ∞
partition[u] ← 0
color[s] ← GRAY
partition[s] ← 1
d[s] ← 0
Q ← [s]
while Queue 'Q' is non-empty
do u ← head [Q]
for each v in Adj[u] do
if partition [u] ← partition [v]
then return 0
else
if color[v] ← WHITE then
then color[v] ← gray
d[v] = d[u] + 1
partition[v] ← 3 − partition[u]
ENQUEUE (Q, v)
DEQUEUE (Q)
Color[u] ← BLACK
Return 1
Correctness
As Bipartite (G, S) traverse the graph it labels the vertices with a partition number consisted with
the graph being bipartite. If at any vertex, algorithm detects an inconsistency, it shows with an
invalid return value. Partition value of u will always be a valid number as it was enqueued at
some point and its partition was assigned at that point. At line 19, partition of v will unchanged if
it already set, otherwise it will be set to a value opposite to that of vertex u.
Analysis
The lines added to BFS algorithm take constant time to execute and so the running time is the
same as that of BFS which is O(V + E).
2. Diameter of Tree
The diameter of a tree T = (V, E) is the largest of all shortest-path distance in the tree and given
by max[dist(u, v)]. As we have mentioned that BSF can be use to compute, for every vertex in
graph, a path with the minimum number of edges between start vertex and current vertex. It is
quite easy to compute the diameter of a tree. For each vertex in the tree, we use BFS algorithm to
get a shortest-path. By using a global variable length, we record the largest of all shortest-paths.
ALGORITHM: TREE_DIAMETER (T)
maxlength ← 0
for s ← 0 to s < |V[T]|
do temp ← BSF(T, S)
if maxlength < temp
maxlength ← temp
increment s by 1
return maxlength
Analysis
This will clearly takes O(V(V + E)) time.
BFS ()
{
Initialize Q:
Insert the root vertex into Q.
While (Q is not empty) {
Delete an item from the Q and store it into vertex;
If (visited flag of vertex is false) {
Visit vertex;
Set visited flag of the vertex is equals to true;
Insert all the neighbors of the vertex in the Q;
}
}
DFS ()
{
Initialize stack;
Push the root vertex into stack.
While (stack is not empty) {
Pop an item from the stack and store it into vertex;
If (visited flag of vertex is false) {
Visit vertex;
Set visited flag of the vertex is equals to true;
Push all the neighbors of the vertex into stack ;
}
}

More Related Content

What's hot

Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Traian Rebedea
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First SearchBenjamin Sach
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Traian Rebedea
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first searchHossain Md Shakhawat
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs applicationUmme habiba
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7Bianca Teşilă
 
Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Traian Rebedea
 
Algorithms of graph
Algorithms of graphAlgorithms of graph
Algorithms of graphgetacew
 
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
 
Skiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivitySkiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivityzukun
 

What's hot (20)

Topological Sort
Topological SortTopological Sort
Topological Sort
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
2.5 graph dfs
2.5 graph dfs2.5 graph dfs
2.5 graph dfs
 
Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
Graph
GraphGraph
Graph
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7
 
Bfs
BfsBfs
Bfs
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
 
Topological sorting
Topological sortingTopological sorting
Topological sorting
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7
 
Data structure
Data structureData structure
Data structure
 
Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9
 
Algorithms of graph
Algorithms of graphAlgorithms of graph
Algorithms of graph
 
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
 
Skiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivitySkiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivity
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 

Similar to BFS, Breadth first search | Search Traversal Algorithm

Similar to BFS, Breadth first search | Search Traversal Algorithm (20)

Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
logic.pptx
logic.pptxlogic.pptx
logic.pptx
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
 
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
 
Lecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.pptLecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.ppt
 
Graphs
GraphsGraphs
Graphs
 
Algorithm Exam Help
Algorithm Exam Help Algorithm Exam Help
Algorithm Exam Help
 
Scalable Online Betweenness Centrality in Evolving Graphs
Scalable Online Betweenness Centrality in Evolving GraphsScalable Online Betweenness Centrality in Evolving Graphs
Scalable Online Betweenness Centrality in Evolving Graphs
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Graph
GraphGraph
Graph
 
DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
 
Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
 
Graph applications chapter
Graph applications chapterGraph applications chapter
Graph applications chapter
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 

More from MSA Technosoft

What is a Digital Signature? | How Digital Signature work?
What is a Digital Signature? | How Digital Signature work?What is a Digital Signature? | How Digital Signature work?
What is a Digital Signature? | How Digital Signature work?MSA Technosoft
 
Cascading Style Sheets - CSS - Tutorial
Cascading Style Sheets - CSS  -  TutorialCascading Style Sheets - CSS  -  Tutorial
Cascading Style Sheets - CSS - TutorialMSA Technosoft
 
www | HTTP | HTML - Tutorial
www | HTTP | HTML - Tutorialwww | HTTP | HTML - Tutorial
www | HTTP | HTML - TutorialMSA Technosoft
 
Responsive Web Design | Website Designing
Responsive Web Design | Website DesigningResponsive Web Design | Website Designing
Responsive Web Design | Website DesigningMSA Technosoft
 
MIS ( Management Information System ) | DEFINITION, IMPORTANCE & BENIFITS
MIS ( Management  Information System ) | DEFINITION, IMPORTANCE & BENIFITSMIS ( Management  Information System ) | DEFINITION, IMPORTANCE & BENIFITS
MIS ( Management Information System ) | DEFINITION, IMPORTANCE & BENIFITSMSA Technosoft
 
eCommerce | Electronic Commerce
eCommerce | Electronic CommerceeCommerce | Electronic Commerce
eCommerce | Electronic CommerceMSA Technosoft
 
Digital Marketing | Internet Marketing | Social Networking
Digital Marketing | Internet Marketing | Social NetworkingDigital Marketing | Internet Marketing | Social Networking
Digital Marketing | Internet Marketing | Social NetworkingMSA Technosoft
 
Cascading Style Sheet | CSS
Cascading Style Sheet | CSSCascading Style Sheet | CSS
Cascading Style Sheet | CSSMSA Technosoft
 
Data communication and computer networks | Network Topologies
Data communication and computer networks | Network TopologiesData communication and computer networks | Network Topologies
Data communication and computer networks | Network TopologiesMSA Technosoft
 

More from MSA Technosoft (14)

Computer networks-4
Computer networks-4Computer networks-4
Computer networks-4
 
Computer networks-3
Computer networks-3Computer networks-3
Computer networks-3
 
Computer networks-2
Computer networks-2Computer networks-2
Computer networks-2
 
Computer networks-1
Computer networks-1Computer networks-1
Computer networks-1
 
What is a Digital Signature? | How Digital Signature work?
What is a Digital Signature? | How Digital Signature work?What is a Digital Signature? | How Digital Signature work?
What is a Digital Signature? | How Digital Signature work?
 
Cascading Style Sheets - CSS - Tutorial
Cascading Style Sheets - CSS  -  TutorialCascading Style Sheets - CSS  -  Tutorial
Cascading Style Sheets - CSS - Tutorial
 
www | HTTP | HTML - Tutorial
www | HTTP | HTML - Tutorialwww | HTTP | HTML - Tutorial
www | HTTP | HTML - Tutorial
 
Responsive Web Design | Website Designing
Responsive Web Design | Website DesigningResponsive Web Design | Website Designing
Responsive Web Design | Website Designing
 
MIS ( Management Information System ) | DEFINITION, IMPORTANCE & BENIFITS
MIS ( Management  Information System ) | DEFINITION, IMPORTANCE & BENIFITSMIS ( Management  Information System ) | DEFINITION, IMPORTANCE & BENIFITS
MIS ( Management Information System ) | DEFINITION, IMPORTANCE & BENIFITS
 
Hacking tutorial
Hacking tutorialHacking tutorial
Hacking tutorial
 
eCommerce | Electronic Commerce
eCommerce | Electronic CommerceeCommerce | Electronic Commerce
eCommerce | Electronic Commerce
 
Digital Marketing | Internet Marketing | Social Networking
Digital Marketing | Internet Marketing | Social NetworkingDigital Marketing | Internet Marketing | Social Networking
Digital Marketing | Internet Marketing | Social Networking
 
Cascading Style Sheet | CSS
Cascading Style Sheet | CSSCascading Style Sheet | CSS
Cascading Style Sheet | CSS
 
Data communication and computer networks | Network Topologies
Data communication and computer networks | Network TopologiesData communication and computer networks | Network Topologies
Data communication and computer networks | Network Topologies
 

Recently uploaded

ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 

Recently uploaded (20)

ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

BFS, Breadth first search | Search Traversal Algorithm

  • 1. Breadth-First Search Traversal Algorithm Breadth-first search is a way to find all the vertices reachable from the a given source vertex, s. Like depth first search, BFS traverse a connected component of a given graph and defines a spanning tree. Intuitively, the basic idea of the breath-first search is this: send a wave out from source s. The wave hits all vertices 1 edge from s. From there, the wave hits all vertices 2 edges from s. Etc. We use FIFO queue Q to maintain the wavefront: v is in Q if and only if wave has hit v but has not come out of v yet. Overall Strategy of BFS Algorithm Breadth-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. To keep track of progress, breadth-first-search colors each vertex. Each vertex of the graph is in one of three states: 1. Undiscovered; 2. Discovered but not fully explored; and 3. Fully explored. The state of a vertex, u, is stored in a color variable as follows: 1. color[u] = White - for the "undiscovered" state, 2. color [u] = Gray - for the "discovered but not fully explored" state, and 3. color [u] = Black - for the "fully explored" state. The BFS(G, s) algorithm develops a breadth-first search tree with the source vertex, s, as its root. The parent or predecessor of any other vertex in the tree is the vertex from which it was first discovered. For each vertex, v, the parent of v is placed in the variable π[v]. Another variable, d[v], computed by BFS contains the number of tree edges on the path from s to v. The breadth- first search uses a FIFO queue, Q, to store gray vertices. Algorithm: Breadth-First Search Traversal
  • 2. BFS(V, E, s) 1. for each u in V − {s} ▷ for each vertex u in V[G] except s. 2. do color[u] ← WHITE 3. d[u] ← infinity 4. π[u] ← NIL 5. color[s] ← GRAY ▷ Source vertex discovered 6. d[s] ← 0 ▷ initialize 7. π[s] ← NIL ▷ initialize 8. Q ← {} ▷ Clear queue Q 9. ENQUEUE(Q, s) 10 while Q is non-empty 11. do u ← DEQUEUE(Q) ▷ That is, u = head[Q] 12. for each v adjacent to u ▷ for loop for every node along with edge. 13. do if color[v] ← WHITE ▷ if color is white you've never seen it before 14. then color[v] ← GRAY 15. d[v] ← d[u] + 1 16. π[v] ← u 17. ENQUEUE(Q, v) 18. DEQUEUE(Q) 19. color[u] ← BLACK Example: The following figure (from CLRS) illustrates the progress of breadth-first search on the undirected sample graph. a. After initialization (paint every vertex white, set d[u] to infinity for each vertex u, and set the parent of every vertex to be NIL), the source vertex is discovered in line 5. Lines 8-9 initialize Q to contain just the source vertex s. b. The algorithm discovers all vertices 1 edge from s i.e., discovered all vertices (w and r) at level 1.
  • 3. c. d. The algorithm discovers all vertices 2 edges from s i.e., discovered all vertices (t, x, and v) at level 2. e. f. g. The algorithm discovers all vertices 3 edges from s i.e., discovered all vertices (u and y) at level 3.
  • 4. h. i. The algorithm terminates when every vertex has been fully explored. Analysis • The while-loop in breadth-first search is executed at most |V| times. The reason is that every vertex enqueued at most once. So, we have O(V). • The for-loop inside the while-loop is executed at most |E| times if G is a directed graph or 2|E| times if G is undirected. The reason is that every vertex dequeued at most once and we examine (u, v) only when u is dequeued. Therefore, every edge examined at most once if directed, at most twice if undirected. So, we have O(E). Therefore, the total running time for breadth-first search traversal is O(V + E). Lemma 22.3 (CLRS) At any time during the execution of BFS suppose that Q contains the vertices {v1, v2, ..., vr} with v1 at the head and vr at the tail. Then d[v1] ≤ d[v2] ≤ ... ≤ d[vr] ≤ d[v1] + 1. Let v be any vertex in V[G]. If v is reachable from s then let δ(s, v) be the minimum number of edges in E[G] that must be traversed to go from vertex s to vertex v. If v is not reachable from s then let δ(s, v) = ∞. Theorem 22.5 (CLRS) If BFS is run on graph G from a source vertex s in V[G] then for all v in V[G], d[v] = δ(s, v) and if v ≠ s is reachable from s then one of the shortest paths from s to v is a shortest path from s to π[v] followed by the edge from π[v] to v. BFS builds a tree called a breadth-first-tree containing all vertices reachable from s. The set of edges in the tree (called tree edges) contain (π[v], v) for all v where π[v] ≠ NIL.
  • 5. If v is reachable from s then there is a unique path of tree edges from s to v. Print-Path(G, s, v) prints the vertices along that path in O(|V|) time. Print-Path(G, s, v) if v = s then print s else if π[v] ← NIL then print "no path exists from " s "to" v" else Print-Path(G, s, π[v]) print v Algorithms based on BFS Based upon the BFS, there are O(V + E)-time algorithms for the following problems: • Testing whether graph is connected. • Computing a spanning forest of graph. • Computing, for every vertex in graph, a path with the minimum number of edges between start vertex and current vertex or reporting that no such path exists. • Computing a cycle in graph or reporting that no such cycle exists. In our course, we will use BFS in the following: • Prim's MST algorithm. (CLRS, Chapter 23.) • Dijkstra's single source shortest path algorithm. (CLRS, Chapter 24.) Some Applications of BFS 1. Bipartite Graph We define bipartite graph as follows: 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. In other to determine if a graph G = (V, E) is bipartite, we perform a BFS on it with a little
  • 6. modification such that 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. Note that this modification does not change the running time of BFS and remains O(V + E). Formally, to check if the given graph is bipartite, the algorithm traverse the graph labeling the vertices 0, 1, or 2 corresponding to unvisited, partition 1 and partition 2 nodes. If an edge is detected between two vertices in the same partition, the algorithm returns. ALGORITHM: BIPARTITE (G, S) For each vertex u in V[G] − {s} do color[u] ← WHITE d[u] ← ∞ partition[u] ← 0 color[s] ← GRAY partition[s] ← 1 d[s] ← 0 Q ← [s] while Queue 'Q' is non-empty do u ← head [Q] for each v in Adj[u] do if partition [u] ← partition [v] then return 0 else if color[v] ← WHITE then then color[v] ← gray d[v] = d[u] + 1 partition[v] ← 3 − partition[u] ENQUEUE (Q, v) DEQUEUE (Q) Color[u] ← BLACK Return 1 Correctness As Bipartite (G, S) traverse the graph it labels the vertices with a partition number consisted with the graph being bipartite. If at any vertex, algorithm detects an inconsistency, it shows with an invalid return value. Partition value of u will always be a valid number as it was enqueued at some point and its partition was assigned at that point. At line 19, partition of v will unchanged if it already set, otherwise it will be set to a value opposite to that of vertex u. Analysis
  • 7. The lines added to BFS algorithm take constant time to execute and so the running time is the same as that of BFS which is O(V + E). 2. Diameter of Tree The diameter of a tree T = (V, E) is the largest of all shortest-path distance in the tree and given by max[dist(u, v)]. As we have mentioned that BSF can be use to compute, for every vertex in graph, a path with the minimum number of edges between start vertex and current vertex. It is quite easy to compute the diameter of a tree. For each vertex in the tree, we use BFS algorithm to get a shortest-path. By using a global variable length, we record the largest of all shortest-paths. ALGORITHM: TREE_DIAMETER (T) maxlength ← 0 for s ← 0 to s < |V[T]| do temp ← BSF(T, S) if maxlength < temp maxlength ← temp increment s by 1 return maxlength Analysis This will clearly takes O(V(V + E)) time.
  • 8. BFS () { Initialize Q: Insert the root vertex into Q. While (Q is not empty) { Delete an item from the Q and store it into vertex; If (visited flag of vertex is false) { Visit vertex; Set visited flag of the vertex is equals to true; Insert all the neighbors of the vertex in the Q; } } DFS () { Initialize stack; Push the root vertex into stack. While (stack is not empty) { Pop an item from the stack and store it into vertex; If (visited flag of vertex is false) { Visit vertex; Set visited flag of the vertex is equals to true; Push all the neighbors of the vertex into stack ; } }