Transcript

Use with ChatGPT / Claude Auto-scroll A

1. Chapter 1 (0:00)

0:00 - 0:18
The following content is provided under a Creative Commons license.Your support will help MIT OpenCourseware continue to offer high-quality educational resources for free.To make a donation or view additional materials from hundreds of MIT courses, visit MIT OpenCourseware at ocw.mit.edu.
All right, let's get started.Uh today's topic would be distributed algorithms.Uh we we will look at two new algorithms, but they are similar to what you have seen in the lectures.So, it will also be a review of uh some concepts in lectures.
So, our first example would be again, leader election, the simple the simplest example.

2. Chapter 2 (1:05)

But this time, uh the network and topology would be a ring.
So, in the in the lectures, uh the example we see is a click, meaning they are fully connected, right?Every node can talk to every other node.Uh there, if you remember, uh our solution is to for everyone to generate a UID or random number, and if you are the maximum, then you output you are the leader.Uh it's you can do that because yeah, you are connected to everybody, so you immediately know what random number everybody generates, and you can compare whether you are the largest.Now, uh the idea is the same if we have a ring.So, you want everyone to generate a ID or random number.Uh I'll just say ID uh from now on.And you want to collect everyone else's ID.So, that you know whether your ID is the largest among uh all of them.Okay?
Like comparing the numbers?Pardon?Where is the comparison happening?Uh okay, the question is where the comparison happen?Um so, we first need a some way to pass the numbers around such that everyone has everyone else's number.Right?You have the number of everyone, then you can compare whether the largest equals yours.Okay?If the largest equals yours, then you know you are the largest, and you're going to output uh I'm the leader.Okay?So, the difficulty is just how to pass the numbers around so that everyone sees everyone else's number.Any ideas?Simple solution?

3. Chapter 3 (3:40)

Well, there's not much you can do.You are just connect to your two neighbors, the left one and right one.
So, how do you propagate the information?
Go ahead.You can take uh the maximum of your ID and his ID.Yeah.And then broadcast that.Okay, so what does broadcast mean here?Like tell your neighbors what the largest of your value and your neighbor's IDs is.Okay.And yes, uh uh let me call them A B CE F.Then yes, C can tell D or can tell B, but how does C tell E?

4. Chapter 4 (4:41)

If later rounds, you would just I guess propagate the maximum.Yep.So, uh it would be just uh say everyone talks to its right neighbor.And then B has A's ID, C has B's ID, D has C's ID, and then continue, right?pass around next time.So, uh just to make it perfectly clear, uh say they generate some random IDs, that's 5 10 20.And then in the next round, A would send its ID to B, B would send its ID to C, C would send its ID to D, and in the next round, B would pass this information along to C,and C would pass the information to D, and just continue, and uh eventually everyone will have everyone else's ID.So, how many rounds do we need?
If there are n nodes
in the system?Are they going to be in just one direction of two neighbors?Um I think you can do it either way.It will be if you propagate both ways, you probably 2x faster, yeah.Yeah, correct.It's just all of them.Right?But to keep it simple, let's say we just propagate in one direction, right?That's also fine.Still all of them.So, how many messages are sent in total?

5. Chapter 5 (6:28)

Pardon?n squared?Yep.Is that obvious to everyone?Can you give an explanation?Uh yeah, I think so.Everyone sends its message to its like neighbor device.Yeah.Yeah.Everyone everyone every round everyone sends a message.Or you can think of every message message is propagated n times, and there are n messages in total.Okay.Yep, that's definitely your solution.Uh well, you can imagine it's probably the naive solution, and can we do better than that?
In this case, uh are we assuming they know that there are n of them?Like they know they're n.Okay.Uh good question.So, if we if they know they are n of them, yep, then they know how to terminate.So, your question is how to terminate, right?Uh if they don't, eventually you will receive your own ID on the other side.If we keep sending left, eventually you will receive it on the left port.And yeah, that's uh that's an indication of termination.
Good question.
Yeah, we usually assume that, yeah.Uh either it's a unique user ID sorry, UID.What does U stand for, by the way?Or uh if you generate random numbers in a large range, it's very unlikely that they collide.

6. Chapter 6 (8:20)

