A C# simulation of six classic leader election algorithms on a unidirectional and bidirectional ring network. Each node runs concurrently on its own thread, communicating exclusively via message passing — no shared memory.
| Algorithm | Direction | Message Complexity |
|---|---|---|
| All the Way | Unidirectional | O(n²) |
| As Far As | Unidirectional | O(n log n) avg, O(n²) worst |
| As Far As Bidirectional | Bidirectional | O(n log n) avg |
| Controlled Distance | Bidirectional | O(n log n) |
| Stages | Bidirectional | O(n log n) |
| Alternate Steps | Bidirectional | O(n log n) |
Every candidate's message travels the full ring. The node with the minimum ID declares itself leader once its own message returns.
A message is forwarded only if it carries a smaller ID than any previously seen. Suppresses losers early, reducing unnecessary traffic.
Extends As Far As to both ring directions simultaneously, halving expected travel distance.
Candidates probe at exponentially doubling distances (2⁰, 2¹, 2², …). A node is eliminated as soon as a smaller-ID probe reaches it within range, giving guaranteed O(n log n) messages.
A bidirectional elimination tournament. In each stage, every surviving candidate sends to both neighbors; only the local maximum within a neighborhood survives to the next stage.
A variant of Stages where each candidate alternates the direction of its probe each round, reducing the constant factor in message cost.
RingElection/
├── Algorithm/ # Six election algorithm implementations
│ ├── ElectionBase.cs # Abstract base with state machine + message passing
│ ├── AllTheWay.cs
│ ├── AsFarAs.cs
│ ├── AsFarAsBidirectional.cs
│ ├── ControlledDistance.cs
│ ├── Stages.cs
│ └── AlternateSteps.cs
├── Common/ # Ring, INode, message types, enums
├── Util/ # CSV export, random list generator, LINQ extensions
├── ElectionEvaluation.cs # Comparative benchmark (outputs CSV)
└── RingElectionMain.cs # Interactive CLI entry point
RingElection.Test/ # NUnit test suite for each algorithm
Nodes are linked in a doubly-connected circular list (Ring). Each node holds two ConcurrentQueue<object> ports (left and right). The ring runs every node in parallel via Parallel.ForEach, with each node spinning on its state machine until it reaches Leader or Follower.
Requirements: Windows, .NET Framework 4.0+
Build: Open RingElection.sln in Visual Studio and build in Release mode.
Run:
RingElection\bin\Release\RingElection.exe
The interactive menu lets you run individual algorithm tests or launch a full comparative evaluation across ring sizes. The evaluation exports message counts and wall-clock times to CSV for analysis.
Example session:
Select test
(1) All the way test
...
(14) Comparative Evaluation
(15) Exit: 1
Enter at least 3 integer IDs separate by ',': 5,2,8,1,9
Node 5 sent 5 to node 2; messages sent 1
...
Node 1 is leader
Unit tests cover all six algorithms with fixed topologies verifying that exactly one leader is elected and all other nodes settle as followers.
Open the solution in Visual Studio and run via Test Explorer, or use the .NET CLI:
dotnet test RingElection.Test/RingElection.Test.csproj