How to apply LEACH Protocol in NS2


Low Energy Adaptive Clustering Hierarchy (LEACH) is the hierarchical clustering approach in which network is divided into set of clusters and each cluster is maintained by clusterhead. Clusterhead is selected based on random number and threshold in multiple rounds. In each round clusterhead role is rotated for all sensors in the network.

Filename: leach.tcl

set cper [expr $nclusters.0 / $val(nn)]
set th [expr $ra % [expr round(1.0 / $cper)]]
set th1 [expr $cper * $th]
set threshold [expr $cper / [expr 1 – $th1]]
if { $random($i) < $threshold } {
set ch $i