Okay?Uh so, can we do better than that?
Have some idea?Um once something like some sort of binary search thing where you only pass it to the one that's bigger.Mhm.Okay, I think you have two ideas, and uh binary search, we'll see how that works later.So, you said uh only forward something that's larger?Um yeah, like only forward to the larger.Mhm.Okay, yeah, that's on the right track.So, uh one obvious thing we can do is that so, we actually don't care what all the IDs are, right?We only care whether a certain ID is the largest.So, for example, in this case, when A sent its ID 5 to B, B knows this 5 won't be the leader.Right?Cuz it's 5 is too small.It's even smaller than his ID.So, B can choose to drop this message.Like there's no point in passing that message further.Same thing for this message message.
C knows that 10 is too small, and it doesn't have to pass it along.

7. Chapter 7 (9:58)

Uh the ID?Uh it's just an integer each node chooses at random, say.Yeah, the only purpose of the ID is to break uh symmetry.So, yeah.Like we have seen in the lecture, if they don't do this, if they don't have any unique identifier, they won't be able to select the leader.And when they have a unique number, then they can select, yeah, the largest one or smallest one or some in in some way.
Okay, does this optimization make sense?We can cut drop several messages that uh have no chance of becoming the leader.What is the upper bound of it?Is it still n squared?Correct.Yeah, that's uh uh yeah, very good question and very good answer, by the way.Uh but how effective is this?Well, in average case, you may we may be able to drop some messages, but there are pathological case where it actually doesn't help at all.Say, this is the ID we choose, then when this 20 is sent around, B cannot drop it, right?Because it it's probably it may be the largest.And when this 10 is sent to C, C cannot drop it.And when 20 comes along, it also cannot drop it.Right, so no node can drop any message.So, it's worst case is still n squared.
Okay, so let's think about the binary search equivalent.Uh yeah, or how to binary search in this case.

8. Chapter 8 (12:07)

So, binary search into what?Uh yeah, good question.It's not very obvi- obvious uh binary searching to what.Um yeah, just some binary idea.Okay.That that will give us a Oh.n log n bound.So, actually that's the this is the better algorithm, but once you have a log n, you can probably guess.Okay, go ahead.How about like kind of merging, like instead of finding the global maximum, finding like local maximum, then continuing?Mhm.Yeah, that's on the right track.Yeah.Um Faster is also n.Yeah.That's definitely the uh right idea.Yeah, let's detail it a uh little bit.How do you carry it out?
Mhm.Um divide the set um two parts, like two parts.Yeah.Okay.Um divide the computer And okay, so then it's let them select their leader Mhm.respectively and compare which one is larger.Mhm, it's interesting thought.
Like First, then the AB finds the maximum between these two.CD finds the maximum.EF maximum.Kind of merging, considering them as a one node.Uh I think the first difficulty I see is that if you cut it by half, it's no longer a ring.Right?
Also, they can't cut themselves in half.They can't ask for Yeah, yeah, correct.Yeah.Yeah, yeah.

9. Chapter 9 (14:44)

Yeah, it's definitely not an easy problem.Another another easy algorithm.
And the idea is to uh well, you had the right idea that we want to uh what's the word?Let the weak candidates shut up early.So, what I mean by that is say, we have several rounds, so this B and A and C, we will let B only propagate to A and C first.If B is the local maximum among ABC, then B will try to talk further, increase its range.If B is not the local maximum of ABC, then yeah, it can be quiet.It doesn't need to send messages anymore.Right, if B succeeds in the next round, then it will further increase its range to try to talk to more people.
Okay.So, how does it work in detail?Uh well, so in round I, uh we will let a node send its message up to 2 raised to I uh hops.Right, in this case it's one and in next round it's two and then four.
If at any point some node, like along this range, decides that you are not the local maximum, then they will reply that, yeah, you can you you you no longer need to send messages anymore.If this message successfully reaches uh this end point that 2 raised to I hops, then this guy will respond and this guy, if it still thinks you are the local maximum, then it will respond a message uh well,saying, yeah, continue.

10. Chapter 10 (17:03)

Okay.And if the sender receives this continue message on both sides, then it will continue into the next round.Otherwise, it will go inactive.
Is the algorithm clear?
Um
And how do we choose the next maximum?Like next the node which has to send the message?Uh say that again.Like after uh receiving like um don't send again, like we have to choose one node which has to send.Oh, okay.In the first round, everyone sent their messages.Okay.Then some of them will go inactive because they learn they are not maximum.And the remaining the surviving surviving ones will, yeah, continue sending messages.And then, yeah, half of them probably will die in the next round.And the surviving ones keep sending messages.
Make sense?

