0% found this document useful (0 votes)
175 views

Leader Election in Rings - O (n2) Algorithm, Bully Algorithm

The document discusses leader election algorithms for distributed systems. It describes two algorithms: 1. An O(n^2) algorithm where each node sends a message with its ID to the left and waits for a message from the right. The node with the largest ID is elected leader. This can require O(n^2) messages in the worst case. 2. An O(n log n) algorithm where nodes probe increasingly larger neighborhoods in phases to find the largest ID. Nodes that "win" their neighborhood in one phase probe further in the next phase. Exactly one node will receive its own probe and be elected leader.

Uploaded by

Venkat Aravind
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
175 views

Leader Election in Rings - O (n2) Algorithm, Bully Algorithm

The document discusses leader election algorithms for distributed systems. It describes two algorithms: 1. An O(n^2) algorithm where each node sends a message with its ID to the left and waits for a message from the right. The node with the largest ID is elected leader. This can require O(n^2) messages in the worst case. 2. An O(n log n) algorithm where nodes probe increasingly larger neighborhoods in phases to find the largest ID. Nodes that "win" their neighborhood in one phase probe further in the next phase. Exactly one node will receive its own probe and be elected leader.

Uploaded by

Venkat Aravind
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

18CS3109 - DISTRIBUTED COMPUTING

Course Outcome 2:
Leader Election in Rings - 𝑂𝑂(𝑛𝑛2 ), O(nlogn)
and Bully algorithms, Mutual Exclusion in
Shared Memory, CLASSICAL
ALGORITHMS- Token Based Algorithm:
Central Solution, Ring Based Mutual
Exclusion, Non-Token Based Algorithm: Ricart-
Agrawala’s Algorithm, Maekawa’s Algorithm.
1. LEADER ELECTION IN RINGS

i) An 𝑂𝑂(𝑛𝑛2) Algorithm / LCR Algorithm


ii)An 𝑂𝑂(𝑛𝑛 log 𝑛𝑛) Algorithm / HS
Algorithm
LEADER ELECTION IN RINGS
1. The Leader Election Problem
Theleader election problem several variants, and most
has general one is defined
below.
 Informally, the problem is for each processor eventually to decide that
either it is the leader or it is not the leader, subject to the constraint that
exactly one processor decides that it is the leader.

 For example, when a deadlock is created, because of processors


waiting in a cycle for each other, the deadlock can be broken by
electing one of the processor as a leader and removing it from the
cycle.
LEADER ELECTION IN RINGS
In terms of formal model, an algorithm is said to solve the
leader election problem if it satisfies the following conditions:
 Each processor has a set of elected(won) and not-elected (lost)
states.
 Once an elected state is entered, processor is
always in an
elected state (and similarly for not-elected).
 In every admissible execution:
 Exactly one processor (the leader) enters an elected state and
 All the remaining processors enters a not-elected state.
LEADER ELECTION IN RINGS
Uses of Leader Election
A leader can be used to coordinate activities of the system:
Find a spanning tree using the leader as the root
Reconstruct a lost token in a token-ring network
LEADER ELECTION IN RINGS
Ring Networks
In an oriented ring, processors have a consistent notion of left and
right.

 For example, if messages are always forwarded on channel 1, they


will cycle clockwise around the ring
LEADER ELECTION IN RINGS
Why to study Rings?
Simple Starting point, easy to analyze
Abstraction of a token ring
Lower bounds and impossibility results for ring
topology also apply to arbitrary topologies.
LEADER ELECTION IN RINGS

i) An 𝑂𝑂(𝑛𝑛2) Algorithm / LCR Algorithm


ii)An 𝑂𝑂(𝑛𝑛 log 𝑛𝑛) Algorithm / HS
Algorithm
LEADER ELECTION IN RINGS
i) An 𝑶𝑶(𝒏𝒏𝟐𝟐) Algorithm / LCR Algorithm
It’s 𝑂𝑂(𝑛𝑛2) Messages Leader Election Algorithm.
It’s also known as LeLann-Chang-Roberts (LCR) Algorithm.
LEADER ELECTION IN RINGS
 In this algorithm, each processor sends a message with its
identifier to its left and then waits for messages from right.

 When it receives such a message, it checks the identifier in this


