Distributed BFS

Suvidha Yadav
8 min readDec 9, 2022

--

Throughout this series, we’ve made connections between things. We’ve seen that linked lists are the basis of stacks and queues, and trees are a subset of graphs. After all, everything that seems super-complex (at least in the world of computer science) isn’t as complex as it first seems. Rather, everything is made up of smaller little things. Recognising this is a big step towards demystifying computer science and everything that seems too hard about it. A big, intimidating idea is really just a small idea built, expanded, and restructured for a different purpose. Understanding (and solving!) a problem is much less if you can break it down into the fewest moving parts.

We use the same methodology today as a first step into the world of somewhat more complex graph traversal algorithms. Last week we stepped into the practical side of graph problems by putting theory into practice. However, just learning about data structures is not enough. You need to know how to manipulate it, how to manipulate it, and often how to look for something in it! Many computer science problems are actually graph search problems. So it’s helpful to know how to search graphs on a practical level.

Let’s continue!

What is BFS Algorithm?

What makes BFS unique is that it is very good at determining the shortest path between each node in a diagram and its “parent” node. In fact, most BFS implementations keep track of each node’s preceding “parent” node. This is useful because the pointer of the path from the starting node to the node can be used to determine the shortest path through the graph.

Learning about algorithms like breadth-first search graphs is exciting, but it’s not as fun if you don’t know how efficient they actually are at traversing the graph! Understanding the runtime complexity of BFS graph traversal The best way to do that is to look at how it actually works in graph representation. That is, to examine how the algorithm works on real graphs in programmatic form.

Parent pointers back to the starting node can be rearranged to form a tree. If you visualise these parent pointers as a tree structure (rather than a diagram), you’ll see that each adjacent node’s level comes into play and becomes much clearer. Additionally, if you backtrack from a node and follow its parent pointer back to the main starting node, the level of the node in the tree corresponds to its level in the diagram, and the level of the node indicates the number of steps required. I understand this. Take from this node and return to the “parent” starting node, node b. A pointer actually points to at least one of these “shortest paths”, but most of the graphs we work with probably have different “shortest paths”.

Before deciding how to traverse this graph, we need a starting point. After all, the first (and perhaps most important) difference between graph and tree traversal is deciding where to start the search! In tree traversal, we always start at the root node, and for each level of the BFS Go down the tree structure. But when it comes to graphs, there is no notion of a “root” node, so there is no obvious beginning.

BFS is an approach for traveling a graph in which you start at a source node and move layer by layer, looking at the nodes that are connected directly to the source node. The next step in BFS traversal is to go on to the neighbour nodes below.

The BFS states that you must travel along the graph in a breadthwise direction:

Begin by traversing the current layer’s nodes horizontally.
continue to the following layer.

The node is kept and labeled as “visited” by Breadth-First Search using a queue data structure until all of its adjacent vertices that are closely related to it were also labeled as such. The First In First Out (FIFO) principle defines how the queue works, thus the neighbours of a node are shown in the order in which the node receives them, beginning with the node that was put first.

How Does the BFS Algorithm Work?

The vertices were stored using a queue data structure technique in breadth-first search. Further, the queue follows to the First In First Out (FIFO) principle, which states that the node’s neighbours will be shown beginning with the node that was put first.

The BFS algorithm’s transverse approaches the nodes in two different ways.

Reached node
Node not visited

How Does the Algorithm Operate?

The source node should come first.

The visited list should now include the node at the head of the queue.

Make a list of the nodes that are near that vertex and mark them as visited.

Once the nodes have been visited, dequeue them.

Until the queue is empty, continue doing the operations.

Why Do You Need Breadth-First Search Algorithm?

The BFS Algorithm should be used to explore graph data structures for a number of reasons. Some of the crucial characteristics that make the BFS algorithm required include :

The BFS algorithm has a simple and robust architecture. The BFS algorithm evaluates the nodes on the diagram and helps determine the shortest path around the nodes. The BFS algorithm can traverse the graph with a minimum number of iterations. The BFS algorithm runs smoothly and the method doesn’t get caught in an infinite loop. Compared to other algorithms, the results of the BFS algorithm show higher accuracy.

Pseudocode Of Breadth-First Search Algorithm

