How to create shortest path for mobile sink in NS2

Description

In the sensor network after clusterhead selection, cluster member sends data to clusterhead. Clusterhead aggregates the received data from cluster member. In mobile sink network, only the sink moves to collect data from clusterhead whereas in static sink case, data is sent in multiple hops to sink from clusterhead. So sink mobility scheme achieves energy optimization with certain delay due to the time taken for the arrival sink nearer to clusterhead. Hence the sink moves along the path as per the shortest path algorithm. The code given below provides the logic for shortest path in grid based clustering.

Initialize the variable
mc=0;
ec =1;
mx(mc), my(mc) = list of coordinates to which the mobile sink is required to move
for ( j= 1;j <= number_grids; j++) {
mpn = ec%2;
if ( mpn == 1) {
for ( i= 1;i <= number_grids; i++) {      px = (i-1 +0.5)*(communication_range);     py = (j-1 +0.5)*(communication_range)      mx(mc) =px;      my(mc) =py      mc =mc+1;     }     } else {     for (i= number_grids; i >= 1; i-1 ) {
px = (i-1 +0.5)*(communication_range);
py = (j-1 +0.5)*(communication_range)
mx(mc) =px;
my(mc) =py
mc =mc+1;
}
}
ec = ec +1;
}

Shortest path Creation for mobile sink in NS2

Sink node starting position is (0, 0).

Shortest path Creation for mobile sink in NS2

After some time, sink node moves to collect data from clusterhead 6.