A Leader Election Algorithm

Source: Internet
Author: User

[Java] According to Matrix67's article "prisoner and Lightbulb": According to Matrix67's article "prisoner and Lightbulb: [java] public class Wakeup {static class People {// records the position 0 that was determined when the first entry was made.-1 left 1 right 1. public int Position; // the ball on the hand public int BallNum; // identifies the public int Id; // whether the ball is still in the seesaw game public boolean OnLine; public People (int Id) {this. id = Id; Position = 0; BallNum = 2; OnLine = true;} private static int lelecount = 100; // Number of participants private static boolean BoxIsEmpt Y = true; // whether the box is empty private static boolean LeftIsWeight = false; // the low public static void main (String [] args) on the left of the seesaw) {People [] thePeoples = new People [100]; for (int I = 0; I <PeopleCount; I ++) {thePeoples [I] = new People (I );} boolean finished = false; int k = 0; int I = 0; int outNum = 0; while (! Finished) {k ++; I = (new Random (). nextInt (PeopleCount) % PeopleCount; People theP = thePeoples [I]; if (theP. onLine) {if (theP. position = 0) {theP. position = LeftIsWeight? -1: 1; LeftIsWeight =! LeftIsWeight;} else {if (theP. Position =-1) {if (LeftIsWeight) {// The left-side low. if there is a ball, take if (! BoxIsEmpty) {BoxIsEmpty = true; theP. ballNum ++ ;}} else {// height of the current side. if it is null, place the ball if (BoxIsEmpty) {if (theP. ballNum> 0) {theP. ballNum --; BoxIsEmpty = false ;}}} else {if (LeftIsWeight) {// The height of the current side. if it is empty, put the ball if (BoxIsEmpty) {if (theP. ballNum> 0) {theP. ballNum --; BoxIsEmpty = false ;}} else {// The current side is low. if (! BoxIsEmpty) {BoxIsEmpty = true; theP. ballNum ++ ;}}}// exit if no ball exists and if (theP) is not used. ballNum <= 0) {LeftIsWeight =! LeftIsWeight; theP. onLine = false; outNum ++; System. out. println ("Out:" + theP. id);} // If 200 or 199 balls are collected and the other ball is in the box, it indicates victory. if (theP. ballNum + (BoxIsEmpty? 0: 1)> = PeopleCount * 2) {System. out. println ("Winner:" + theP. id + ", executed in total:" + k); finished = true ;}}}}} public class Wakeup {static class People {// records the position 0 determined when the first entry is made.-1 left and 1 right. public int Position; // the ball on the hand public int BallNum; // identifies the public int Id; // whether the ball is still in the seesaw game public boolean OnLine; public People (int Id) {this. id = Id; Position = 0; BallNum = 2; OnLine = true ;}} private static int lelecount = 100; // participant Private static boolean BoxIsEmpty = true; // whether the box is empty private static boolean LeftIsWeight = false; // The left-side low public static void main (String [] args) {People [] thePeoples = new People [100]; for (int I = 0; I <PeopleCount; I ++) {thePeoples [I] = new People (I );} boolean finished = false; int k = 0; int I = 0; int outNum = 0; while (! Finished) {k ++; I = (new Random (). nextInt (PeopleCount) % PeopleCount; People theP = thePeoples [I]; if (theP. onLine) {if (theP. position = 0) {theP. position = LeftIsWeight? -1: 1; LeftIsWeight =! LeftIsWeight;} else {if (theP. Position =-1) {if (LeftIsWeight) {// The left-side low. if there is a ball, take if (! BoxIsEmpty) {BoxIsEmpty = true; theP. ballNum ++ ;}} else {// height of the current side. if it is null, place the ball if (BoxIsEmpty) {if (theP. ballNum> 0) {theP. ballNum --; BoxIsEmpty = false ;}}} else {if (LeftIsWeight) {// The height of the current side. if it is empty, put the ball if (BoxIsEmpty) {if (theP. ballNum> 0) {theP. ballNum --; BoxIsEmpty = false ;}} else {// The current side is low. if (! BoxIsEmpty) {BoxIsEmpty = true; theP. ballNum ++ ;}}}// exit if no ball exists and if (theP) is not used. ballNum <= 0) {LeftIsWeight =! LeftIsWeight; theP. onLine = false; outNum ++; System. out. println ("Out:" + theP. id);} // If 200 or 199 balls are collected and the other ball is in the box, it indicates victory. if (theP. ballNum + (BoxIsEmpty? 0: 1)> = PeopleCount * 2) {System. out. println ("Winner:" + theP. id + ", executed in total:" + k); finished = true ;}}}ps: This algorithm provides a good idea, however, it is generally not used in the actual election strategy, because the conditions are not as harsh as described in the article.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.