11. Chapter 11 (18:19)

How How many send messages to like if you only communicate with neighbor, right?Not the node to I.Mhm.Mhm.Yeah.So, we will send a message of this form, uh say, well, some message.
And then we will send the hop uh and the direction, either left or right.And this hop will initially be set to 2 raised to I, the number of rounds.Then when this guy receives the message, it will decrement the hop count and pass it along.Right?And every node, when forwarding the message, will decrease the a that h by one.And finally, when it reaches here, that number becomes zero.And when you see a when it when a node sees a message with zero hop count, it going it's going to reverse it, send it in the other direction, and again uh set it back to 2 raised toI.
Uh this message, I should say, uh ID.
And at at certain point, mhm, set a certain node may decide this ID is too small, it doesn't have a chance, then yeah, it can directly uh send it in the opposite direction replying a message saying, "Yeah, youare too small."
Okay?So, uh any more questions on the algorithm itself?If not, what's the next step?

12. Chapter 12 (20:17)

Yeah, time complexity.Oh, I already claimed it's n log n.Is it?
Uh This is round.This is message.This is also a message complexity.
So, why is it n log n?

13. Chapter 13 (21:14)

Is it log n by the way?Like Mhm.So, we have uh a certain number of rounds.So, how many rounds do we have?
Yeah, maybe.But, yeah, I'll just say log n.
Because yeah, you are increasing your uh hop length.And we're going to compute like how many nodes are still active in each round.And how many messages are sent in that round.So, uh well, the number of nodes active just will just be this number if we start from zero.Why?Cuz the first time everyone is active.And next time only 1/3 of them will be active, but we said uh we are conservative here and we put 1/2.Right, next round is actually 1/5.Cuz the local maximum, if it's local maximum, it means like these two and those two will go inactive.But, we put this as a uh upper bound.So, this is the number of nodes that are still active.And they will send the message up to

14. Chapter 14 (22:51)

this many hops.
Right?And there are two directions.
And you send the message back and then uh yeah, sorry, send message forward and someone will reply.But, in the end this is n log n.

15. Chapter 15 (23:24)

Yeah, I think I got eight n log n.So, my recitation notes says it's four log n.I'm not entirely sure what the what's going on.Yeah, but you can double check whether this is correct or the recitation note is correct.
What is I again?I is the uh num- number of rounds in the ith round.Yeah, this many nodes are still active and each of them will send uh message this many hops.
And I didn't mention whether this is whether the net- network is synchronous or asynchronous.And it turns out it doesn't care.This algorithm can work for both synchronous and asynchronous.Apparently, it works for synchronous networks.If it's asynchronous, then the uh what changes is that different nodes are in different rounds, right?A certain round may a certain node may be far ahead than the others, but it's fine.Eventually, they will converge to the correct result.Okay?So, let's look at the second problem.

16. Chapter 16 (25:07)

Well, problem is defined even simpler.Uh we just want to count how many nodes are out there.And we want the algorithm to uh work both synchronously and asynchronously.Uh By that, I just mean we have a network.
Well, say you have a lot of nodes.After that, just want to count how many nodes are there in this network.
So, I'll give you say 1 minute to let's first come up with a high-level plan.

17. Chapter 17 (25:56)

Now, is the problem clear?In the worst case or worst case?Mhm.The worst case.Um okay, I haven't defined that.Let's not worry about uh complexity now for now.The complexity will depend on number of nodes n and the number of edges e.Yeah.
Let's just get it functionally correct.
Can anyone share a high-level strategy?

18. Chapter 18 (28:01)

Go ahead.Um so, each node store the like um IDs of the other places it has received.So, basically like it's going to send propagate these sets of the uh nodes its nodes.Mhm.Mhm.Uh um what edge?On this one.Yeah, it will send in both on both edges.Mhm.It will force Mhm.Okay, so then maybe it send the one here and send the one here there.Then this node will think it has a children uh it has a child, right?Which is this one.Let's just say this is this is the entire network.And what message does this does it send to this guy?The first step.Mhm.
Like you probably need send two here because you have one and this is possibly its child, right?But, then we're double counting this node.You see the problem?
Do nodes have their IDs?Oh, yeah, they they have their IDs.We can send the IDs instead of the Oh.Okay.We're going to send all the IDs.Yeah, neighbors.

