{"id":165,"date":"2026-09-03T07:07:05","date_gmt":"2026-09-03T07:07:05","guid":{"rendered":"https:\/\/alpeshconnect.in\/blog\/?p=165"},"modified":"2026-09-03T07:07:05","modified_gmt":"2026-09-03T07:07:05","slug":"write-a-program-to-implement-bfs","status":"publish","type":"post","link":"https:\/\/alpeshconnect.in\/blog\/2026\/09\/03\/write-a-program-to-implement-bfs\/","title":{"rendered":"Write a program to implement BFS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from collections import deque\n\ndef bfs(graph, start_node):\n    \"\"\"\n    Performs Breadth-First Search (BFS) on a graph.\n    \"\"\"\n    # Set to keep track of visited nodes\n    visited = set()\n    # Queue to manage the nodes to visit (FIFO)\n    queue = deque([start_node])\n    # List to store the order of traversal\n    traversal_order = []\n\n    # Mark the start node as visited\n    visited.add(start_node)\n\n    while queue:\n        # Dequeue a node from the front of the queue\n        node = queue.popleft()\n        traversal_order.append(node)\n\n        # Iterate through all neighbors of the current node\n        for neighbor in graph[node]:\n            if neighbor not in visited:\n                # Mark neighbor as visited and enqueue it\n                visited.add(neighbor)\n                queue.append(neighbor)\n\n    return traversal_order\n\n# --- Example Usage (Graph of Indian Cities) ---\n# Representing the graph as an Adjacency List\ncity_graph = {\n    'Mumbai': ['Delhi', 'Chennai', 'Hyderabad'],\n    'Delhi': ['Mumbai', 'Kolkata', 'Jaipur'],\n    'Chennai': ['Mumbai', 'Hyderabad', 'Bangalore'],\n    'Hyderabad': ['Mumbai', 'Chennai', 'Bangalore'],\n    'Kolkata': ['Delhi'],\n    'Jaipur': ['Delhi'],\n    'Bangalore': ['Chennai', 'Hyderabad']\n}\n\nprint(\"BFS Traversal starting from 'Mumbai':\")\nresult = bfs(city_graph, 'Mumbai')\nprint(\" -&gt; \".join(result))\n\nprint(\"\\nBFS Traversal starting from 'Delhi':\")\nresult = bfs(city_graph, 'Delhi')\nprint(\" -&gt; \".join(result))<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>from collections import deque def bfs(graph, start_node): &#8220;&#8221;&#8221; Performs Breadth-First Search (BFS) on a graph. &#8220;&#8221;&#8221; # Set to keep track of visited nodes visited = set() # Queue to manage the nodes to visit (FIFO) queue = deque([start_node]) # List to store the order of traversal traversal_order = [] # Mark the start node [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":96,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,3],"tags":[52,25,26,45],"class_list":["post-165","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aiopd","category-bca","tag-aiopd","tag-bca","tag-bca-paper-solution","tag-python"],"_links":{"self":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":1,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"predecessor-version":[{"id":166,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/165\/revisions\/166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media\/96"}],"wp:attachment":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}