Professional Documents
Culture Documents
2.1 Initialise Nodes: GHS Implementation
2.1 Initialise Nodes: GHS Implementation
1 Overview
GHS algorithm is used to find MST of a graph in a distributed setting. This means that each node is aware
of only it’s immediate neighbour. The code is written in JAVA in which each node is simulated using a single
thread. For emulating message passing, each thread can access a common message buffer which is accessible
to all the nodes. This is implemented by a list of BlockQueue which is thread safe.
2 Code Snippets
2.1 Initialise nodes
1 public Node ( int nodeId , HashMap < Integer , Integer > neighboursWtMap , ArrayList < BlockingQueue <
Message > > messageQueue , BlockingQueue < Boolean > algoStops ) throws I n t e r r u p t e d E x c e p t i o n {
2
3 this . nodeId = nodeId ;
4 this . ne ig h bo ur sW t Ma p = n ei g hb ou rs W tM ap ;
5 this . allNeighbours = ne i gh bo ur s Wt Ma p . keySet () ;
6 this . messageQueue = messageQueue ;
7 this . algoStops = algoStops ;
8 this . t o t a l M e s s a g e s S e n t = 0;
9 for ( Integer i : allNeighbours ) {
10 n e i g h b o u r s S t a t u s M a p . put (i , BASIC ) ;
11 }
12 int minWtNeigh = g e t M i n W t N e i g h b o u r () ;
13 n e i g h b o u r s S t a t u s M a p . put ( minWtNeigh , BRANCH ) ;
14 level = 0;
15 state = FOUND ;
16 rec = 0;
17 name = this . nodeId ;
18 parent = -1;
19 bestNode = -1;
20 bestWt = Integer . MAX_VALUE ;
21 testNode = -1;
22
23 Message tmp = new Message () ;
24 tmp . se tMessag eType ( " connect " ) ;
25 tmp . se tS e nd er N od eI d ( nodeId ) ;
26 ArrayList < Integer > a = new ArrayList < >() ;
27 a . add (0) ;
28 tmp . s e t M e s s a g e P a r a m s ( a ) ;
29 to ta l M e s s a g e s S e n t += 1;
30 messageQueue . get ( minWtNeigh ) . put ( tmp ) ;
31
32 }
This is just initialization of each node. Each nodes initialize it’s map to store the weights of it’s neighbours,
map to store the status of all it’s neighbours, sets the status minimum weight neighbour to B̈RANCHänd sends
a connect message to it.
1
22 messageQueue . get ( nodeId ) . put ( message ) ;
23 }
24
25 else {
26 Message tmp = new Message () ;
27 tmp . s etMessag eType ( " i n i t i a t e " ) ;
28 tmp . se t Se nd er N od eI d ( nodeId ) ;
29 ArrayList < Integer > a = new ArrayList < >() ;
30 a . add ( level +1) ;
31 a . add ( ne i gh bo ur s Wt Ma p . get ( q ) ) ;
32 a . add ( FIND ) ;
33 tmp . s e t M e s s a g e P a r a m s ( a ) ;
34 t o t a l M e s s a g e s S e n t += 1;
35 messageQueue . get ( q ) . put ( tmp ) ;
36 }
37 }
The connect message is handled as follows. If the received level is less than the node’s level, this means that
the other fragment wants to merge. In that case we send it the initiate message. Otherwise if the status of the
node from which it sends if basic, we wait for sometime. Otherwise we combine with rule equal to and send
an initiate message with level incremented and name changed.
If a node receives an initiate message, it updates it’s fragment information and reports other node in it’s
fragment about the new data.
2
23 ArrayList < Integer > a = new ArrayList < >() ;
24 t o t a l M e s s a g e s S e n t += 1;
25 messageQueue . get ( q ) . put ( tmp ) ;
26 }
27
28 else {
29 findMin () ;
30 }
31
32 }
33 else {
34 Message tmp = new Message () ;
35 tmp . s etMessag eType ( " accept " ) ;
36 tmp . se t Se nd er N od eI d ( nodeId ) ;
37 ArrayList < Integer > a = new ArrayList < >() ;
38 t o t a l M e s s a g e s S e n t += 1;
39 messageQueue . get ( q ) . put ( tmp ) ;
40 }
41
42 }
When a test message is received, if the sender’s fragment is greater than the receiver, we wait. Otherwise if
they belong to the same fragment, an accept message is sent, otherwise a reject message is sent.
When an accept message is received, it updates the information and calls report(). report() waits for all it’s
neighbours other than it’s parent to send tell their status and then report it back to it’s own parent.
When a reject message is received, the node changes the status of the branch to reject and then checks if it has
the possibility of sending a test message to one of it’s node which is not rejected yet.
3
23
24 else if ( omega == bestWt && omega == Integer . MAX_VALUE ) {
25 System . out . println ( " Stops + + + + + + + + " ) ;
26 algoStops . add ( true ) ;
27 }
28 }
29
30 }
When a report message is received, if it’s not from it’s parent, if the weight received is less than the best weight
the node has, it changes the status of best weight and best node corresponding to the sender. It then calls
the report() function. If the message is received from it’s parent, if the node is still in the process of finding,
it waits. Otherwise if the weight received is greater than the best weight, changeRoot() function is called.
Otherwise if both the received weight and best weight is infinity, the algorithm has come to end and thus halt.
3 Results
3.1 Results with dense graphs
The probability of an edge between two nodes is 0.8 in our implementation of dense graph. The message
complexity is 2E + O(NlogN) and the plot adheres to this.
4
3.2 Results with sparse graphs
The probability of an edge between two nodes is 0.2 in our implementation of sparse graph. The message
complexity is 2E + O(NlogN) and the plot adheres to this.