Example of Breadth-First Search Algorithm

Graph traversal in a tree structure requires an algorithm to visit, check, and update each unvisited node. The order in which a graph traversal visits nodes in a graph classifies them.

This is especially useful for the huge graphs commonly found in computer science problems. For example, in the image shown here, I have many nodes and many levels, and I want to know how many steps are required from level 0 to the last level (whatever that is). Having easy access to the shortest path in this diagram is very helpful in solving this problem.

Of course, this relies on tracking parent pointers. Most implementations of BFS use a set of lists (or something similar) to keep track of the shortest path between the starting “parent” node and each terminal node in the path. Also note that in larger graphs there are always many ways to get from one node to another, and there can be many “shortest paths” between one node and another. please give me. But if we know the level of nodes relative to the starting “parent” node, we also know, for each proxy, the minimum number of steps required to find the shortest path back to the “parent node” from any node that we what started.

The BFS algorithm starts at the first starting node in the graph and traverses it completely. After successfully traversing the first node, visit the next vertex that does not move on the graph and display it.

Step 10: Because the queue is now empty, the bfs traversal has ended.

Complexity Of Breadth-First Search Algorithm

Learning about algorithms like breadth-first search graphs is exciting, but it’s not as fun if you don’t know how efficient they actually are at traversing the graph! Understanding the runtime complexity of BFS graph traversal The best way to do that is to look at how it actually works in graph representation. That is, to examine how the algorithm works on real graphs in programmatic form.

Time complexity of breadth-first search algorithm:
The time complexity of the breadth-first search algorithm, in the worst case, can be specified as O(|V|+|E|), since it examines all nodes and all edges. The graph has |V| vertices and |E| edges. that is.

Spatial complexity of breadth-first search algorithm:
The spatial complexity can be defined as O(|V|). where |V| is: is the number of vertices in the graph, and various data structures are needed to determine which vertices have already been added to the queue. This is also the space required for the graph and depends on the graph representation used in the algorithm implementation.

A bit of runtime complexity becomes apparent when you consider what you need to do for a single node. You will loop through the vertex adjacency list once for each vertex in the graph. That is, we need to look at the adjacent nodes (which in the adjacency list are really just representations of all edges) exactly once. This is done when dequeuing a node and enqueuing all required neighbors.

Recall, however, that if the graph is undirected, each edge appears twice in the adjacency list, once for each connected vertex. But if the graph is directed, each edge appears only once in his one adjacency list of nodes.

Application Of Breadth-First Search Algorithm

The advantage of using breadth-first search to traverse the graph is that it easily tells you the shortest path from one node to another.

  1. For unweighted plots, the tree with the shortest path and smallest distance should be used.
    The shortest path in an unweighted graph is the path with the fewest edges. Using width first will always reach the vertex from the given source with the fewest number of edges. In an unweighted graph, each spanning tree is the smallest spanning tree and can be identified by first traversing depth or width.
  2. Peer-to-peer network
    Breadth-first search is used in peer-to-peer networks like BitTorrent to find all neighbouring nodes.
  3. Search engine crawler
    The crawler builds the index on a breadth-first basis. The goal is to start from the original page, follow all the links there and then repeat. Crawlers can also use depth first traversal. However, the advantage of breadth-first traversal is that you can limit the depth or layers of the tree created.
  4. Social networking sites
    A breadth search can be used to identify people within a certain distance “d” of a person in the social network, up to “d” levels.
  5. GPS navigation system
    Find all nearby places using breadth-first search.
  6. Broadcast network
    Broadcast packets on the network reach all nodes using a breadth-first search.
  7. Garbage collection
    Cheney’s technique uses a breadth-first search to duplicate garbage collection. Breadth-first search is preferred over depth-first search algorithms due to better locality of reference.
  8. Diagram cycle detection
    Cycle detection in undirected graphs can be performed using either breadth-first or depth-first search. BFS can also be used to find cycles in directed graphs.
  9. Identify the route
    Breadth-first or depth-first traversal can be used to check if there is a path between two vertices.
  10. Find all nodes inside connected components
    To find all nodes reachable from a given node, either width traversal or depth traversal can be used.

--

--

Suvidha Yadav
Suvidha Yadav

No responses yet