message.
 If identifier > own identifier
• Forward identifier to left
 If identifier < own identifier
• Swallow and do not forward.
LEADER ELECTION IN RINGS
 If identifier=own identifier
• Declares itself as leader
• Sendstermination message to left and terminates
as a
leader.
•A processor that receives a termination
message forwards it to the left and terminates as a non-leader.
LEADER ELECTION IN RINGS
Correctness:
Elects a processor with largest id.
Message containing largest id passes through every processor.

Time: o(n)

Message Complexity: Depends on how the ids are arranged.


Largest id travels all around the ring (n messages)
2nd Largest id travels until reaching largest
3rd Largest id travels until reaching largest or
second largest etc.
LEADER ELECTION IN RINGS
Analysis of 𝑶𝑶(𝒏𝒏𝟐𝟐)
Algorithm

Figure: Ring with Θ(𝑛𝑛2) messages


LEADER ELECTION IN RINGS
Analysis of 𝑶𝑶(𝒏𝒏𝟐𝟐) Algorithm
Clearly algorithm never sends more than 𝑂𝑂(𝑛𝑛2) messages in any
admissible execution.

Moreover, there is an admissible execution in which


the algorithm sends Θ(𝑛𝑛2) messages.

Consider the ring where the identifiers of the processors are 0,


…….., n-1 and they are ordered as in figure.
LEADER ELECTION IN RINGS
Analysis of 𝑶𝑶(𝒏𝒏𝟐𝟐) Algorithm
In this configuration, the message of processor with identifier i is
sent exactly i+1 times.

Thus the total number of messages, including the n


termination messages, is
𝑛𝑛 + ∑𝑛𝑛−1 𝑖𝑖 + 1
= Θ(𝑛𝑛2)
𝑖𝑖=0
LEADER ELECTION IN RINGS
Analysis of 𝑶𝑶(𝒏𝒏𝟐𝟐)
Algorithm

 Total no. of messages is n+(n-1)+(n-2)+…..+ 1=


2
LEADER ELECTION IN RINGS
ii) An 𝑂𝑂(𝑛𝑛 log𝑛𝑛) Algorithm / HS Algorithm
Hirschberg and Sinclair (1980)
Each processor tries to probe successively larger neighborhoods
in both directions.
If probe reaches a node with a larger id, the probe stops.
If probe reaches end of its neighborhood, then a reply is sent back
to initiator.
If initiator gets back replies from both directions, then go to next
phase.
If processor receives a probe with its own id, it elects itself.
LEADER ELECTION IN RINGS
Phase 0: Send (id) to left and right
LEADER ELECTION IN RINGS
If : received id > current id
Then: send a reply(OK)
LEADER ELECTION IN RINGS
If: a node receives both replies
Then: it becomes a temporary leader and proceeds to the next phase
LEADER ELECTION IN RINGS
Phase 1: send(id) to left and right
LEADER ELECTION IN RINGS
If: received id >current id
Then: forward(id)
LEADER ELECTION IN RINGS
If: received id > current id
Then: send a reply(id)
LEADER ELECTION IN RINGS
If: a node receives a reply with another id
Then: forward it
If: a node receives both replies
Then: it becomes a temporary leader
LEADER ELECTION IN RINGS
Phase 2: send id
LEADER ELECTION IN RINGS
If: received id > current id
Then: send a reply
LEADER ELECTION IN RINGS
If: a node receives both replies
Then: it becomes temporary
leader
LEADER ELECTION IN RINGS
Phase 3: The node with id 8 will receive its own probe
message, and then becomes the leader!
LEADER ELECTION IN RINGS

In general:
LEADER ELECTION IN RINGS
Correctness: Similar to LCR algorithm.

Message Complexity:
 Each message belongs to a particular phase
and is initiated by a particular processor.
 Probe distance in phase i is 2𝑖𝑖.
 Number of messages initiated by a processor in phase i is at
most 4· 2𝑖𝑖 (probes and replies in both directions).
LEADER ELECTION IN RINGS
How many processors initiate probes in phase k ?
For k = 0, every proc. does
For k > 0, every proc. that is a "winner" in phase k-1
does
– "winner" means has largest id in its 2k-1
neighborhood

You might also like