BFS,TOPOLOGICAL
SORTING
Breadth First Traversal (BFS)
● BFS traversal of a graph produces a spanning tree (a graph without loop as final
result.
● Queue data structure is used to implement BFS traversal
● Breadth First Search (BFS) is a fundamental graph traversal algorithm. It
involves visiting all the connected nodes of a graph in a level-by-level manner.
● Breadth First Search (BFS) is a graph traversal algorithm that explores all the
vertices in a graph at the current depth before moving on to the vertices at the next
depth level.
● It starts at a specified vertex and visits all its neighbors before moving on to the
next level of neighbors.
● BFS is commonly used in algorithms for pathfinding, connected components, and
shortest path problems in graphs.
Breadth First Search (BFS) algorithm
BFS is a graph traversal algorithm that explores all the vertices in a graph at the current depth level before moving on to the
vertices at the next depth level.
Here is the pseudocode for BFS:
BFS(G, start):
create an empty queue q
create an empty list visited to keep track of visited nodes
add start to q
mark start as visited
while q is not empty:
remove a vertex u from q
for each vertex v in G.neighbors(u):
if v is not visited:
add v to q
mark v as visited
Breadth First Traversal: Steps to Implement
BFS
Input: A Graph G=(V,E),Output: Spanning Tree
Step 1 - Define a Queue of size total number of vertices in the graph.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert into the Queue.
Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the Queue and
insert them into the Queue.
Step 4 - When there is no new vertex to be visited from the vertex which is at front of the Queue
then delete that vertex.
Step 5 - Repeat steps 3 and 4 until queue becomes empty
TIME COMPLEXITY OF BFS
BFS is a graph traversal algorithm that explores all the vertices in a graph at the current depth level before moving on
to the vertices at the next depth level. Here is the pseudocode for BFS:
BFS is a graph traversal algorithm that explores all the vertices in a graph at the current depth level before moving on
to the vertices at the next depth level. It uses a queue data structure to keep track of the vertices to be explored.
Here's the time complexity analysis of BFS algorithm step by step:
• Initialize a queue and mark the starting vertex as visited. Add the starting vertex to the queue. Time complexity: O(1)
• While the queue is not empty, remove a vertex from the queue and examine its adjacent vertices. For each adjacent
vertex, if it has not been visited yet, mark it as visited and add it to the queue.
• Time complexity: In the worst case, we may need to visit all the vertices and edges in the graph, so the time
complexity is O(V + E), where V is the number of vertices and E is the number of edges. • Repeat step 2 until the queue
is empty.
Therefore, the overall time complexity of the BFS algorithm is O(V + E), where V is the number of vertices and E is the
number of edges in the graph.
The space complexity of BFS algorithm is O(V), since in the worst case, we may need to store all the vertices in the
queue
Breadth First Traversal: Example
Breadth First Traversal: Example
bfs tree searching ,sortingUntitled presentation.pptx

bfs tree searching ,sortingUntitled presentation.pptx

  • 1.
  • 2.
    Breadth First Traversal(BFS) ● BFS traversal of a graph produces a spanning tree (a graph without loop as final result. ● Queue data structure is used to implement BFS traversal ● Breadth First Search (BFS) is a fundamental graph traversal algorithm. It involves visiting all the connected nodes of a graph in a level-by-level manner. ● Breadth First Search (BFS) is a graph traversal algorithm that explores all the vertices in a graph at the current depth before moving on to the vertices at the next depth level. ● It starts at a specified vertex and visits all its neighbors before moving on to the next level of neighbors. ● BFS is commonly used in algorithms for pathfinding, connected components, and shortest path problems in graphs.
  • 3.
    Breadth First Search(BFS) algorithm BFS is a graph traversal algorithm that explores all the vertices in a graph at the current depth level before moving on to the vertices at the next depth level. Here is the pseudocode for BFS: BFS(G, start): create an empty queue q create an empty list visited to keep track of visited nodes add start to q mark start as visited while q is not empty: remove a vertex u from q for each vertex v in G.neighbors(u): if v is not visited: add v to q mark v as visited
  • 4.
    Breadth First Traversal:Steps to Implement BFS Input: A Graph G=(V,E),Output: Spanning Tree Step 1 - Define a Queue of size total number of vertices in the graph. Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert into the Queue. Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the Queue and insert them into the Queue. Step 4 - When there is no new vertex to be visited from the vertex which is at front of the Queue then delete that vertex. Step 5 - Repeat steps 3 and 4 until queue becomes empty
  • 5.
    TIME COMPLEXITY OFBFS BFS is a graph traversal algorithm that explores all the vertices in a graph at the current depth level before moving on to the vertices at the next depth level. Here is the pseudocode for BFS: BFS is a graph traversal algorithm that explores all the vertices in a graph at the current depth level before moving on to the vertices at the next depth level. It uses a queue data structure to keep track of the vertices to be explored. Here's the time complexity analysis of BFS algorithm step by step: • Initialize a queue and mark the starting vertex as visited. Add the starting vertex to the queue. Time complexity: O(1) • While the queue is not empty, remove a vertex from the queue and examine its adjacent vertices. For each adjacent vertex, if it has not been visited yet, mark it as visited and add it to the queue. • Time complexity: In the worst case, we may need to visit all the vertices and edges in the graph, so the time complexity is O(V + E), where V is the number of vertices and E is the number of edges. • Repeat step 2 until the queue is empty. Therefore, the overall time complexity of the BFS algorithm is O(V + E), where V is the number of vertices and E is the number of edges in the graph. The space complexity of BFS algorithm is O(V), since in the worst case, we may need to store all the vertices in the queue
  • 6.
  • 7.