Skip to content

ybakhan/distributed-leader-election

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Distributed Leader Election

C# .NET Framework Platform Algorithms Concurrency

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.


Algorithms

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)

All the Way

Every candidate's message travels the full ring. The node with the minimum ID declares itself leader once its own message returns.

As Far As

A message is forwarded only if it carries a smaller ID than any previously seen. Suppresses losers early, reducing unnecessary traffic.

As Far As Bidirectional

Extends As Far As to both ring directions simultaneously, halving expected travel distance.

Controlled 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.

Stages

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.

Alternate Steps

A variant of Stages where each candidate alternates the direction of its probe each round, reducing the constant factor in message cost.


Architecture

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.


Getting Started

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

Tests

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

About

C# simulation of six classic leader election algorithms on a concurrent ring network — All the Way, As Far As, Controlled Distance, Stages, and more

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages