├── Grid.m ├── README.md ├── Screenshot (35).png ├── Screenshot (50).png ├── finalproject.m ├── pro.m ├── pro1.m └── simulationproject.m /Grid.m: -------------------------------------------------------------------------------- 1 | x=zeros(500); % x,y are points represents the position of the sensor node 2 | 3 | y=zeros(500); % x,y are points represents the position of the sensor node 4 | 5 | flag=zeros(500,1); % represents the sensor belong to which group 6 | 7 | dist=zeros(500,500); % represents the distance between any two nodes 8 | 9 | ch=zeros(500,1); % selects the cluster heads among all the nodes 10 | 11 | ch1=zeros(100,1); % represents the cluster heads 12 | 13 | dist1=zeros(100,1); % represents the distance between center head and clusterheads 14 | 15 | match=zeros(500,100); % represents the nodes to which cluster head it should match 16 | 17 | neig=zeros(500,1); % gives the number of neighbouring nodes for a perticular node 18 | 19 | residual=zeros(500); % calculates the residual energy of each node 20 | 21 | energy=zeros(100,1); % calculate the residual energy of cluster head 22 | 23 | join=zeros(100,100); % represents the flag of cluster heads ehich have send the information 24 | 25 | flag2=zeros(100,100); % represents the cluster head to join the another cluster head or not based on the condition distance from sink 26 | 27 | join1=zeros(100,1); 28 | 29 | distance=zeros(100,100); 30 | 31 | for i=1:500 32 | x(i)=rand()*1000; 33 | y(i)=rand()*1000; 34 | residual(i)=2; % assigning the postion of 500 nodes at random 35 | end 36 | for i=1:500 37 | plot(x(i),y(i),'o'); 38 | hold on; % plotting those points in the graph 39 | end 40 | for i=1:500 %calculating distance among any two nodes 41 | for j=1:500 42 | dist(i,j)=sqrt(power(((x(i)-x(j))),2)+power(((y(i)-y(j))),2)); 43 | end 44 | end 45 | for i=1:500 % dividing the nodes into various blocks of size 100*100 and assigining flag value 46 | a=floor(x(i)/100); 47 | b=floor(y(i)/100); 48 | flag(i,1)=a*10+b+1; 49 | end 50 | for i=1:500 %calculating the number of neigbouring nodes for each node in each block 62.5*100 51 | for j=1:500 52 | if(flag(i,1)==flag(j,1)) 53 | if(((x(j)<=x(i)+62.5)&&(y(j)<=y(i)+100))&&((y(j)>=y(i)-62.5)&&(x(j)>=x(i)-100))) 54 | neig(i,1)=neig(i,1)+1; 55 | end 56 | end 57 | end 58 | end 59 | x1=1000; 60 | y1=1000; % x1,y1 are sink co-ordinates 61 | plot(x1,y1,'o'); 62 | hold on; 63 | plot(x1,y1,'k*'); 64 | hold on; 65 | for i=1:500 % connecting all nodes which are near to the sink to the sink and assigining flag value 0 66 | if((x(i)<=x1+100)&&(x(i)>=x1-100)&&(y(i)<=y1+100)&&(y(i)>=y1-100)) 67 | flag(i,1)=0; 68 | plot([x(i) x1] ,[y(i) y1],'m'); 69 | hold on; 70 | end 71 | end 72 | 73 | for i=1:100 % finding the cluster heads by using formulae which is inversly proportional to no of neighbours 74 | min=0; 75 | for j=1:500 76 | if((flag(j,1)==i)&&(min<=neig(j))) 77 | min=neig(j); 78 | p=j; 79 | end 80 | end 81 | plot(x(p),y(p),'r*'); 82 | ch(p,1)=1; 83 | hold on; 84 | end 85 | p=1; 86 | for i=1:500 %finding the position of cluster heads 87 | if(ch(i,1)==1) 88 | ch1(p,1)=i; 89 | p=p+1; 90 | end 91 | end 92 | for i=1:100 93 | if(ch1(i,1)) % calculating the distance of cluster heads from sink 94 | dist1(i,1)=sqrt(power(((x(ch1(i,1))-x1)),2)+power(((y(ch1(i,1))-y1)),2)); 95 | end 96 | end 97 | for i=1:100 98 | for j=1:500 99 | if(ch1(i,1)) 100 | if(flag(j,1)==flag(ch1(i,1),1)) % joining the remaining cluster nodes with their corresponding clusterheads 101 | plot([x(j) x(ch1(i,1))],[y(j) y(ch1(i,1))],'g'); 102 | hold on; 103 | end 104 | end 105 | end 106 | end 107 | for i=1:100 % joining near cluster heads to sink 108 | if(ch1(i,1)) 109 | dist1(i,1)=sqrt(power(((x(ch1(i,1))-x1)),2)+power(((y(ch1(i,1))-y1)),2)); 110 | if(dist1(i,1)<200) 111 | join1(i,1)=1; 112 | plot([x1 x(ch1(i,1))],[y1 y(ch1(i,1))],'k'); 113 | end 114 | end 115 | end 116 | l=sum(ch); 117 | for i=1:l %finding the distance of the cluster head among themselves 118 | for j=1:l 119 | if(ch1(i,1)&&ch1(j,1)) 120 | distance(i,j)=sqrt(power(((x(ch1(i,1))-x(ch1(j,1)))),2)+power(((y(ch1(i,1))-y(ch1(j,1)))),2)); 121 | end 122 | end 123 | end 124 | for i=1:l % joining the cluster head among themselves 125 | min=999999; 126 | p=i; 127 | for j=1:l 128 | if(((join1(i,1)==0)&&(join(j,i)==0)&&(ch1(i,1))&&(ch1(j,1)&&i~=j))&&(flag2(i,j)==0)) 129 | if((min>distance(i,j))&&(dist1(i,1)>dist1(j,1))) 130 | min=distance(i,j); 131 | p=j; 132 | end 133 | end 134 | end 135 | join(i,p)=1; 136 | if(ch1(i,1)&&ch1(p,1)) 137 | plot([x(ch1(i,1)) x(ch1(p,1))],[y(ch1(i,1)) y(ch1(p,1))],'k'); 138 | end 139 | end -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Routing-algorithm-in-WSN-Grid-based-approach- 2 | The algorithm shows how the sensed data from the sensor nodes is being routed to the sink efficiently.The energy of the sensor nodes is very less and the cost of transmitting data of 1Kb from the sensor nodes to a distance of 100 meters is same as performing 3 billion instructions in a general purpose register.Thus there is a requirement in minimising the number of transmissions and length of transmission and every node in the network must be covered . So a Grid is chosen of dimensions m*m (where m is maximum distance the node can communicate) and among all the nodes a centre head is chosen where all the nodes in the grid sends the data to the cluster head and all the cluster heads sends data to nearest cluster head to save the energy until it reaches the sink 3 | (https://user-images.githubusercontent.com/32775691/31572830-78f3c396-b0cc-11e7-8941-3fbea69812cd.png) 4 | -------------------------------------------------------------------------------- /Screenshot (35).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhanu1131/Routing-algorithm-in-WSN-Grid-based-approach-/42ef0a23f56e080ea8be804240b4b911870d065e/Screenshot (35).png -------------------------------------------------------------------------------- /Screenshot (50).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhanu1131/Routing-algorithm-in-WSN-Grid-based-approach-/42ef0a23f56e080ea8be804240b4b911870d065e/Screenshot (50).png -------------------------------------------------------------------------------- /finalproject.m: -------------------------------------------------------------------------------- 1 | x=zeros(1000); 2 | y=zeros(1000); % x,y are points represents the position of the sensor node 3 | 4 | flag=zeros(1000,1); % represents the sensor belong to which group 5 | 6 | dist=zeros(1000,1000); % represents the distance between any two nodes 7 | 8 | ch=zeros(1000,1); % selects the cluster heads among all the nodes 9 | 10 | ch1=zeros(32,1); % represents the cluster heads 11 | 12 | dist1=zeros(32,1); % represents the distance between center head and clusterheads 13 | 14 | match=zeros(1000,32); % represents the nodes to which cluster head it should match 15 | 16 | neig=zeros(1000,1); % gives the number of neighbouring nodes for a perticular node 17 | 18 | residual=zeros(1000); % calculates the residual energy of each node 19 | 20 | energy=zeros(32,1); % calculate the residual energy of cluster head 21 | 22 | join=zeros(32,32); % represents the flag of cluster heads ehich have send the information 23 | 24 | join1=zeros(32,1); 25 | distance=zeros(32,32); 26 | for i=1:1000 27 | x(i)=rand()*1000; 28 | y(i)=rand()*1000; 29 | residual(i)=2; % assigning the postion of 1000 nodes at random 30 | end 31 | for i=1:1000 32 | plot(x(i),y(i),'o'); 33 | hold on; % plotting those points in the graph 34 | end 35 | for i=1:1000 %calculating distance among any two nodes 36 | for j=1:1000 37 | dist(i,j)=sqrt(power(((x(i)-x(j))),2)+power(((y(i)-y(j))),2)); 38 | end 39 | end 40 | for i=1:1000 % dividing the nodes into various blocks of size 125*250 and assigining flag value 41 | if((floor(x(i)/125)==0)&&(floor(y(i)/250)==0)) 42 | flag(i,1)=1; 43 | end 44 | if((floor(x(i)/125)==0)&&(floor(y(i)/250)==1)) 45 | flag(i,1)=2; 46 | end 47 | if((floor(x(i)/125)==0)&&(floor(y(i)/250)==2)) 48 | flag(i,1)=3; 49 | end 50 | if((floor(x(i)/125)==0)&&(floor(y(i)/250)==3)) 51 | flag(i,1)=4; 52 | end 53 | if((floor(x(i)/125)==1)&&(floor(y(i)/250)==0)) 54 | flag(i,1)=5; 55 | end 56 | if((floor(x(i)/125)==1)&&(floor(y(i)/250)==1)) 57 | flag(i,1)=6; 58 | end 59 | if((floor(x(i)/125)==1)&&(floor(y(i)/250)==2)) 60 | flag(i,1)=7; 61 | end 62 | if((floor(x(i)/125)==1)&&(floor(y(i)/250)==3)) 63 | flag(i,1)=8; 64 | end 65 | if((floor(x(i)/125)==2)&&(floor(y(i)/250)==0)) 66 | flag(i,1)=9; 67 | end 68 | if((floor(x(i)/125)==2)&&(floor(y(i)/250)==1)) 69 | flag(i,1)=10; 70 | end 71 | if((floor(x(i)/125)==2)&&(floor(y(i)/250)==2)) 72 | flag(i,1)=11; 73 | end 74 | if((floor(x(i)/125)==2)&&(floor(y(i)/250)==3)) 75 | flag(i,1)=12; 76 | end 77 | if((floor(x(i)/125)==3)&&(floor(y(i)/250)==0)) 78 | flag(i,1)=13; 79 | end 80 | if((floor(x(i)/125)==3)&&(floor(y(i)/250)==1)) 81 | flag(i,1)=14; 82 | end 83 | if((floor(x(i)/125)==3)&&(floor(y(i)/250)==2)) 84 | flag(i,1)=15; 85 | end 86 | if((floor(x(i)/125)==3)&&(floor(y(i)/250)==3)) 87 | flag(i,1)=16; 88 | end 89 | if((floor(x(i)/125)==4)&&(floor(y(i)/250)==0)) 90 | flag(i,1)=17; 91 | end 92 | if((floor(x(i)/125)==4)&&(floor(y(i)/250)==1)) 93 | flag(i,1)=18; 94 | end 95 | if((floor(x(i)/125)==4)&&(floor(y(i)/250)==2)) 96 | flag(i,1)=19; 97 | end 98 | if((floor(x(i)/125)==4)&&(floor(y(i)/250)==3)) 99 | flag(i,1)=20; 100 | end 101 | if((floor(x(i)/125)==5)&&(floor(y(i)/250)==0)) 102 | flag(i,1)=21; 103 | end 104 | if((floor(x(i)/125)==5)&&(floor(y(i)/250)==1)) 105 | flag(i,1)=22; 106 | end 107 | if((floor(x(i)/125)==5)&&(floor(y(i)/250)==2)) 108 | flag(i,1)=23; 109 | end 110 | if((floor(x(i)/125)==5)&&(floor(y(i)/250)==3)) 111 | flag(i,1)=24; 112 | end 113 | if((floor(x(i)/125)==6)&&(floor(y(i)/250)==0)) 114 | flag(i,1)=25; 115 | end 116 | if((floor(x(i)/125)==6)&&(floor(y(i)/250)==1)) 117 | flag(i,1)=26; 118 | end 119 | if((floor(x(i)/125)==6)&&(floor(y(i)/250)==2)) 120 | flag(i,1)=27; 121 | end 122 | if((floor(x(i)/125)==6)&&(floor(y(i)/250)==3)) 123 | flag(i,1)=28; 124 | end 125 | if((floor(x(i)/125)==7)&&(floor(y(i)/250)==0)) 126 | flag(i,1)=29; 127 | end 128 | if((floor(x(i)/125)==7)&&(floor(y(i)/250)==1)) 129 | flag(i,1)=30; 130 | end 131 | if((floor(x(i)/125)==7)&&(floor(y(i)/250)==2)) 132 | flag(i,1)=31; 133 | end 134 | if((floor(x(i)/125)==7)&&(floor(y(i)/250)==3)) 135 | flag(i,1)=32; 136 | end 137 | end 138 | for i=1:1000 %calculating the number of neigbouring nodes for each node in each block 62.5*125 139 | for j=1:1000 140 | if(flag(i,1)==flag(j,1)) 141 | if(((x(j)<=x(i)+62.5)&&(y(j)<=y(i)+125))&&((y(j)>=y(i)-62.5)&&(x(j)>=x(i)-125))) 142 | neig(i,1)=neig(i,1)+1; 143 | end 144 | end 145 | end 146 | end 147 | x1=500; 148 | y1=500; % x1,y1 are sink co-ordinates 149 | plot(x1,y1,'o'); 150 | hold on; 151 | plot(x1,y1,'k*'); 152 | hold on; 153 | for i=1:1000 % connecting all nodes which are near to the sink to the sink and assigining flag value 0 154 | if((x(i)<=x1+125)&&(x(i)>=x1-125)&&(y(i)<=y1+125)&&(y(i)>=y1-125)) 155 | flag(i,1)=0; 156 | plot([x(i) x1] ,[y(i) y1],'m'); 157 | hold on; 158 | end 159 | end 160 | for i=1:1000 % calculate the number of neighbours of each node 161 | for j=1:1000 162 | if(flag(i,1)==flag(j,1)) 163 | if(((x(j)<=x(i)+62.5)&&(y(j)<=y(i)+125))&&((y(j)>=y(i)-62.5)&&(x(j)>=x(i)-125))) 164 | neig(i,1)=neig(i,1)+1; 165 | end 166 | end 167 | end 168 | end 169 | for i=1:32 % finding the cluster heads by using formulae which is inversly proportional to no of neighbours 170 | min=0; 171 | for j=1:1000 172 | if((flag(j,1)==i)&&(min<=neig(j))) 173 | min=neig(j); 174 | p=j; 175 | end 176 | end 177 | plot(x(p),y(p),'r*'); 178 | ch(p,1)=1; 179 | hold on; 180 | end 181 | p=1; 182 | for i=1:1000 %finding the position of cluster heads 183 | if(ch(i,1)==1) 184 | ch1(p,1)=i; 185 | p=p+1; 186 | end 187 | end 188 | for i=1:32 % calculating the distance of cluster heads from sink 189 | dist1(i,1)=sqrt(power(((x(ch1(i,1))-x1)),2)+power(((y(ch1(i,1))-y1)),2)); 190 | end 191 | for i=1:32 192 | for j=1:1000 193 | if(flag(j,1)==flag(ch1(i,1),1)) % joining the remaining cluster nodes with their corresponding clusterheads 194 | plot([x(j) x(ch1(i,1))],[y(j) y(ch1(i,1))],'m'); 195 | hold on; 196 | end 197 | end 198 | end 199 | for i=1:32 200 | dist1(i,1)=sqrt(power(((x(ch1(i,1))-x1)),2)+power(((y(ch1(i,1))-y1)),2)); 201 | if(dist1(i,1)<225) 202 | join1(i,1)=1; 203 | plot([x1 x(ch1(i,1))],[y1 y(ch1(i,1))],'k'); 204 | end 205 | end 206 | for i=1:32 207 | for j=1:32 208 | distance(i,j)=sqrt(power(((x(ch1(i,1))-x(ch1(j,1)))),2)+power(((y(ch1(i,1))-y(ch1(j,1)))),2)); 209 | end 210 | end 211 | for i=1:32 212 | min=999999; 213 | if(join1(i,1)==0) 214 | for j=1:32 215 | if((min>distance(i,j))&&(distance(i,j))&&join(i,j)==0) 216 | min=distance(i,j); 217 | p=j; 218 | end 219 | end 220 | join(i,p)=1; 221 | plot([x(ch1(i,1)) x(ch1(p,1))],[y(ch1(i,1)) y(ch1(p,1))],'k'); 222 | end 223 | end 224 | 225 | -------------------------------------------------------------------------------- /pro.m: -------------------------------------------------------------------------------- 1 | x=zeros(200); 2 | y=zeros(200); 3 | con=zeros(5,5); 4 | nei=zeros(5,5); 5 | neig=zeros(200,1); 6 | flag=zeros(200,1); 7 | dist=zeros(200,200); 8 | residual=zeros(200); 9 | energy=zeros(200,1); 10 | ch=zeros(200,1); 11 | ch1=zeros(32,1); 12 | dist1=zeros(32,1); 13 | match=zeros(200,32); 14 | relay=zeros(32,32); 15 | num=zeros(32,1); 16 | for i=1:200 17 | x(i)=rand()*1000; 18 | y(i)=rand()*1000; 19 | residual(i)=2; 20 | end 21 | for i=1:200 22 | plot(x(i),y(i),'o'); 23 | hold on; 24 | end 25 | for i=1:200 26 | for j=1:200 27 | dist(i,j)=sqrt(power(((x(i)-x(j))),2)+power(((y(i)-y(j))),2)); 28 | end 29 | end 30 | for i=1:200 31 | p=floor(x(i)/125); 32 | p1=floor(y(i)/250); 33 | for j=1:200 34 | q=floor(x(j)/125); 35 | q1=floor(y(j)/250); 36 | if((p==q&&p1==q1)&&dist(i,j)) 37 | energy(i,1)=energy(i,1)+residual(i)*residual(i)/dist(i,j); 38 | end 39 | end 40 | end 41 | j=1; 42 | for i=1:200 43 | if((floor(x(i)/125)==0)&&(floor(y(i)/250)==0)) 44 | flag(i,1)=1; 45 | end 46 | if((floor(x(i)/125)==0)&&(floor(y(i)/250)==1)) 47 | flag(i,1)=2; 48 | end 49 | if((floor(x(i)/125)==0)&&(floor(y(i)/250)==2)) 50 | flag(i,1)=3; 51 | end 52 | if((floor(x(i)/125)==0)&&(floor(y(i)/250)==3)) 53 | flag(i,1)=4; 54 | end 55 | if((floor(x(i)/125)==1)&&(floor(y(i)/250)==0)) 56 | flag(i,1)=5; 57 | end 58 | if((floor(x(i)/125)==1)&&(floor(y(i)/250)==1)) 59 | flag(i,1)=6; 60 | end 61 | if((floor(x(i)/125)==1)&&(floor(y(i)/250)==2)) 62 | flag(i,1)=7; 63 | end 64 | if((floor(x(i)/125)==1)&&(floor(y(i)/250)==3)) 65 | flag(i,1)=8; 66 | end 67 | if((floor(x(i)/125)==2)&&(floor(y(i)/250)==0)) 68 | flag(i,1)=9; 69 | end 70 | if((floor(x(i)/125)==2)&&(floor(y(i)/250)==1)) 71 | flag(i,1)=10; 72 | end 73 | if((floor(x(i)/125)==2)&&(floor(y(i)/250)==2)) 74 | flag(i,1)=11; 75 | end 76 | if((floor(x(i)/125)==2)&&(floor(y(i)/250)==3)) 77 | flag(i,1)=12; 78 | end 79 | if((floor(x(i)/125)==3)&&(floor(y(i)/250)==0)) 80 | flag(i,1)=13; 81 | end 82 | if((floor(x(i)/125)==3)&&(floor(y(i)/250)==1)) 83 | flag(i,1)=14; 84 | end 85 | if((floor(x(i)/125)==3)&&(floor(y(i)/250)==2)) 86 | flag(i,1)=15; 87 | end 88 | if((floor(x(i)/125)==3)&&(floor(y(i)/250)==3)) 89 | flag(i,1)=16; 90 | end 91 | if((floor(x(i)/125)==4)&&(floor(y(i)/250)==0)) 92 | flag(i,1)=17; 93 | end 94 | if((floor(x(i)/125)==4)&&(floor(y(i)/250)==1)) 95 | flag(i,1)=18; 96 | end 97 | if((floor(x(i)/125)==4)&&(floor(y(i)/250)==2)) 98 | flag(i,1)=19; 99 | end 100 | if((floor(x(i)/125)==4)&&(floor(y(i)/250)==3)) 101 | flag(i,1)=20; 102 | end 103 | if((floor(x(i)/125)==5)&&(floor(y(i)/250)==0)) 104 | flag(i,1)=21; 105 | end 106 | if((floor(x(i)/125)==5)&&(floor(y(i)/250)==1)) 107 | flag(i,1)=22; 108 | end 109 | if((floor(x(i)/125)==5)&&(floor(y(i)/250)==2)) 110 | flag(i,1)=23; 111 | end 112 | if((floor(x(i)/125)==5)&&(floor(y(i)/250)==3)) 113 | flag(i,1)=24; 114 | end 115 | if((floor(x(i)/125)==6)&&(floor(y(i)/250)==0)) 116 | flag(i,1)=25; 117 | end 118 | if((floor(x(i)/125)==6)&&(floor(y(i)/250)==1)) 119 | flag(i,1)=26; 120 | end 121 | if((floor(x(i)/125)==6)&&(floor(y(i)/250)==2)) 122 | flag(i,1)=27; 123 | end 124 | if((floor(x(i)/125)==6)&&(floor(y(i)/250)==3)) 125 | flag(i,1)=28; 126 | end 127 | if((floor(x(i)/125)==7)&&(floor(y(i)/250)==0)) 128 | flag(i,1)=29; 129 | end 130 | if((floor(x(i)/125)==7)&&(floor(y(i)/250)==1)) 131 | flag(i,1)=30; 132 | end 133 | if((floor(x(i)/125)==7)&&(floor(y(i)/250)==2)) 134 | flag(i,1)=31; 135 | end 136 | if((floor(x(i)/125)==7)&&(floor(y(i)/250)==3)) 137 | flag(i,1)=32; 138 | end 139 | end 140 | for i=1:200 141 | for j=1:200 142 | if(flag(i,1)==flag(j,1)) 143 | if(((x(j)<=x(i)+62.5)&&(y(j)<=y(i)+125))&&((y(j)>=y(i)-62.5)&&(x(j)>=x(i)-125))) 144 | neig(i,1)=neig(i,1)+1; 145 | end 146 | end 147 | end 148 | end 149 | for i=1:32 150 | min=99; 151 | for j=1:200 152 | if((flag(j,1)==i)&&(min>=neig(j))) 153 | min=neig(j); 154 | p=j; 155 | end 156 | end 157 | plot(x(p),y(p),'r*'); 158 | ch(p,1)=1; 159 | hold on; 160 | end 161 | p=1; 162 | for i=1:200 163 | if(ch(i,1)==1) 164 | ch1(p,1)=i; 165 | p=p+1; 166 | end 167 | end 168 | x1=500; 169 | y1=500; 170 | for i=1:32 171 | dist1(i,1)=sqrt(power(((x(ch1(i,1))-x1)),2)+power(((y(ch1(i,1))-y1)),2)); 172 | end 173 | for i=1:200 174 | p=1; 175 | for j=1:200 176 | if((ch(j)==1)&&(dist(i,j))) 177 | match(i,p)=dist1(p,1)/dist(i,j); 178 | p=p+1; 179 | end 180 | end 181 | end 182 | for i=1:200 183 | max=match(i,1); 184 | a=1; 185 | for j=2:32 186 | if(maxdist(ch1(i,1),ch1(j,1)))) 204 | ch1(j,1) 205 | min=dist(ch1(i,1),ch1(j,1)); 206 | p=ch1(j,1); 207 | end 208 | end 209 | plot([x(ch1(i,1)) x(ch1(p,1))], [y(ch1(i,1)) y(ch1(p,1))],'k'); 210 | hold on; 211 | end 212 | -------------------------------------------------------------------------------- /pro1.m: -------------------------------------------------------------------------------- 1 | x=zeros(200); 2 | y=zeros(200); 3 | con=zeros(5,5); 4 | nei=zeros(5,5); 5 | neig=zeros(200,1); 6 | flag=zeros(200,1); 7 | dist=zeros(200,200); 8 | residual=zeros(200); 9 | energy=zeros(200,1); 10 | for i=1:200 11 | x(i)=rand()*1000; 12 | y(i)=rand()*1000; 13 | residual(i)=2; 14 | end 15 | for i=1:200 16 | plot(x(i),y(i),'o'); 17 | hold on; 18 | end 19 | for i=1:200 20 | for j=1:200 21 | dist(i,j)=sqrt(power(((x(i)-x(j))),2)+power(((y(i)-y(j))),2)); 22 | end 23 | end 24 | for i=1:200 25 | p=floor(x(i)/125); 26 | p1=floor(y(i)/250); 27 | for j=1:200 28 | q=floor(x(j)/125); 29 | q1=floor(y(j)/250); 30 | if((p==q&&p1==q1)&&dist(i,j)) 31 | energy(i,1)=energy(i,1)+residual(i)*residual(i)/dist(i,j); 32 | end 33 | end 34 | end 35 | x1=[250,250]; 36 | y1=[0,1000]; 37 | plot(x1,y1,'r'); 38 | hold on; 39 | x1=[125,125]; 40 | y1=[0,1000]; 41 | plot(x1,y1,'r'); 42 | hold on; 43 | x1=[375,375]; 44 | y1=[0,1000]; 45 | plot(x1,y1,'r'); 46 | hold on; 47 | x1=[625,625]; 48 | y1=[0,1000]; 49 | plot(x1,y1,'r'); 50 | hold on; 51 | x1=[500,500]; 52 | y1=[0,1000]; 53 | plot(x1,y1,'r'); 54 | hold on; 55 | x1=[750,750]; 56 | y1=[0,1000]; 57 | plot(x1,y1,'r'); 58 | hold on; 59 | x1=[1000,1000]; 60 | y1=[0,1000]; 61 | plot(x1,y1,'r'); 62 | hold on; 63 | x1=[875,875]; 64 | y1=[0,1000]; 65 | plot(x1,y1,'r'); 66 | hold on; 67 | x1=[0,1000]; 68 | y1=[250,250]; 69 | plot(x1,y1,'r'); 70 | hold on; 71 | x1=[0,1000]; 72 | y1=[500,500]; 73 | plot(x1,y1,'r'); 74 | hold on; 75 | x1=[0,1000]; 76 | y1=[750,750]; 77 | plot(x1,y1,'r'); 78 | hold on; 79 | j=1; 80 | for i=1:200 81 | if((floor(x(i)/125)==0)&&(floor(y(i)/250)==0)) 82 | flag(i,1)=1; 83 | end 84 | if((floor(x(i)/125)==0)&&(floor(y(i)/250)==1)) 85 | flag(i,1)=2; 86 | end 87 | if((floor(x(i)/125)==0)&&(floor(y(i)/250)==2)) 88 | flag(i,1)=3; 89 | end 90 | if((floor(x(i)/125)==0)&&(floor(y(i)/250)==3)) 91 | flag(i,1)=4; 92 | end 93 | if((floor(x(i)/125)==1)&&(floor(y(i)/250)==0)) 94 | flag(i,1)=5; 95 | end 96 | if((floor(x(i)/125)==1)&&(floor(y(i)/250)==1)) 97 | flag(i,1)=6; 98 | end 99 | if((floor(x(i)/125)==1)&&(floor(y(i)/250)==2)) 100 | flag(i,1)=7; 101 | end 102 | if((floor(x(i)/125)==1)&&(floor(y(i)/250)==3)) 103 | flag(i,1)=8; 104 | end 105 | if((floor(x(i)/125)==2)&&(floor(y(i)/250)==0)) 106 | flag(i,1)=9; 107 | end 108 | if((floor(x(i)/125)==2)&&(floor(y(i)/250)==1)) 109 | flag(i,1)=10; 110 | end 111 | if((floor(x(i)/125)==2)&&(floor(y(i)/250)==2)) 112 | flag(i,1)=11; 113 | end 114 | if((floor(x(i)/125)==2)&&(floor(y(i)/250)==3)) 115 | flag(i,1)=12; 116 | end 117 | if((floor(x(i)/125)==3)&&(floor(y(i)/250)==0)) 118 | flag(i,1)=13; 119 | end 120 | if((floor(x(i)/125)==3)&&(floor(y(i)/250)==1)) 121 | flag(i,1)=14; 122 | end 123 | if((floor(x(i)/125)==3)&&(floor(y(i)/250)==2)) 124 | flag(i,1)=15; 125 | end 126 | if((floor(x(i)/125)==3)&&(floor(y(i)/250)==3)) 127 | flag(i,1)=16; 128 | end 129 | if((floor(x(i)/125)==4)&&(floor(y(i)/250)==0)) 130 | flag(i,1)=17; 131 | end 132 | if((floor(x(i)/125)==4)&&(floor(y(i)/250)==1)) 133 | flag(i,1)=18; 134 | end 135 | if((floor(x(i)/125)==4)&&(floor(y(i)/250)==2)) 136 | flag(i,1)=19; 137 | end 138 | if((floor(x(i)/125)==4)&&(floor(y(i)/250)==3)) 139 | flag(i,1)=20; 140 | end 141 | if((floor(x(i)/125)==5)&&(floor(y(i)/250)==0)) 142 | flag(i,1)=21; 143 | end 144 | if((floor(x(i)/125)==5)&&(floor(y(i)/250)==1)) 145 | flag(i,1)=22; 146 | end 147 | if((floor(x(i)/125)==5)&&(floor(y(i)/250)==2)) 148 | flag(i,1)=23; 149 | end 150 | if((floor(x(i)/125)==5)&&(floor(y(i)/250)==3)) 151 | flag(i,1)=24; 152 | end 153 | if((floor(x(i)/125)==6)&&(floor(y(i)/250)==0)) 154 | flag(i,1)=25; 155 | end 156 | if((floor(x(i)/125)==6)&&(floor(y(i)/250)==1)) 157 | flag(i,1)=26; 158 | end 159 | if((floor(x(i)/125)==6)&&(floor(y(i)/250)==2)) 160 | flag(i,1)=27; 161 | end 162 | if((floor(x(i)/125)==6)&&(floor(y(i)/250)==3)) 163 | flag(i,1)=28; 164 | end 165 | if((floor(x(i)/125)==7)&&(floor(y(i)/250)==0)) 166 | flag(i,1)=29; 167 | end 168 | if((floor(x(i)/125)==7)&&(floor(y(i)/250)==1)) 169 | flag(i,1)=30; 170 | end 171 | if((floor(x(i)/125)==7)&&(floor(y(i)/250)==2)) 172 | flag(i,1)=31; 173 | end 174 | if((floor(x(i)/125)==7)&&(floor(y(i)/250)==3)) 175 | flag(i,1)=32; 176 | end 177 | end 178 | for i=1:200 179 | for j=1:200 180 | if(flag(i,1)==flag(j,1)) 181 | if(((x(j)<=x(i)+62.5)&&(y(j)<=y(i)+125))&&((y(j)>=y(i)-62.5)&&(x(j)>=x(i)-125))) 182 | neig(i,1)=neig(i,1)+1; 183 | end 184 | end 185 | end 186 | end 187 | for i=1:32 188 | min=99; 189 | for j=1:200 190 | if((flag(j,1)==i)&&(min>=neig(j))) 191 | min=neig(j); 192 | p=j; 193 | end 194 | end 195 | plot(x(p),y(p),'r*'); 196 | hold on; 197 | end 198 | neig -------------------------------------------------------------------------------- /simulationproject.m: -------------------------------------------------------------------------------- 1 | x=zeros(1000); % x,y are points represents the position of the sensor node 2 | 3 | y=zeros(1000); % x,y are points represents the position of the sensor node 4 | 5 | flag=zeros(1000,1); % represents the sensor belong to which group 6 | 7 | dist=zeros(1000,1000); % represents the distance between any two nodes 8 | 9 | ch=zeros(1000,1); % selects the cluster heads among all the nodes 10 | 11 | ch1=zeros(100,1); % represents the cluster heads 12 | 13 | dist1=zeros(100,1); % represents the distance between center head and sink 14 | 15 | match=zeros(1000,100); % represents the nodes to which cluster head it should match 16 | 17 | neig=zeros(1000,1); % gives the number of neighbouring nodes for a perticular node 18 | 19 | residual=zeros(1000); % calculates the residual energy of each node 20 | 21 | energy=zeros(100,1); % calculate the residual energy of cluster head 22 | 23 | join=zeros(100,100); % represents the flag of cluster heads which have send the information from node i to node j 24 | 25 | join1=zeros(100,1); % represents the flag of cluster heads ehich have send the information 26 | 27 | distance=zeros(100,100); % represents the distance between center heads 28 | 29 | for i=1:1000 30 | x(i)=rand()*1000; 31 | y(i)=rand()*1000; 32 | residual(i)=2; % assigning the postion of 1000 nodes at random 33 | end 34 | for i=1:1000 35 | plot(x(i),y(i),'o'); % plotting those points in the graph 36 | hold on; 37 | end 38 | for i=1:1000 %calculating distance among any two nodes 39 | for j=1:1000 40 | dist(i,j)=sqrt(power(((x(i)-x(j))),2)+power(((y(i)-y(j))),2)); 41 | end 42 | end 43 | for i=1:1000 % dividing the nodes into various blocks of size 100*100 and assigining flag value 44 | a=floor(x(i)/100); 45 | b=floor(y(i)/100); 46 | flag(i,1)=a*10+b+1; 47 | end 48 | for i=1:1000 %calculating the number of neigbouring nodes for each node in each block 62.5*100 49 | for j=1:1000 50 | if(flag(i,1)==flag(j,1)) 51 | neig(i,1)=neig(i,1)+dist(i,j); 52 | end 53 | end 54 | end 55 | x1=500; 56 | y1=500; % x1,y1 are sink co-ordinates 57 | plot(x1,y1,'o'); 58 | hold on; 59 | plot(x1,y1,'k*'); 60 | hold on; 61 | for i=1:1000 % connecting all nodes which are near to the sink to the sink and assigining flag value 0 62 | if((x(i)<=x1+100)&&(x(i)>=x1-100)&&(y(i)<=y1+100)&&(y(i)>=y1-100)) 63 | flag(i,1)=0; 64 | plot([x(i) x1] ,[y(i) y1],'g'); 65 | hold on; 66 | end 67 | end 68 | 69 | for i=1:100 % finding the cluster heads by using formulae which is inversly proportional to no of neighbours 70 | min=999999999; 71 | for j=1:1000 72 | if((flag(j,1)==i)&&(min>=neig(j,1))) 73 | min=neig(j); 74 | p=j; 75 | end 76 | end 77 | plot(x(p),y(p),'r*'); 78 | ch(p,1)=1; 79 | hold on; 80 | end 81 | p=1; 82 | for i=1:1000 %finding the position of cluster heads 83 | if(ch(i,1)==1) 84 | ch1(p,1)=i; 85 | p=p+1; 86 | end 87 | end 88 | for i=1:100 89 | if(ch1(i,1)) % calculating the distance of cluster heads from sink 90 | dist1(i,1)=sqrt(power(((x(ch1(i,1))-x1)),2)+power(((y(ch1(i,1))-y1)),2)); 91 | end 92 | end 93 | for i=1:100 94 | for j=1:1000 95 | if(ch1(i,1)) 96 | if(flag(j,1)==flag(ch1(i,1),1)) % joining the remaining cluster nodes with their corresponding clusterheads 97 | plot([x(j) x(ch1(i,1))],[y(j) y(ch1(i,1))],'g'); 98 | hold on; 99 | end 100 | end 101 | end 102 | end 103 | for i=1:100 % joining near cluster heads to sink 104 | if(ch1(i,1)) 105 | dist1(i,1)=sqrt(power(((x(ch1(i,1))-x1)),2)+power(((y(ch1(i,1))-y1)),2)); 106 | if(dist1(i,1)<200) 107 | join1(i,1)=1; 108 | plot([x1 x(ch1(i,1))],[y1 y(ch1(i,1))],'k'); 109 | end 110 | end 111 | end 112 | l=sum(ch); 113 | for i=1:l %finding the distance of the cluster head among themselves 114 | for j=1:l 115 | if(ch1(i,1)&&ch1(j,1)) 116 | distance(i,j)=sqrt(power(((x(ch1(i,1))-x(ch1(j,1)))),2)+power(((y(ch1(i,1))-y(ch1(j,1)))),2)); 117 | end 118 | end 119 | end 120 | for i=1:l % joining the cluster head among themselves 121 | min=999999; 122 | p=i; 123 | for j=1:l 124 | if(((join1(i,1)==0)&&(join(j,i)==0)&&(ch1(i,1))&&(ch1(j,1)&&i~=j))) 125 | if((min>distance(i,j))&&(dist1(i,1)>dist1(j,1))) 126 | min=distance(i,j); 127 | p=j; 128 | end 129 | end 130 | end 131 | join(i,p)=1; 132 | if(ch1(i,1)&&ch1(p,1)) 133 | plot([x(ch1(i,1)) x(ch1(p,1))],[y(ch1(i,1)) y(ch1(p,1))],'k'); 134 | end 135 | end --------------------------------------------------------------------------------