Distributed Bellman-Ford: Computing Shortest Paths in Decentralized Networks

Arpit Bhayani

Arpit Bhayani

Sep 02, 2022 • 7 min read

Play

Distributed Bellman-Ford: Computing Shortest Paths in Decentralized Networks

In standard centralized graph algorithms, calculating the shortest path between nodes assumes global knowledge of the network: an algorithm like Dijkstra’s or Bellman-Ford has immediate in-memory access to the entire adjacency list, all vertices, and all edge weights.

In a distributed system, this assumption completely breaks down. There is no central authority and no single node that knows the global topology. Instead:

  • Each node only knows about its immediate neighbors.
  • Each node only knows the weights of its incident edges (edges connected directly to it).
  • Each node knows the total number of nodes in the network (nn).

Despite having purely local information, nodes must collaborate to route messages along the most optimal path. The distributed variant of the Bellman-Ford algorithm provides an elegant, synchronous framework to compute single-source shortest paths (SSSP) across an entire decentralized network.


What Does “Shortest Path” Mean in Distributed Systems?

In textbook graph theory, edge weights are almost always modeled as geometric distance or latency. In production distributed architectures and networking layers, edge weight is an arbitrary cost function quantifying multiple operational metrics:

  1. Network Congestion: An edge connecting two nodes might have high queue depths or buffer saturation, making it more expensive.
  2. Bandwidth Capacity: A 10 Gbps link is prioritized over a saturated 1 Gbps link by assigning lower weights to higher-capacity channels.
  3. Financial Cost: Data egress between different cloud regions, data centers, or across tier-1 ISP transit lines can vary drastically (e.g., 10/TBvs.10/TB vs. 20/TB). Shortest-path routing can optimize directly for monetary cost.
  4. Reliability & Packet Loss: Highly unstable connections (e.g., noisy wireless hops or degraded cross-connects) can be assigned artificially high penalty weights.

By assigning a composite cost as the weight of each link, finding the “shortest path” translates to finding the most efficient, reliable, and cost-effective communication route across the cluster or peer-to-peer (P2P) network.


System Model & Assumptions

To run Bellman-Ford in a distributed environment, we assume the following system model:

  • Graph Representation: The network is represented as G=(V,E)G = (V, E), where V=n|V| = n and E=m|E| = m.
  • Local Knowledge: Each node uu knows only its own identifier, the total node count nn, its set of neighbors N(u)N(u), and the weight w(u,v)w(u, v) for each neighbor vN(u)v \in N(u).
  • Synchronous Execution: The algorithm operates in discrete, synchronized rounds (lock-step progression). In every round, a node can send a message to its neighbors, receive messages sent by neighbors in the same round, and perform local computations.
  • Source Node (i0i_0): A designated source node initiates the shortest-path tree construction to all other nodes in the network.
flowchart LR
    subgraph Local Knowledge of Node B
        A((Node A)) ---|w=4| B((Node B))
        B ---|w=2| C((Node C))
        B ---|w=7| D((Node D))
    end
    style B fill:#f96,stroke:#333,stroke-width:2px

Node B does not know how Node D connects to the rest of the cluster; it only knows its direct edges to A, C, and D, their respective weights, and the total node count nn.


Node State and Variables

Each node uu maintains two key pieces of state:

  1. dist (Distance): The current shortest estimated distance from the source node i0i_0 to node uu.
  2. parent (Predecessor): The immediate neighbor through which node uu achieves its current dist. This enables the reconstruction of the shortest path tree without any node storing the entire end-to-end path.

Initialization

At round r=0r = 0, every node initializes its state as follows:

dist[u]={0if u=i0if ui0\text{dist}[u] = \begin{cases} 0 & \text{if } u = i_0 \\ \infty & \text{if } u \neq i_0 \end{cases} parent[u]=null\text{parent}[u] = \text{null}

The Distributed Algorithm Mechanics

The distributed Bellman-Ford algorithm proceeds in synchronized rounds from r=1r = 1 to n1n - 1.

sequenceDiagram
    autonumber
    participant Source as Source (i0)
    participant Neighbor as Neighbor (v)
    participant Remote as Remote Node (w)

    Note over Source, Remote: Round 1
    Source->>Neighbor: Broadcast dist = 0
    Neighbor->>Neighbor: Relax: dist = min(inf, 0 + weight)
    Neighbor->>Neighbor: Update parent = i0
    
    Note over Source, Remote: Round 2
    Neighbor->>Remote: Broadcast updated dist
    Remote->>Remote: Relax: dist = min(inf, dist_v + weight)
    Remote->>Remote: Update parent = v

Round-by-Round Execution

During each round rr (where 1rn11 \le r \le n - 1):

  1. Message Dispatch: Every node uu sends its current dist[u] value to all of its direct neighbors vN(u)v \in N(u).
  2. Message Ingestion: Each node uu receives the incoming distance value dist[v] from each neighbor vv.
  3. Local Relaxation Step: For every received distance dist[v], node uu evaluates whether routing through neighbor vv offers a cheaper path to i0i_0 than its current estimate:
candidate_dist=dist[v]+w(v,u)\text{candidate\_dist} = \text{dist}[v] + w(v, u) If candidate_dist<dist[u]:\text{If } \text{candidate\_dist} < \text{dist}[u]: dist[u]candidate_dist\text{dist}[u] \leftarrow \text{candidate\_dist} parent[u]v\text{parent}[u] \leftarrow v
  1. Barrier Synchronization: Once all local updates are computed, the round terminates. The nodes proceed synchronously to round r+1r + 1.

Why Exactly n1n - 1 Rounds?

A simple path in a graph with nn vertices can contain at most n1n - 1 edges (otherwise, it would contain a cycle).

  • In round 1, all nodes adjacent to i0i_0 discover their shortest 1-hop paths.
  • In round 2, nodes up to 2 hops away discover their shortest paths, or existing nodes discover shorter 2-hop alternatives.
  • By induction, after round kk, every node has computed the shortest path from i0i_0 that uses at most kk edges.
  • Therefore, after n1n - 1 rounds, all possible simple shortest paths across the network are guaranteed to have stabilized.

Because every node independently knows the total network size nn, each node independently stops broadcasting after round n1n - 1. No additional distributed termination detection protocol is required.


Pseudocode

Below is the algorithmic representation executed locally on every node uVu \in V:

def distributed_bellman_ford(node_id, is_source, neighbors, edge_weights, n):
    # Initialization
    if is_source:
        dist = 0
    else:
        dist = float("inf")
    
    parent = None

    # Synchronous rounds
    for round in range(1, n):
        # 1. Send current distance to all immediate neighbors
        for neighbor in neighbors:
            send_message(to=neighbor, data=dist)
        
        # 2. Receive distance updates from all immediate neighbors
        incoming_messages = receive_messages_from_all_neighbors()
        
        # 3. Perform edge relaxation
        for neighbor, neighbor_dist in incoming_messages.items():
            cost_via_neighbor = neighbor_dist + edge_weights[neighbor]
            
            if cost_via_neighbor < dist:
                dist = cost_via_neighbor
                parent = neighbor
                
    return dist, parent

Complexity Analysis

When evaluating distributed algorithms, complexity is measured in two dimensions: Time Complexity (number of synchronous communication rounds) and Communication Complexity (total number of individual messages transmitted across the network).

MetricComplexityExplanation
Time ComplexityO(n)\mathcal{O}(n)The algorithm strictly executes n1n - 1 rounds.
Communication Complexity$\mathcal{O}(n \cdotE
Space Complexity per NodeO(deg(u))\mathcal{O}(\text{deg}(u))Each node only stores its neighbors, incident edge weights, scalar dist, and pointer parent.

Optimization: Message Pruning

In the naive version, every node transmits its distance value in every round, even if its local dist did not change.

A standard optimization is event-driven suppression: a node only sends an update to its neighbors if its dist was successfully relaxed in the preceding round. While the worst-case communication complexity remains O(nE)\mathcal{O}(n \cdot |E|), practical message overhead drops significantly across average graph topologies.


Summary of Key Takeaways

  • No Global Topology Needed: Nodes only need awareness of their immediate connections, incident weights, and total node count nn.
  • Decentralized Next-Hop Routing: Nodes do not compute complete end-to-end paths. Instead, the collective collection of parent pointers forms a distributed spanning tree directed toward the source node i0i_0.
  • Single-Source to All-Nodes Efficiency: Because message passing in distributed networks carries high overhead, finding the shortest path from a source to all destinations simultaneously amortizes communication costs compared to point-to-point queries.
  • Synchronous Predictability: Operating in synchronized rounds allows simple termination detection at exactly n1n - 1 rounds without complex consensus or distributed termination mechanisms.
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