430 likes | 520 Views
Leader in a clique in O(nlog(n)) messages. By Pierre A. Humblet and the help of Miki Asa. Rules of the game. Initialization : Each node has {1 . . N-1} edges + edge (–1) to itself. master(id) = id, state = active. Node attempt to capture other nodes (increase the size of their domain).
E N D
Leader in a clique in O(nlog(n)) messages By Pierre A. Humblet and the help of Miki Asa
Rules of the game • Initialization : • Each node has {1 . . N-1} edges + edge (–1) to itself. • master(id) = id, state = active. • Node attempt to capture other nodes (increase the size of their domain). • A node that has been captured stop capturing and it’s state = stopped. • Node can be captured by many other nodes but has only one master the last one that captured it. • Node that captured all the nodes leader
How it’s done 2 messages • TEST(size,id) • An attempt to capture other node. • A TEST from a node I didn’t captured forward to my master. • A TEST from a node I captured answer it with WINNER(id). • WINNER(id) • The result of the fight id won the fight. • Determined by who got bigger domain (if equal bigger id)
A B State = active Master = A State = active Master = B
B wins Waiting A B B wins AB : TEST(size(A),id(A)) BB : TEST(size(A),id(A)) BB : WINNER(B)
B wins A B State = active Master = A State = active Master = B B wins AB : TEST(size(A),id(A)) BB : TEST(size(A),id(A)) BB : WINNER(B)
A wins A B A wins AB : TEST(size(A),id(A)) BB : TEST(size(A),id(A)) BB : WINNER(A) BA : WINNER(A)
A wins A B State = active Master = A State = stop Master = A A wins AB : TEST(size(A),id(A)) BB : TEST(size(A),id(A)) BB : WINNER(A) BA : WINNER(A)
State = active Master = A A C B State = active Master = C State = stop Master = A
A wins A Waiting C B A wins CB : TEST(size(C),id(C)) BA : TEST(size(C),id(C)) AB : WINNER(A)
A wins State = active Master = A A C B State = stop Master = A State = active Master = C A wins CB : TEST(size(C),id(C)) BA : TEST(size(C),id(C)) AB : WINNER(A)
C wins A C B C wins CB : TEST(size(C),id(C)) BA : TEST(size(C),id(C)) AB : WINNER(C) BC : WINNER(C)
C wins State = stop Master = A A C B State = stop Master = C State = active Master = C C wins CB : TEST(size(C),id(C)) BA : TEST(size(C),id(C)) AB : WINNER(C) BC : WINNER(C)
Variables STATE : active – won all the fights stopped – lost a fight won’t be a leader SIZE : number of nodes I captured (send me winner (ID)) including me (size of my domain). MASTER : the id of my master. EDGE(id) : the edge leading to node id Dealing with messages PENDING_SET : messages waiting to be forward to the master PENDING : number of messages in PENDING_SET Initialization Each awaken node with ID receive winner(ID) on edge > 0.
Receiving WINNER(id) on edge e If (id = ID) // I won the fight { if (e > 0 & state = active) // fight that I initiated { SIZE ++ if SIZE = N // I capture all the nodes stop // announce elected else send TEST(SIZE,ID) on edge SIZE //continue } else // I didn’t initiate the fight do nothing }
Receiving WINNER(id) (cont.) Else // id != ID { if (MASTER != id) // my master lost { MASTER = id // change the master Send WINNER(id) on EDGE(id) // inform the new master } Else // my master won { do nothing } PENDING - - // continue as a slave if PENDING > 0 send from PENDING_SET on EDGE(MASTER) }
Receiving TEST(size,id) on edge e If e < SIZE // message from my domain { if (size,id) > (SIZE,ID) // I lost this fight { STATE = stopped // out of the game send WINNER(id) on EDGE e // inform the new master } else // I won send WINNER(ID) on EDGE e // inform my slave that I won } Else // not from my domain { EDGE(id) = e // update my data-base PENDING ++ // send TEST to the master If PENDING = 1 send TEST(size,id) on EDGE (MASTER) else put TEST(size,id) on EDGE (MASTER) on PENDING_SET }
state Size Master active 1 2 2 active 1 3 active 1 1 1 3 6 4 active 1 4 active 1 6 5 active 1 5
state Size Master active 1 2 2 active 2 3 stop 1 3 1 3 6 4 active 1 4 active 1 6 5 active 1 5
state Size Master active 1 2 2 active 2 4 active 2 3 stop 1 4 stop 1 3 1 1 3 4 Waiting 8 6 4 active 1 4 active 1 6 5 active 1 5
state Size Master active 1 2 2 active 2 3 stop 1 3 1 3 Waiting 6 4 active 1 4 active 1 6 5 active 1 5
state Size Master active 1 2 2 active 2 3 stop 1 3 1 3 Waiting 6 4 stop 1 6 active 2 6 5 active 1 5
state Size Master active 1 2 2 active 2 3 stop 1 3 1 3 Waiting Waiting 6 4 stop 1 6 active 2 6 5 active 1 5
state Size Master active 1 2 2 active 2 3 stop 1 3 1 3 Waiting Waiting 6 4 stop 1 6 active 2 6 5 active 1 5
state Size Master active 1 2 2 stop 2 3 stop 1 6 1 3 Waiting Waiting 6 4 stop 1 6 active 3 6 5 active 1 5
state Size Master active 1 2 2 stop 2 3 stop 1 6 1 3 Waiting Waiting 6 4 stop 1 6 active 3 6 5 active 1 5
state Size Master active 1 2 2 stop 2 3 stop 1 6 1 3 Waiting 6 4 stop 1 6 active 3 6 5 stop 1 3
state Size Master active 1 2 2 stop 2 3 stop 1 6 1 3 Waiting 6 4 stop 1 6 active 3 6 5 stop 1 3
state Size Master active 1 2 2 stop 2 6 stop 1 6 1 3 Waiting 6 4 stop 1 6 active 4 6 5 stop 1 3
state Size Master active 1 2 Waiting 2 stop 2 6 stop 1 6 1 3 Waiting 6 4 stop 1 6 active 4 6 5 stop 1 3
state Size Master active 1 2 Waiting 2 stop 2 6 stop 1 6 1 3 Waiting 6 4 stop 1 6 active 4 6 5 stop 1 3
state Size Master active 1 6 Waiting 2 stop 2 6 stop 1 6 1 3 Waiting 6 4 stop 1 6 active 5 6 5 stop 1 3
state Size Master active 1 6 Waiting 2 stop 2 6 stop 1 6 1 3 Waiting 6 4 stop 1 6 active 5 6 5 stop 1 3
state Size Master active 1 6 Waiting 2 stop 2 6 stop 1 6 1 3 Waiting 6 4 stop 1 6 active 6 6 5 stop 1 6
Termination • Each node can’t send more then N-1 TEST. • Each TEST cause at the most 3 more messages.
Correctness Lemma 1 At the end of the algorithm there will be a node with size = N. Proof • Size only grows with time. • For each TEST sent • size++ or • Node is “Waiting” • Therefore Enough to prove there is no deadlock
Liveness • Suppose there is a deadlock • Lets take the node with the biggest (size,ID) it’s active. • Eventually it will get an answer for the last test it initiated. Winner(ID). • Once he’ll get that answer size++ • Size = N leader • Send TEST for the next node
complexity My domain: the nodes from which I received winner(ID) while I was active + myself. Lemma 1 SIZE = size of my domain. If (id = ID) // I won the fight { if (e > 0 & state = active) // fight that I initiated { SIZE ++ • Got winner(ID) from other node & state = active all conditions fulfill size++ • Size++ all conditions fulfill Got winner(ID) from other node & state = active
Lemma 2 If SIZE(A) at time Ta = SIZE(B) at time Tb then the domains at Ta & Tb are disjoint. Proof: Assume domain(A) & domain(B) are joint at Ta or Tb They are joint at max(Ta,Tb) There is a Tm < max(Ta,Tb) s.t. At Tm a node from domain(A) join domain(B) After Tm domain(A) doesn’t grow At Tm Size(A) >= SIZE Tm > Ta After Tm domain(B) > domain(A) >= SIZE Tm > Tb Contradiction
Lemma 3 Rank the nodes in decreasing order by their SIZE at termination. S1, S2, S3, . . . , Sn-1, Sn Then Sk <= N/k Proof S1 – Sk-1 had once the size of Sk there all disjoint (lemma 2) conclusion There is only one node with SIZE=N one leader
Number of TEST sent If (id = ID) // I won the fight { if (e > 0 & state = active) // fight that I initiated { SIZE ++ if SIZE = N // I capture all the nodes stop // announce elected else send TEST(SIZE,ID) on EDGE(SIZE) //continue } For each TEST sent SIZE++ Each TEST cause at the most 3 more messages
Total number of messages is no more then 4*(N/1 + N/2 + N/3 + . . . + N/K) = O(N log(K)) K = the number of nodes awake.
Improvements • The bound for the number of messages can be tightened by noticing that the first TEST received by a generates at the most one more message. • The algorithm can stop as soon as SIZE = N/2 (then announce elected).