Synchronous Breadth First Search Algorithm to power broadcast in Distributed Systems



2114 views Distributed Systems



Breadth First Search is a critical algorithm in Distributed systems as it powers key features like

  • Broadcast in minimum time
  • Building topological understanding
  • Topological stat like Diameter

In this gist, we discuss a synchronous approach which means every node moves forward in the algorithm in sync. There are ways to achieve this, but the implementation of synchronous behavior is out of the scope of this gist.

Output of BFS

The output of this traversal is a Breadth-First Directed Spanning tree that covers all the nodes but a subset of edges. This output is important because we can use this spanning tree as a foundation for other applications and algorithms.

The algorithm

The node i0 initiates the BFS and it sends the search message to its neighbors. The nodes can either be marked or unmarked. If marked, they are part of the spanning tree already.

When an unmarked node receives the search message,

  • it marks itself
  • updates its parent to the node it received the search message from

In the next round, the nodes that received the message in the previous round participate, and send search messages to their neighbors, and the nodes receiving the messages do the needful.

Eventually, every node will be receiving the search message from some or the other node and will be part of the spanning tree.

Complexity Analysis

Time taken to complete the BFS is proportional to the diameter of the network and the number of messages exchanged will be equal to the number of outgoing edges in the network.

Conveying the children

With the current algorithm, every node knows its parent in the spanning tree but every node also needs to know which of its neighbors are its children in the spanning tree.

To achieve this, each node has to respond to the search message with a parent/non-parent message that tells the node if it was chosen to be the parent or not. This way, every node will know its parent and children in the spanning tree.

Termination of BFS

The most important part of any distributed algorithm is its termination. How would the node know that BFS is done?

The approach we use is called Convergecast.

The idea is to respond to the search message only when it received responses from all its children. This ensures that the node initiating the BFS would receive the messages from its children only after all the nodes respond to their corresponding parents.

Applications

After constructing the BFS Spanning Tree, we can use this constructed path to

  • do an efficient broadcast on the network
  • do distributed computation in the network

Arpit Bhayani

Arpit's Newsletter

CS newsletter for the curious engineers

❤️ by 38000+ readers

If you like what you read subscribe you can always subscribe to my newsletter and get the post delivered straight to your inbox. I write essays on various engineering topics and share it through my weekly newsletter.




Other essays that you might like



Be a better engineer

A set of courses designed to make you a better engineer and excel at your career; no-fluff, pure engineering.


Paid Courses

System Design for Beginners

A masterclass that helps early engineers and product managers become great at designing scalable systems.

300+ learners

Details →

System Design Masterclass

A masterclass that helps you become great at designing scalable, fault-tolerant, and highly available systems.

1000+ learners

Details →

Redis Internals

Learn internals of Redis by re-implementing some of the core features in Golang.

98+ learners

Details →

Free Courses

Designing Microservices

A free playlist to help you understand Microservices and their high-level patterns in depth.

823+ learners

Details →

GitHub Outage Dissections

A free playlist to help you learn core engineering from outages that happened at GitHub.

651+ learners

Details →

Hash Table Internals

A free playlist to help you understand the internal workings and construction of Hash Tables.

1027+ learners

Details →

BitTorrent Internals

A free playlist to help you understand the algorithms and strategies that power P2P networks and BitTorrent.

692+ learners

Details →