Gadgets 4 Students Career Guide Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
562 views
in RTU/BTU B.Tech(CSE-III Sem) DSA LAB by Goeduhub's Expert (7.6k points)
edited by
Write a program to implement Breadth first traversal of graphs represented using adjacency matrix and list.

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

Ques.Write a program to implement Breadth first traversal of graphs represented using adjacency matrix and list.

Answer :

Breadth-first search : BFS is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root, and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level. The aim of BFS algorithm is to traverse the graph as close as possible to the root node. Queue is used in the implementation of the breadth first search. 

Steps  : 

  1. Push the root node in the Queue.
  2. Loop until the queue is empty.
  3. Remove the node from the Queue.
  4. If the removed node has unvisited child nodes, mark them as visited and insert the unvisited children in the queue.

Example : 

Program : 

graph = {'A': ['B', 'C', 'E'],

         'B': ['A','D', 'E'],

         'C': ['A', 'F', 'G'],

         'D': ['B'],

         'E': ['A', 'B','D'],

         'F': ['C'],

         'G': ['C']}

def bfs_connected_component(graph, start):

   explored = []

   queue = [start]

   while queue:

       node = queue.pop(0)

       if node not in explored:

           explored.append(node)

           neighbours = graph[node]

           for neighbour in neighbours:

               queue.append(neighbour)

   return explored

bfs_connected_component(graph,'A')

Output : ['A', 'B', 'C', 'E', 'D', 'F', 'G']


For more Rajasthan Technical University CSE-III Sem DSA Lab Experiments  CLICK HERE

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...