A robust, asynchronous simulation of a distributed Spanning Tree algorithm. This project demonstrates how network nodes can dynamically discover the most efficient path to a root node using local communication only, similar to Distance Vector routing protocols.
- Custom Graph Parsing: Uses Regex to parse flexible graph definitions from text files.
- Asynchronous Simulation: Simulates network jitter and asynchronous PDU processing using randomized node selection.
- Convergence Detection: Automatically stops execution when the network stabilizes (no state changes for
Nrounds). - Modern Python: Built using
dataclasses, type hinting, andpathlibfor clean, maintainable code.
The algorithm is based on the Bellman-Ford principle. The node with the lowest ID is statically elected as the Root. Every other node initializes its distance to infinity (
In every round of the simulation, nodes broadcast their current distance to neighbors. A node
Where:
-
$D_v$ is the current shortest distance of node$v$ to the Root. -
$C_{u,v}$ is the cost of the link between$u$ and$v$ .
- Python 3.8 or higher.
- No external dependencies (Standard Library only).
Clone the repository:
git clone [https://github.com/yourusername/spanning-tree-sim.git](https://github.com/yourusername/spanning-tree-sim.git)
cd spanning-tree-sim- Create an input file named
graph_input.txtin the root directory (see format below). - Run the simulation:
python main.py- Check the output:
- Console: Displays the routing table (Next Hop for every node).
- File: Generates
spanning_tree_result.txtcontaining the final tree structure.
The parser allows comments (//) and is whitespace-tolerant.
- Define the Graph Name.
- Define Nodes (
Name=ID;). - Define Edges (
NodeA-NodeB:Cost;).
Graph MyNetwork // Name of the graph
// Node Definitions
RouterA = 1;
RouterB = 2;
RouterC = 3;
// Edge Definitions (Name-Name: Cost)
RouterA - RouterB : 10;
RouterB - RouterC : 5;
RouterA - RouterC : 20;
The script exports the edges that form the Minimum Spanning Tree (MST).
Spanning-Tree of MyNetwork {
Root: 1;
RouterB - RouterA;
RouterC - RouterB;
}
The core simulation loop runs until the network reaches a stable state (convergence).
| Parameter | Value | Description |
|---|---|---|
MAX_ROUNDS |
10,000 | Failsafe to prevent infinite loops. |
STABILITY_THRESHOLD |
100 | Rounds without change required to declare convergence. |
Selection Strategy |
Random | A random node acts as the "sender" in each step to simulate async behavior. |