19. Chapter 19 (29:34)

Mhm.
Mhm.Okay, yeah, that's interesting thought and then you still you may still need a root.And that one have will have everyone's ID.And then see how many unique guys are there.Okay, interesting.Does this algorithm work?Yeah, I don't see any problem.
Okay, let me still uh repeat what our algorithm is.It's cuz it's closer to what's in the lecture.So, we're going to uh find a spanning treeof this network.I I a spanning tree means well, like I have to cut one of these edges.
If I have a tree, that then I can have every child report to its parent.Like how many offsprings, including myself, do I have?Right.And this this node will sum up uh all its all its children and report to its parent.

20. Chapter 20 (31:18)

Does everyone get that?So, first we'll find a spanning tree.
And second, uh we'll have child reports to parent.
How can How can you find a spanning tree?Good question.So, in the lecture we have seen algorithm that finds BFS spanning tree.

21. Chapter 21 (32:09)

For synchronous networks.
Okay, this is review of the lecture.
How does it work?

22. Chapter 22 (32:31)

Mhm.
Each node will say we we we need to first to choose a root.And our root will just send a message to its neighbor.And yeah, saying you are my child, you're my child you are my child, you are my child.And then every node upon receiving uh this message from the mess from the parent will search among its neighbors.Right.So, the neighbors that haven't got a parent will acknowledge this sender as the parent.
Okay, that's a little messy.

23. Chapter 23 (33:27)

So, this node will search to uh the leaf node.But then it will also try to search for this guy.But this guy already has a parent, then yeah, it will say I already I already got a parent and blah blah blah.This will give us the BFS spanning tree.What does it mean?It's a spanning tree found by BFS.Does it work for asynchronous network?
Go ahead.This version does, but there's a different version that uses uh edge relaxation.Yeah, correct.And so, why does this version not work work for asynchronous network?Because uh different nodes can be on a different round number, so that uh the could end up going to the node and Yeah, exactly.So, let me give a concrete example.Uh what does asynchronous network mean is that well, messages travel at different speed.Say this link for some reason is temporarily down.Right.And this node doesn't receive the message from the root.And then this message travels very fast.And this message also travels very fast.So, this message may reach uh this node earlier.And then this uh this node will think of it as a child of this node.Okay.And then this message comes along finally, but this node says, "Yeah, I already have a parent and I'm going to reject you."
So, it still makes a spanning tree.Yes.Good point.This is not a BFS spanning tree, but it's it's still a spanning tree.Right?So, in our problem, we're totally fine with it.
Yeah, but you do have to know uh if you really want a BFS spanning tree in a asynchronous network, then you have to like record the distance and do the relaxation and so on and so forth.Okay.So, we'll just use this algorithm, the BFS spanning tree algorithm, uh just run it asynchronously.It doesn't find a BFS spanning tree, but it finds some finds a some spanning tree.

24. Chapter 24 (35:47)

Okay.How does this algorithm work?Uh
we will have several variables.The first one is parent.We will initialize it to this undefined symbol.
Okay.Then every node will pass around this search message.Mhm, I'll use the slightly well, shorthand notation than from the lecture.

25. Chapter 25 (36:32)

Okay, let's say uh the code I wrote is for process U.
Okay.If we receive a message a search message from V uh to U, that means U uh sorry, V is trying to become a parent.Okay.
And if I do not have a parent yet, I should set the parentto V.

26. Chapter 26 (37:25)

Okay, so what's the next step?
Now, we got the search message from our parent.And we have to pass it along.
Should we send back to the like I'm your child to the parent?Oh, yeah.Great.UhYeah, we first need to respond by saying Okay, I'll use the well, shorter notation.Send V is a Q that that's the message uh that will be sent to V at some point.Well, the message we sent is parent one.Parent true.This is a uh response to V saying uh yeah, you are my parent.

27. Chapter 27 (38:34)

Okay, then else we also have to re- respond
by saying parent zero.You are not my parent cuz I already got some other parent.Okay, I'm missing a step here.Um
If we receive a search message, go ahead.We send messages to all the other nodes.Yeah, we need to pass it to all my potential children.So, I'll use this gamma U mean meaning the neighbors of U.Right.And then I'll send them a message.
What message?Search.

28. Chapter 28 (39:47)

