7/31/2025
DISTRIBUTED
SYSTEM
LAB: 03
“Election Algorithm”
7/31/2025 Compiled by: Er. Bibat Thokar 1
Election algorithm
Bully Algorithm
Ring Algorithm
7/31/2025 Compiled by: Er. Bibat Thokar 2
Compiled By: Er. Bibat Thokar 1
7/31/2025
Bully algorithm
The Bully algorithm is a classic algorithm used for leader election in distributed systems.
It allows nodes in a network to determine a coordinator or leader among themselves.
7/31/2025 Compiled by: Er. Bibat Thokar 3
Bully algorithm
7/31/2025 Compiled by: Er. Bibat Thokar 4
Compiled By: Er. Bibat Thokar 2
7/31/2025
Bully algorithm
Algorithm
Each node in the distributed system is assigned a unique identifier or process ID.
When a node detects that the coordinator is unresponsive (e.g., due to failure or network
disconnection), it initiates an election process by sending an Election message to all nodes
with higher process IDs.
Upon receiving an Election message, the higher process ID nodes respond with an OK
message to the initiating node.
If no higher process ID nodes respond within a specified timeout period, the initiating
node assumes that it has the highest ID and becomes the coordinator.
7/31/2025 Compiled by: Er. Bibat Thokar 5
Bully algorithm
Algorithm
It sends a Coordinator message to all other nodes, notifying them of its election as the
new leader.
If a higher process ID node responds to the Election message, it starts its own election
process by sending Election messages to all nodes with higher process IDs.
Steps 2-5 repeat until a node with the highest ID is found, and it becomes the coordinator.
Once the new coordinator is elected, all nodes update their local state to reflect the new
leader.
7/31/2025 Compiled by: Er. Bibat Thokar 6
Compiled By: Er. Bibat Thokar 3
7/31/2025
Bully Algorithm Implementation
7/31/2025 Compiled by: Er. Bibat Thokar 7
Bully Algorithm Implementation
7/31/2025 Compiled by: Er. Bibat Thokar 8
Compiled By: Er. Bibat Thokar 4
7/31/2025
Bully Algorithm Implementation
7/31/2025 Compiled by: Er. Bibat Thokar 9
Bully Algorithm Implementation
7/31/2025 Compiled by: Er. Bibat Thokar 10
Compiled By: Er. Bibat Thokar 5
7/31/2025
Bully Algorithm Implementation
Output
7/31/2025 Compiled by: Er. Bibat Thokar 11
Ring algorithm
The Ring algorithm is a well-known algorithm used in distributed systems to elect a
leader among a group of nodes arranged in a ring topology
The basic idea behind the Ring algorithm is that nodes pass a message around the ring
until it reaches the node that should be elected as the leader
7/31/2025 Compiled by: Er. Bibat Thokar 12
Compiled By: Er. Bibat Thokar 6
7/31/2025
Ring algorithm
7/31/2025 Compiled by: Er. Bibat Thokar 13
Ring algorithm
Algorithm
Each node in the ring is assigned a unique identifier or ID.
Initially, no node is aware of which node should be elected as the leader.
Each node starts in an idle state.
The nodes are logically arranged in a ring, where each node has a successor and a
predecessor.
The last node in the ring has the first node as its successor, and the first node has the last
node as its predecessor, forming a closed loop.
To start the election process, a node initiates the process by sending a token or message
containing its own ID to its successor node.
7/31/2025 Compiled by: Er. Bibat Thokar 14
Compiled By: Er. Bibat Thokar 7
7/31/2025
Ring algorithm
Algorithm
When a node receives the token, it compares the ID in the token with its own ID.
If the ID in the token is greater than its own ID, it forwards the token to its successor without
making any changes.
If the ID in the token is smaller than its own ID, the node replaces the ID in the token with its
own ID and forwards the updated token to its successor.
The token continues to circulate around the ring until it reaches the node with the highest ID.
This node is considered the leader.
7/31/2025 Compiled by: Er. Bibat Thokar 15
Ring algorithm
Algorithm
Once a node receives the token and realizes that it has the highest ID, it
broadcasts a message to inform all other nodes about its election as the leader.
Each node, upon receiving the leader's message, updates its own knowledge
of the leader.
The algorithm terminates when all nodes have received the leader's message
and have updated their knowledge of the leader.
7/31/2025 Compiled by: Er. Bibat Thokar 16
Compiled By: Er. Bibat Thokar 8
7/31/2025
Ring Algorithm Implementation
7/31/2025 Compiled by: Er. Bibat Thokar 17
Ring Algorithm Implementation
7/31/2025 Compiled by: Er. Bibat Thokar 18
Compiled By: Er. Bibat Thokar 9
7/31/2025
Ring Algorithm Implementation
7/31/2025 Compiled by: Er. Bibat Thokar 19
Ring Algorithm Implementation
7/31/2025 Compiled by: Er. Bibat Thokar 20
Compiled By: Er. Bibat Thokar 10
7/31/2025
Ring Algorithm Implementation
Output
7/31/2025 Compiled by: Er. Bibat Thokar 21
Thank You!
7/31/2025 Compiled by: Er. Bibat Thokar 22
Compiled By: Er. Bibat Thokar 11