Part I Fundamental Algorithms
5.2 Breadth-First-Search Algorithms
5.2.1 Synchronous BFS Construction
Our first distributed algorithm called Synch_BFS is the synchronous distributed ver-sion of Dijkstra’s algorithm for the single-source shortest path problem. It has a single initiator, and in each synchronous round, a partial BFS tree is formed. The already formed branches of the tree are used to carry the synchronization messages and the leaves search for the new nodes to be added. The depth of the tree is incre-mented in each round until all nodes are covered.
As an introductory example, we will explicitly show all messages that are needed for the synchronous operation of this algorithm. Providing synchronization using special control messages as such eliminates the need for other synchronization methods like using a synchronizer. Also, full termination detection is provided so that all of the nodes know when the BFS tree is constructed and can be used. The following are the types of messages used in this algorithm:
• round(k): Sent by the root at the beginning of roundpand is transferred by all of the member nodes of the partial BFS tree to their children.
5.2 Breadth-First-Search Algorithms 55 Algorithm 5.1 Synch_BFS
1: int parent←⊥,k←1,i, j
2: set of int childs, others, phased, finished←∅ 3: message types round, probe, ack, reject, upcast 4: boolean leaf _flag←false
12: finish: finished←finished∪ {j}
13: if finished=childs then
14: send terminate to childs
15: exit root terminates
16: if phased=childs then start next round
17: k←k+1
18: send round(k)to childs, phased←0 19: end while
26: round(k): if state=interm then intermediate node
27: send round(k)to childs
28: else leaf node
29: send probe(k)toΓ (i)\ {parent}
30: round_recvd←true
31: probe(k): if parent=⊥then non-member node
32: parent←j; state←new_leaf
33: send ack(k)to j
34: else a member node
35: send reject(k)toj
36: ack(k): childs←childs∪ {j}
37: if state=interm then interm_flag←true
38: reject(k): others←others∪ {j}
39: if others=Γ (i)\ {parent}then a leaf node finishes
40: send finish to parent
41: upcast(k): phased←phased∪ {j}
42: finish: finished←finished∪ {j}
43: if (finished=childs)∧(state=interm)then
44: send finish to parent
45: terminate: if childs=0 then
46: send terminate to childs
47: exit all nodes terminate
56 5 Graph Traversals 48: if round_recvd then
49: if ((state=leaf)∧((childs∪others)=(Γ (i)\ {parent}))∨((state= interm)∧(childs=phased∪finished))then check end of round
50: send upcast(p) to parent
51: if state=new_leaf then update states
52: state←leaf
53: else if interm_flag then
54: state←interm
55: end if
56: phased, others←∅; round_recvd←false; round_over←true
57: end if
58: end if
59: end while 60: end while 61: end if
• probe(k): Sent by leaves of the partial BFS tree to unsearched neighbors.
• ack(k)/reject(k): Sent by the searched node to accept/reject being a child of the sending leaf node.
• upcast(k): Sent first by the leaf nodes of the partial BFS tree and then by the intermediate nodes to their parents, to signal the end of neighbor search and also the end of the round.
• finish: Sent by the leaf nodes to their parents to signal that their part is over as either they have no neighbors other than parent or they do not have any children.
• terminate: Broadcast by the root to all nodes to signal that the construction of the BFS tree is completed.
The root starts the algorithm by sending the first round message to its neighbors, which in turn respond by ack messages as shown in Algorithm5.1. This message is transferred over the partial BFS tree to the leaf nodes, which in turn search nodes for the next level of the BFS tree by the probe messages. In order to detect the end of each round, each leaf node that has received ack or reject messages from all of its neighbors except the parent returns the upcast message to its parent, which in turn convergecasts upcast message to its parent. When all upcast messages from the neighbors of the root are received, root starts the next round by issuing the next round message.
The depth of the BFS tree formed would beO(d), and the root could terminate all nodes after d rounds. However, this would require that the diameter of the net-work be known by the root beforehand. Closer inspection shows that when all the neighbors of a node i except its parent have responded by reject messages or when a leaf node does not have any neighbors other than its parent, part of i is over. Al-gorithm5.1uses this observation, and when the set of unrelated neighbors of a leaf node is equal to the set of all of its neighbors or if it does not have any neighbors other than the parent, it sends the finish message to its parent which is convergecast to root. When root receives the finish messages from all of its children, it further
5.2 Breadth-First-Search Algorithms 57
Fig. 5.2 Synch_BFS execution example
broadcasts a terminate message over the formed BFS tree to inform every node that the process is over.
5.2.1.1 Example
An example network in Fig.5.2shows six nodes numbered 1, . . . ,6, where node 6 initiates the algorithm by the probe(1)message. Each message is labeled by the round number it has transmitted. Each node that receives probe for the first time responds by the ack message and marks the sender as its parent. After two rounds, the BFS tree is formed as shown by bold arrows.
5.2.1.2 Analysis
Lemma 5.1 Algorithm Synch_BFS correctly constructs a BFS tree.
Proof We show this by induction. Assuming that, at stepk, a BFS treeTkrooted at ris formed, at step k+1, only the leaves of the tree will be active in sending the probe message to their neighbors that are one hop away. Therefore any added nodes
will bek+1 hops away fromr, formingTk.
Theorem 5.1 The time complexity of Algorithm5.1, Time(Synch_BFS), isO(d2).
Its message complexity Msg(Synch_BFS)isO(nd+m)wheredis the diameter and mis the number of edges of graphG.
Proof Broadcasting of the message round(k)to currentTk takeskunits and con-vergecast of message upcast similarly is performed in k units, where k is the depth
58 5 Graph Traversals ofTk. There is also additional time for the probe and ack/reject messages at each step totalling 2k+2 time for each step. Therefore, assuming that the final BFS tree will be at most of depthd, For the message complexity, we need to consider the synchronization messages and tree forming messages separately. In roundp, if the tree formed hask nodes, there will bek−1 round messages andk−1 upcast messages for a total of 2k−1 messages providing a maximum number of synchronization messages in a round as O(n). Since the formed BFS tree will have diameterd as the depth, the message complexity for the synchronization process isO(nd). In each round, new edges of the BFS tree will be determined by the probe and ack/nack messages, and therefore, the total traversals for discovery of these edges will be at most 2m. Summation of the synchronization and discovery messages yields:
The second algorithm to build a BFS tree of a graphGis the distributed version of the Bellman–Ford algorithm called Update_BFS. We have a single initiator as before, which starts the algorithm by sending the layer(l)message that contains its distance to its neighbors as unity. Any node receiving a layer(1)message compares the layer valuelcontained in the message with its known distance to the root, and if the new value is smaller, the sender of the layer message is labeled as the new parent, and the distance is updated tol. Since the new distance to the root will affect all neighbors and other nodes, the layer(l+1)message containing the new distance is sent to all neighbors except the new parent as shown in Algorithm5.2. It can be seen that this process eventually builds a BFS tree starting from the root. The termination condition would be the traversing of the longest shortest path between any two nodes, which would be the diameter of the graphG.
5.2.2.1 Example
An example is shown in Fig.5.3with six nodes numbered 1, . . . ,6, where the layer message carries the distance, and the time frame it is delivered as layer(distance,