Okay.
Okay.So, uh Well, naturally since we created a we have a search message and we know how to deal with it.Now, we are sending this parent message with a better deal with it, right?So, then the next chunk of code should beif we receive this parent message I'll say parent B, meaning this message come comes with the either true or false.Oh.I received this message from some node W.All right.Okay.I'm still you here.Cuz I just send all the message to to all the Ws, to all my neighbors, and they should give me a response.
Okay.If B is one, that means this particular W uh take me as its parent.Make sense?Okay.So, I'd better have a list of my children.I want to track that.I want to keep track of that.How?We will create a new variable called children.It's going to be initialized to what?Yeah, empty set.

29. Chapter 29 (41:37)

And now, if this B is one, then I'm going to put W in this children list.
Okay.Uh I'm leaving some space here.
Because our root it will should be slightly different.So, every other node will send search messages when it receive the search message.We need someone to initiate.Make sense?So, if U equals root, uh we say the root is V0.

30. Chapter 30 (42:26)

So, two things should happen.First, we should set its parent to some special value.Like, just say root.
And then, uh I should copy this block of blob of code to here.
Okay.This is a little crowded, but um hope it's still eligible.

31. Chapter 31 (43:14)

Okay.This is already a almost the correct algorithm.Except that we don't know how to terminate.Right?If we wait long enough, then everyone will receive all the responses and everyone will know um its parent and its child list.But, how do we terminate?After Mhm.Mhm.After N rounds.Yeah, that's a that's one way to do it, after N rounds, but the whole point is we are trying to find what N is.We don't know how many nodes are there.Um if we receive rejects for all if we send all the searches Mhm.Say again.If all for all the search um if we receive a reject Oh, yeah.If you re- receive a response either either parent zero or parent one from all your neighbors, then your job is pretty much done.Right?But, uh the others have not.Right?So, you send the message very fast, they responded to you, and they are still working very hard.
Okay.So, then we need to use the technique from the lecture that's called converge cast.So, everyone will send a done signal when it is done and all its children are also done.
Okay.Uh to do that, I'm going to define a new variable called searched.Searched means uh someone has responded.Okay.In this case, uh W has responded, I'll put it into the searched list.No matter whether it accepts me as parent or reject.Okay.Then, naturally, I need to define this variable here.

32. Chapter 32 (45:25)

As a last step,if
this searched listequals my neighbor list, that means everyone has responded,
and all my children are done,

33. Chapter 33 (45:51)

then I need a new variable called done.That's another list tracking
who has finished and who has not.
Okay.Say again.What does the first one mean?This one?Mhm.Searched means uh someone has responded to the search message.Okay.Done means all its children are done.Okay, it's uh I I haven't write uh what how done is defined.Okay, give me a minute.In this case, I'm going to send my parent a message.This is the step of converge cast.What do I send?Uh I'm going to send I'm done.

34. Chapter 34 (46:48)

Okay.Then, whenever we create a message, well, we should deal with that message.If we I if we receive a message, I'm done, what do we do?Okay, from from W, then we're going to mark that node as done.
Okay, this is uh there's a subtle point.So, someone has to initiate the done signal.That's going to be our leaves.Why?Because when this condition check, they don't have any children.Their children list is empty set.And their done list is also empty set.So, they're going to send the I'm done signal first.And then, all the intermediate nodes will send the done signal when all its children are done.Right?So, this is the converge cast version.Only gives us uh termination point of the of our spanning tree search.Right?We haven't count the number of nodes in the network.
Uh that's a small modification.Uh we're just going to include that number in the I'm done signal.So, then I'm I need to define another variable called total that's initialized to zero.This variable will track how many nodes do I have in my subtree, including me and all my children.Then, when I send the I'm done signal, I'm going I'm going to send this Uh sorry.That's not right.Wait.When I'm sending the I'm done signal, I'm going to send my total number of offspring with it.Right?And when I receive someone's my child my one of my children reports that I do have T children,I need to increment my total by that amount.

35. Chapter 35 (49:20)

Mhm.Okay, I made a mistake again.Should be total plus one.Right?Cuz I'm counting all my children, and then I should include myself.
So, that's the complete algorithm?
Yeah, one purpose is just to create a different angle to look at distributed algorithm.We usually just draw that network graphs, but sometimes it's helpful to think about how the code actually works.
Okay.
Yeah, that's all for today.