Synchronous Breadth-First Search (BFS) in Distributed Systems

Arpit Bhayani

Arpit Bhayani

Aug 31, 2022 • 7 min read

Play

Synchronous Breadth-First Search (BFS) in Distributed Systems

In standard single-machine algorithm design, a Breadth-First Search (BFS) operates on an in-memory graph where the global topology, adjacency lists, and visited states are centrally accessible. However, in a distributed system, no single node holds the complete topological map. Each node only knows its immediate neighbors via physical or virtual communication links.

Performing a BFS in such an environment requires decentralized coordination. The distributed synchronous BFS algorithm establishes a structured traversal pattern, allowing nodes to collaboratively build a directed spanning tree, broadcast messages with minimal latency, compute topological metrics (such as the network diameter), and execute distributed computations.


1. The Distributed Execution Model

Local vs. Global Knowledge

In a distributed network modeled as a graph G=(V,E)G = (V, E):

  • Nodes (VV): Represent independent computing processes.
  • Edges (EE): Represent communication channels (e.g., persistent TCP connections).
  • Local Horizon: A node uu is aware only of its incident edges (neighbors N(u)N(u)). It has no upfront visibility into the global network size (V|V|) or diameter (DD).

The Synchronous Model

The algorithm operates under a synchronous network model. Computation proceeds in discrete, synchronized rounds (or lockstep phases):

  1. In each round, every participating node can send messages to its neighbors.
  2. Messages sent in round rr arrive at their destination before round r+1r + 1.
  3. Synchronization can be maintained using centralized clock ticks, global rounds, or local synchronizers (message-passing barriers).

2. Objective: Directed BFS Spanning Tree

The goal of running a distributed BFS from an initiator node i0i_0 is to construct a Directed Breadth-First Spanning Tree rooted at i0i_0:

  • Spanning: The tree covers all reachable nodes in the network.
  • Minimal Edge Subset: Redundant cycles are broken so each node (except the root) selects exactly one primary upstream neighbor as its parent.
  • Shortest Path Property: The tree preserves the shortest hop-count distance from i0i_0 to every node vVv \in V.
graph TD
    subgraph Cyclic Physical Topology
        A --- B
        A --- C
        B --- C
        B --- D
        C --- E
        D --- E
    end
graph TD
    subgraph BFS Directed Spanning Tree
        A((A: Root)) --> B((B))
        A --> C((C))
        B --> D((D))
        C --> E((E))
    end

3. Algorithm Mechanics: Round-by-Round Traversal

Let i0i_0 be the designated root node that initiates the search.

Round 1: Initiation

  1. Node i0i_0 marks itself as visited (marked = true) and sets parent = self.
  2. Node i0i_0 sends a SEARCH message to all its immediate neighbors N(i0)N(i_0).

Round r>1r > 1: Exploration & Parent Election

Nodes that receive one or more SEARCH messages during round rr evaluate their local state:

  • Unmarked Nodes: If a node uu receives SEARCH for the first time:
    • It marks itself as visited (marked = true).
    • It designates the sender as its parent. If multiple SEARCH messages arrive in the same round, an arbitrary deterministic tie-breaker is applied (e.g., lowest node ID).
    • In round r+1r + 1, node uu will forward the SEARCH message along all its outgoing edges except the edge to its parent.
  • Marked Nodes: If a node uu is already marked, it ignores the traversal and will not forward the message.

4. Establishing Parent-Child Relationships

Having nodes know their parent is not enough. For routing, aggregation, and termination detection, a parent must know its exact set of children in the spanning tree.

Because multiple nodes might send a SEARCH message to an already-visited neighbor, the receiving node must explicitly notify the sender whether or not it was chosen as a parent.

sequenceDiagram
    autonumber
    participant P as Node A (Potential Parent)
    participant C as Node B (Neighbor)
    
    P->>C: SEARCH Message
    alt B is Unmarked
        Note over C: Mark visited, set parent = Node A
        C-->>P: PARENT Acknowledgment
        Note over P: Add Node B to children list
    else B is Already Marked
        Note over C: Ignore new parent assignment
        C-->>P: NON_PARENT Acknowledgment
        Note over P: Do not add Node B to children list
    end

Feedback Protocol

Whenever a node vv receives a SEARCH message from node uu:

  1. If vv elects uu as its parent, it responds with a PARENT message.
  2. If vv rejects uu (because vv is already marked), it responds with a NON_PARENT message.

This explicit acknowledgment guarantees that parents maintain an accurate registry of their downstream children without hanging indefinitely.


5. Termination Detection: The Convergecast Primitive

One of the hardest problems in distributed systems is knowing when an algorithm has finished. In a decentralized graph, individual leaf nodes do not know the network depth, and the root node does not know how many total nodes exist.

To solve this, the synchronous BFS relies on Convergecast—the structural opposite of a broadcast.

graph BT
    D((Node D: Leaf)) -- ACK / Aggregated Result --> B((Node B))
    E((Node E: Leaf)) -- ACK / Aggregated Result --> C((Node C))
    B -- Aggregated Response --> A((Node A: Root))
    C -- Aggregated Response --> A

Convergecast Rules

  1. Leaf Identification: A node realizes it is a leaf in the spanning tree if all its neighbors respond with NON_PARENT messages (or it has no other outgoing edges).
  2. Synchronization Blocker: A non-leaf node uu cannot send its final response to its parent until it has received responses (PARENT or NON_PARENT and downstream completions) from all of its outgoing neighbors.
  3. Bottom-Up Propagation: Leaves initiate acknowledgments upward. Intermediate nodes aggregate reports from their children and forward the aggregated status to their parents.
  4. Root Completion: Once the root i0i_0 receives acknowledgments from all of its direct children, the entire network traversal is complete. The root can guarantee that all reachable nodes in the graph are discovered.

Channel TypeConvergecast MechanismComplexity Impact
Bi-directional LinksResponses travel back on the same physical link over which SEARCH was received.Constant per-edge overhead; clean round-trip communication.
Unidirectional LinksA child node cannot send a message directly back to its parent. It must initiate a secondary BFS routed through other paths to reach the parent.Exponential/multiplicative increase in message exchange; significantly higher coordination overhead.

In real-world distributed architectures, assuming bi-directional communication channels (e.g., standard TCP/IP sockets) avoids the massive message explosion required to acknowledge unidirectional flows.


7. Complexity Analysis

Distributed algorithms are evaluated primarily along two axes: Time Complexity (number of synchronous rounds) and Communication Complexity (total number of point-to-point messages exchanged).

Time Complexity: O(D)O(D)

  • Let DD be the diameter of the network (the maximum shortest-path distance between any two nodes in GG).
  • The exploration phase reaches the furthest node in at most DD rounds.
  • The convergecast phase takes at most another DD rounds to propagate completions back to the root.
  • Total execution time is bounded by 2D2D rounds, yielding O(D)O(D) time complexity.

Communication Complexity: O(E)O(|E|)

  • Over every communication link (edge eEe \in E), a SEARCH message is sent at most once per direction.
  • Every SEARCH message receives either a PARENT or NON_PARENT response.
  • For a graph with E|E| edges, the total message volume is bounded by O(E)O(|E|) messages in a bi-directional network.

8. Practical Applications in Distributed Systems

  1. Optimal Broadcast Routing: Once the directed BFS spanning tree is built, a broadcast from the root takes optimal time (O(D)O(D)) and minimal message overhead (V1|V| - 1 messages), completely avoiding duplicate transmissions across cycles.

  2. Distributed MapReduce & Aggregation: Convergecast operates as a distributed post-order tree traversal. A root node can dispatch computation tasks downward, wait for workers to process partitions, and aggregate summary metrics (e.g., sums, counts, min/max) upwards back to the coordinator.

  3. Network Diameter & Topology Discovery: By having nodes initiate concurrent BFS instances with localized node identifiers, the network can perform all-pairs shortest path calculations and establish the true diameter DD of the network without central orchestration.

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses