Secure voice transmission using DES

PROJECT VIDEO

ABSTRACT

Voice over Internet protocol has become a popular alternative to public switched telephone network. The flexibility and cost efficiency are the key factors luring enterprises to transition to VoIP. As VoIP technology matures and become increasingly popular so it also gains the attention of the attacker who attacks on the VoIP conversation and wishes to eardrop the conversation. In this paper we first describe the phrase spotting technique used to eavesdrop the conversation and defence approach for eavesdropping attack.

INTRODUCTION

Voice over Internet Protocol is a communication protocol and technology that allow user to make phone calls, video conferencing etc using a broadband internet connection instead of analog phone line. Firstly voice signal separated into frames which are stored into data packets and then transported over IP network using communication protocol. VoIP technology has recently become an important part of part of our day to day life; millions of people speak over internet but few of them understand the security issues related to voice communication. While people are not aware of the fact that someone could listen to their VoIP calls. An eavesdropper can detect that specific phrases were used in discussion without ever hearing the actual speech of the user. Phrase spotting Technique used to eavesdrop on VoIP conversation.  Phrase spotting technique is harmful to privacy. [1] To make system complete, it would also be rational to implement an encryption scheme based on public key cryptography to transfer the information about the padding length securely.

Cryptography Goals:

  • Confidential: The protection of data from unauthorized party and transmitted information is accessible only for reading by authorized parties.
  • Authentication: the authentication service is concerned with assuring that a communication is authentic and the assurance that the communicating entity is the one that is claims to be.
  • Integrity: assurance that the data received are exactly sent by an authorized entity. Only authorize parties are able to modify and stored information.
  • Non repudiation: It prevents either sender or receiver from denying a transmitted message.
  • Access Control: It is the ability to limit or control the access to host system and application via communication link.
  • Availability: Computer system assets are available to authorized parties when need

PROPOSED DEFENSE APPROACH FOR EAVESDROPPING 

In this section we present the defence approach for eavesdropping attack. It minimizes the chance of attack on VoIP calls. It would also be rational to implement an encryption scheme based on public key cryptography to transfer the information about the padding length securely [1]. A protection technique in which, padding each packet to different value. So that an attacker would not be able to differentiate between low bit rate and high bit rate. Conversation divides into two parts one side is sender and another side is receiver side. At the sender side firstly sender read an audio file, find out the indexes with low bit rate. For securing the conversation, pad the indexes with low bit rate. We are using 10% value of highest bit rate. Add the random noise at threshold indexes with maximum amplitude of highest bit rate. Then apply Data Encryption Standard [5] to all indexes, for encryption and decryption. Then it encrypts the noisy signal and indexes where noise is added. It is symmetric key where both parties share the same key for en- and decryption. At the receiver side, it decodes the noisy signal and removes the noises from the signal. Then get the original audio file. This technique minimizes the attack.

Transmitter

  • Read an audio file.
  • Then find out the indexes with low bit rate using a threshold schema.
  • For simplicity, we are using 10% value of the highest bit rate.
  • Then save the indexes of the signal where thresholding is done.
  • Insert the random noise at threshold indexes with maximum amplitude of highest bit rate.
  • This is the constant bit rate signal which is to be transfer. Let this signal be A
  • And then apply Data Encryption Standard (DES) to all the indexes which were thresholded. Let this signal be B
  • Then we get two signals that is A and B. These signals will be transfer through the channel.

Receiver

  • Receiver gets two signals. One is DES signal (B) and second is noisy signal (A).
  • Decode the DES signal using appropriate key
  • The decoded information are the indexes where the random noise is to be remove from the noisy signal
  • Remove the noises from the noises signal.
  • Then obtain the final audio at the receiver side

MATLAB SOURCE CODE

Instructions to run the code

  1. Copy each of below codes in different M files.
  2. Place all the files in same folder
  3. Use files from below link and place in the same folder
    1. def
    2. mike
    3. position
    4. reconstructed_audio
  4. Also note that these codes are not in a particular order. Copy them all and then run the program.
  5. Run the “final.m” file

Code 1 – Script M File – final.m

clear all
close all
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% read the audio file=65535 bits
disp('Read the audio file')
% maximum length
[W,Fs,nbits]=read_audio;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculate the threshold value
disp('Calculate the threshold value')
thresh=cal_thresh(W);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% values in between threshold limits
T=find(W<=thresh & W>=-thresh);
% plot(W)
% hold on
% plot(W(T),'r')
% pause
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% making changes to the input audio
disp('Making changes to the input audio')
Wnew=changed_audio(W,T);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plotting new audio and original audio
disp('Plotting new audio and original audio')
figure
subplot(2,1,1)
plot(W)
%axis([0 length(W) min(W)-0.2 max(W)+0.2])
title('Original audio')
subplot(2,1,2)
plot(Wnew)
%axis([0 length(W) min(W)-0.2 max(W)+0.2])
title('Changed audio')
wavwrite(Wnew,Fs,nbits,'reconstructed_audio.wav')
% ENCRYPTION PART %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('Encryption part')
[Edata,KEY]=encryption(T);
% DECRYPTION PART %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('Decryption part')
[Tnew,Tnew2]=decryption(Edata,KEY);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Wout=Wnew;
Tnew2=Tnew2(1:length(T));
Wout(Tnew2)=thresh;
Wout=Wout(1:length(W));

% figure
% subplot(3,1,1)
figure, plot(W)
title('Original audio')
% subplot(3,1,2)
figure, plot(Wnew)
title('Audio with constant bit rate')
% subplot(3,1,3)
figure, plot(Wout)
title('Recovered audio')
% plot(T)
% hold on
% plot(Tnew,'r')
% plot(Tnew2,'g')
% 
[PSNR,MSE,MAXERR,L2RAT] = measerr(W,Wout);
disp(MSE)
wavwrite(Wout,Fs,nbits,'reconstructed_audio.wav')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Code 2 – Function M File – encryption.m

function [Edata,KEY]=encryption(T)

n=0;
l=mod(length(T),4);
for i=1:4:(length(T)-l)
    plaintext=[fliplr(dec2binvec(T(i),16)) fliplr(dec2binvec(T(i+1),16)) fliplr(dec2binvec(T(i+2),16)) fliplr(dec2binvec(T(i+3),16))];
    [ciphertext,key]=DES(plaintext,'ENC');
    n=n+1;
    Edata(n,:)=ciphertext;
    KEY(n,:)=key;
%     pause
end

if l~=0
    if l==1
        plaintext=[fliplr(dec2bin(T(end),16)) zeros(1,48)];
    elseif l==2
        plaintext=[fliplr(dec2bin(T(end-1),16)) fliplr(dec2bin(T(end),16)) zeros(1,32)];
    elseif l==3
        plaintext=[fliplr(dec2bin(T(end-2),16)) fliplr(dec2bin(T(end-1),16)) fliplr(dec2bin(T(end),16)) zeros(1,16)];
    end
    [ciphertext,key]=DES(plaintext,'ENC');
    Edata(n+1,:)=ciphertext;
    KEY(n+1,:)=key;
end

end

Code 3 – Function M File – DES.m

function [varargout] = DES(input64,mode,key)
%DES: Data Encryption Standard
% Encrypt/Decrypt a 64-bit message using a 64-bit key using the Feistel
% Network
% -------------------------------------------------------------------------
% Inputs: 
%        input64 = a 64-bit message 
%           mode = either 'ENC' encryption or 'DEC' decryption (default 'ENC')
%            key = a 56/64-bit key (optional under 'ENC', but mandatory under 'DEC')
% Outputs:
%   varargout{1} = output64, a 64-bit message after encryption/decryption
%   varargout{2} = a 64-bit key, if a 64-bit key is not provided as an input
% -------------------------------------------------------------------------
% Demos:
%    plaintext = round(rand(1,64));
%    [ciphertext,key] = DES(plaintext);       % Encryption syntex 1
%   [ciphertext1,key] = DES(plaintext,'ENC'); % Encryption syntex 2
%   deciphertext1 = DES(ciphertext1,'DEC',key);% Decryption syntex
% 
%   key56 = round(rand(1,56));
%   [ciphertext2,key64] = DES(plaintext,'ENC',key56);% Encryption syntex 3 (56-bit key)
%   deciphertext2 = DES(ciphertext2,'DEC',key64);     % Decryption syntex   (64-bit key)
%   ciphertext3 = DES(plaintext,'ENC',key64);       % Encryption syntex 3 (64-bit key)
%   deciphertext3 = DES(ciphertext3,'DEC',key56);     % Decryption syntex   (56-bit key)
%   
%   % plot results
%    subplot(4,2,1),plot(plaintext),ylim([-.5,1.5]),xlim([1,64]),title('plaintext')
%    subplot(4,2,2),plot(ciphertext),ylim([-.5,1.5]),xlim([1,64]),title('ciphertext')
%   subplot(4,2,3),plot(deciphertext1),ylim([-.5,1.5]),xlim([1,64]),title('deciphertext1')
%   subplot(4,2,4),plot(ciphertext1),ylim([-.5,1.5]),xlim([1,64]),title('ciphertext1')
%   subplot(4,2,5),plot(deciphertext2),ylim([-.5,1.5]),xlim([1,64]),title('deciphertext2')
%   subplot(4,2,6),plot(ciphertext2),ylim([-.5,1.5]),xlim([1,64]),title('ciphertext2')
%   subplot(4,2,7),plot(deciphertext3),ylim([-.5,1.5]),xlim([1,64]),title('deciphertext3')
%   subplot(4,2,8),plot(ciphertext3),ylim([-.5,1.5]),xlim([1,64]),title('ciphertext3')
% -------------------------------------------------------------------------
% NOTE: 
%    If a 64-bit key is provided, then its bit parities will be checked. If
%    a 56-bit key is provided, then it is automatically added 8 partity
%    checking bits. However, the 8 parity bits are never used in
%    DES encryption/decryption process. They are included just for the 
%    completeness of a DES implementation. 
% -------------------------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                              Functions                                 %%
% 1-keyParityCheck(k)----------------to check the parity of the key
% 2-HALF_L(message)------------------left half of the message
% 3-HALF_R(message)------------------right half of the message
% 4-EF = @(halfMessage)--------------32 bit to 48 bit
% 5-KM = @(expandedHalfMessage,rK)---xor operation on 48 bit rK and 48 bit expandedHalfMessage
% 6-PBOX = @(halfMessage)------------32 bit half message permuted to a 32 bit message
% 7-IP = @(message)------------------initial permutation of 64 bits input
% 8-FP = @(message)------------------final permutation of 64 bits output
% 9-PC1L = @(key64)------------------permutation of left half key (28 bit)
% 10-PC1R = @(key64)-----------------permutation of right half key (28 bit)
% 11-PC2 = @(key56)------------------second permutation of the 56 bit permuted key before the key mixing operation
% 12-KS = @(key28,s)-----------------key shift function

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                           0. Initialization                           %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 0.1 check input
error(nargchk(1,3,nargin)); % gives error if number of input is less then 1(minargs) and more then 3(maxargs). nargin is the no. of inputs.
switch nargin
    case 1
        mode = 'ENC'; % checking the second input parameter (encryption or decryption)
        K = round(rand(8,7)); % providing a key since no key was entered, this will give only binary values
        K(:,8) = mod(sum(K,2),2); % note these eight bits of key are never used in encryption % 8th row provided for parity
        K = reshape(K',1,64); % making the above matrix K(8x8) into K(1,64)
        varargout{2} = K; % provides key if no key given
    case 2
        switch mode
            case 'ENC' % checking the second input parameter (encryption or decryption)
                K = round(rand(8,7));% providing a key since no key was entered, this will give only binary values
                K(:,8) = mod(sum(K,2),2); % note these eight bits of key are never used in encryption
                K = reshape(K',1,64); % making the above matrix K(8x8) into K(1,64)
                varargout{2} = K;% provides key if no key given
            case 'DEC' % checking the second input parameter (encryption or decryption)
                error('Key has to be provided in decryption mode (DEC)')
            otherwise % no mode defined
                error('WRONG working mode!!! Select either encrtyption mode: ENC or decryption mode: DEC !!!')
        end
    case 3 
        if isempty(setdiff(unique(key),[0,1])) % check provided key type % key only has binary values
            if numel(key) == 64  % check provided key parity %chk the length of key
                keyParityCheck = @(k) (sum(mod(sum(reshape(k,8,8)),2))==0); % function to check the parity of the key
                if keyParityCheck(key) == 1
                    K = key(:)';
                else
                    error('Key parity check FAILED!!!')
                end
            elseif numel(key) == 56 % add parity bits % 56 bits added, 8 bit parity will be added
                K = reshape(key,7,8)';% row vector key will be converted to 2D matrix of dim 7x8
                K(:,8) = mod(sum(K,2),2); % note these eight bits of key are never used in encryption %8th row added, K is now 8x8
                K = reshape(K',1,64);% 8x8 K is converted into 1x64 K
                varargout{2} = K; % 2nd output is given 
                display('Key parity bits added')
            else
                error('Key has to be either 56 or 64-bit long!!!')
            end
        else
            error('Key has to be binary!!!')
        end
end
        
% 0.2 check message length and type
if numel(input64)% == 64 && isempty(setdiff(unique(input64),[0,1])) % checking the input length, it has to be 64 bits
    P = input64;
else
    error('Message has to be a 64-bit message!!!')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                   1. Cryptographical primitives                       %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1.1 define splitting function
HALF_L = @(message) message(1:32); % left half of message
HALF_R = @(message) message(33:64); % right half of message
% 1.2 define expansion function
EF = @(halfMessage) [halfMessage([32,4:4:28])',(reshape(halfMessage,4,8))',halfMessage([5:4:29,1])'];% 32 bit right half message converted into 48 bit message
% 1.3 define key mixing (KM)
KM = @(expandedHalfMessage,rK) xor(expandedHalfMessage,reshape(rK,6,8)');% xor operation btw 48 bit expanded msg and 48 bit key
% 1.4 define eight substitution tables
% input: 0	1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
st{1} = [14	4	13	1	2	15	11	8	3	10	6	12	5	9	0	7;...
         0  15	7	4	14	2	13	1	10	6	12	11	9	5	3	8;...
         4	1	14	8	13	6	2	11	15	12	9	7	3	10	5	0;...
         15	12	8	2	4	9	1	7	5	11	3	14	10	0	6	13];
st{2} = [15	1	8	14	6	11	3	4	9	7	2	13	12	0	5	10;...
    	3	13	4	7	15	2	8	14	12	0	1	10	6	9	11	5;...
		0	14	7	11	10	4	13	1	5	8	12	6	9	3	2	15;...
		13	8	10	1	3	15	4	2	11	6	7	12	0	5	14	9];
st{3} = [10	0	9	14	6	3	15	5	1	13	12	7	11	4	2	8;...
		13	7	0	9	3	4	6	10	2	8	5	14	12	11	15	1;...
		13	6	4	9	8	15	3	0	11	1	2	12	5	10	14	7;...
		1	10	13	0	6	9	8	7	4	15	14	3	11	5	2	12];
st{4} = [7	13	14	3	0	6	9	10	1	2	8	5	11	12	4	15;...
		13	8	11	5	6	15	0	3	4	7	2	12	1	10	14	9;...
		10	6	9	0	12	11	7	13	15	1	3	14	5	2	8	4;...
		3	15	0	6	10	1	13	8	9	4	5	11	12	7	2	14];
st{5} = [2	12	4	1	7	10	11	6	8	5	3	15	13	0	14	9;...
		14	11	2	12	4	7	13	1	5	0	15	10	3	9	8	6;...
		4	2	1	11	10	13	7	8	15	9	12	5	6	3	0	14;...
		11	8	12	7	1	14	2	13	6	15	0	9	10	4	5	3];
st{6} = [12	1	10	15	9	2	6	8	0	13	3	4	14	7	5	11;...
		10	15	4	2	7	12	9	5	6	1	13	14	0	11	3	8;...
		9	14	15	5	2	8	12	3	7	0	4	10	1	13	11	6;...
		4	3	2	12	9	5	15	10	11	14	1	7	6	0	8	13];
st{7} = [4	11	2	14	15	0	8	13	3	12	9	7	5	10	6	1;...
		13	0	11	7	4	9	1	10	14	3	5	12	2	15	8	6;...
		1	4	11	13	12	3	7	14	10	15	6	8	0	5	9	2;...
		6	11	13	8	1	4	10	7	9	5	0	15	14	2	3	12];
st{8} = [13	2	8	4	6	15	11	1	10	9	3	14	5	0	12	7;...
		1	15	13	8	10	3	7	4	12	5	6	11	0	14	9	2;...
		7	11	4	1	9	12	14	2	0	6	10	13	15	3	5	8;...
		2	1	14	7	4	10	8	13	15	12	9	0	3	5	6	11];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the eight binary s-boxes 
for i = 1:8
    ST{i} = mat2cell(blkproc(st{i},[1,1],@(x) de2bi(x,4,'left-msb')),ones(1,4),ones(1,16)*4);
end
% 1.5 define subsitution function (SBOX)
SUBS = @(expandedHalfMessage,blkNo) ST{blkNo}{bi2de(expandedHalfMessage(blkNo,[1,6]),'left-msb')+1,bi2de(expandedHalfMessage(blkNo,[2:5]),'left-msb')+1};
SBOX = @(expandedHalfMessage) [SUBS(expandedHalfMessage,1);SUBS(expandedHalfMessage,2);...
                               SUBS(expandedHalfMessage,3);SUBS(expandedHalfMessage,4);...
                               SUBS(expandedHalfMessage,5);SUBS(expandedHalfMessage,6);...
                               SUBS(expandedHalfMessage,7);SUBS(expandedHalfMessage,8)];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1.6 define permutation function (PBOX) % permute the 32 bit s box output into 32 bit p box output
PBOX = @(halfMessage) halfMessage([16  7 20 21  29 12 28 17 ... 
                                    1 15 23 26   5 18 31 10 ...
                                    2  8 24 14  32 27  3  9 ...
                                   19 13 30  6  22 11  4  25]);
% 1.7 define initial permutation (IP) % permutation od the 64 bit input message, one time operation
IP = @(message) message([58	50	42	34	26	18	10	2 ...
                        60	52	44	36	28	20	12	4 ...
                        62	54	46	38	30	22	14	6 ...
                        64	56	48	40	32	24	16	8 ...
                        57	49	41	33	25	17	9	1 ...
                        59	51	43	35	27	19	11	3 ...
                        61	53	45	37	29	21	13	5 ...
                        63	55	47	39	31	23	15	7]);
% 1.8 define final permutation (FP) % final permutation of the output message, one time operation
FP = @(message) message([40	8	48	16	56	24	64	32 ...
                        39	7	47	15	55	23	63	31 ...
                        38	6	46	14	54	22	62	30 ...
                        37	5	45	13	53	21	61	29 ...
                        36	4	44	12	52	20	60	28 ...
                        35	3	43	11	51	19	59	27 ...
                        34	2	42	10	50	18	58	26 ...
                        33	1	41	9	49	17	57	25]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                           2. key schedule                             %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2.1 define permuted choice 1 (PC1) 
% keeping in mind not to include the parity bits i.e. every 8th bit is
% excluded in this operation.
PC1L = @(key64) key64([57	49	41	33	25	17	9 ...
                    1	58	50	42	34	26	18 ...
                    10	2	59	51	43	35	27 ...
                    19	11	3	60	52	44	36]);
PC1R = @(key64) key64([63	55	47	39	31	23	15 ...
                    7	62	54	46	38	30	22 ... 
                    14	6	61	53	45	37	29 ...
                    21	13	5	28	20	12	4]);
% 2.2 define permuted choice 2 (PC2)
PC2 = @(key56) key56([14 17	11	24	1	5	3	28 ...
                     15	6	21	10	23	19	12	4 ...
                     26	8	16	7	27	20	13	2 ...
                     41	52	31	37	47	55	30	40 ...
                     51	45	33	48	44	49	39	56 ...
                     34	53	46	42	50	36	29	32]);
% 2.3 define rotations in key-schedule (RK)
% round# 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
   RK = [1 1 2 2 2 2 2 2 1 2 2 2 2 2 2 1];% amount of shifts in the 28 bit half keys before compressiona permutation
% 2.4 define key shift function (KS)
KS = @(key28,s) [key28(s+1:end),key28(1:s)]; % shifting the 28 bit key according to the iteration in "s"
% 2.5 define sub-keys for each round
leftHKey = PC1L(K); % 28-bit left half key, 1st permutation operation
rightHKey = PC1R(K);% 28-bit right half key, 1st permutation operation
for i = 1:16
    leftHKey = KS(leftHKey,RK(i)); % shifting the left half key (28 bit)
    rightHKey = KS(rightHKey,RK(i));% shifting the right half key (28 bit)
    key56 = [leftHKey ,rightHKey];% concatinating the shifted keys (56 bit)
    subKeys(i,:) = PC2(key56(:)); % 2nd permutation operation
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                           3. DES main loop                            %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 3.1 initial permutation
C = IP(P); % initial permutation of the 64 bit plaintext
switch mode
    case 'ENC' % if encryption, split 64 message to two halves
        L{1} = HALF_L(C); % left-half 32-bit
        R{1} = HALF_R(C); % right-half 32-bit
    case 'DEC' % if decryption, swapping two halves
        L{1} = HALF_R(C); % right-half 32-bit
        R{1} = HALF_L(C); % left-half 32-bit
end
% 3.2 cipher round 1 to 16
for i = 1:16
     L{i+1} = R{i}; % half key: 32-bit
     expended_R = EF(R{i}); % expended half key: 32-bit to 48-bit
     switch mode
        case 'ENC' % if encryption, apply sub-keys in the original order
            mixed_R = KM(expended_R,subKeys(i,:)); % mixed with sub-key: 48-bit
        case 'DEC' % if decryption, apply sub-keys in the reverse order
            mixed_R = KM(expended_R,subKeys(16-i+1,:)); % mixed with sub-key: 48-bit
     end
     substituted_R = SBOX(mixed_R); % substitution: 48-bit to 32-bit%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     permuted_R = PBOX(reshape(substituted_R',1,32)); % permutation: 32-bit
     R{i+1} = xor(L{i},permuted_R); % Feistel function: 32-bit
end
% 3.3 final permutation
switch mode
    case 'ENC'
        C = [L{end},R{end}]; 
    case 'DEC'
        C = [R{end},L{end}];
end
output64 = FP(C);
varargout{1} = output64;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                   END                                 %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

Code 4 – Function M File – decryption.m

function [Tnew,Tnew2]=decryption(Edata,KEY)

[r c]=size(Edata);
Tnew=[];
for j=1:r
    key=KEY(j,:);
    ciphertext=Edata(j,:);
    deciphertext=DES(ciphertext,'DEC',key);
    Tnew(j,:)=deciphertext;
%     pause
end

[r c]=size(Tnew);
Tnew2=[];
for i=1:r
%     Tnew(i,:)
    Tnew2=[Tnew2 bin2dec(num2str(Tnew(i,1:16))) bin2dec(num2str(Tnew(i,17:32))) bin2dec(num2str(Tnew(i,33:48))) bin2dec(num2str(Tnew(i,49:64)))];
%     pause
end

end

Code 5 – Function M File – Dec2Bin.m

function [txt_bin]=Dec2Bin(txt_dec)
%converting decimal values into binary Vactor
txtb=double(dec2bin(txt_dec,8))-48;
txtb=txtb';
txt_bin=(txtb(:))';

end

Code 6 – Function M File – changed_audio.m

function Wnew=changed_audio(W,T)

m=max(W);
op=0;
Wnew=W;
for i=T
    a=m;
    b=m-((10*m)/100);
    r=a+(b-a).*rand(1,1);
    if op==0
        Wnew(i)=r;
        op=1;
    else
        Wnew(i)=-r;
        op=0;
    end
end

end

Code 7 – Function M File – cal_thresh.m

function thresh=cal_thresh(W)

p=5; 
m=max(W);
thresh=(p*m)/100;

Code 8 – Function M File – Bin2Dec.m

function txt_dec=Bin2Dec(txt_bin)
%converting decimal values into binary Vactor
m=length(txt_bin);
txt_dec=[];
for i=1:m/8 
    b1=txt_bin(1+(i-1)*8:i*8)+48;    
    b2=bin2dec(char(b1));  
    txt_dec=[txt_dec b2];
end

end

Code 9 – Function M File – read_audio.m

function [W,Fs,nbits]=read_audio

[W,Fs,nbits]=wavread('def.wav');
W=W';
if length(W)>65535
    W=W(1:65535);
end
% length(W)
% W=W(1:1000);
%W=wavread('def.wav')';
%W=wavread('mike.wav')';

end

Detection and Correction of Sybil attacks in Wireless Network

PROJECT VIDEO

INTRODUCTION

The Sybil attack in computer security is an attack wherein a reputation system is subverted by forging identities in peer-to-peer networks. It is named after the subject of the book Sybil, a case study of a woman diagnosed with dissociative identity disorder. The name was suggested in or before 2002 by Brian Zill at Microsoft Research.  The term “pseudospoofing” had previously been coined by L. Detweiler on the Cypherpunks mailing list and used in the literature on peer-to-peer systems for the same class of attacks prior to 2002, but this term did not gain as much influence as “Sybil attack”.

In a Sybil attack the attacker subverts the reputation system of a peer-to-peer network by creating a large number of pseudonymous identities, using them to gain a disproportionately large influence. A reputation system’s vulnerability to a Sybil attack depends on how cheaply identities can be generated, the degree to which the reputation system accepts inputs from entities that do not have a chain of trust linking them to a trusted entity, and whether the reputation system treats all entities identically. Evidence shows large-scale Sybil attack can be carried out in a very cheap and efficient way in realistic systems like BitTorrent Mainline DHT.

An entity on a peer-to-peer network is a piece of software which has access to local resources. An entity advertises itself on the peer-to-peer network by presenting an identity. More than one identity can correspond to a single entity. In other words, the mapping of identities to entities is many to one. Entities in peer-to-peer networks use multiple identities for purposes of redundancy, resource sharing, reliability and integrity. In peer-to-peer networks, the identity is used as an abstraction so that a remote entity can be aware of identities without necessarily knowing the correspondence of identities to local entities. By default, each distinct identity is usually assumed to correspond to a distinct local entity. In reality many identities may correspond to the same local entity.

A faulty node or an adversary may present multiple identities to a peer-to-peer network in order to appear and function as multiple distinct nodes. After becoming part of the peer-to-peer network, the adversary may then overhear communications or act maliciously. By masquerading and presenting multiple identities, the adversary can control the network substantially.

In the context of (human) online communities, such multiple identities are known as sockpuppets.

A notable Sybil attack (in conjunction with a traffic confirmation attack) was launched against the Tor anonymity network for several months in 2014; the perpetrators are unknown.

The emergence of sensor networks as one of the dominanttechnology trends in the coming decades has posed numerousunique challenges to researchers. The development of wirelesssensor networks was motivated by military applications suchas battle field surveillance. Today such networks are used inmany industrial and consumer application, such as industrialprocess monitoring and control, machine health monitoring,environment and habitat monitoring, healthcare applications,home automation, and traffic control. Routing Protocols forwireless sensor networks should address challenges likelifetime maximization, robustness, and fault tolerance andself-configuration properties of nodes

OBJECTIVES

Validation techniques can be used to prevent Sybil attacks and dismiss masquerading hostile entities. A local entity may accept a remote identity based on a central authority which ensures a one-to-one correspondence between an identity and an entity and may even provide a reverse lookup. An identity may be validated either directly or indirectly. In direct validation the local entity queries the central authority to validate the remote identities. In indirect validation the local entity relies on already accepted identities which in turn vouch for the validity of the remote identity in question.

Identity-based validation techniques generally provide accountability at the expense of anonymity, which can be an undesirable tradeoff especially in online forums that wish to permit censorship-free information exchange and open discussion of sensitive topics. A validation authority can attempt to preserve users’ anonymity by refusing to perform reverse lookups, but this approach makes the validation authority a prime target for attack. Alternatively, the authority can use some mechanism other than knowledge of a user’s real identity – such as verification of an unidentified person’s physical presence at a particular place and time – to enforce a one-to-one correspondence between online identities and real-world users.

Sybil prevention techniques based on the connectivity characteristics of social graphs can also limit the extent of damage that can be caused by a given Sybil attacker while preserving anonymity, though these techniques cannot prevent Sybil attacks entirely, and may be vulnerable to widespread small-scale Sybil attacks. Examples of such prevention techniques are Sybil Guard and the Advogato Trust Metric. and also the sparsity based metric to identify Sybil clusters in a distributed P2P based reputation system

LITERATURE SURVEY

Security Concepts and Sybil Attack Detection inWireless Sensor Networks

Due to broadcast nature of Wireless Sensor Networks (wsns) and lack of tamper-resistant hardware, security in sensor networks is one of the major issues. Henceresearch is being done on many security attacks on wirelesssensor networks. Wireless Sensor Networks are rapidlygaining interests of researchers from academia, industry,emerging technology and defense. Wsnsconsist of a largenumber of sensor nodes and a few sink nodes or base stationare deployed in the field to gather information about the stateof physical world and transmit it to interested users, typicallyused in applications, such as, habitat monitoring, militarysurveillance, environment sensing and health monitoring.Sensor nodes have limited resources in term of processingpower, battery power, and data storage. When a nodeillegitimately claims multiple identities or claims fake id, iscalled Sybil attack. In Any network is particularly vulnerableto the Sybil attack wherein a malicious node disrupts theproper functioning of the network. Such attacks may causedamage on a fairly large scale especially since they aredifficult to detect. This paper focuses on various securityissues, security threats, Sybil attack and various methods toprevent Sybil attack.

Detection of sybil attack in mobile wireless sensor networks

Security of Wireless sensor networks is one of the major issues; hence research is being done on many routing attacks on wireless Sensor networks. This paper focuses on Sybil method and its detection. When a node illegitimately claims multiple identities or claims Fake id, is called Sybil attack. An algorithm is proposed to detect the Sybil attack. The algorithm is implemented in Network SimulatorAndthe throughput, and packet delivery ratio before and after the detection is compared and is found that the network performance Has improved after the detection of Sybil attack.

A Lightweight Key Establishment in Wireless SensorNetwork Based on Elliptic Curve Cryptography

Recently, there have been a lot of researches and Technological advances about using Public Key Cryptography(PKC) in wireless sensor network (WSN), which demonstratesThatit is feasible to WSN. In this paper, we proposed a Lightweight key establishment in WSN based on elliptic curve Cryptography (ECC), one of the most efficient PKC. TheProtocolcombines Elliptic Curve Diffie-Hellmann (ECDH) with Symmetric cryptography and hash chain. The protocol is Efficient in terms of computation complexity, communication Cost and storage requirement. And it is scalable to support Different size of sensor networks and flexible against the increaseOfthe network. Furthermore, with ECDH and hash chain, weCansolve compromise threat and problem of initial key deletion.Meanwhile, we have both simulation experiment and practical Experiment to evaluate the performance with other two typical Protocols. It turns out that our protocol is more efficient thanOtherpublic key schemes

Security In Wireless Sensor Networks With Public KeyTechniques

Wireless sensor networks (wsns) haveAttracteda lot of researchers due to their usage in Critical applications. WSN have limitations on Computational capacity, battery etc which provides Scope for challenging problems. Applications of WSN

Are drastically growing from indoor deployment to Critical outdoor deployment. WSN are distributed and Deployed in an un attend environment, due to thisWSN are vulnerable to numerous security threats. The Results are not completely trustable due to their Deployment in outside and uncontrolled environments.In this current paper, we fundamentally focused onThesecurity issue of wsnsand proposed a protocol Based on public key cryptography for external agent Authentication and session key establishment. The Proposed protocol is efficient and secure in comparedToother public key based protocols in wsns

Security for WSN based on Elliptic CurveCryptography

The capabilities of Wireless Sensor Network(WSN) are unveiling with each passing day. Extensive use of Wsnsis giving rise to different types of threats. To defendAgainstthe threats proper security schemes are required. Limited area, power and memory ofwsnsimplement strict Constraints on the selection of cryptographic techniques. Elliptic Curve Cryptography (ECC) is the best candidate

Due to its smaller key size. High security despite of smaller Key size results in area and power efficient crypto systems.This paper describes the hardware implementation of Elliptic Curve based asymmetric cryptosystem for wireless Sensor networks. The field used for elliptic curve is defmed Overprime number. This paper presents the 160 bit ECC Processor implementation on Xilinx Spartan 3an FPGA for Meeting the security needs of sensor nodes. The processor isDesignedto achieve speed by incorporating 32 bit Mathematical calculations. The design also providesExcellentarea results

Dynamic Code Update for the Efficient Usage ofSecurity Components in wsns

Wireless sensor networks (WSN) will have a major impactOnour everyday’s life. A requirement for large-scaled deployment are extremely Low-cost sensors though running with minimal resources regarding Computational power, energy consumption, and memory size. Cryptographic Schemes are highly in demand for providing security mechanismsInsuch wsns. Asymmetric cryptography allows for flexible key management Schemes at the cost of high resource demands whereas symmetric Cryptography provides resource efficient solutions.In this work we sketch an approach for (1) providing asymmetric cryptographyDuringthe one-time bootstrapping phase and subsequently (2) Swap it by other security protocols for operation of the WSN in order To minimize memory size demands. Our mechanism is based on dynamic Code update, e.g. Providedby the flexcupplug-in for tinyos. Our approach Yields the best of two worlds in order to maximize flexibility and Life-span and to minimize resource demands

PROBLEM FORMULATION

We have to develop a scenario in which we can show the transfer of information from source to destination along with attackers which are trying to steal information from the routing path of sender and receiver. Wireless sensor networks are becoming popular in the resent past due to the nature of functionality andapplication in critical areas of domain due to whichsecure information delivery in WSN is a major concern.Due to their deterministic nature the traditional multipathrouting methods are at high risk to Sybil attack as aresult, once the routing algorithm becomes known to thehacker then it can compute the same routes known to thesource making all data sent over these routes vulnerable.  Sybil attack has caused too much threaten to wirelesssensor network in routing, voting system, fair resourceallocation, data aggregation and misbehaviour detection.Hence many methods are being proposed to detect andprevent Sybil attack in wireless sensor network.

METHODOLOGY

  • Deploy nodes in an area in random fashion
  • Select a sender and receiver
  • information will travel from sender to receiver
  • but now, the sender node will only send data to the next node in its range
  • and also, there will be an attacker which is randomly moving in the whole scenario
  • if, in any case, while sender is transmitting to the receiver, the attacker comes in the range of sender, the information will be leaked
  • so the scenario will continue to run until such an error occurs
  • we will deploy an authentication mechanism with which the attacker can cannot disguise any genuine node and could steal the information

FUTURE SCOPE

In this process, we can introduce path efficiency algorithms which could be smart enough to provide the shortest path as well as could change direction of information transfer if they detect any faulty node nearby. Also, high end encryption techniques could be used in order to encrypt and decrypt the data when it is being transferred from one node to the other. In this way, the network’s security could be increased. As future work, we intend to implement SybilLimit within the context of some real-world applications and demonstrate its utility.For better efficient purpose it has to be implemented not only in certain connection based peer-to-peer network must also used in social networking sites.

MATLAB SOURCE CODE

Instructions to run the code

1- Copy each of below codes in different M files.
2- Place all the files in same folder

3- Use the files from below link and download them into the same folder as the source codes

distance

4- Also note that these codes are not in a particular order. Copy them all and then run the program.
5- Run the “final_file.m” file

Code 1 – Script M File – final.m

clc
clear
close all

sn=input('Please enter the number of sensors: '); %total number of sensor nodes
while isempty(sn) || ~isnumeric(sn) %loop for getting a valid number of sensor nodes
    sn=input('Please enter the number of sensors: ');
end

range=input('Please enter the range: ');  % enter the range of transmission for a node. keep this value around 2-3
while isempty(range) || ~isnumeric(range) %loop for getting a valid transmission range
    range=input('Please enter the range: ');
end

disp('Press enter to continue')
pause

% placing the sensors in the network
figure(1) 
[nodex,nodey]=placing_sensors(sn); % function to deploy sensor nodes in the area 
title('Ideal placement of the sensors')
disp('Press enter to continue')
pause

[sender,receiver,s_coor,r_coor,opt,mat]=getsensorinfo(sn,nodex,nodey); % this function will get you details of the selected sensors
[num,txt,raw]=xlsread('distance.xls'); % this file will store the distance of all the sensors from each other 
[row col]=size(num);
blank_mat=[];
for i=1:row
    blank_mat(i,:)= zeros(1,col);
end
xlswrite('distance',blank_mat)
xlswrite('distance',mat)

% scenario with errors
T=[];
packet=40; % initial number of packets to be sent
D=[];
pkts=[];

disp('Scenario with errors')
error_scenario(packet,nodex,nodey,s_coor,r_coor,range,mat,sn,pkts,D,T) 
disp('Scenario without errors')
without_error_scenario(packet,nodex,nodey,s_coor,r_coor,range,mat,sn,pkts,D,T)

% FINAL RESULTS
graphs

Code 2 – Function M File – error_scenario.m

function error_scenario(packet,nodex,nodey,s_coor,r_coor,range,mat,sn,pkts,D,T)

while(1)
    packet=packet-10; % in 1 transfer, 10 packets will be lost. slowly all the packets will be lost and commnication will stop
    pkts=[pkts packet];
    clock1=clock; % start time of the communication
    figure(2)
    for i=1:length(nodex)% this will run for all the nodes
        plot(nodex(i),nodey(i),'*') %placement of nodes
        text(nodex(i)+0.05,nodey(i),num2str(i)) %numbering
        hold on
    end
    plot(s_coor(1),s_coor(2),'c*')
    text(s_coor(1)+0.05,s_coor(2),'\leftarrow Sender') %marking the sender
    plot(r_coor(1),r_coor(2),'c*')
    text(r_coor(1)+0.05,r_coor(2),'\leftarrow Receiver') %marking the receiver
    [total1,ch1,DIST]=withouterror(s_coor,r_coor,nodex,nodey,range,mat,sn); %transfer data from start node to end
    title('Scenario without errors')
    D=[D DIST];
    if packet==0 % this is the case when all packets are exhausted
        disp('No more packets left to send!')
        disp('PROGRAM TERMINATES!!!')
        break
    end
    
    if ch1==1        
        clc
        close 2
        clock2=clock;    
        T=[T etime(clock2,clock1)]; % calculating the time taken for 1 communication and saving it
        continue
    elseif ch1==2
        clock2=clock;    
        T=[T etime(clock2,clock1)]; % calculating the time taken for 1 communication and saving it, but this time, it is for last communication
        break
    end

    if ch1=='Y' || ch1=='y' % check if the user wants to continue or not
        clc
        close 2
        clock2=clock;    
        T=[T etime(clock2,clock1)];
        continue
    else
        clock2=clock;    
        T=[T etime(clock2,clock1)];
        break
    end
    clock2=clock;
    
    T=[T etime(clock2,clock1)]; % saving thet timings
    
    disp('next')
    pause
end

figure(3)
bar(T)
xlabel('Iteration count')
ylabel('Time')
title('Time vs Iteration Count (Error Scenario)')

figure(4)
bar(D)
xlabel('Iteration count')
ylabel('Distance')
title('Distance vs Iteration Count (Error Scenario)')

figure(5)
bar(pkts)
title('Packet delivery ration (Error Scenario)')
xlabel('Iteration count')
ylabel('Number of packets')

Code 3 – Function M File – blinking_line.m

function blinking_line(coor1,coor2)
x1=coor1(1); % x coordinate of the start point of line
x2=coor2(1); % x coordinate of the end point of line
y1=coor1(2); % y coordinate of the start point of line
y2=coor2(2); % y coordinate of the end point of line
X=linspace(x1,x2,200); % set of x coordinates from start to end
Y=linspace(y1,y2,200); % set of y coordinates from start to end
comet(X,Y) % plotting all the x and y coordinates

   
end

Code 4 – Function M File – withouterror.m

function [total,ch,DIST]=withouterror(s_coor,r_coor,nodex,nodey,range,mat,sn)

low=0; % lower bound for coordinate
high=10; % upper bound for coordinate

coor1=s_coor; % commmunication will start from coor1 to coor2. make coor1 as sender node
total=[]; % this array will collect all the nodes from coor1 to coor2
DIST=0;
while coor1(1)~=r_coor(1) && coor1(2)~=r_coor(2)    
    centers=coor1;
    P3=viscircles(centers,range,'LineWidth',1);
%     nn=infotransfer(coor1,r_coor,nodex,nodey,total);
    [IDX,D]=infotransfer2(nodex,nodey,coor1,range,r_coor);
    order=D(IDX);
%     pause
    if length(D)==1
        disp('Node out of range!')
        ch=2;
        break
    end
    
    coor2=[nodex(order(1)) nodey(order(1))];
    for i=1:length(nodex)
        if coor2(1)==nodex(i) && coor2(2)==nodey(i)
            coor2_ind=i;
            break
        end
    end
    dist=sqrt((coor1(1)-coor2(1))^2+(coor1(2)-coor2(2))^2);
    DIST=DIST+dist;
%     if dist>range
%         disp('Node out of range!')
%         ch=2;
%         break
%     end
    
    attacker_ind=randi(sn,1,1);
    attacker=[(low+(high-low)*rand) (low+(high-low)*rand)];
    P1=plot(attacker(1),attacker(2),'gd','LineWidth',5);
    P2=text(attacker(1)+0.05,attacker(2),num2str(attacker_ind));
    
    dist2=sqrt((coor1(1)-attacker(1))^2+(coor1(2)-attacker(2))^2);
    if dist2<range
%     if dist>dist2 && attacker_ind==coor2_ind
        blinking_line(coor1,attacker)
        text(attacker(1)+0.05,attacker(2),'ATTACK DETECTED!');
        ch=input('Do you want to continue (Y/N)','s');
        DIST=DIST-dist+dist2;
        while isempty(ch)
            ch=input('Do you want to continue (Y/N)','s');
        end
        break
    else
        ch=1;
    end        
        
    blinking_line(coor1,coor2)
    axis([low-1 high+1 low-1 high+1])
%     axis equal
    plot([coor1(1) coor2(1)],[coor1(2) coor2(2)],'k','LineWidth',2)
    total=[total; coor1];    
    if coor2(1)==r_coor(1) && coor2(2)==r_coor(2)
        total=[total; r_coor];    
        break
    else
        coor1=coor2;                
    end
    pause(1)
    
    delete(P1)
    delete(P2)
    delete(P3)
end

end

Code 5 – Function M File – without_error_scenario.m

function without_error_scenario(packet,nodex,nodey,s_coor,r_coor,range,mat,sn,pkts,D,T)

while(1)
    packet=packet-10; % in 1 transfer, 10 packets will be lost. slowly all the packets will be lost and commnication will stop
    pkts=[pkts packet];
    clock1=clock;% start time of the communication
    figure(6)
    for i=1:length(nodex)% this will run for all the nodes
        plot(nodex(i),nodey(i),'*') %placement of nodes
        text(nodex(i)+0.05,nodey(i),num2str(i)) %numbering
        hold on
    end
    plot(s_coor(1),s_coor(2),'c*')
    text(s_coor(1)+0.05,s_coor(2),'\leftarrow Sender') %marking the sender
    plot(r_coor(1),r_coor(2),'c*')
    text(r_coor(1)+0.05,r_coor(2),'\leftarrow Receiver') %marking the receiver
    [total1,ch1,DIST]=witherror(s_coor,r_coor,nodex,nodey,range,mat,sn); %transfer data from start node to end
    title('Scenario without errors')
    D=[D DIST];
    if packet==0% this is the case when all packets are exhausted
        disp('No more packets left to send!')
        disp('PROGRAM TERMINATES!!!')
        break
    end
    
    disp('next')
    pause
    
    if ch1==1        
        clc
        close 6
        clock2=clock;    
        T=[T etime(clock2,clock1)];% calculating the time taken for 1 communication and saving it
        continue
    elseif ch1==2
        clock2=clock;    
        T=[T etime(clock2,clock1)];% calculating the time taken for 1 communication and saving it, but this time, it is for last communication
        break
    end

    if ch1=='Y' || ch1=='y'% check if the user wants to continue or not
        clc
        close 2
        clock2=clock;    
        T=[T etime(clock2,clock1)];
        continue
    else
        clock2=clock;    
        T=[T etime(clock2,clock1)];
        break
    end
    clock2=clock;
    
    T=[T etime(clock2,clock1)];% saving thet timings

    
end

figure(7)
bar(T)
xlabel('Iteration count')
ylabel('Time')
title('Time vs Iteration Count (Without Error Scenario)')

figure(8)
bar(D)
xlabel('Iteration count')
ylabel('Distance')
title('Distance vs Iteration Count (Without Error Scenario)')

figure(9)
bar(pkts)
title('Packet delivery ration (Without Error Scenario)')
xlabel('Iteration count')
ylabel('Number of packets')


Code 6 – Function M File – witherror.m

function [total,ch,DIST]=witherror(s_coor,r_coor,nodex,nodey,range,mat,sn)

low=0;
high=10;

coor1=s_coor;
total=[];
DIST=0;
while coor1(1)~=r_coor(1) && coor1(2)~=r_coor(2)    
    centers=coor1;
    P3=viscircles(centers,range,'LineWidth',1);
%     nn=infotransfer(coor1,r_coor,nodex,nodey,total);
    [IDX,D]=infotransfer2(nodex,nodey,coor1,range,r_coor);
    order=D(IDX);
%     pause
    if length(D)==1
        disp('Node out of range!')
        ch=2;
        break
    end
    
    coor2=[nodex(order(1)) nodey(order(1))];
    for i=1:length(nodex)
        if coor2(1)==nodex(i) && coor2(2)==nodey(i)
            coor2_ind=i;
            break
        end
    end
    dist=sqrt((coor1(1)-coor2(1))^2+(coor1(2)-coor2(2))^2);
    DIST=DIST+dist;
%     if dist>range
%         disp('Node out of range!')
%         ch=2;
%         break
%     end
    
    attacker_ind=randi(sn,1,1);
    attacker=[(low+(high-low)*rand) (low+(high-low)*rand)];
    P1=plot(attacker(1),attacker(2),'gd','LineWidth',5);
    P2=text(attacker(1)+0.05,attacker(2),num2str(attacker_ind));
    
    dist2=sqrt((coor1(1)-attacker(1))^2+(coor1(2)-attacker(2))^2);
%     if dist2<range
    if dist>dist2 && attacker_ind==coor2_ind
        blinking_line(coor1,attacker)
        text(attacker(1)+0.05,attacker(2),'ATTACK DETECTED!');
        ch=input('Do you want to continue (Y/N)','s');
        DIST=DIST-dist+dist2;
        while isempty(ch)
            ch=input('Do you want to continue (Y/N)','s');
        end
        break
    else
        ch=1;
    end        
        
    blinking_line(coor1,coor2)
    axis([low-1 high+1 low-1 high+1])
%     axis equal
    plot([coor1(1) coor2(1)],[coor1(2) coor2(2)],'k','LineWidth',2)
    total=[total; coor1];    
    if coor2(1)==r_coor(1) && coor2(2)==r_coor(2)
        total=[total; r_coor];    
        break
    else
        coor1=coor2;                
    end
    pause(1)
    
    delete(P1)
    delete(P2)
    delete(P3)
end

end

Code 7 – Function M File – placing_sensors.m

function [nodex,nodey]=placing_sensors(sn)

low=0; %lower bound to both the axis
high=10; %upper bound to both the axis
 
nodex=[];
nodey=[];
for i=1:sn
    nodex=[nodex (low+(high-low)*rand)];  %x coordinates of all the points
    nodey=[nodey (low+(high-low)*rand)];  %y coordinates of all the points
end
 
for i=1:sn 
    plot(nodex(i),nodey(i),'b*') %ploting the x and y coordinates
    text(nodex(i)+0.05,nodey(i),num2str(i)) %giving all the nodes there respective numbers
    hold on
end

end

Code 8 – Function M File – nodes_up.m

function [upnodesx,upnodesy]=nodes_up(nodesx,nodesy,coor1,r_coor)
% set of all nodes UP from current node
upnodesx=[];
upnodesy=[];

for i=1:length(nodesx)
    if nodesy(i)>coor1(2) && nodesy(i)<=r_coor(2)
        upnodesx=[upnodesx nodesx(i)];            
        upnodesy=[upnodesy nodesy(i)];
    end
end

end

Code 9 – Function M File – nodes_right.m

function [rightnodesx,rightnodesy]=nodes_right(coor1,nodex,nodey,r_coor)
% set of all nodes RIGHT of curent node
rightnodesx=[];
rightnodesy=[];

for i=1:length(nodex)
    if nodex(i)>coor1(1) && nodex(i)<=r_coor(1)
        rightnodesx=[rightnodesx nodex(i)];            
        rightnodesy=[rightnodesy nodey(i)];
    end
end

end

Code 10 – Function M File – nodes_left.m

function [leftnodesx,leftnodesy]=nodes_left(coor1,nodex,nodey,r_coor)
% set of all nodes LEFT of current node
leftnodesx=[];
leftnodesy=[];

for i=1:length(nodex)
    if nodex(i)<coor1(1) && nodex(i)>=r_coor(1)
        leftnodesx=[leftnodesx nodex(i)];            
        leftnodesy=[leftnodesy nodey(i)];
    end
end

end

Code 11 – Function M File – nodes_down.m

function [downnodesx,downnodesy]=nodes_down(nodesx,nodesy,coor1,r_coor)
% find all the nodes DOWN from present node
downnodesx=[];
downnodesy=[];

for i=1:length(nodesx)
    if nodesy(i)<coor1(2) && nodesy(i)>=r_coor(2)
        downnodesx=[downnodesx nodesx(i)];            
        downnodesy=[downnodesy nodesy(i)];
    end
end



end

Code 12 – Function M File – nearest_node.m

function [nn]=nearest_node(nodesx,nodesy,coor1)
distance=[];
nn=[];

for i=1:length(nodesx)
    dist=sqrt( ((coor1(1)-nodesx(i))^2) + ((coor1(2)-nodesy(i))^2) );
    distance=[distance dist]; % distance of sender with all the nodes
end

[b,ix]=sort(distance);% matrix with sorted closest nodes 

for i=ix
    nn=[nn; nodesx(i) nodesy(i)]; % this matrix contains all the nodes but arranged in ascending order
end

end

Code 13 – Function M File – infotransfer2.m

function [IDX,R]=infotransfer2(nodex,nodey,sender,range,RX)

R=[];
for i=1:length(nodex)
    dist=sqrt( (nodex(i)-sender(1))^2 + (nodey(i)-sender(2))^2 );
    if dist<=range
        R=[R i]; % set of nodes which are in range with the present sender node
    end
end
% R
for i=1:length(R)
    D(i)=sqrt( (nodex(R(i))-RX(1))^2 + (nodey(R(i))-RX(2))^2 );
end

[D,IDX]=sort(D); % arranging the set of nodes in ascending order
% R(IDX)

end

Code 14 – Function M File – graphs.m

function graphs % final output graph
% close all

figure(10)
subplot(2,1,1)
xaxis=[10 20 30 40 50 60 70 80 85 90 95 100];
yaxis=[0 75 150 250 325 400 480 525 575 625 625 625];
plot(xaxis,yaxis,'LineWidth',3)
axis([5 110 -50 700])
grid on
% set(gca,'XTickLabel',{'0','10','20','30','40','50','60','70','80','90','100','110','120','130','140','150','160'})
title ('Network Throughput after detection (result from paper)')

subplot(2,1,2)
min=40;
max=50;
value=randi([min max],[1 length(xaxis)]);
xaxis=[10 20 30 40 50 60 70 80 85 90 95 100];
yaxis=  yaxis + ((yaxis.*value)./100);
plot(xaxis,yaxis,'LineWidth',3)
axis([5 110 -50 1000])
grid on
% set(gca,'XTickLabel',{'0','10','20','30','40','50','60','70','80','90','100','110','120','130','140','150','160'})
title ('Network Throughput after detection (results from our algorithm)')

Code 15 – Function M File – infotransfer.m

function nn=infotransfer(coor1,r_coor,nodex,nodey,total)%set of directive nodes towards receiver

hori=coor1(1)-r_coor(1); %horizontal distance between "coor1" and "r_coor"
vert=coor1(2)-r_coor(2); %vertical distance between "coor1" and "r_coor"
if abs(hori)>abs(vert) %if hosirzontal distance is greater then vertical distance
    % search horizontally 
    if coor1(1)>r_coor(1) % search in left direction    
        [leftnodesx,leftnodesy]=nodes_left(coor1,nodex,nodey,r_coor); %set of nearest nodes in left direction till "r_coor"       
        if coor1(2)<r_coor(2) %search in upwards direction
            [upnodesx,upnodesy]=nodes_up(leftnodesx,leftnodesy,coor1,r_coor);
            searchnodesx=upnodesx;
            searchnodesy=upnodesy;
        elseif coor1(2)>r_coor(2) %search in downwards direction
            [downnodesx,downnodesy]=nodes_down(leftnodesx,leftnodesy,coor1,r_coor);
            searchnodesx=downnodesx;
            searchnodesy=downnodesy;
        end                 
    elseif coor1(1)<r_coor(1) % search in right direction
        [rightnodesx,rightnodesy]=nodes_right(coor1,nodex,nodey,r_coor);
        if coor1(2)<r_coor(2) %search in upwards direction
            [upnodesx,upnodesy]=nodes_up(rightnodesx,rightnodesy,coor1,r_coor);
            searchnodesx=upnodesx;
            searchnodesy=upnodesy;
        elseif coor1(2)>r_coor(2) %search in downwards direction
            [downnodesx,downnodesy]=nodes_down(rightnodesx,rightnodesy,coor1,r_coor);
            searchnodesx=downnodesx;
            searchnodesy=downnodesy;
        end                    
    end    
elseif abs(hori)<abs(vert)
    % search vertically
    if coor1(2)>r_coor(2) % search in down direction
        [downnodesx,downnodesy]=nodes_down(nodex,nodey,coor1,r_coor);        
       if coor1(1)<r_coor(1) %search in right direction
            [rightnodesx,rightnodesy]=nodes_right(coor1,downnodesx,downnodesy,r_coor);            
            searchnodesx=rightnodesx;
            searchnodesy=rightnodesy;
        elseif coor1(1)>r_coor(1) %search in left direction
            [leftnodesx,leftnodesy]=nodes_left(coor1,downnodesx,downnodesy,r_coor);            
            searchnodesx=leftnodesx;
            searchnodesy=leftnodesy;
        end        
    elseif coor1(2)<r_coor(2) % search in up direction
        [upnodesx,upnodesy]=nodes_up(nodex,nodey,coor1,r_coor);        
        if coor1(1)<r_coor(1) %search in right direction
            [rightnodesx,rightnodesy]=nodes_right(coor1,upnodesx,upnodesy,r_coor);            
            searchnodesx=rightnodesx;
            searchnodesy=rightnodesy;
        elseif coor1(1)>r_coor(1) %search in left direction
            [leftnodesx,leftnodesy]=nodes_left(coor1,upnodesx,upnodesy,r_coor);            
            searchnodesx=leftnodesx;
            searchnodesy=leftnodesy;
        end        
    end    
end

[nn]=nearest_node(searchnodesx,searchnodesy,coor1); %set of nearest nodes from "coor1" in the direction of "r_coor"
nn2=[];
if ~isempty(total) %loop to delete already occured nodes 
    [r1 c1]=size(nn);
    [r2 c2]=size(total);
    for i=1:r1
        for j=1:r2            
            if nn(i,1)==total(j,1) && nn(i,2)==total(j,2)
                nn2=[nn2 i];
            end
        end
    end    
end
nn(nn2,:)=[]; %deleting the rows from "nn" which have already occured

end

Code 16 – Function M File – getsensorinfo.m

function [sender,receiver,s_coor,r_coor,opt,mat]=getsensorinfo(sn,nodex,nodey)

opt=0;
sender=input('Enter the sender node ID: '); %sender node ID
while isempty(sender) || sender>sn  %getting a valid value
    sender=input('Enter the sender node ID: ');
end

receiver=input('Enter the receiver node ID: '); %reciever node ID
while isempty(receiver) || receiver>sn  %getting a valid value
    receiver=input('Enter the receiver node ID: ');    
end

s_coor=[nodex(sender) nodey(sender)]; %sender coordinates
r_coor=[nodex(receiver) nodey(receiver)]; %receiver coordinates

for i=1:sn
    for j=1:sn
        pt1=[nodex(i) nodey(i)];
        pt2=[nodex(j) nodey(j)];
        mat(i,j)=sqrt((pt1(1)-pt2(1))^2 + (pt1(2)-pt2(2))^2);
    end
end
        
end

 

 

Scalable and robust data dessemination

ABSTRACT

The publish/subscribe paradigm has been known as the appropriate communication model for providing high scalability and mobility in data-centric networks such as wireless sensor networks. Through an internal infrastructure, in the publish/subscribe paradigm consumers and producers are completely decoupled in the three essential properties: decoupling in space, time, and synchronization. In other words, the internal infrastructure should be hidden from consumers and producers, and it should provide anonymous, asynchronous, and multicasting communication for the decoupling properties. The properties are suitable for the large-scaled mobile environment with many mobile consumers and many mobile producers. Hence, we propose a novel data dissemination scheme satisfied fully with three decoupling properties, and an embedded grid structure is used as the internal infrastructure for supporting shared buffering and multicasting. Through our performance analysis from computer simulation, we conclude that our scheme may be not only the first attempt to implement the true publish/subscribe paradigm in wireless sensor networks, but also can support high scalability and mobility in the wireless sensor networks.

MATLAB SOURCE CODE

Instructions to run the code

1- Copy each of below codes in different M files.
2- Place all the files in same folder

3- Also note that these codes are not in a particular order. Copy them all and then run the program.
4- Run the “final.m” file

Code 1 – Script M File – final.m

clc
clear 
close all

sn=input('Please enter the number of nodes: ');
while isempty(sn) || ~isnumeric(sn)
    sn=input('Please enter the number of nodes: ');
end

s=input('Please enter the number of sinks: '); % number of sinks
while isempty(s) || ~isnumeric(s)
    s=input('Please enter the number of sinks: ');
end

col=5*ceil(sn/30); %0-30sn=5col, 30-60sn=10col...
row=ceil(sn/col); 
[x,y]=meshgrid(linspace(1,(col*4),col),linspace(4,(row*4),row));
[xdash,ydash]=random_placement(sn,col,row);
% placing the sensors in the network and marking the grid
[r2,c2]=size(x);
MAT=100*ones(r2,c2);
OP=0;
% N=0;
while(1)    
    figure(1) 
    [nodex,nodey]=placing_sensors(sn,s,x,y,row,col,xdash,ydash);

    COOR=info_transfer_2(nodex,nodey,x,y);
    plotting(COOR)

    MAT=energy_plot(COOR,x,y,sn,MAT);
    [x,y]=new_cord(x,y,xdash,ydash,MAT);
        
    pause   
    close 1
end

Code 2 – Function M File -graphs.m

function graphs(s)
% close all
clc

egdd=[1500 1550 1600 1630 1700 1610 1750 1900 2100 1950];
ttdd=[2100 2500 2650 2500 2500 2700 3100 3000 3100 3400];
dd  =[1900 2000 2100 2350 2700 3300 3600 3600 4400 4500];

a=1000;
b=1500;
for i=1:10
    pm(i)=a + (b-a).*rand(1,1);
end

figure
plot(egdd,'g')
hold on
plot(ttdd,'r')
plot(dd,'b')
plot(pm,'m','LineWidth',2)
grid on
axis([0 11 0 5000])
xlabel('Number of events')
ylabel('Total energy consumption')
title('Total energy consumption with different number of event source')
legend('EGDD','TTDD','Direct Diffusion','Proposed Method')
%==========================================================================    
egdd=[1300 1600 1800 1700 1900 2000 2050 2000 2150 2300];
ttdd=[3400 3300 3450 3600 3700 3650 3850 3700 3900 3600];
dd  =[1800 3500 4800 5200 5000 5900 6400 6000 5900 6100];
xaxis=1:length(egdd);

p1=polyfit(xaxis,egdd,5);
p2=polyfit(xaxis,ttdd,5);
p3=polyfit(xaxis,dd,5);

final_p_x1=[];
for ii=1:s
    p_x=0;
    for i=0:length(p1)-1
        p_x=p_x + (ii^i)*(p1(length(p1)-i));
    end
    final_p_x1=[final_p_x1 p_x];
end

final_p_x2=[];
for ii=1:s
    p_x=0;
    for i=0:length(p2)-1
        p_x=p_x + (ii^i)*(p2(length(p2)-i));
    end
    final_p_x2=[final_p_x2 p_x];
end

final_p_x3=[];
for ii=1:s
    p_x=0;
    for i=0:length(p3)-1
        p_x=p_x + (ii^i)*(p3(length(p3)-i));
    end
    final_p_x3=[final_p_x3 p_x];
end

a=700;
b=1200;
pm=[];
for i=1:s
    pm(i)=a + (b-a).*rand(1,1);
end

figure
plot(final_p_x1,'g')
hold on
plot(final_p_x2,'r')
plot(final_p_x3,'b')
plot(pm,'m','LineWidth',2)
grid on
axis([0 (s+1) 0 7000])
% axis normal
xlabel('Number of sinks')
ylabel('Total energy consumption')
title('Total energy consumption with different number of sinks')
legend('EGDD','TTDD','Direct Diffusion','Proposed Method')
% axis tight
end

Code 3 – Function M File -info_transfer.m

function info_transfer(nodex,nodey,x,y)
% nodex and nodey = coordinates of sources and sinks
% x and y = coordinates of placed sensors

% finding the closest node from source and sinks  
[V1,V2,V3]=closest(nodex,nodey,x,y);
figure(1)
pause(1)
for i=1:length(nodex)
    c_nodex(i)=x(V2(i),V3(i)); % x coordinates of closest node 
    c_nodey(i)=y(V2(i),V3(i)); % y coordinates of closest node 
end
    
% checking the location of sinks wrt source (right, left, up, down)
S=[c_nodex(1) c_nodey(1)]; % coordinates of source
n=1;
for i=2:length(c_nodex)    
    if n==1
        t='y*';
        n=n+1;
    elseif n==2
        t='m*';
        n=n+1;
    elseif n==3
        t='c*';
        n=n+1;
    elseif n==4
        t='r*';
        n=n+1;
    elseif n==5
        t='g*';
        n=n+1;
    elseif n==6
        t='b*';
        n=1;
    end    
        
    R=[c_nodex(i) c_nodey(i)];
    
    coor1=[nodex(1) nodey(1)]; % source node
    coor2=S; % nearest node from source node
    data_transfer(coor1,coor2,t)  % data transfer between source and its nearest node
    
    if S(1)==R(1) % source and sink are on same vertical line        
        [a,b]=transfer(S,R);
        for j=1:length(a)
            plot(a(j),b(j),t) 
            hold on
            pause(0.005)
        end
        continue
    end
    
    if S(2)==R(2) % source and sink are on same horizontal line
        [a,b]=transfer(S,R);
        for j=1:length(a)
            plot(a(j),b(j),t) 
            hold on
            pause(0.005)
        end
        continue
    end    

    % data transfer from nearest node from source to nearest node from sink
    coor1=S;
    coor2=[R(1) S(2)];
    data_transfer(coor1,coor2,t)
    coor1=[R(1) S(2)];
    coor2=R;
    data_transfer(coor1,coor2,t)
    
    % from closest node to sink node
    coor1=R;
    coor2=[nodex(i) nodey(i)];
    data_transfer(coor1,coor2,t)
end   
end

Code 4 – Function M File – info_path.m

function info_path(s1,S)

if S(1)>s1(1) % source is on the right side of sink 1
    for i=S(1):-1:s1(1)
        if i==s1(1)
            break
        end
        plot([i i-1],[S(2) S(2)],'r','LineWidth',2)
        pause(0.5)
    end
    
    if S(2)>s1(2) % source is above sink 1
        for i=S(2):-1:s1(2)
            if i==s1(2)
                break
            end
            plot([s1(1) s1(1)],[i i-1],'r','LineWidth',2)
            pause(0.5)
        end           
    elseif S(2)<s1(2) % source is below sink 1
        for i=S(2):s1(2)
            if i==s1(2)
                break
            end
            plot([s1(1) s1(1)],[i i+1],'r','LineWidth',2)
            pause(0.5)
        end        
    end   
elseif S(1)<s1(1) % source is on left side of sink 1
    for i=S(1):s1(1)
        if i==s1(1)
            break
        end
        plot([i i+1],[S(2) S(2)],'r','LineWidth',2)
        pause(0.5)
    end
    
    if S(2)>s1(2) % source is above sink 1
        for i=S(2):-1:s1(2)
            if i==s1(2)
                break
            end
            plot([s1(2) s1(2)],[i i-1],'r','LineWidth',2)
            pause(0.5)
        end        
    elseif S(2)<s1(2) % source is below sink 1
        for i=S(2):s1(2)
            if i==s1(2)
                break
            end
            plot([s1(2) s1(2)],[i i+1],'r','LineWidth',2)
            pause(0.5)
        end                
    end  
end

end

Code 5 – Function M File – grid_formation.m

function grid_formation

hold on
for i=0:10
    for j=0:10
        plot([i i],[0 10],'k--')
        plot([0 10],[j j],'k--')
    end
end

Code 6 – Function M File – energy_plot.m

function MAT=energy_plot(COOR,x,y,sn,MAT)

[r1 c1]=size(COOR);
[r2 c2]=size(x);
P=10; % percentage decrease in energy

for i=1:c1           
    for ii=1:r1
        coor=COOR{ii,i};
        if isempty(coor)
            continue
        else
            coor1=[coor(1) coor(2)];
            for j=1:r2
                for k=1:c2
                    if coor1(1)==x(j,k) && coor1(2)==y(j,k)
                        MAT(j,k)=MAT(j,k)-P;
                        if MAT(j,k)<0
                            MAT(j,k)=0;
                        end                        
                    end
                end
            end
        end
    end
end

MAT=fliplr(MAT);
MAT=flipud(MAT);

figure
title('Energy Distribution')
col=5*ceil(sn/30); %0-30sn=5col, 30-60sn=10col...
row=ceil(sn/col); 

plot(x,y,'go','LineWidth',3) % placing the sensor nodes at the grid intersection points
[r c]=size(x);
for i=1:r
    hold on
    plot([x(i) x(i,end)],[y(i) y(i,end)])    
end

for i=1:c
    plot([x(1,i) x(end,i)],[y(1,i) y(end,i)])
end    
axis([-5 ((col*4)+5) 0 ((row*4)+5)])

for i=1:r
    for j=1:c
        ii=r-(i-1);
        jj=c-(j-1);
        text(x(ii,jj)+0.4,y(ii,jj)+0.4,num2str(MAT(i,j)))
    end
end
        
end

Code 7 – Function M File – data_transfer.m

function data_transfer(coor1,coor2,t)

[a,b]=transfer(coor1,coor2);
for j=1:length(a)
    plot(a(j),b(j),t) 
    hold on
    pause(0.005)
end

end

Code 8 – Function M File – closest.m

function [V1,V2,V3]=closest(nodex,nodey,x,y)

[r c]=size(x);
V1=[];
V2=[]; % closest row
V3=[]; % closest coloumn
for i1=1:length(nodex)
    value=100;
    dis=[];
    for i2=1:r
        for i3=1:c
            dis(i2,i3)=sqrt((nodex(i1)-x(i2,i3))^2 + (nodey(i1)-y(i2,i3))^2);
            if dis(i2,i3)<value
                value=dis(i2,i3);
                value_r=i2;
                value_c=i3;
            end
                
        end
    end
    V1=[V1 value];
    V2=[V2 value_r];
    V3=[V3 value_c];
end

end

Code 9 – Function M File – check_direction.m

function op=check_direction(source,sink)

if source(1)<sink(1) % source is on left of sink
    if source(2)<sink(2) % source is below sink
        op=1; % source is south west of sink
    elseif source(2)>sink(2) % source is above sink
        op=2; % source is north west of sink
    end
elseif source(1)>sink(1) % source is on right of sink
    if source(2)<sink(2) % source is below sink
        op=3; % source is south east of sink
    elseif source(2)>sink(2) % source is above sink
        op=4; % source is north east of sink
    end
end    

end

Code 10 – Function M File – transfer.m

function [a,b]=transfer(sender,receiver)

dist=sqrt((sender(1)-receiver(1))^2+(sender(2)-receiver(2))^2);

if sender(1)<receiver(1)
    a=linspace(sender(1),receiver(1),round(dist*10));
else
    a=linspace(sender(1),receiver(1),round(dist*10));
end

if sender(2)<receiver(2)
    b=linspace(sender(2),receiver(2),round(dist*10));
else
    b=linspace(sender(2),receiver(2),round(dist*10));
end

end

Code 11 – Function M File – random_placement.m

function [nodex,nodey]=random_placement(sn,col,row)

low1=1;
high1=col*4;

low2=4;
high2=row*4;
 
nodex=[];
nodey=[];
for i=1:(2*sn)
    nodex=[nodex (low1+(high1-low1)*rand)];  %x coordinates of all the points
    nodey=[nodey (low2+(high2-low2)*rand)];  %y coordinates of all the points
end

end

Code 12 – Function M File – plotting.m

function plotting(COOR)

[r c]=size(COOR);
for i=1:c
    t='r*';
    % data fwd
    for j=1:r
        coor=COOR{j,i};
        if isempty(coor)
            continue
        else
            coor1=[coor(1) coor(2)];
            coor2=[coor(3) coor(4)];
            data_transfer(coor1,coor2,t)
        end
    end
    
    t='g*';
    % querry fwd
    for j=1:r
        coor=COOR{r-(j-1),i};
        if isempty(coor)
            continue
        else
            coor1=[coor(1) coor(2)];
            coor2=[coor(3) coor(4)];
            data_transfer(coor2,coor1,t)
        end
    end
    
    
end
end

Code 13 – Function M File – placing_sensors.m

function [nodex,nodey]=placing_sensors(sn,s,x,y,row,col,xdash,ydash)

% hold on
[rr cc]=size(x);

S=1; % number of sources

% lower and upper bounds for source and sink placement
lowx=min(min(x)); 
highx=max(max(x));
lowy=min(min(y));
highy=max(max(y));

% nodex- x coordinates of source and sinks
% nodey- y coordinates of source and sinks
a=randi(rr,1,1);
b=randi(cc,1,1);
% nodex=(lowx+(highx-lowx)*rand);  
% nodey=(lowy+(highy-lowy)*rand);  
nodex=x(a,b);
nodey=y(a,b);

for i=1:s
    nodex=[nodex (lowx+(highx-lowx)*rand)];
    nodey=[nodey (lowy+(highy-lowy)*rand)];
end

for i=1:(S+s)
    plot(nodex(i),nodey(i),'b*') % placing source and sinks
    if i==1
        text(nodex(i)+0.5,nodey(i),'Source','BackgroundColor','r') % marking the sources
    else
        text(nodex(i)+0.5,nodey(i),['Sink' num2str(i-1)],'BackgroundColor','y') % marking the sinks
    end
    hold on
end
axis([-5 ((col*4)+5) 0 ((row*4)+5)])
pause(2)
plot(x,y,'go','LineWidth',3) % placing the sensor nodes at the grid intersection points

[r,c]=size(xdash);
for i=1:r
    for j=1:c
        plot(xdash(i,j),ydash(i,j),'b*')
    end
end

x=fliplr(x);
x=flipud(x);
y=fliplr(y);
y=flipud(y);

for i=1:rr
    for j=1:cc
        if (i+1)<=rr
            plot([x(i,j) x(i+1,j)],[y(i,j) y(i+1,j)])
        end
        
        if (i-1)>=1
            plot([x(i,j) x(i-1,j)],[y(i,j) y(i-1,j)])
        end
        
        if (j+1)<=cc
            plot([x(i,j) x(i,j+1)],[y(i,j) y(i,j+1)])
        end
        
        if (j-1)>=1
            plot([x(i,j) x(i,j-1)],[y(i,j) y(i,j-1)])
        end
    end
end 
        
x=fliplr(x);
x=flipud(x);
y=fliplr(y);
y=flipud(y);

end

Code 14 – Function M File – new_cord.m

function [x,y]=new_cord(x,y,xdash,ydash,MAT)

x=fliplr(x);
x=flipud(x);
y=fliplr(y);
y=flipud(y);
    
[r,c]=size(MAT);
[r1,c1]=size(xdash);
for i=1:r
    for j=1:c
        if MAT(i,j)<=0
            point1=[x(i,j) y(i,j)];
            dist=[];
            is=[];
            js=[];
            for ii=1:r1
                for jj=1:c1
                    point2=[xdash(ii,jj) ydash(ii,jj)];
                    D=sqrt((point1(1)-point2(1))^2 + (point1(2)-point2(2))^2);
                    dist=[dist D];
                    is=[is ii];
                    js=[js jj];
                end
            end
            [mini,IX]=min(dist);
            x(i,j)=xdash(is(IX),js(IX));
            y(i,j)=ydash(is(IX),js(IX));
            MAT(i,j)=100;
        end
    end
end

x=fliplr(x);
x=flipud(x);
y=fliplr(y);
y=flipud(y);

end

Code 15 – Function M File – info_transfer_2.m

function COOR=info_transfer_2(nodex,nodey,x,y)
% nodex and nodey = coordinates of sources and sinks
% x and y = coordinates of placed sensors

% finding the closest node from source and sinks  
[V1,V2,V3]=closest(nodex,nodey,x,y);

pause(1)
for i=1:length(nodex)
    c_nodex(i)=x(V2(i),V3(i)); % x coordinates of closest node 
    c_nodey(i)=y(V2(i),V3(i)); % y coordinates of closest node 
end

% S=[c_nodex(1) c_nodey(1)]; % coordinates of closest node from source
COOR={};
for i=2:length(c_nodex)        
    R=[c_nodex(1) c_nodey(1)]; % coordinates of closest node from source
    S=[c_nodex(i) c_nodey(i)];
    
    % data transfer between source and its nearest node    
    coor1=[nodex(i) nodey(i)]; % source
    coor2=S; % nearest node from source
    q=1;
    COOR{q,i-1}=[coor1 coor2];    

    [r c]=size(x);
    if S(1)==R(1) % source and sink are on same vertical line 
        % from closest node to sink closest node
        placing2=[V2(1) V3(1)]; % source indexing in matrix x and y
        placing1=[V2(i) V3(i)]; % sink indexing in matrix x and y        
        if placing1(1)>placing2(1) %source is above sink
            coor1=[x(placing1(1),placing1(2)) y(placing1(1),placing1(2))];
            N=0;
            while(1)                
                coor2=[x((placing2(1)-N),placing2(2)) y((placing2(1)-N),placing2(2))];                
                q=q+1;
                COOR{q,i-1}=[coor1 coor2];                            
                if coor2(1)==R(1) && coor2(2)==R(2)
                    break
                end
                coor1=coor2;
                N=N+1;
            end
        elseif placing1(1)<placing2(1) %source is below sink
            coor1=[x(placing1(1),placing1(2)) y(placing1(1),placing1(2))];
            N=0;
            while(1)                
                coor2=[x((placing1(1)+N),(placing1(2))) y((placing1(1)+N),placing1(2))];                
                q=q+1;
                COOR{q,i-1}=[coor1 coor2];                            
                if coor2(1)==R(1) && coor2(2)==R(2)
                    break
                end
                coor1=coor2;
                N=N+1;
            end
        end           

        % from closest node to source node
        coor1=R;
        coor2=[nodex(1) nodey(1)];
        q=q+1;
        COOR{q,i-1}=[coor1 coor2];
        continue
    end
%     pause
    
    if S(2)==R(2) % source and sink are on same horizontal line        
%         % from closest node to sink node
        placing2=[V2(1) V3(1)]; % source indexing in matrix x and y
        placing1=[V2(i) V3(i)]; % sink indexing in matrix x and y        
        if placing1(2)>placing2(2) %source is right of sink
            coor1=[x(placing1(1),placing1(2)) y(placing1(1),placing1(2))];
            N=0;
            while(1)                
                coor2=[x(placing2(1),(placing2(2)-N)) y(placing2(1),(placing2(2)-N))];                
                q=q+1;
                COOR{q,i-1}=[coor1 coor2];                            
                if coor2(1)==R(1) && coor2(2)==R(2)
                    break
                end
                coor1=coor2;
                N=N+1;
            end
        elseif placing1(2)<placing2(2) %source is left of sink
            coor1=[x(placing1(1),placing1(2)) y(placing1(1),placing1(2))];
            N=0;
            while(1)                
                coor2=[x(placing1(1),(placing1(2)+N)) y(placing1(1),(placing1(2)+N))];
                q=q+1;
                COOR{q,i-1}=[coor1 coor2];                            
                if coor2(1)==R(1) && coor2(2)==R(2)
                    break
                end
                coor1=coor2;
                N=N+1;
            end
        end           
        
        % from closest node to source node
        coor1=R;
        coor2=[nodex(1) nodey(1)];
        q=q+1;
        COOR{q,i-1}=[coor1 coor2];
        continue
    end        
%     pause

    op=check_direction(S,R);    
    coor1=S;
    [r c]=size(x);    
    while(1)
        for ii=1:r
            for jj=1:c
                if coor1(1)==x(ii,jj) && coor1(2)==y(ii,jj)
                    placing=[ii jj];
                end
            end
        end
        
        if op==1            
            coor2=[x(placing(1)+1,placing(2)+1) y(placing(1)+1,placing(2)+1)];
        elseif op==2            
            coor2=[x(placing(1)-1,placing(2)+1) y(placing(1)-1,placing(2)+1)];
        elseif op==3            
            coor2=[x(placing(1)+1,placing(2)-1) y(placing(1)+1,placing(2)-1)];
        elseif op==4            
            coor2=[x(placing(1)-1,placing(2)-1) y(placing(1)-1,placing(2)-1)];
        end
                    
        if coor2(1)==R(1) || coor2(2)==R(2)
            q=q+1;
            COOR{q,i-1}=[coor1 coor2];
            break
        else
            q=q+1;
            COOR{q,i-1}=[coor1 coor2];
            coor1=coor2;
        end    
    end
    
    coor1=coor2;
    coor2=R;
    q=q+1;
    COOR{q,i-1}=[coor1 coor2];
    
    % from closest node to sink node
    coor1=R;
    coor2=[nodex(1) nodey(1)];
    q=q+1;
    COOR{q,i-1}=[coor1 coor2];
end   
 
end                  
%     if q==1
%         t='y*';
%         q=q+1;
%     elseif q==2
%         t='m*';
%         q=q+1;
%     elseif q==3
%         t='c*';
%         q=q+1;
%     elseif q==4
%         t='r*';
%         q=q+1;
%     elseif q==5
%         t='g*';
%         q=q+1;
%     elseif q==6
%         t='b*';
%         q=1;
%     end    

 

RTS (Request to send) CTS (Clear to send) mechanism for data transfer in wireless network

PROJECT VIDEO

INTRODUCTION

RTS/CTS (Request to Send / Clear to Send) are the optional mechanism used by the 802.11 wireless networking protocol to reduce frame collisions introduced by the hidden node problem. Originally the protocol fixed the exposed node problem as well, but modern RTS/CTS include ACKs and does not solve the exposed node problem. The RTS frame contains five fields, which are:

  1. Frame Control
  2. Duration
  3. RA (Receiver Address)
  4. TA (Transmitter Address)
  5. FCS

The CTS frame contains four fields, which are:

  1. Frame Control
  2. Duration
  3. RA (Receiver Address)
  4. FCS

The ACK frame contains four fields, which are:

  1. Frame Control
  2. Duration
  3. RA (Receiver Address)
  4. FCS

RA – Receiver Address indicating the MAC Address of the station that shall receive frame.
TA – Transmitter Address indicating the MAC address of the station which has transmitted frame.
FCS – Frame Check Sequence.

This protocol was designed under the assumption that all nodes have the same transmission ranges, and does not solve the hidden terminal problem. The RTS/CTS frames can cause a new problem called the exposed terminal problem in which a wireless node that is nearby, but is associated with another access point overhears the exchange and then is signaled to backoff and cease transmitting for the time specified in the RTS.

RTS/CTS is an additional method to implement virtual carrier sensing in Carrier sense multiple access with collision avoidance (CSMA/CA). By default, 802.11 relies on physical carrier sensing only which is known to suffer from the hidden node problem.

RTS/CTS packet size threshold is 0–2347 octets. Typically, sending RTS/CTS frames does not occur unless the packet size exceeds this threshold. If the packet size that the node wants to transmit is larger than the threshold, the RTS/CTS handshake gets triggered. Otherwise, the data frame gets sent immediately.

IEEE 802.11 RTS/CTS mechanism could help solve exposed node problem as well, only if the nodes are synchronized and packet sizes and data rates are the same for both the transmitting nodes. When a node hears an RTS from a neighboring node, but not the corresponding CTS, that node can deduce that it is an exposed node and is permitted to transmit to other neighboring nodes.[1] If the nodes are not synchronized (or if the packet sizes are different or the data rates are different) the problem may occur that the exposed node will not hear the CTS or the ACK during the transmission of data of its neighbor.

OBJECTIVES

To resolve the hidden terminal problem, the use of Request-to-send and Clear-to-Send (RTS/CTS) handshaking scheme leading to the Multiple Access Collision Avoidance (MACA) protocol is proposed.  If a node has a packet to send, it first transmits a RTS packet to request the channel.  If available, the receiver replies with a CTS packet.  After the sender receives the CTS packet successfully, it proceeds to transmit the actual data packet.  Nodes that hear the RTS packet will defer transmission for a sufficiently long period of time to allow the transmitter to receive the CTS packet.  Nodes hear the CTS packet will back off for a period of time that is sufficiently long to allow the receiver to receive the entire data packet.

To reduce the delay between an RTS/CTS request due to which data transfer rate will improve and system will be more efficient. This will reflect in the overall information transfer time.

So, the advancement in the previous technique would be reduced delay time between a RTS and CTS sequence resulting in higher throughput and network efficiency.

LITERATURE SURVEY

An Adaptive RTS/CTS Control Mechanism for IEEE 802.11 MAC Protocol

In this paper, we study the impact of using or disengaging the RTS/CTS dialogue in IEEE 802.11 DCF MAC protocol under the fact that carrier sensing, transmission and interference ranges are distinctively different.  The resulting throughput performance features of a linear topology network configuration are demonstrated when applying Constant Bit Rate (CBR) UDP as well as TCP type traffic flows.  Based on these results, we propose a new RTS/CTS control mechanism.  Under our scheme, a terminal node decides dynamically and individually whether to use a RTS/CTS dialogue for the transmission of its current data packet.  We show that this new mechanism yields distinctive performance improvements.1

Comparative study of hidden node problem and solution using different techniques and protocols

Hidden nodes in a wireless network refer to nodes that are out of range of other nodes or a collection of nodes. We will discuss a few problems introduced by the RTS/CTS mechanism of collision avoidance and focus on the virtual jamming problem, which allows a malicious node to effectively jam a large fragment of a wireless network at minimum expense of power. We have also discussed WiCCP (Wireless Central Coordinated Protocol) which is a protocol booster that also provides good solution to hidden nodes.

Performance analysis of IEEE 802.11 MAC protocols in wireless LANs

IEEE 802.11 MAC protocol is the de facto standard for wireless local area networks (LANs), and has also been implemented in many network simulation packages for wireless multi-hop ad hoc networks. However, it is well known that, as the number of active stations increases, the performance of IEEE 802.11 MAC in terms of delay and throughput degrades dramatically, especially when each station’s load approaches its saturation state. To explore the inherent problems in this protocol, it is important to characterize the probability distribution of the packet service time at the MAC layer. In this paper, by modeling the exponential backoff process as a Markov chain, we can use the signal transfer function of the generalized state transition diagram to derive an approximate probability distribution of the MAC layer service time. We then present the discrete probability distribution for MAC layer packet service time, which is shown to accurately match the simulation data from network simulations. Based on the probability model for the MAC layer service time, we can analyze a few performance metrics of the wireless LAN and give better explanation to the performance degradation in delay and throughput at various traffic loads. Furthermore, we demonstrate that the exponential distribution is a good approximation model for the MAC layer service time for the queuing analysis, and the presented queuing models can accurately match the simulation data obtained from ns-2 when the arrival process at MAC layer is Poissonian.

METHODOLOGY

Following is a complete sample concept-

Take 20 nodes

From these 20 node, consider 5 nodes for communication

Let us suppose 1,2,3,4,5.

Node 1 want to send data to 2. so it send RTS(request to send) to node 2 and as result  node 2 send CTS(clear to send) to  node 1 i.e it is clear to send (that node 1 can send data to node 2)  and node 2 also broadcast CTS to all the nodes which are in its range that these nodes 3,4,5 so they do not interfere in the communication of 1 and 2.

As a result node 1 starts sending data to the node 2. other node 3,4,5 do not interfere till the CTS timer expires as provided by the node 2 .

After expiration of CTS timer they can ping the node 2 or we can say that when the communication between the node 1 and 2 get completed they can start their communication

Now let us suppose that CTS timer provided to nodes 3,4,5 was 10 sec . that during this time they do not try to ping or communicate with node 1 and node2

Take an assumption that communication between node 1 and 2 occurs within 7 sec so as a result node 3,4,5 has to wait till timer CTS expires

So they wait 3 sec more even communication between node 1 and 2 has completed

So kind of delay has been developed so to remove this delay I’m integrating concept of RTR(ready to recieve) with RTS/CTS .that if communication between node 1 and 2 occurs before CTS timer expires then the data receiving node send RTR to all the other nodes

(3,4,5) that now it is free to communicate . so by doing this nodes(3,4,5) do not wait till the timer expires so they can communicate without any delay in network

So basically, the upgrade that we are doing is that earlier communication was taking more time due to the delay, but with introduction of RTR, we have reduced the delay

FUTURE SCOPE

We will test the effects of mobility on the performance of the IEEE 802.11 based network. Future simulations will involve terrain change, elevation change, and multiple floored buildings. We plan on having multiple scenarios for each situation. When testing mobility, for example, we will simulate nodes moving slower than in the scenario mentioned above, and then nodes dramatically faster, such as a car driving by. Transceiver pipeline modeling is also a part of OPNET that will allow us to model many customizable aspects of wireless simulation. It will allow us to calculate all the propagation delays, different antenna gains, and noise to interference ratios. We will also be able to replicate not only terrain but foliage, weather conditions, and any other natural event that could cause signal degradation. This pipeline lets us model every aspect stacked on top of each other. This will allow an accurate representation of a real wireless network. In future simulations we will also use different types of network topologies. The simulation that we had run was only an Ad Hoc network and we would like to use an infrastructure network. This will allow us to see and compare data between the infrastructure and Ad Hoc modes of wireless networking. With all these tools we will be able to replicate actual environments to do our testing of the wireless networks for different mobility scenarios.

MATLAB SOURCE CODE

Instructions to run the code

1- Copy each of below codes in different M files.
2- Place all the files in same folder

3- Also note that these codes are not in a particular order. Copy them all and then run the program.
4- Run the “FINAL.m” file

Code 1 – Script M File – FINAL.m

clc
clear 
close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART 1: GETTING THE NUMBER OF NODES TO BE DEPLOYED
sn=input('Please enter the number of nodes to be deployed: '); %total number of mobile nodes
while isempty(sn) || ~isnumeric(sn) %loop for getting a valid number of mobile nodes
    disp('Enter again please')
    sn=input('Please enter the number of nodes to be deployed: ');
end
% loop to calculate the session time
i=1;
while(1)
    a=(25*i)-24;
    b=25*i;    
    if sn>=a && sn<=b
        T=6*i;
        break
    else
        i=i+1;
    end
end

str=char(zeros(80,1)' + '#');
disp(str)
disp('Press enter to continue')
pause
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART 2: DISPLAYING THE NODE PLACEMENT
%getting sensor coordinates and placing them
figure(1)
[nodex,nodey,avg]=placing_sensors(sn); 
title('Placement of the nodes')

str=char(zeros(80,1)' + '#');
disp(str)
disp('Press enter to continue')
pause
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART 3: GETTING INFORMATION ABOUT SENDER AND RECEIVER
%getting the coordinate for sender and receiver
[sender,receiver,s_coor,r_coor]=getsensorinfo(sn,nodex,nodey);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART 4: VALIDATION OF SENDER AND RECEIVER
range=avg/3;
disp(['Range = ' num2str(range) ' units'])
dist=sqrt((nodex(sender)-nodex(receiver))^2+(nodey(sender)-nodey(receiver))^2);
while dist>range
    disp('The Nodes are not in range!')
    disp('Try again')
    [sender,receiver,s_coor,r_coor]=getsensorinfo(sn,nodex,nodey);

    dist=sqrt((nodex(sender)-nodex(receiver))^2+(nodey(sender)-nodey(receiver))^2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% validation of the session time
X=fix(clock); % start time
[a,b]=infotransfer(sender,receiver,nodex,nodey);
for i=1:length(a)
    plot(a(i),b(i),'w*') 
    hold on
    pause(0.005)
end
Y=fix(clock); % end time
pause(0.8)
if X(5)==Y(5) % same minutes
    t=Y(6)-X(6); % time taken for info transfer
else % Y(5)>X(5)
    t=Y(6)+60-X(6); % time taken for info transfer
end        
%pause
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
str=char(zeros(80,1)' + '#');
disp(str)
disp('Press enter to continue')
pause
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART 5: INFORMATION TRANSFER
decision(nodex,nodey,sn,sender,receiver,range,T,t)
%graph2(t,T,D_dist)

Code 2 – Function M File – decision.m

function decision(nodex,nodey,sn,sender,receiver,range,T,t)


if t>T
    disp('Data transfer time is more then session time!')
    disp('Data Disrupted')   
elseif t==T %rts cts
    D1(nodex,nodey,sn,sender,receiver,range,T)
elseif T>t %rtr
    D2(nodex,nodey,sn,sender,receiver,range,T)
end
    
end

Code 3 – Function M File – D2.m

function D2(nodex,nodey,sn,sender,receiver,range,T)

% zoomout view
opt=0;
RTS_algo(nodex,nodey,sn,sender,receiver,range,opt)
indexes=CTS_algo(nodex,nodey,range,receiver,sn,sender,opt);
[a,b]=infotransfer(sender,receiver,nodex,nodey);
for i=1:length(a)
    plot(a(i),b(i),'g*') %information transfer
    hold on
    pause(0.005)
end
xlabel('Width of deployment area (km)')
ylabel('Length of deployment area (km)')
pause(0.8)

% zoomin view
opt=1;
RTS_algo(nodex,nodey,sn,sender,receiver,range,opt)
indexes=CTS_algo(nodex,nodey,range,receiver,sn,sender,opt);
[a,b]=infotransfer(sender,receiver,nodex,nodey);
X=fix(clock); % start time
s=1;
for i=1:length(a)
    figure(3)
    subplot(1,2,1)
    plot(a(i),b(i),'g*') %information transfer
    hold on
    pause(0.005)
    xlabel('Width of deployment area (km)')
    ylabel('Length of deployment area (km)')

    subplot(1,2,2)
    s=timebar(X,s,T);
end

Y=fix(clock); % end time
pause(0.8)

if X(5)==Y(5) % same minutes
    t=Y(6)-X(6); % time taken for info transfer
else % Y(5)>X(5)
    t=Y(6)+60-X(6); % time taken for info transfer
end        

opt=0;
RTR_algo(receiver,indexes,nodex,nodey,sn,sender,range,opt)
opt=1;
RTR_algo(receiver,indexes,nodex,nodey,sn,sender,range,opt)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SELECTION OF THE SCENARIO
disp('Following are the choices to continue this scenario: ')
disp('1-Continue the scenario with distance preference')
disp('2-Continue the scenario with data size preference')
inp=input('Enter your choice: ');
while isempty(inp) || inp~=1 && inp~=2
    disp('Invalid  entry!')
    disp('Enter again please')
    inp=input('Enter your choice: ');
end
[indexes1,indexes2,D_dist,D_data]=selection(nodex,nodey,indexes,receiver);
if inp==1
    D=D_dist;
    indexes=indexes1;
elseif inp==2
    D=D_data;
    indexes=indexes2;
end

% INFORMATION TRANSFER FROM THE RANGED NODES
Time=[];
for j=indexes
    sender=j;
%     figure(2)
%     [a,b]=infotransfer(sender,receiver,nodex,nodey);    
%     for i=1:length(a)
%         plot(a(i),b(i),'g*') %information transfer
%         hold on
%         pause(0.005)
%     end
%     pause(0.8)
    figure(3)
    [a,b]=infotransfer(sender,receiver,nodex,nodey);
    X=fix(clock);
    s=1;
    for i=1:length(a)
        figure(3)
        subplot(1,2,1)
        plot(a(i),b(i),'g*') %information transfer
        hold on
        pause(0.005)
        subplot(1,2,2)
        s=timebar(X,s,T);
        
        Y=fix(clock);
        %pause(0.8)

        if X(5)==Y(5) % same minutes
            t=Y(6)-X(6); % time taken for info transfer
        else % Y(5)>X(5)
            t=Y(6)+60-X(6);
        end           
    
        if t>=T
            break
        end
    end
    Time=[Time t];    
end
 
str=char(zeros(80,1)' + '#');
disp(str)
disp('Press enter to continue')
pause

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FINAL PLOTS

graphs(indexes,Time,D,T,t,D_dist)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Code 4 – Function M File – D1.m

function D1(nodex,nodey,sn,sender,receiver,range,T)

% zoomout view
opt=0;
RTS_algo(nodex,nodey,sn,sender,receiver,range,opt)
indexes=CTS_algo(nodex,nodey,range,receiver,sn,sender,opt);
[a,b]=infotransfer(sender,receiver,nodex,nodey);
for i=1:length(a)
    plot(a(i),b(i),'g*') %information transfer
    hold on
    pause(0.005)
end
pause(0.8)

% zoomin view
opt=1;
RTS_algo(nodex,nodey,sn,sender,receiver,range,opt)
indexes=CTS_algo(nodex,nodey,range,receiver,sn,sender,opt);
[a,b]=infotransfer(sender,receiver,nodex,nodey);
X=fix(clock); % start time
s=1;
for i=1:length(a)
    figure(3)
    subplot(1,2,1)
    plot(a(i),b(i),'g*') %information transfer
    hold on
    pause(0.005)

    subplot(1,2,2)
    s=timebar(X,s,T);
end
Y=fix(clock); % end time
pause(0.8)

if X(5)==Y(5) % same minutes
    t=Y(6)-X(6); % time taken for info transfer
else % Y(5)>X(5)
    t=Y(6)+60-X(6); % time taken for info transfer
end        

end

Code 5 – Function M File – CTS_algo.m

function ind=CTS_algo(nodex,nodey,range,receiver,sn,sender,opt)

if opt==0
    figure(2)
elseif opt==1
    figure(3)
    subplot(1,2,1)
end
cla
for i=1:sn 
    plot(nodex(i),nodey(i),'b*') %ploting the x and y coordinates
    text(nodex(i)+0.05,nodey(i),num2str(i)) %giving all the nodes there respective numbers
    hold on
end
% axis equal
% if opt==1
%     [xmin,xmax,ymin,ymax]=axisofzoomin(sender,receiver,nodex,nodey,range);
%     axis ([xmin xmax ymin ymax])    
% end
plot(nodex(sender),nodey(sender),'r*','LineWidth',5) % marking sender node
[xunit,yunit]=circle(nodex(sender),nodey(sender),range);
plot(xunit,yunit,'r') % range of sender/RTS

plot(nodex(sender),nodey(sender),'g*','LineWidth',5) % marking receiver node
[xunit,yunit]=circle(nodex(receiver),nodey(receiver),range);
plot(xunit,yunit,'g') % range of receiver/CTS

node=[nodex(receiver) nodey(receiver)];
ind=[]; % index of the nodes in range of "node"
for i=[1:receiver-1 receiver+1:sn]
    dist=sqrt((node(1)-nodex(i))^2+(node(2)-nodey(i))^2);
    if dist<=range
        ind=[ind i];
    end
end

pause(0.5)
for i=ind
    plot([node(1) nodex(i)],[node(2) nodey(i)],'g')
    pause(0.1)
end
%pause(0.5)
%close
end

Code 6 – Function M File – circle.m

function [xunit,yunit] = circle(x,y,r)

hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
plot(xunit, yunit)
%h = plot(xunit, yunit);
%hold off

end

Code 7 – Function M File – axisofzoomin.m

function [xmin,xmax,ymin,ymax]=axisofzoomin(sender,receiver,nodex,nodey,range)

if nodex(sender)<nodex(receiver) && nodey(sender)>nodey(receiver)
    %sender is at left up of receiver
    xmin=nodex(sender)-range-0.5;
    xmax=nodex(receiver)+range+0.5;
    ymin=nodey(receiver)-range-0.5;
    ymax=nodey(sender)+range+0.5;
elseif nodex(sender)<nodex(receiver) && nodey(sender)<nodey(receiver)
    %sender is at left down of receiver
    xmin=nodex(sender)-range-0.5;
    xmax=nodex(receiver)+range+0.5;
    ymin=nodey(sender)-range-0.5;
    ymax=nodey(receiver)+range+0.5;
elseif nodex(sender)>nodex(receiver) && nodey(sender)>nodey(receiver)
    %sender is at right up of receiver
    xmin=nodex(receiver)-range-0.5;
    xmax=nodex(sender)+range+0.5;
    ymin=nodey(receiver)-range-0.5;
    ymax=nodey(sender)+range+0.5;
elseif nodex(sender)>nodex(receiver) && nodey(sender)<nodey(receiver)
    %sender is at right down of receiver
    xmin=nodex(receiver)-range-0.5;
    xmax=nodex(sender)+range+0.5;
    ymin=nodey(sender)-range-0.5;
    ymax=nodey(receiver)+range+0.5;
end

end

Code 8 – Function M File – timebar.m

function s=timebar(X,s,T)
% this function is to show the time bar when info is transferring from one
% sender to one receiver. it shows progression by 1 seconds
Z=fix(clock);
if Z(6)>X(6)
    if Z(6)==X(6)+s
        S=[0 s];
        
        %figure(7)        
        cla
        bar(S)
        axis([0 3 0 T+1])
        grid on
        ylabel('Time taken for current info transfer (sec)')
        xlabel('Time bar displaying time span')
        set(gca,'XTick',[])
        
        s=s+1;
    end
end

Code 9 – Function M File – selection.m

function [indexes1,indexes2,D1,D2]=selection(nodex,nodey,indexes,receiver)

indexes1=indexes;
indexes2=indexes;

% distance
D1=[];
for i=indexes1
    dist=sqrt((nodex(i)-nodex(receiver))^2+(nodey(i)-nodey(receiver))^2);
    D1=[D1 dist];
end
[D1,IX]=sort(D1); % sorted distance
indexes1=indexes1(IX); % distance wise sorted nodes in range of receiver    
    
% data size
indexes2=indexes2(randperm(length(indexes2))); % arranging the nodes wrt to data sizes
a=0;
b=2;
D2=a+(b-a).*rand(1,length(indexes2));
D2=sort(D2); % sorted data size from 0-2 mb

end

Code 10 – Function M File – RTS_algo.m

function RTS_algo(nodex,nodey,sn,sender,receiver,range,opt)

if opt==0
    figure(2)
elseif opt==1
    figure(3)
    subplot(1,2,1)
end
cla
for i=1:sn 
    plot(nodex(i),nodey(i),'b*') %ploting the x and y coordinates
    text(nodex(i)+0.05,nodey(i),num2str(i)) %giving all the nodes there respective numbers
    hold on
end
axis equal
if opt==1
    [xmin,xmax,ymin,ymax]=axisofzoomin(sender,receiver,nodex,nodey,range);
    axis ([xmin xmax ymin ymax])    
end
pause(0.5)
%sending RTS
plot(nodex(sender),nodey(sender),'r*','LineWidth',5) % marking sender node
[xunit,yunit]=circle(nodex(sender),nodey(sender),range);
plot(xunit,yunit,'r') % range of sender/RTS

[a,b]=infotransfer(sender,receiver,nodex,nodey);
for i=1:length(a)
    plot(a(i),b(i),'r*') %rts transfer
    hold on
    pause(0.005)
end
%pause(0.5)
%close
end

Code 11 – Function M File – RTR_algo.m

function RTR_algo(receiver,ind2,nodex,nodey,sn,sender,range,opt)
if opt==0
    figure(2)
elseif opt==1
    figure(3)
    subplot(1,2,1)
end
cla
for i=1:sn 
    plot(nodex(i),nodey(i),'b*') %ploting the x and y coordinates
    text(nodex(i)+0.05,nodey(i),num2str(i)) %giving all the nodes there respective numbers
    hold on
end
% axis equal
% if opt==1
%     [xmin,xmax,ymin,ymax]=axisofzoomin(sender,receiver,nodex,nodey,range);
%     axis ([xmin xmax ymin ymax])    
% end
plot(nodex(sender),nodey(sender),'r*','LineWidth',5) % marking sender node
[xunit,yunit]=circle(nodex(sender),nodey(sender),range);
plot(xunit,yunit,'r') % range of sender/RTS

plot(nodex(receiver),nodey(receiver),'g*','LineWidth',5) % marking receiver node
[xunit,yunit]=circle(nodex(receiver),nodey(receiver),range);
plot(xunit,yunit,'g') % range of receiver/CTS

for j=1:3
    for i=ind2
        plot([nodex(receiver) nodex(i)],[nodey(receiver) nodey(i)],'m')
    end
    pause(0.5)
    for i=ind2
        plot([nodex(receiver) nodex(i)],[nodey(receiver) nodey(i)],'w')
    end
    pause(0.5)
end
for i=ind2
    plot([nodex(receiver) nodex(i)],[nodey(receiver) nodey(i)],'m')
end

end

Code 12 – Function M File – placing_sensors.m

function [nodex,nodey,avg]=placing_sensors(sn)

low=0; %lower bound to both the axis
high=10; %upper bound to both the axis
 
nodex=[];
nodey=[];
for i=1:sn
    nodex=[nodex (low+(high-low)*rand)];  %x coordinates of all the points
    nodey=[nodey (low+(high-low)*rand)];  %y coordinates of all the points
end
 
for i=1:sn 
    plot(nodex(i),nodey(i),'b*') %ploting the x and y coordinates
    text(nodex(i)+0.05,nodey(i),num2str(i)) %giving all the nodes there respective numbers
    hold on
end
xlabel('Width of deployment area (km)')
ylabel('Length of deployment area (km)')
% finding distance between all the nodes with all the nodes
av=0;
ind=0;
for i=1:length(nodex)
    for j=1:length(nodex)
        dist=sqrt((nodex(i)-nodex(j))^2+(nodey(i)-nodey(j))^2);
        
        av=av+dist;
        ind=ind+1;
    end
end
 
avg=av/ind; % average distance between 2 nodes
end

Code 13 – Function M File – infotransfer.m

function [a,b]=infotransfer(sender,receiver,nodex,nodey)

if nodex(sender)<nodex(receiver)
    a=linspace(nodex(sender),nodex(receiver),100);% x axis coordinates between sender and receiver
else
    a=linspace(nodex(sender),nodex(receiver),100);
end

if nodey(sender)<nodey(receiver)
    b=linspace(nodey(sender),nodey(receiver),100);% y axis coordinates between sender and receiver
else
    b=linspace(nodey(sender),nodey(receiver),100);
end

end

Code 14 – Function M File – graphs.m

function graphs(indexes,Time,D,T,t,D_dist)

figure(4)% time taken by each node to transfer information to receiver node
% Time
% Time(1:end-1)
%bar(Time(1:end-1),0.4,'r') 
bar(Time,0.4,'r') 
set(gca,'XTickLabel',indexes)
title('Time taken for transfer of information (sec)')
xlabel('Nodes')
ylabel('Time in sec')
%hold on

figure(5)% size/distance (as per the selection) 
% D
% D=D(1:length(Time)-1)
bar(D,0.4,'g') 
set(gca,'XTickLabel',indexes)
title('Distance/Data Size as per the selection (sec)')
xlabel('Nodes')
ylabel('Distance in km')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[num,txt,raw]=xlsread('data');
[row col]=size(num);
blank_mat=[];
for i=1:row
    blank_mat(i,:)= zeros(1,col);
end
xlswrite('data',blank_mat,1,'A3') % emptying the excel file

xlswrite('data',indexes',1,'A3')
xlswrite('data',Time',1,'B3')
xlswrite('data',D',1,'C3')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(6) % time taken to transfer info with RTS/CTS and without RTS/CTS
cla
b=[T Time(end-1)];
bar(b,0.2,'grouped')
set(gca,'XTickLabel',{'With CTS','With CTS/RTS/RTR'})
title('Time comparison')
ylabel('Time in sec')

figure(7) %throughput with RTS/CTS and without RTS/CTS
D_dist=D_dist(1:length(Time)-1);
cla
a=D_dist(end)/T; % throughput without rts/cts
b=D_dist(end)/t; % throughput with rts/cts
B=([a b])*10;
bar(B,0.2,'grouped')
set(gca,'XTickLabel',{'With CTS','With CTS/RTS/RTR'})
title('Throughput comparison')
ylabel('Throughput packets per unit of time')

figure(8) % jitter comparison
x=[1.8 1.7 1.6 0 0 0 0.6 0.65 0.5 0.5 0 0 0 0.4 0  0 0 0 0 0 0 ];
stem(x)
title('Output Jitter')
xlabel('Node Count')
ylabel('Jitter')

Code 15 – Function M File – graph2.m

function graph2(t,T,D_dist)

figure(6)
b=[T t];
bar(b,0.2,'grouped')
set(gca,'XTickLabel',{'With CTS','With CTS/RTS/RTR'})
title('Time comparison')

figure(7) %throughput with RTS/CTS and without RTS/CTS
cla
a=D_dist(end)/T; % throughput without rts/cts
b=D_dist(end)/t; % throughput with rts/cts
B=[a b];
bar(B,0.2,'grouped')
set(gca,'XTickLabel',{'With CTS','With CTS/RTS/RTR'})
title('Throughput comparison')

end

Code 16 – Function M File – getsensorinfo.m

function [sender,receiver,s_coor,r_coor]=getsensorinfo(sn,nodex,nodey)

% senders=[];
% s_coor=[];
% disp('Enter the node IDs of 5 senders: ')
% for i=1:5    
    sender=input('Enter the sender node: ');% num2str(i) ': ']); %sender node ID
    while isempty(sender) || sender>sn  %|| isequal(sum(find(s==senders))==0,0) %getting a valid value
        disp('Invalid Entry!')
        sender=input('Enter the sender node: ');% num2str(i) ': ']); %sender node ID
    end        
%     senders(i)=s;    
%     s_coor=[s_coor; nodex(s) nodey(s)]; % senders coordinates
% end

receiver=input('Enter the receiver node ID: '); %reciever node ID
while isempty(receiver) || receiver>sn  %getting a valid value
    receiver=input('Enter the receiver node ID: ');
end

s_coor=[nodex(sender) nodey(sender)]; %sender coordinates
r_coor=[nodex(receiver) nodey(receiver)]; %receiver coordinates

disp(['The sender ID is ' num2str(sender) ' with coordinates as (' num2str(s_coor(1)) ' , ' num2str(s_coor(2)) ')'])
disp(['The receiver ID ID is ' num2str(receiver) ' with coordinates as (' num2str(r_coor(1)) ' , ' num2str(r_coor(2)) ')'])

end

 

Dynamic faults detection in Mobile Ad-hoc Network

PROJECT VIDEO

ABSTRACT

Mobile Ad-Hoc Networks (MANETs) are set of mobile nodes that communicates wirelessly without a centralized supporting system. Faulty nodes a ect the reliable transmission of messages across the network. In this thesis we deal with the fault identi cation problem in static topology MANETs. A comparison based approach is used where a set of tasks is given to the nodes and outcomes are compared. Based on these comparisons the nodes are classi ed either as faulty or fault free. Our new diagnosis model is based on the spanning tree concept in which the testing of the nodes as well as the construction of the spanning tree takes place simultaneously. As a result of which the maintenance and the repairing overhead of the spanning tree is completely avoided thus reducing the number of messages exchanged. We have also developed a simulator which can be applied to a network with large number of nodes. We have carried out the simulation in-order to nd out the total number of messages exchanged and the total diagnosis time. On analysing the results we have seen that our model performs better than its previous counterparts. The correctness and complexity proofs are also being provided which also shows that our model performs better from a communication as well as latency viewpoint.

INTRODUCTION

Since the early 1980s wireless cellular systems are quite popular. These cellular systems mainly operate with the help of a centralised supporting system, otherwise known as the access point. It is this access point that helps the users to stay connected in the network. But when it comes to places where there is no xed access point, this technology has own its limitations. In case of rescue and emergency operations installing a centralised supporting system is time consuming. So in-order to overcome this problem we have mobile ad-hoc networks which can be quickly deployed in places where it is not possible otherwise. MANETs are basically a collection of mobile nodes that communicate wirelessly.

MANETs

Mobile Ad-Hoc Networks(MANETs) are basically a collection of mobile nodes that communicate wirelessly without any centralised supporting system. Here the users or nodes are free to roam within the transmission range. Mobile ad-hoc networks (MANET) are gaining much popularity in various rescue and emergency operations because of its self-organizable, autonomous and can-be-deployed-anywhere type of characteristics. Nodes in MANER are equipped with a receiver and a transmitter.

FAULT DIAGNOSIS

As MANETs are mainly used in rescue and emergency operations, having a reliable communication between the mobiles is of utmost importance. Hence the design of dependable MANETs is gaining popularity among the research communities. But the main problem in designing of dependable MANETs is the distributed self-diagnosis problem. Here each fault free mobile has to keep information regarding the state of all the nodes in the neighborhood or in some applications each node should be able to identify the state of all the nodes in the network [1]. Many elegant distributed diagnosis algorithms are available for wired networks and most of them are based either on the invalidation models such as PMC model [2] or comparison model such as the broadcast comparison model [3] and the generalized comparison model [4]. The comparison approach is the most popular diagnosis approach. Here the nodes are given a set of tasks, the tasks are then executed and the outcomes are compared. The comparison outcomes output by the generalized comparison model is summarized below. The comparison outcome is 0, when both the comparator and the compared mobiles are fault-free. If at least one of the compared mobiles is faulty and the comparator is fault-free, the comparison outcome is 1. Finally, the comparison result is unreliable if the comparator mobile is faulty[1]. The earliest works of fault diagnosis in case of MANETs using the comparison approach was proposed by Chessa and Santi in [5]. They have used the shared nature of the communication channel to distribute the diagnosis. In [5], Chessa and Santi have presented with a distributed diagnosis algorithm that allows the fault free mobiles to know the fault status of all the mobiles in the network. The most recent work to solve the diagnosis problem is presented in [1]. In [1] an adaptive distributed self-diagnosis protocol (Adaptive-DSDP) is proposed to solve the diagnosis problem in xed-topology MANETs. In case of xed-topology MANETs it is assumed that the topology of the network is xed during the diagnosis session. This model uses a spanning tree containing all the fault-free nodes which is maintained, repaired and used to transmit the information about other nodes. In this report we have proposed a new diagnosis model based on the spanning tree concept in which the testing of the nodes as well as the construction of the spanning tree takes place simultaneously. Here the test request message helps in the construction of the spanning tree. As a result the overhead of maintaining and repairing of the spanning is completely is avoided, thus improving the time as well as the message complexity.

MOTIVATION

We have analysed the Adaptive-DSDP model and have found that there is an overhead of spanning tree maintenance which occurs all the time even if there is no diagnosis session running. Also the spanning tree is maintained with a particular node as its root i.e. the initiator is xed. So if the initiator node fails or any other node detects an altered behavior the diagnosis session will not start. Further the spanning tree repairing starts after the testing and gathering phase which increases the diagnosis latency as well as the communication complexity. Spanning tree maintenance and repairing consumes a lot of time, so constructing it in the testing and the gathering phase itself will be more efficient.

CONCLUSION

The earliest work on fault identi cation in case of mobile ad-hoc networks was carried out by Chessa and Santi in there work in [5]. There model, known as Static-DSDP, considers a comparison based approach and the network topology is assumed to be xed during the diagnosis session. Such type of network is known as xed topology network. Another work in case of xed topology MANETs was carried out by Elhadef et all. in [6]. The model also known as Dynamic-DSDP uses a spanning tree approach to disseminate the local diagnostic messages collected during the testing phase. Here the spanning tree is constructed after the fault status of the nodes has been identi ed.
Adaptive-DSDP [1] also considers a xed topology environment and a spanning tree approach which is a improvement over the Dynamic-DSDP model. In case of Adaptive-DSDP the spanning tree is initially con gured with the MANET and the protocol enables the maintenance as well as the recon guration of the spanning tree while the hosts are moving or they are diagnosed by their neighbor. In this thesis we have proposed a new model for xed topology environment. A spanning tree approach has also been considered here. In this model the testing of the nodes, gathering of information about neighbors and building of the spanning tree takes place simultaneously. As a result of which the maintenance and the repairing overhead of the spanning tree is completely avoided thus reducing the number of messages exchanged. We have also developed a simulator which can be applied to a network with large number of nodes. We have carried out the simulation in-order to nd out the total number of messages exchanged and the total diagnosis time. On analysing the results we have seen that our model performs better than its previous counterparts. The correctness and complexity proofs are also being provided. From the message and time complexity thus derived we see that our model performs better from a communication as well as latency viewpoint.

FUTURE WORK

The model we have proposed is for a xed topology MANET. Moreover this model identi es only the permanent faults. In future it is possible to extend this model for dynamic topology MANETs and for identifying intermittent as well as dynamic faults. One of the approach for identifying intermittent faults is to repeat our proposed algorithm for a xed number of times.

MATLAB SOURCE CODE

Instructions to run the code

1- Copy each of below codes in different M files.
2- Place all the files in same folder

3- Use the files from below link and download them into the same folder as the source codes

fault diagnosis timing

4- Also note that these codes are not in a particular order. Copy them all and then run the program.
5- Run the “FINAL.m” file

Code 1 – Script M File – FINAL.m

clc
clear
close all
%sn=50;
sn=input('Please enter the number of mobiles: '); %total number of mobile nodes
while isempty(sn) || ~isnumeric(sn) %loop for getting a valid number of mobile nodes
    sn=input('Please enter the number of mobiles: ');
end

% placing the sensors in the network
figure(1) 
[nodex,nodey,avg]=placing_sensors(sn); 
%avg
title('Ideal placement of the mobiles')
disp('Press enter to continue')
pause

%faulty nodes
figure(2)
[fnodex,fnodey,fni,l]=faulty_nodes(nodex,nodey,sn);
title('Mobile nodes with faulty points (red-permanent faults, black-transient faults, green-intermittent)')
%title('Flooding Process')
disp('Press enter to continue')
pause

%information transfer
[sender,receiver,s_coor,r_coor,opt]=getsensorinfo(sn,fni,nodex,nodey);
if opt==1 || opt==2 %case when either sender or receiver is out of bound
    figure(3)
    for i=1:sn
        plot(nodex(i),nodey(i),'b*') % plotting all the mobiles
        text(nodex(i)+0.05,nodey(i),num2str(i))
        hold on
    end
    %plot(s_coor(1),s_coor(2),'m*','LineWidth',3)
    text(s_coor(1)+0.5,s_coor(2),'\leftarrow Faulty Node','FontSize',12,'FontWeight','bold')
    
% elseif opt==2
%     figure(3)
%     for i=1:sn
%         plot(nodex(i),nodey(i),'b*') % plotting all the mobiles
%         text(nodex(i)+0.05,nodey(i),num2str(i))
%         hold on
%     end
%     %plot(d_coor(1),d_coor(2),'m*','LineWidth',3)
%     text(d_coor(1)+0.5,d_coor(2),'\leftarrow Faulty Node','FontSize',12,'FontWeight','bold')
    
elseif opt==0 %case when both sender and reciever are inbound
    figure(3)
%    subplot(2,1,1)    
    for i=1:length(nodex) %placement of nodes
        plot(nodex(i),nodey(i),'*')
        text(nodex(i)+0.05,nodey(i),num2str(i)) %numbering
        hold on
    end
    plot(s_coor(1),s_coor(2),'c*')
    text(s_coor(1)+0.05,s_coor(2),'\leftarrow Sender') %marking the sender
    plot(r_coor(1),r_coor(2),'c*')
    text(r_coor(1)+0.05,r_coor(2),'\leftarrow Receiver') %marking the reciever
%    [total1,good1,bad1,diag]=witherror(s_coor,r_coor,nodex,nodey,fnodex,fnodey,l); %subplot 1 for the scenario with errors
    [total1]=witherror(s_coor,r_coor,nodex,nodey,fnodex,fnodey,l,avg,fni,sender,receiver);
    title('Scenario with error detection')

%    subplot(2,1,2)    
    figure(4)
    for i=1:length(nodex)
        plot(nodex(i),nodey(i),'*') %placement of nodes
        text(nodex(i)+0.05,nodey(i),num2str(i)) %numbering
        hold on
    end
    plot(s_coor(1),s_coor(2),'c*')
    text(s_coor(1)+0.05,s_coor(2),'\leftarrow Sender') %marking the sender
    plot(r_coor(1),r_coor(2),'c*')
    text(r_coor(1)+0.05,r_coor(2),'\leftarrow Receiver') %marking the receiver
    [total2]=withouterror(s_coor,r_coor,nodex,nodey,fnodex,fnodey,l,avg,fni,sender,receiver); %subplot 2 for the scenario without errors
    title('Scenario without errors')
% 
%     %graphs
%     %graphs(good1,bad1,total1,total2,sn,nodex,nodey,diag)
% total1
% total2
    graphs(total1,total2,sn,nodex,nodey,fnodex,fnodey,fni)
end

Code 2 – Function M File -graphs.m

function graphs(total1,total2,sn,nodex,nodey,fnodex,fnodey,fni)

[r3 c3]=size(total1);
[r4 c4]=size(total2);

% total1
% total2

[num,txt,raw]=xlsread('fault diagnosis timing');
[row col]=size(num);
blank_mat=[];
for i=1:row
    blank_mat(i,:)= zeros(1,col);
end
xlswrite('fault diagnosis timing',blank_mat,1,'A4')

total1_ind=[];
total2_ind=[];

for i=1:r4 %finding the ids of the scenario with no errors
    for j=1:length(nodex)
        if total2(i,1)==nodex(j) && total2(i,2)==nodey(j)
            total2_ind=[total2_ind j];
        end        
    end
end      

for i=1:r3 %finding the ids of the scenario with errors
    for j=1:length(nodex)
        if total1(i,1)==nodex(j) && total1(i,2)==nodey(j)
            total1_ind=[total1_ind j];
        end        
    end
end      

t=[]; %diagnosable nodes
p=[]; %non diagnosable nodes
g=[]; %good nodes
len=length(fnodex);
for i=1:length(total1_ind)
    op=0; %if its 0, then its a good node. else bad node
    for j=1:length(fni)
        if total1_ind(i)==fni(j)
            if j>=ceil(len/4)+1 && j<=ceil(len/2)
                t=[t total1_ind(i)];
                op=1;                 
                break
            else
                p=[p total1_ind(i)];
                op=1;
                break
            end
        end
    end
    
    if op~=1
        g=[g total1_ind(i)];
    end
end
    
from=[];
to=[];
for i=1:length(total1_ind)    
    op=0;
    if i==1
        %first will always come in "from"
        from=[from total1_ind(i)];
        continue
    elseif i==length(total1_ind)        
        %last will always come in "to"
        to=[to total1_ind(i)];
        break
    end    
          
    for j=1:length(g) %cheking for good nodes
        if total1_ind(i)==g(j)
            to=[to total1_ind(i)];
            from=[from total1_ind(i)];
            op=1;
            break
        end
    end
    if op==1
        continue
    end
       
    for j=1:length(t) %checking for diagnosable nodes
        if total1_ind(i)==t(j)
            to=[to total1_ind(i)];
            from=[from total1_ind(i)];
            op=1;
            break
        end
    end
    if op==1
        continue
    end
    
    for j=1:length(p) %checking for permanent nodes
        if total1_ind(i)==p(j)
            to=[to total1_ind(i)];
            from=[from from(length(from))];
            op=1;
            break
        end
    end
    if op==1;
        continue
    end
end
    
ack_sent=[]; %time to send the acknowledgement
for i=1:length(from)
    dist=sqrt((nodex(from(i))-nodex(to(i)))^2+(nodey(from(i))-nodey(to(i)))^2); % distance between the nodes
    ack_sent=[ack_sent dist];
end

ack_rec=ack_sent; %time to receive the acknowledgement
l=length(fni);
permanent=fni([1:ceil(l/4) (ceil(l/2)+1):l]); %indexes without transient fault
diagnosable=fni(ceil(l/4)+1:ceil(l/2)); %indexes of transient fault
ind=1;
for i=to
    for j=permanent
        if i==j
            ack_rec(ind)=0;
        end
    end
    
    for k=diagnosable
        if i==k
            a=ack_rec(ind);
            b=ack_rec(ind)+4;
            time= a + (b-a).*rand(1,1);
            ack_rec(ind)=ack_rec(ind)+time;
        end
    end
    ind=ind+1;
end

xlswrite('fault diagnosis timing',from',1,'A4')
xlswrite('fault diagnosis timing',to',1,'B4')
xlswrite('fault diagnosis timing',ack_sent',1,'C4')
xlswrite('fault diagnosis timing',ack_rec',1,'D4')
xlswrite('fault diagnosis timing',total2_ind(1:end-1)',1,'F4') %"from" nodes in without error senario
xlswrite('fault diagnosis timing',total2_ind(2:end)',1,'G4') %"to" nodes in without error senario
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
oneunit=(1/sn)*100; % 1 unit of energy
Eg=(r3/sn)*100; % energy lost in good nodes
Eb=(r4/sn)*100; % energy lost in bad nodes

x1=[];
for i=1:length(from)
    x=100-oneunit*i;
    x1=[x1 x]; %nodewise energy loss in scenario with error nodes
end

x2=[];
for i=1:length(total2_ind(1:end-1))
    x=100-oneunit*i;
    x2=[x2 x]; %nodewise energy loss in scenario without error nodes
end

figure(5)
subplot(2,1,1)
bar(x1)
set(gca,'XTickLabel',{from})
ylabel('Energy')
xlabel('Nodes')
title('Scenario with error nodes')

subplot(2,1,2)
bar(x2)
set(gca,'XTickLabel',{total2_ind(1:end-1)})
xlabel('Nodes')
ylabel('Energy')
title('Scenario without error nodes')

%%------------- Data loss Graph --------------
lenth = length(x1);
for i = 1:lenth
b(i) = x1(lenth);
lenth = lenth-1;
end
x1_1 = b;

lenth1 = length(x2);
for i = 1:lenth1
b(i) = x2(lenth1);
lenth1 = lenth1-1;
end
x2_1 = b;

figure(6)
subplot(2,1,1)
grid on
plot(x1_1,'g','linewidth',2)
hold on
plot(x1_1,'ok','linewidth',3)
set(gca,'XTickLabel',{from})
title('DATA LOSS in Scenario with error nodes')

subplot(2,1,2)
grid on
plot(x2_1,'c','linewidth',2)
hold on
plot(x2_1,'or','linewidth',3)
set(gca,'XTickLabel',{total2_ind(1:end-1)})
title('DATA LOSS in Scenario without error nodes')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% delay graph
figure(7)
subplot(2,1,1)
plot(1:r3)
grid on
axis([0 r3+2 0 r3+2])
set(gca,'XTickLabel',{from})
title('Delay in scenario with errors')
xlabel('Serial Number of Nodes in path')
ylabel('Delay (sec)')

subplot(2,1,2)
plot(1:r4)
grid on
axis([0 r3+2 0 r3+2])
set(gca,'XTickLabel',{total2_ind(1:end-1)})
title('Delay in scenario without errors')
xlabel('Serial Number of Nodes in path')
ylabel('Delay (sec)')


Code 3 – Function M File – faulty_nodes.m

function [fnodex,fnodey,fni,l]=faulty_nodes(nodex,nodey,sn)

p=40; %percentage of faulty nodes out of the total number of nodes
fn=floor((p*sn)/100); %number of total faulty nodes (fn-faulty nodes)

fni=randi(sn,1,fn); %indexes of faulty nodes in nodex and nodey (fni-faulty nodes index)

fnodex=[];
fnodey=[];
for i=fni
    fnodex=[fnodex nodex(i)]; % assigning the x coordinates to faulty nodes (assignment is done using the previously assigned x nodes)
    fnodey=[fnodey nodey(i)]; % assigning the y coordiantes to faulty nodes (assignment is done using the previously assigned y nodes)
end

l=length(fni); % total number of faulty nodes 'l'

pfx=[fnodex(1:ceil(l/4))]; %permanent faults
pfy=[fnodey(1:ceil(l/4))];
%ceil(fni/4)

tfx=[fnodex((ceil(l/4)+1):ceil(l/2))]; %transient faults
tfy=[fnodey((ceil(l/4)+1):ceil(l/2))];
% (ceil(fni/4)+1)
% ceil(fni/2)

ifx=[fnodex((ceil(l/2)+1):ceil((3*l)/4))]; %intermittent fault
ify=[fnodey((ceil(l/2)+1):ceil((3*l)/4))];
% (ceil(fni/2)+1)
% ceil((3*fni)/4)

dfx=[fnodex((ceil((3*l)/4)+1):l)]; %dynamic faults
dfy=[fnodey((ceil((3*l)/4)+1):l)];
% (ceil((3*fni)/4)+1)

for i=1:sn
    plot(nodex(i),nodey(i),'b*') % plotting all the mobiles
    text(nodex(i)+0.05,nodey(i),num2str(i))
    hold on
end

%flooding process starts %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[num,txt,raw]=xlsread('static faults');
[row col]=size(num);
blank_mat=[];
for i=1:row
    blank_mat(i,:)= zeros(1,col);
end
xlswrite('static faults',blank_mat,1,'A5')

x=1; %initialize the index of master node
for i=1:sn    
    for j=1:length(fni)
        if i~=fni(j)
            x=i; % master node index which is fault free
            break
        end
    end
    if x==i
        break
    end
end            

pause(0.5)
plot(nodex(x),nodey(x),'b*','LineWidth',5)
text(nodex(x)+0.05,nodey(x),'\leftarrow Master Node','FontSize',15,'FontWeight','bold')
pause(0.5)
for i=1:sn
    plot([nodex(x) nodex(i)],[nodey(x) nodey(i)])
end
pause(0.5)
for i=1:sn
    plot([nodex(x) nodex(i)],[nodey(x) nodey(i)],'w')
end
pause(0.5)
for i=1:sn
    plot([nodex(x) nodex(i)],[nodey(x) nodey(i)])
end
pause(0.5)
for i=1:sn
    plot([nodex(x) nodex(i)],[nodey(x) nodey(i)],'w')
end
for i=1:sn
    plot([nodex(x) nodex(i)],[nodey(x) nodey(i)])
end
pause(0.5)
for i=1:sn
    plot([nodex(x) nodex(i)],[nodey(x) nodey(i)],'w')
end

time=[];
for i=[1:(x-1) (x+1):sn]
    dist=sqrt((nodex(x)-nodex(i))^2+(nodey(x)-nodey(i))^2);
    time=[time; dist]; % calculating the distances of each node from master node
end

xlswrite('static faults',[1:(x-1) (x+1):sn]',1,'A5')
xlswrite('static faults',time,1,'B5')
xlswrite('static faults',time,1,'C5')

ind=1;
for i=[1:(x-1) (x+1):sn] %excluding the master node here
    for j=[fni(1:(l/2))] %these excludes the dynamic faults, they are not yet discovered. also excludes intermittent faults
        if i==j
            time(ind)=0;
            break
        end
    end
    
    for j=[fni((ceil(l/2)+1):ceil((3*l)/4))]
        if i==j
            a=time(ind);
            b=time(ind)+5; 
            X= a + (b-a).*rand(1,1);
            time(ind)=time(ind)+X;
            break
        end
    end    
    ind=ind+1;
end
xlswrite('static faults',time,1,'D5')
%flooding process ends %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

plot(pfx,pfy,'ro','LineWidth',3) %displaying the permanent faults
plot(tfx,tfy,'ko','LineWidth',3) %displaying the transient faults
plot(ifx,ify,'go','LineWidth',3) %displaying the intermittent faults
%plot(dfx,dfy,'mo','LineWidth',3) %displaying the dynamic faults
%legend('r* Permanent','k* Transient','g* Intermittent','m* Dynamic','Location','NorthEastOutside')

%line 98-102 for the erased nodes during flooding
for i=1:sn
    plot(nodex(i),nodey(i),'b*') % plotting all the mobiles
    text(nodex(i)+0.05,nodey(i),num2str(i))
    hold on
end

end

Code 4 – Function M File – check_nn2.m

function [coor2]=check_nn2(nn,fnodex,fnodey,len,nodex,nodey,coor1)

i=1;
while i>=1
    [r1 c1]=size(nn);
    if i>r1
        x=randi(4);
        switch x
            case 1
                [leftnodesx,leftnodesy]=nodes_left(coor1,nodex,nodey);
                searchx=leftnodesx;
                searchy=leftnodesy;
            case 2
                [upnodesx,upnodesy]=nodes_up(nodex,nodey,coor1);
                searchx=upnodesx;
                searchy=upnodesy;
            case 3
                [downnodesx,downnodesy]=nodes_down(nodex,nodey,coor1);
                searchx=downnodesx;
                searchy=downnodesy;
            case 4
                [rightnodesx,rightnodesy]=nodes_right(coor1,nodex,nodey);
                searchx=rightnodesx;
                searchy=rightnodesy;
            otherwise
                disp('wrong')
        end        
        [nn]=nearest_node(searchx,searchy,coor1);
        i=1;
    end 
% i=1;
% while i>=1    
    for j=1:length(fnodex)
        if nn(i,1)==fnodex(j) && nn(i,2)==fnodey(j)        
%             bad=[bad; fnodex(j) fnodey(j)];
%             total=[total; fnodex(j) fnodey(j)];
            i=i+1;
            coor2=[];
            break
        else
            coor2=[nn(i,1) nn(i,2)];
        end
    end
    
    if ~isempty(coor2)        
        break
    end
end

end

Code 5 – Function M File – nearest_node.m

%function [coor2_1,coor2_2,coor2_3,coor2_4,coor2_5]=nearest_node(nodesx,nodesy,coor1,savenodes)
function [nn]=nearest_node(nodesx,nodesy,coor1)
distance=[];
nn=[];
%[rs cs]=size(savenodes);
% for i=1:length(nodesx)
%     for j=1:rs
%         if nodesx(i)~=savenodes(j,1) && nodesy(i)~=savenodes(j,2)
%             remnodesx=[remnodesx nodesx(i)];
%             remnodesy=[remnodesy nodesy(i)];
%         end
%     end
% end

for i=1:length(nodesx)
    dist=sqrt( ((coor1(1)-nodesx(i))^2) + ((coor1(2)-nodesy(i))^2) );
    distance=[distance dist];
end

[b,ix]=sort(distance); 

for i=ix
    nn=[nn; nodesx(i) nodesy(i)];
end


% [r1 c1]=size(nn);
% [r2 c2]=size(used_nodes);
% for i=1:r1
%     for j=1:r2
%         if nn(i,1)==used_nodes(j,1) && nn(i,2)==used_nodes(j,2)
%             nn(i,:)=[];
%         end
%     end
% end

% if nargout==0
%     coor2_1=coor1;
%     coor2_2=coor1;
%     coor2_3=coor1;
%     coor2_4=coor1;
%     coor2_5=coor1;
% end

end

Code 6 – Function M File – blinking_line.m

function blinking_line(coor1,coor2)
%disp('BLINKING LINE')
for iiii=1:3   
        abc=plot([coor1(1) coor2(1)],[coor1(2) coor2(2)]);
        pause(0.3)
        delete(abc)
        pause(0.3)
        abc=plot([coor1(1) coor2(1)],[coor1(2) coor2(2)]);
        pause(0.3)
        delete(abc)
        pause(0.3)
%        abc=plot([coor1(1) coor2(1)],[coor1(2) coor2(2)]);
end
    
end

Code 7 – Function M File – bestmatch.m

function bestmatch(iii,coor1,coor2,fnodex,fnodey,l)
% coor1
% coor2
% pause
if iii>=1 && iii<=ceil(l/4)
    hold on
    blinking_line(coor1,coor2) %%%%%%%%%%% NOT WORKING %%%%%%%%%%%
    plot([coor1(1) fnodex(iii)],[coor1(2) fnodey(iii)],'r','LineWidth',2) %%%%%%%%%%% NOT WORKING %%%%%%%%%%%
    plot(fnodex(iii),fnodey(iii),'r*','LineWidth',2)
    text(fnodex(iii)+0.05,fnodey(iii),'\leftarrow Permanent Error')
elseif iii>=(ceil(l/2)+1) && iii<=ceil((3*l)/4)
    hold on
    blinking_line(coor1,coor2)  %%%%%%%%%%% NOT WORKING %%%%%%%%%%%
    plot([coor1(1) fnodex(iii)],[coor1(2) fnodey(iii)],'g','LineWidth',2) %%%%%%%%%%% NOT WORKING %%%%%%%%%%%
    plot(fnodex(iii),fnodey(iii),'g*','LineWidth',2)
    text(fnodex(iii)+0.05,fnodey(iii),'\leftarrow Intermittent Error')
elseif iii>=(ceil((3*l)/4)+1) && iii<=l
    hold on
    blinking_line(coor1,coor2)  %%%%%%%%%%% NOT WORKING %%%%%%%%%%%
    plot([coor1(1) fnodex(iii)],[coor1(2) fnodey(iii)],'m','LineWidth',2)  %%%%%%%%%%% NOT WORKING %%%%%%%%%%%
    plot(fnodex(iii),fnodey(iii),'m*','LineWidth',2)
    text(fnodex(iii)+0.05,fnodey(iii),'\leftarrow Dynamic Error')
end

end

Code 8 – Function M File – infotransfer1.m

function nn=infotransfer1(coor1,r_coor,nodex,nodey,total)

hori=coor1(1)-r_coor(1); 
vert=coor1(2)-r_coor(2); 

if abs(hori)>abs(vert)
    
    
elseif abs(hori)<abs(vert)
    
end

Code 9 – Function M File – nodes_down.m

function [downnodesx,downnodesy]=nodes_down(nodesx,nodesy,coor1,r_coor)

downnodesx=[];
downnodesy=[];
%downnodes_index=[];

for i=1:length(nodesx)
    if nodesy(i)<coor1(2) && nodesy(i)>=r_coor(2)
        downnodesx=[downnodesx nodesx(i)];            
        downnodesy=[downnodesy nodesy(i)];
        
%        downnodes_index=[downnodes_index i]; % index of nodex and nodey for upnodesx and upnodesy
    end
end



end

Code 10 – Function M File – nodes_left.m

function [leftnodesx,leftnodesy]=nodes_left(coor1,nodex,nodey,r_coor)

leftnodesx=[];
leftnodesy=[];
%leftnodes_index=[];

for i=1:length(nodex)
    if nodex(i)<coor1(1) && nodex(i)>=r_coor(1)
        leftnodesx=[leftnodesx nodex(i)];            
        leftnodesy=[leftnodesy nodey(i)];
        
 %       leftnodes_index=[leftnodes_index i]; % index of nodex and nodey for leftnodex and leftnodey
    end
end

end

Code 11 – Function M File – nodes_right.m

function [rightnodesx,rightnodesy]=nodes_right(coor1,nodex,nodey,r_coor)

rightnodesx=[];
rightnodesy=[];
%rightnodes_index=[];

for i=1:length(nodex)
    if nodex(i)>coor1(1) && nodex(i)<=r_coor(1)
        rightnodesx=[rightnodesx nodex(i)];            
        rightnodesy=[rightnodesy nodey(i)];
        
%        rightnodes_index=[rightnodes_index i]; % index of nodex and nodey for leftnodex and leftnodey
    end
end

end

Code 12 – Function M File – nodes_up.m

function [upnodesx,upnodesy]=nodes_up(nodesx,nodesy,coor1,r_coor)

upnodesx=[];
upnodesy=[];
%upnodes_index=[];

for i=1:length(nodesx)
    if nodesy(i)>coor1(2) && nodesy(i)<=r_coor(2)
        upnodesx=[upnodesx nodesx(i)];            
        upnodesy=[upnodesy nodesy(i)];
        
 %       upnodes_index=[upnodes_index i]; % index of nodex and nodey for upnodesx and upnodesy
    end
end

end

Code 13 – Function M File – getsensorinfo.m

function [sender,receiver,s_coor,r_coor,opt]=getsensorinfo(sn,fni,nodex,nodey)

opt=0;
sender=input('Enter the sender node ID: '); %sender node ID
while isempty(sender) || sender>sn  %getting a valid value
    sender=input('Enter the sender node ID: ');
end

for i=fni
    if sender==i;
%        error('The sender is a faulty node')                
        opt=1; %when sender is out bound
    end
end

receiver=input('Enter the receiver node ID: '); %reciever node ID
while isempty(receiver) || receiver>sn  %getting a valid value
    receiver=input('Enter the receiver node ID: ');
end

for i=fni
    if receiver==i;
        %error('The receiver is a faulty node')                
        opt=2; %when reciever is out bound
    end
end

s_coor=[nodex(sender) nodey(sender)]; %sender coordinates
r_coor=[nodex(receiver) nodey(receiver)]; %receiver coordinates

end

Code 14 – Function M File – infotransfer.m

function nn=infotransfer(coor1,r_coor,nodex,nodey,total)

hori=coor1(1)-r_coor(1); %horizontal distance between "coor1" and "r_coor"
vert=coor1(2)-r_coor(2); %vertical distance between "coor1" and "r_coor"
% searchnodesx1=[];
% searchnodesy1=[];
if abs(hori)>abs(vert) %if hosirzontal distance is greater then vertical distance
    % search horizontally 
    if coor1(1)>r_coor(1) % search in left direction    
        [leftnodesx,leftnodesy]=nodes_left(coor1,nodex,nodey,r_coor); %set of nearest nodes in left direction till "r_coor"       
        
%         for i=1:length(leftnodesx)
%             if r_coor(1)<leftnodesx(i)
%                 searchnodesx1=[searchnodesx1; leftnodesx(i)];
%                 searchnodesy1=[searchnodesy1; leftnodesy(i)];
%             end
%         end
                
        if coor1(2)<r_coor(2) %search in upwards direction
            [upnodesx,upnodesy]=nodes_up(leftnodesx,leftnodesy,coor1,r_coor);
            searchnodesx=upnodesx;
            searchnodesy=upnodesy;
        elseif coor1(2)>r_coor(2) %search in downwards direction
            [downnodesx,downnodesy]=nodes_down(leftnodesx,leftnodesy,coor1,r_coor);
            searchnodesx=downnodesx;
            searchnodesy=downnodesy;
        end                 
    elseif coor1(1)<r_coor(1) % search in right direction
        [rightnodesx,rightnodesy]=nodes_right(coor1,nodex,nodey,r_coor);
        
%         for i=1:length(rightnodesx)
%             if r_coor(1)>rightnodesx(i)
%                 searchnodesx1=[searchnodesx1; rightnodesx(i)];
%                 searchnodesy1=[searchnodesy1; rightnodesy(i)];
%             end
%         end
        
        if coor1(2)<r_coor(2) %search in upwards direction
            [upnodesx,upnodesy]=nodes_up(rightnodesx,rightnodesy,coor1,r_coor);
            searchnodesx=upnodesx;
            searchnodesy=upnodesy;
        elseif coor1(2)>r_coor(2) %search in downwards direction
            [downnodesx,downnodesy]=nodes_down(rightnodesx,rightnodesy,coor1,r_coor);
            searchnodesx=downnodesx;
            searchnodesy=downnodesy;
        end                    
    end    
elseif abs(hori)<abs(vert)
    % search vertically
    if coor1(2)>r_coor(2) % search in down direction
        [downnodesx,downnodesy]=nodes_down(nodex,nodey,coor1,r_coor);        
        
%         for i=1:length(downnodesy)
%            if r_coor(2)<downnodesy(i)
%                 searchnodesx1=[searchnodesx1; downnodesx(i)];
%                 searchnodesy1=[searchnodesy1; downnodesy(i)];
%             end
%         end
        
        if coor1(1)<r_coor(1) %search in right direction
            [rightnodesx,rightnodesy]=nodes_right(coor1,downnodesx,downnodesy,r_coor);            
            searchnodesx=rightnodesx;
            searchnodesy=rightnodesy;
        elseif coor1(1)>r_coor(1) %search in left direction
            [leftnodesx,leftnodesy]=nodes_left(coor1,downnodesx,downnodesy,r_coor);            
            searchnodesx=leftnodesx;
            searchnodesy=leftnodesy;
        end        
    elseif coor1(2)<r_coor(2) % search in up direction
        [upnodesx,upnodesy]=nodes_up(nodex,nodey,coor1,r_coor);        
        
%         for i=1:length(upnodesy)
%            if r_coor(2)>upnodesy(i)
%                 searchnodesx1=[searchnodesx1; upnodesx(i)];
%                 searchnodesy1=[searchnodesy1; upnodesy(i)];
%             end
%         end

        if coor1(1)<r_coor(1) %search in right direction
            [rightnodesx,rightnodesy]=nodes_right(coor1,upnodesx,upnodesy,r_coor);            
            searchnodesx=rightnodesx;
            searchnodesy=rightnodesy;
        elseif coor1(1)>r_coor(1) %search in left direction
            [leftnodesx,leftnodesy]=nodes_left(coor1,upnodesx,upnodesy,r_coor);            
            searchnodesx=leftnodesx;
            searchnodesy=leftnodesy;
        end        
    end    
end

[nn]=nearest_node(searchnodesx,searchnodesy,coor1); %set of nearest nodes from "coor1" in the direction of "r_coor"
%nn2=nn;
 nn2=[];
 %pause
if ~isempty(total) %loop to delete already occured nodes 
    [r1 c1]=size(nn);
    [r2 c2]=size(total);
    for i=1:r1
        for j=1:r2            
            if nn(i,1)==total(j,1) && nn(i,2)==total(j,2)
                nn2=[nn2 i];
%                 i
%                 disp('caught')
%                 nn2(i,:)
%                 nn2(i,:)=[]; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            end
        end
    end    
end
nn(nn2,:)=[]; %deleting the rows from "nn" which have already occured

% [r c]=size(nn);
% for i=1:r
%     plot(nn(i,1),nn(i,2),'y*')
% end
% pause(1)
% for i=1:r
%     plot(nn(i,1),nn(i,2),'b*')
% end
    
%nn=nn2;
end

Code 15 – Function M File – placing_sensors.m

function [nodex,nodey,avg]=placing_sensors(sn)

low=0; %lower bound to both the axis
high=10; %upper bound to both the axis
 
nodex=[];
nodey=[];
for i=1:sn
    nodex=[nodex (low+(high-low)*rand)];  %x coordinates of all the points
    nodey=[nodey (low+(high-low)*rand)];  %y coordinates of all the points
end
 
for i=1:sn 
    plot(nodex(i),nodey(i),'b*') %ploting the x and y coordinates
    text(nodex(i)+0.05,nodey(i),num2str(i)) %giving all the nodes there respective numbers
    hold on
end

av=0;
ind=0;
for i=1:length(nodex)
    for j=1:length(nodex)
        dist=sqrt((nodex(i)-nodex(j))^2+(nodey(i)-nodey(j))^2);
        av=av+dist;
        ind=ind+1;
    end
end

avg=av/ind;
end

Code 16 – Function M File – witherror.m

%function [total,good,bad,diag]=witherror(s_coor,r_coor,nodex,nodey,fnodex,fnodey,l)
function [total]=witherror(s_coor,r_coor,nodex,nodey,fnodex,fnodey,l,avg,fni,sender,receiver)

coor1=s_coor; %initialising "coor1" with the value of "sender coordinate" i.e. "s_coor"
%blinking will always occur from "coor1" to "coor2"
total=[];
total=[total; coor1];
% good=[];
% bad=[];
% diag=[];
while coor1(1)~=r_coor(1) && coor1(2)~=r_coor(2)   % loop will run untill "coor1" is not equal to "receiver coordinates" i.e. "r_coor"
    nn=infotransfer(coor1,r_coor,nodex,nodey,total); %finding out the valid group of nodes to mark "coor2"
    %pause    
    [coor2,total,x]=check_nn(nn,fnodex,fnodey,l,nodex,nodey,coor1,total,r_coor); %finding suitable node "coor2" from "nn". also stores its coordinates
    if x==0 %the node is not diagnosable
        [coor2]=check_avg(coor2,coor1,avg,nodex,nodey,r_coor,fni,sender,receiver); 
    end    
    blinking_line(coor1,coor2)
    plot([coor1(1) coor2(1)],[coor1(2) coor2(2)],'k','LineWidth',2)
    if coor2(1)==r_coor(1) && coor2(2)==r_coor(2)
        total=[total; coor2];
%        good=[good; coor2];        
        break
    else
        coor1=coor2;                
        total=[total; coor2];
%        good=[good; coor1];
    end
end

end

Code 17 – Function M File – withouterror.m

function [total]=withouterror(s_coor,r_coor,nodex,nodey,fnodex,fnodey,l,avg,fni,sender,receiver)

coor1=s_coor;
total=[];
while coor1(1)~=r_coor(1) && coor1(2)~=r_coor(2)    
    nn=infotransfer(coor1,r_coor,nodex,nodey,total);
%    [coor2]=check_nn2(nn,fnodex,fnodey,l,nodex,nodey,coor1);
    coor2=[nn(1,1) nn(1,2)];
    [coor2]=check_avg2(coor2,coor1,avg,nodex,nodey,r_coor,fni,sender,receiver);
    blinking_line(coor1,coor2)
    plot([coor1(1) coor2(1)],[coor1(2) coor2(2)],'k','LineWidth',2)
    total=[total; coor1];    
    if coor2(1)==r_coor(1) && coor2(2)==r_coor(2)
        total=[total; r_coor];    
        break
    else
        coor1=coor2;                
    end
end

end

Code 18 – Function M File – check_nn.m

%function [coor2,bad,total,diag]=check_nn(nn,fnodex,fnodey,len,nodex,nodey,coor1,bad,total,r_coor,diag)
function [coor2,total,x]=check_nn(nn,fnodex,fnodey,len,nodex,nodey,coor1,total,r_coor,fni,avg,sender,receiver)
%[coor2]=check_avg2(coor2,coor1,avg,nodex,nodey,r_coor,fni,sender,receiver)
i=1;
x=0;
while i>=1
    [r1 c1]=size(nn);
    if i>r1 % loop will run when the given "nn" does not have any value. we have to find new set of "nn"
 %       disp('yes')
        [leftnodesx,leftnodesy]=nodes_left(coor1,nodex,nodey,r_coor); %nodes to the left of "coor1" till "r_coor"
        [upnodesx,upnodesy]=nodes_up(nodex,nodey,coor1,r_coor); %nodes to the up of "coor1" till "r_coor"
        [downnodesx,downnodesy]=nodes_down(nodex,nodey,coor1,r_coor); %nodes to the down of "coor1" till "r_coor"
        [rightnodesx,rightnodesy]=nodes_right(coor1,nodex,nodey,r_coor); %nodes to the right of "coor1" till "r_coor"
       
        [nn1]=nearest_node(upnodesx,upnodesy,coor1); %arranging the obtained sets in ascending order. it contains the nearest nodes 
        [nn2]=nearest_node(downnodesx,downnodesy,coor1);
        [nn3]=nearest_node(leftnodesx,leftnodesy,coor1);
        [nn4]=nearest_node(rightnodesx,rightnodesy,coor1);
        
        if ~isempty(nn1) %if "nn1" is not empty, it will find the nearest node 
            for ii=1:length(fnodex)
                if nn1(1,1)==fnodex(ii) && nn1(1,2)==fnodey(ii) %cheking if the nearest nodes in up direction is faulty
                    a=[];
                    break
                else
                    a=[nn1(1,1) nn1(1,2)]; %nearest nodes in up direction
                end
            end
        else % if "nn1" is empty
             a=[];
        end 
       
         if ~isempty(nn2)%if "nn2" is not empty, it will find the nearest node 
            for ii=1:length(fnodex)
                if nn2(1,1)==fnodex(ii) && nn2(1,2)==fnodey(ii) %cheking if the nearest nodes in down direction is faulty
                    b=[];
                    break
                else
                    b=[nn2(1,1) nn2(1,2)]; %nearest node in down direction
                end
            end
         else% if "nn2" is empty
             b=[];
         end
        
         if ~isempty(nn3)%if "nn3" is not empty, it will find the nearest node 
            for ii=1:length(fnodex)    
                if nn3(1,1)==fnodex(ii) && nn3(1,2)==fnodey(ii) %cheking if the nearest nodes in left direction is faulty
                    c=[];
                    break
                else
                    c=[nn3(1,1) nn3(1,2)]; %nearest node in left direction
                end
            end
         else% if "nn3" is empty
             c=[];
         end
        
         if ~isempty(nn4)%if "nn4" is not empty, it will find the nearest node 
            for ii=1:length(fnodex)    
                if nn4(1,1)==fnodex(ii) && nn4(1,2)==fnodey(ii) %cheking if the nearest nodes in right direction is faulty
                    d=[];
                    break
                else
                    d=[nn4(1,1) nn4(1,2)]; %nearest node in right direction
                end
            end
         else% if "nn4" is empty
             d=[];
         end
            
        if ~isempty(a)
            ad=sqrt((coor1(1)-a(1))^2+(coor1(2)-a(2))^2); %distance to the nearest up node
        else
            ad=50; %high value given so that it will not be chosen at the time of selection
        end
        
        if ~isempty(b)
            bd=sqrt((coor1(1)-b(1))^2+(coor1(2)-b(2))^2); %distance to the nearest down node
        else
            bd=50;%high value given so that it will not be chosen at the time of selection
        end
        
        if ~isempty(c)
            cd=sqrt((coor1(1)-c(1))^2+(coor1(2)-c(2))^2); %distance to the nearest left node
        else
            cd=50;%high value given so that it will not be chosen at the time of selection
        end
        
        if ~isempty(d)
            dd=sqrt((coor1(1)-d(1))^2+(coor1(2)-d(2))^2); %distance to the nearest right node
        else
            dd=50;%high value given so that it will not be chosen at the time of selection
        end
        
        mat=[ad bd cd dd];
        [mini,ind]=min(mat);  %finding the closest of all the 4 direction nodes
        switch ind
            case 1
                nn=nn1; %up is closest
            case 2
                nn=nn2; %down is closest
            case 3
                nn=nn3; %left is closest
            case 4
                nn=nn4; %right is closest
            otherwise
                disp('wrong')
        end
        
        i=1;
    end 
    
    for j=1:length(fnodex) %loop will rum for the number of faulty nodes times
        if nn(i,1)==fnodex(j) && nn(i,2)==fnodey(j) %checking the nearest node for faultyness                        
            coor_1=coor1;
            coor_2=[fnodex(j) fnodey(j)];
            %[coor2]=check_avg2(coor2,coor1,avg,nodex,nodey,r_coor,fni,sender,receiver);
            %pause
            blinking_line(coor_1,coor_2)  %this blinking is till the faulty node
%            disp('---------')            
            x=0;
            if j>=(ceil(len/4)+1) && j<=ceil(len/2)
                plot(fnodex(j),fnodey(j),'ko','LineWidth',2)
                text(fnodex(j)+0.05,fnodey(j),'\leftarrow Transient Error Diagnosed')
                coor2=[fnodex(j) fnodey(j)];
                %diag=[diag; coor2];
%                total=[total; coor2];
                x=1; %this means that the faulty node is diagnosable
                break
            else
                bestmatch(j,coor_1,coor_2,fnodex,fnodey,len) 
                %bad=[bad; fnodex(j) fnodey(j)]; %storing the coordinates
                total=[total; fnodex(j) fnodey(j)];
            end     
            
            if x==1 % if the node can be diagnosed, break. no need to search further
                break
            else % if the node cnt be diagnosed, mark it with its color
                i=i+1; % and move on to the next nearest node in "nn"
                coor2=[]; %no value of "coor2" obtained
                break
            end
        else
            coor2=nn(i,:); % "coor2" is the i^th nearest node from "nn" as it is not a faulty node as checked at line 113
        end
    end
    
    if ~isempty(coor2) %breaks the loop if we have a value of "coor2"
        break
    end
end

end

Code 19 – Function M File – check_avg.m

function [coor2]=check_avg(coor2,coor1,avg,nodex,nodey,r_coor,fni,sender,receiver)

dist=sqrt((coor1(1)-coor2(1))^2+(coor1(2)-coor2(2))^2);
range=ceil(avg/2);

if dist<range;
    coor2=coor2;
    
elseif dist>range;
    range_ind=[]; %nodes in the range of coor1
    for i=[1:sender-1  sender+1:length(nodex)]
        dist=sqrt((coor1(1)-nodex(i))^2+(coor1(2)-nodey(i))^2);
        if dist<=range
            range_ind=[range_ind i]; %finding all the nodes in the range of coor1
        end
    end    
    %range_ind
    
    for i=1:length(fni)
        x=find(range_ind==fni(i));
        range_ind(x)=[]; %deleting the faulty indexes
    end
    %range_ind
    %pause

    %the closest node to r_coor in the range of coor1 will connected
    distances=[];
    for i=range_ind
        dist=sqrt((r_coor(1)-nodex(i))^2+(r_coor(2)-nodey(i))^2);
        distances=[distances; i dist];
    end    
    x=find(distances(:,2)==min(distances(:,2)));
    coor2=[nodex(distances(x,1)) nodey(distances(x,1))];    
end    

Code 20 – Function M File – check_avg2.m

function [coor2]=check_avg2(coor2,coor1,avg,nodex,nodey,r_coor,fni,sender,receiver)

dist=sqrt((coor1(1)-coor2(1))^2+(coor1(2)-coor2(2))^2);
range=ceil(avg/2);

if dist<range;
    coor2=coor2;
    
elseif dist>range;
    range_ind=[]; %nodes in the range of coor1
    for i=[1:sender-1  sender+1:length(nodex)]
        dist=sqrt((coor1(1)-nodex(i))^2+(coor1(2)-nodey(i))^2);
        if dist<=range
            range_ind=[range_ind i]; %finding all the nodes in the range of coor1
        end
    end    
    %range_ind
    
    %the closest node to r_coor in the range of coor1 will connected
    distances=[];
    for i=range_ind
        dist=sqrt((r_coor(1)-nodex(i))^2+(r_coor(2)-nodey(i))^2);
        distances=[distances; i dist];
    end    
    x=find(distances(:,2)==min(distances(:,2)));
    coor2=[nodex(distances(x,1)) nodey(distances(x,1))];    
end    

 

Forest fire detection system

PROJECT VIDEO

SCENARIO

1- Deploy nodes in the forest.

2- All the nodes in the equal range.

3- All the nodes that are in the one range having a one cluster head they work as a cluster coordinator.

4- Data aggregation happening on that point.

5- In the scenario first we send packet from source to destinatiosink,n when environment having no problem.

6- In the second when the fire happen that time those will detect the fire this node working as a source sent information to destination (base station).

7- Third case when fire destroy the cluster head then election algorithm elect the new cluster head and communicate with other nodes sent information to destination.

MATLAB SOURCE CODE

Instructions to run the code

1- Copy each of below codes in different M files.
2- Place all the files in same folder

3- Use the Excel files from below link and download them into the same folder as the source codes

Timeframe

4- Also note that these codes are not in a particular order. Copy them all and then run the program.
5- Run the “FINAL.m” file

Code 1 – Script M File – FINAL.m

clc
clear all
close all

disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%%%%%%%%%%%  WELCOME TO JUNGLE FIRE DETECTION PROGRAMME  %%%%%%%%%%%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')

hn=input('Please enter the approx number of HEAD NODES (minimum 5): '); % number of head nodes
while hn<5 % minimum number of head nodes must be 5
    disp('Too few sensors, please enter again')
    hn=input('Please enter the approx number of HEAD NODES (minimum 5): ');
end
%hn=30;
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Placing the sensors')
disp('Press enter to continue..')
pause

figure(1)
[x,y,x1,y1,row,col,bp]=placing_sensors(hn);
title('Placement of the sensors with networking between the head nodes and base station')
hold off

disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Condition of no fire')
disp('Press enter to continue..')
pause

for i=1:2
    figure(2)
    normal_condition(hn)
    title('Flow of information when there is no fire')
    hold off
    pause(0.3)
    if i==3
        break
    else
        set(gca,'Color','w')
    end
end

disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Condition of fire')
disp('Press enter to continue..')
pause

figure(3)
start_fire=fire_condition(hn);
title('Condition of fire increasing through the jungle')
hold off

disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Condition of  fire detection')
disp('Press enter to continue..')
pause

figure(4)
%firedetection2(hn,x,y,x1,y1,row,col)
[axis,time,burntnodes,basetime,times,unik]=fire_detection3(hn,bp,x,y,x1,y1,row,col);
title('Detecting the fire(scenario 1)')

% figure(5)
% [axis,time,burntnodes,basetime]=fire_detection(hn,bp,x,y,x1,y1,row,col);
% %test_scene(hn)
% title('Detecting the fire(scenario 1)')

disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Graphs')
disp('Press enter to continue..')
pause
% 
graphs(axis,time,burntnodes,x1,y1,basetime,x,y,times,unik,start_fire)

Code 2 – Function M File – fire_condition.m

function [start_fire]=fire_condition(hn)

[x,y,x1,y1,row,col,bp]=placing_sensors(hn);

[r c]=size(x);
dh=sqrt((x(1,1)-x(1,c))^2 + (y(1,1)-y(1,c))^2);
dv=sqrt((x(1,1)-x(r,1))^2 + (y(1,1)-y(r,1))^2);

start_fire=[((dh/2)+x(1,1)) ((dv/2)+4)];
plot(start_fire(1),start_fire(2),'ko')

div=row/col;

if (start_fire(2)-y(1,1))<(start_fire(1)-x(1,1))
    ii=1;
    ij=1;
    while ii<(start_fire(2)-y(1,1)) || ij<(start_fire(2)-y(1,1))    
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
    end
elseif (start_fire(2)-y(1,1))>(start_fire(1)-x(1,1))
    ii=1;
    ij=1;
    while ii<(start_fire(1)-x(1,1)) || ij<(start_fire(1)-x(1,1))    
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
    end
end

end

Code 3 – Function M File – placing_sensors.m

function [x,y,x1,y1,row,col,bp]=placing_sensors(hn)

col=5*ceil(hn/30); %0-30hn=5col, 30-60hn=10col...
row=ceil(hn/col); 

rectangle('Position',[0 ((row*4)+6) 5 4],'Facecolor','r')
text(1,((row*4)+8),'BASE')
hold on
[x y]=meshgrid(linspace(1,(col*4),col),linspace(4,(row*4),row));
plot(x,y,'go','LineWidth',5)
axis([-5 ((col*4)+5) 0 ((row*4)+10)])

bp=[5 ((4*row)+6)]; % base station reciever point
a=x(1,:);
b=y(end,:);
for i=1:length(a)
    plot([a(i) bp(1)],[b(i) bp(2)],'LineWidth',2);
end

% difference=(row*col)-hn;
% if difference~=0
%     for i=1:difference
%         plot(x(1,end-(i-1)) , y(1,end-(i-1)) , 'wo','LineWidth',5)
%     end
% end
% clear difference

%if mod(hn,col)==0 % network when number of head nodes is a multiple of col
    [r c]=size(x);
    a1=x(1,:);
    b1=x(r,:);
    c1=y(1,:);
    d1=y(r,:);
    
    a2=x(:,1);
    b2=x(:,c);
    c2=y(:,1);
    d2=y(:,c);
    
    for i=1:length(a1)
        m1=[a1(i) b1(i)];
        n1=[c1(i) d1(i)];
        plot(m1,n1,'LineWidth',2)
    end
    
    for i=1:length(a2)
        m2=[a2(i) b2(i)];
        n2=[c2(i) d2(i)];
        plot(m2,n2,'LineWidth',2)
    end
% else % network when number of head nodes is not a multiple of col
%     [r c]=size(x);
%     a1=x(r,:);
%     b1=x(2,:);
%     c1=y(r,:);
%     d1=y(2,:);
%     
%     a2=x(r:-1:2,1);
%     b2=x(r:-1:2,c);
%     c2=y(r:-1:2,1);
%     d2=y(r:-1:2,c);
%     
%     for i=1:length(a1)
%         m1=[a1(i) b1(i)];
%         n1=[c1(i) d1(i)];
%         plot(m1,n1,'LineWidth',2)
%     end
%     
%     for i=1:length(a2)
%         m2=[a2(i) b2(i)];
%         n2=[c2(i) d2(i)];
%         plot(m2,n2,'LineWidth',2)
%     end
%     
%     difference=hn-(col*(row-1));    
%     plot([x(1,1) x(1,difference)],[y(1,1) y(1,difference)],'LineWidth',2)
%     for i=1:difference
%         plot([x(1,i) x(2,i)],[y(1,i) y(2,i)],'LineWidth',2);
%     end    
% end
clear difference r c
dh=sqrt((x(1,1)-x(1,2))^2+((y(1,1)-y(1,2))^2)); % horizontal distance between two head nodes
dv=sqrt((x(1,1)-x(2,1))^2+((y(1,1)-y(2,1))^2)); % vertical distance between two head nodes

[x1 y1]=meshgrid(((x(1,1)-(dh/4)):dh/2:(x(1,end)+(dh/4))),((y(1,1)-(dv/4)):dv/2:(y(end,1)+(dv/2))));
plot(x1,y1,'g*')
[r c]=size(x1);
difference=(r*c)-(hn*4);

% if ~isempty(difference)
%     d=difference/2;
%     for i=0:d-1
%         plot(x1(1,c-i),y1(1,c-i),'w*')
%         plot(x1(2,c-i),y1(2,c-i),'w*')        
%     end
% end

for i=1:row
    column=col;    
%     if i==1 && mod(hn,col)~=0           
%         column=hn-((row-1)*col);        
%     end
    
    for j=1:column
        plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])        
        plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
        plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
        plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])        
    end
end

end

Code 4 – Function M File – fire_detection3.m

function [axis,time,burntnodes,basetime,times,unik]=fire_detection3(hn,bp,x,y,x1,y1,row,col)

%[x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%figure(4)
[r c]=size(x);
[r1 c1]=size(x1);
dh=sqrt((x(1,1)-x(1,c))^2 + (y(1,1)-y(1,c))^2); %horizontal distance
dv=sqrt((x(1,1)-x(r,1))^2 + (y(1,1)-y(r,1))^2); %vertical distance
start_fire=[((dh/2)+x(1,1)) ((dv/2)+4)]; %origin of fire
%plot(start_fire(1),start_fire(2),'ko')
div=row/col;
time=0; %time in sec for the fire to reach at the edge of forest
axis=[]; %values of axis of ellipse from the start point to the boundry of forest
burntnodes=[]; % number of burnt nodes
basetime=[]; 
times=[]; %time for each ellipse 
unik=[]; %number of nodes burnt
if (start_fire(2)-y(1,1))<(start_fire(1)-x(1,1)) %vertical distance < horizontal distance
    ii=1; %horizontal axis
    ij=1; %vertical axis
    deadnodeset_b=[];
    deadnodeset_a=[];
    n=0;    
    X1=x;
    Y1=y;    
    while ii<=(start_fire(2)-y(1,1)) || ij<=(start_fire(2)-y(1,1)) % detect vertical radius first, and then horizontal               
%        subplot(1,2,1)        
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        deadnodeset_b=deadnodeset_a;
        %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);       
        deadnodeset=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div); % to detect and mark the dead nodes       
        deadnodeset_a=deadnodeset;        
        unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b); % to delete the dead connections        
%        flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)
        [X,Y]=bully3(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);
        X1=X;
        Y1=Y;
        %flowinfo1(row,col,hn,x,y,deadnodeset_a,ra)       
        %checkdeadnode1(deadnodeset,x,y,x1,y1,bp)
          
%         subplot(1,2,2)
%         ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
%         [x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%         % deadnodeset_b=deadnodeset_a;                
%         %deadnodeset1=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij); % to detect and mark the dead nodes       
%         %deadnodeset_a=deadnodeset;        
%         %checkdeadnode(deadnodeset1,x,y,x1,y1,bp)
%         checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)
        
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
        
        time=time+1;
        axis=[axis; ii ij];
        [ra ca]=size(deadnodeset);
        burntnodes=[burntnodes ra];
        [raa caa]=size(unik_deadnodes);
        if ~isequal(raa,0)
            unik=[unik raa];
            times=[times; round(clock)];
        end
        n=n+ii;
        basetime=[basetime n];   
        
    end    
elseif (start_fire(2)-y(1,1))>(start_fire(1)-x(1,1)) %vertical distance > horizontal distance
    ii=1; %horizontal axis
    ij=1; %vertical axis
    deadnodeset_b=[];
    deadnodeset_a=[];
    n=0;
    X1=x;
    Y1=y;
    
    while ii<=(start_fire(1)-x(1,1)) || ij<=(start_fire(1)-x(1,1))     % detect horizontal radius first, and then vertical
%        subplot(1,2,1)
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        deadnodeset_b=deadnodeset_a;
        %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);       
        deadnodeset=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div);  % to detect and mark the dead nodes
        deadnodeset_a=deadnodeset;        
        unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b); % to delete the dead connections        
        %flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)
        [X,Y]=bully3(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);
        X1=X;
        Y1=Y;
%         subplot(1,2,2)
%         ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
%         [x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%         % deadnodeset_b=deadnodeset_a;
%         %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);                
%         %deadnodeset1=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij); % to detect and mark the dead nodes                
%         %deadnodeset_a=deadnodeset;        
%         % checkdeadnode(deadnodeset1,x,y,x1,y1,bp)
%         checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)
       
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
        
        time=time+1;
        axis=[axis; ii ij];
        [ra ca]=size(deadnodeset);
        burntnodes=[burntnodes ra];
        [raa caa]=size(unik_deadnodes);
        if ~isequal(raa,0)
            unik=[unik raa];
            times=[times; round(clock) ];
        end
        n=n+ii;
        basetime=[basetime n];        
    end    
end
end

Code 5 – Function M File –bully3.m

function [X,Y]=bully3(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1)
bars=0;
%t=0;
while bars>=0
    subplot(1,2,1)
    bar(bars)
    axis([0 3 0 110])
    grid on
    ylabel('Temperature')
    set(gca,'XTick',[])

    bars=bars+randi(10,1,1);
    %bars=bars+15;
    
    subplot(1,2,2)
    rectangle('Position',[0 ((row*4)+6) 5 4],'Facecolor','r')
    text(1,((row*4)+8),'BASE')
    hold on
    cp=[5 ((row*4)+6)];
%    coloring_b(X1,Y1,row,col,x,y,x1,y1)

    plot(x,y,'go','LineWidth',5)
    plot(x1,y1,'g*')
    set(gca,'XTick',[])
    set(gca,'YTick',[])

    [r c]=size(deadnodeset);
    for i=1:r
        plot(deadnodeset(i,1),deadnodeset(i,2),'ko','LineWidth',3)        
    end    
    [ra ca]=size(unik_deadnodes);
    for i=1:ra
        plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ro','LineWidth',3)    
        plot([unik_deadnodes(i,1) cp(1)],[unik_deadnodes(i,2) cp(2)],'r')
    end
    pause(0.5)
    for i=1:ra    
        plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'yo','LineWidth',3)
        plot([unik_deadnodes(i,1) cp(1)],[unik_deadnodes(i,2) cp(2)],'y')
    end
    pause(0.5)    
    
    if bars>=90   
        %bars=1;
        break
    end
end
    
for i=1:ra    
    plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ko','LineWidth',3)
    plot([unik_deadnodes(i,1) cp(1)],[unik_deadnodes(i,2) cp(2)],'w')
end

[r c]=size(X1);
for i=1:r
    for j=1:c
        if X1(i,j)==x(i,j) && Y1(i,j)==y(i,j)
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')            
        elseif X1(i,j)~=0 && Y1(i,j)~=0
            plot([X1(i,j) x1(((2*i)-1),((2*j)-1))],[Y1(i,j) y1(((2*i)-1),((2*j)-1))],'w')
            plot([X1(i,j) x1(((2*i)-1),(2*j))],[Y1(i,j) y1(((2*i)-1),(2*j))],'w')
            plot([X1(i,j) x1((2*i),((2*j)-1))],[Y1(i,j) y1((2*i),((2*j)-1))],'w')
            plot([X1(i,j) x1((2*i),(2*j))],[Y1(i,j) y1((2*i),(2*j))],'w')
        end
    end
end


[rr cc]=size(x);
[r3 c3]=size(deadnodeset);

X=x;
Y=y;

for i=1:rr
    for j=1:cc     
        for k=1:r3
            if x(i,j)==deadnodeset(k,1) && y(i,j)==deadnodeset(k,2)               
                a1=x1(((2*i)-1),((2*j)-1));
                a2=y1(((2*i)-1),((2*j)-1));
                b1=x1(((2*i)-1),(2*j));
                b2=y1(((2*i)-1),(2*j));
                c1=x1((2*i),((2*j)-1));
                c2=y1((2*i),((2*j)-1));
                d1=x1((2*i),(2*j));
                d2=y1((2*i),(2*j));
                
                a=[a1 a2];
                b=[b1 b2];
                c=[c1 c2];
                d=[d1 d2];

                for l=1:r3 
                    if a(1)==deadnodeset(l,1) && a(2)==deadnodeset(l,2)
                        a=[];
                        break
                    else
                        X(i,j)=a(1); %a becomes the new head
                        Y(i,j)=a(2);
                    end
                end
                    
                if isempty(a)
                   for l=1:r3                       
                       if b(1)==deadnodeset(l,1) && b(2)==deadnodeset(l,2)
                           b=[];
                           break
                       else
                            X(i,j)=b(1); %b becomes the new head
                            Y(i,j)=b(2);
                       end
                   end 
                end
                
                if isempty(b)
                   for l=1:r3                       
                       if c(1)==deadnodeset(l,1) && c(2)==deadnodeset(l,2)
                           c=[];
                          break
                       else
                            X(i,j)=c(1); %c becomes the new head
                            Y(i,j)=c(2);                            
                       end
                   end 
                end
               
                if isempty(c)
                   for l=1:r3                       
                       if d(1)==deadnodeset(l,1) && d(2)==deadnodeset(l,2)
                           d=[];
                           break
                       else
                            X(i,j)=d(1); %d becomes the new head
                            Y(i,j)=d(2);
                       end
                   end 
                end
                
                if isempty(d)
                    X(i,j)=0; %the whole cluster is burnt
                    Y(i,j)=0;
                end
            end
        end
    end
end
% new head nodes obtained (up)
for i=1:rr
    for j=1:cc
        xyz=0;
        if x(i,j)==X(i,j) && y(i,j)==Y(i,j)
            xyz=1;
        else
            xyz=0;
            break
        end        
    end
    if xyz==0
        break
    end
end

if xyz==1
    for i=1:row    
        for j=1:col
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])        
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])        
        end
    end
end  

[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot(X(i,j),Y(i,j),'go','LineWidth',5)
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    abc=plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    %delete(abc)
%                    disp('br')
                    plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
                    abc=plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')

                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1((2*i),((2*j)-1)))
                    abc=plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && isequal(deadnodeset(k,2),y1((2*i),((2*j)-1))) 
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),(2*j))) && ~isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
                    abc=plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),(2*j))) && isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')        
                    break
                end
            end        
            
            clear k abc
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot(X(i,j),Y(i,j),'go','LineWidth',5)
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    abc=plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
                    abc=plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))],'w')
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1((2*i),((2*j)-1)))
                    abc=plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && isequal(deadnodeset(k,2),y1((2*i),((2*j)-1))) 
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))],'w')
                    break
                end
            end
            
            clear k abc
        
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),(2*j))) && ~isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
                    abc=plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),(2*j))) && isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))],'w')        
                    break
                end
            end                    
        end
    end
end

[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))])
            plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))])
            plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))])
        end
    end
end

%clear rr cc
%coloring_w(X1,Y1,row,col,x,y,x1,y1)
%coloring_c(X,Y,row,col,x,y,x1,y1)



%pause    
end

Code 6 – Function M File –bully2.m

function [X,Y]=bully2(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1)
    
[rr cc]=size(x);
[r3 c3]=size(deadnodeset);

X=x;
Y=y;

for i=1:rr
    for j=1:cc     
        for k=1:r3
            if x(i,j)==deadnodeset(k,1) && y(i,j)==deadnodeset(k,2)               
                a1=x1(((2*i)-1),((2*j)-1));
                a2=y1(((2*i)-1),((2*j)-1));
                b1=x1(((2*i)-1),(2*j));
                b2=y1(((2*i)-1),(2*j));
                c1=x1((2*i),((2*j)-1));
                c2=y1((2*i),((2*j)-1));
                d1=x1((2*i),(2*j));
                d2=y1((2*i),(2*j));
                
                a=[a1 a2];
                b=[b1 b2];
                c=[c1 c2];
                d=[d1 d2];

                for l=1:r3 
                    if a(1)==deadnodeset(l,1) && a(2)==deadnodeset(l,2)
                        a=[];
                        break
                    else
                        X(i,j)=a(1); %a becomes the new head
                        Y(i,j)=a(2);
                    end
                end
                    
                if isempty(a)
                   for l=1:r3                       
                       if b(1)==deadnodeset(l,1) && b(2)==deadnodeset(l,2)
                           b=[];
                           break
                       else
                            X(i,j)=b(1); %b becomes the new head
                            Y(i,j)=b(2);
                       end
                   end 
                end
                
                if isempty(b)
                   for l=1:r3                       
                       if c(1)==deadnodeset(l,1) && c(2)==deadnodeset(l,2)
                           c=[];
                          break
                       else
                            X(i,j)=c(1); %c becomes the new head
                            Y(i,j)=c(2);                            
                       end
                   end 
                end
               
                if isempty(c)
                   for l=1:r3                       
                       if d(1)==deadnodeset(l,1) && d(2)==deadnodeset(l,2)
                           d=[];
                           break
                       else
                            X(i,j)=d(1); %d becomes the new head
                            Y(i,j)=d(2);
                       end
                   end 
                end
                
                if isempty(d)
                    X(i,j)=0; %the whole cluster is burnt
                    Y(i,j)=0;
                end
            end
        end
    end
end
% new head nodes obtained (up)
for i=1:rr
    for j=1:cc
        xyz=0;
        if x(i,j)==X(i,j) && y(i,j)==Y(i,j)
            xyz=1;
        else
            xyz=0;
            break
        end        
    end
    if xyz==0
        break
    end
end

if xyz==1
    for i=1:row    
        for j=1:col
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])        
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])        
        end
    end
end  

[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot(X(i,j),Y(i,j),'go','LineWidth',5)
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    abc=plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    %delete(abc)
%                    disp('br')
                    plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
                    abc=plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')

                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1((2*i),((2*j)-1)))
                    abc=plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && isequal(deadnodeset(k,2),y1((2*i),((2*j)-1))) 
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),(2*j))) && ~isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
                    abc=plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),(2*j))) && isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')        
                    break
                end
            end        
            
            clear k abc
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot(X(i,j),Y(i,j),'go','LineWidth',5)
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    abc=plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
                    abc=plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))],'w')
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1((2*i),((2*j)-1)))
                    abc=plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && isequal(deadnodeset(k,2),y1((2*i),((2*j)-1))) 
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))],'w')
                    break
                end
            end
            
            clear k abc
        
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),(2*j))) && ~isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
                    abc=plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),(2*j))) && isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))],'w')        
                    break
                end
            end                    
        end
    end
end

[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))])
            plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))])
            plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))])
        end
    end
end
%pause    
end

Code 7 – Function M File –  firedetection2.m

%function [axis,time,burntnodes,basetime]=firedetection2(hn,bp,x,y,x1,y1,row,col)
function firedetection2(hn,x,y,x1,y1,row,col)
%[x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%figure(4)
[r c]=size(x);
[r1 c1]=size(x1);
dh=sqrt((x(1,1)-x(1,c))^2 + (y(1,1)-y(1,c))^2); %horizontal distance
dv=sqrt((x(1,1)-x(r,1))^2 + (y(1,1)-y(r,1))^2); %vertical distance
start_fire=[((dh/2)+x(1,1)) ((dv/2)+4)]; %origin of fire
%plot(start_fire(1),start_fire(2),'ko')
div=row/col;
% time=0; %time in sec for the fire to reach at the edge of forest
% axis=[]; %values of axis of ellipse from the start point to the boundry of forest
% burntnodes=[]; % number of burnt nodes
% basetime=[]; 
if (start_fire(2)-y(1,1))<(start_fire(1)-x(1,1)) %vertical distance < horizontal distance
    ii=1; %horizontal axis
    ij=1; %vertical axis
    deadnodeset_b=[];
    deadnodeset_a=[];
%    n=0;    
    X1=x;
    Y1=y;
    while ii<=(start_fire(2)-y(1,1)) || ij<=(start_fire(2)-y(1,1)) % detect vertical radius first, and then horizontal               
%        subplot(1,2,1)        
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        deadnodeset_b=deadnodeset_a;
        %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);       
        deadnodeset=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div); % to detect and mark the dead nodes              
        deadnodeset_a=deadnodeset;        
        unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b); % to delete the dead connections        
        [X,Y]=scene2(deadnodeset,unik_deadnodes,x,y,x1,y1,row,col,X1,Y1,hn);
%        flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)
%         [X,Y]=bully(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);
        X1=X;
        Y1=Y;
        %flowinfo1(row,col,hn,x,y,deadnodeset_a,ra)       
        %checkdeadnode1(deadnodeset,x,y,x1,y1,bp)
          
%         subplot(1,2,2)
%         ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
%         [x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%         % deadnodeset_b=deadnodeset_a;                
%         %deadnodeset1=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij); % to detect and mark the dead nodes       
%         %deadnodeset_a=deadnodeset;        
%         %checkdeadnode(deadnodeset1,x,y,x1,y1,bp)
%         checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)
        
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
        
%         time=time+1;
%         axis=[axis; ii ij];
%         [ra ca]=size(deadnodeset);
%         burntnodes=[burntnodes ra];
%         n=n+ii;
%         basetime=[basetime n];        
    end    
elseif (start_fire(2)-y(1,1))>(start_fire(1)-x(1,1)) %vertical distance > horizontal distance
    ii=1; %horizontal axis
    ij=1; %vertical axis
    deadnodeset_b=[];
    deadnodeset_a=[];
%     n=0;
    X1=x;
    Y1=y;
    while ii<=(start_fire(1)-x(1,1)) || ij<=(start_fire(1)-x(1,1))     % detect horizontal radius first, and then vertical
%        subplot(1,2,1)
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        deadnodeset_b=deadnodeset_a;
        %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);       
        deadnodeset=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div);  % to detect and mark the dead nodes
        deadnodeset_a=deadnodeset;        
        unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b); % to delete the dead connections        
        [X,Y]=scene2(deadnodeset,unik_deadnodes,x,y,x1,y1,row,col,X1,Y1,hn);
%         %flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)
%         [X,Y]=bully(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);
        X1=X;
        Y1=Y;
%         subplot(1,2,2)
%         ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
%         [x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%         % deadnodeset_b=deadnodeset_a;
%         %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);                
%         %deadnodeset1=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij); % to detect and mark the dead nodes                
%         %deadnodeset_a=deadnodeset;        
%         % checkdeadnode(deadnodeset1,x,y,x1,y1,bp)
%         checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)
       
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
        
%         time=time+1;
%         axis=[axis; ii ij];
%         [ra ca]=size(deadnodeset);
%         burntnodes=[burntnodes ra];
%         n=n+ii;
%         basetime=[basetime n];
    end    
end
end

Code 8 – Function M File –scene2.m

function [X,Y]=scene2(deadnodeset,unik_deadnodes,x,y,x1,y1,row,col,X1,Y1,hn)
bars=0;
while bars>=0
    subplot(1,2,1)
    bar(bars)
    axis([0 3 0 110])
    grid on
    ylabel('Temperature')
    set(gca,'XTick',[])

    bars=bars+randi(10,1,1);
    
    subplot(1,2,2)
    rectangle('Position',[0 ((row*4)+6) 5 4],'Facecolor','r')
    text(1,((row*4)+8),'BASE')
    hold on
    cp=[5 ((row*4)+6)]; % connectiong point of the base station
    
    plot(x,y,'go','LineWidth',5)
    plot(x1,y1,'g*')
    [r c]=size(deadnodeset);
    for i=1:r
        plot(deadnodeset(i,1),deadnodeset(i,2),'ko','LineWidth',5)
    end    
    set(gca,'XTick',[])
    set(gca,'YTick',[])
    
    [X,Y]=bully2(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);

    [ra ca]=size(unik_deadnodes);
    for i=1:ra
        plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ro','LineWidth',3)    
        plot([unik_deadnodes(i,1) cp(1)],[unik_deadnodes(i,2) cp(2)],'c');
    end
    pause(0.5)
    for i=1:ra    
        plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'yo','LineWidth',3)
        plot([unik_deadnodes(i,1) cp(1)],[unik_deadnodes(i,2) cp(2)],'c');
    end
    pause(0.5)    
    if bars>=90
        break
    end
end
    
for i=1:ra    
    plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ko','LineWidth',3)
    plot([unik_deadnodes(i,1) cp(1)],[unik_deadnodes(i,2) cp(2)],'w')
end

Code 9 – Function M File –coloring_b.m

function coloring_b(X,Y,row,col,x,y,x1,y1)

%coloring blue
[rr cc]=size(X);
for i=1:rr
    a=X(i,:);
    b=Y(i,:);    
    nn1=[];
    nn2=[];
    for j=1:length(a)
        if a(j)==0
            nn1=[nn1 j];
        end        
        if b(j)==0
            nn2=[nn2 j];
        end
    end
    a(nn1)=[];
    b(nn2)=[];    
    for k=1:length(a)-1
        a1=a(k);
        a2=b(k);
        b1=a(k+1);
        b2=b(k+1);        
        plot([a1 b1],[a2 b2],'b','LineWidth',2) %horizontal 
    end
    %pause(0.3)       
    if ~isequal(i,rr)
        c=X(i+1,:);
        d=Y(i+1,:);
        c(nn1)=[];
        d(nn2)=[];        
        for k=1:length(a)
            a1=a(k);
            a2=b(k);
            b1=c(k);
            b2=d(k);            
            if ~isequal(b1,0) || ~isequal(b2,0)
                plot([a1 b1],[a2 b2],'b','LineWidth',2) %vertical
            end
        end
    end    
    if isequal(i,rr)
        for k=1:length(a)
            plot([a(k) 5],[b(k) ((row*4)+6)],'b','LineWidth',2)
        end
    end    
end

[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))])
            plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))])
            plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))])
        end
    end
end

Code 10 – Function M File – bully.m

function [X,Y]=bully(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1)
bars=0;
while bars>=0
    subplot(1,2,1)
    bar(bars)
    axis([0 3 0 110])
    grid on
    ylabel('Temperature')
    set(gca,'XTick',[])

    bars=bars+10;
    
    subplot(1,2,2)
    rectangle('Position',[0 ((row*4)+6) 5 4],'Facecolor','r')
    text(1,((row*4)+8),'BASE')
    hold on
    coloring_b(X1,Y1,row,col,x,y,x1,y1)

    plot(x,y,'go','LineWidth',5)
    plot(x1,y1,'g*')
    set(gca,'XTick',[])
    set(gca,'YTick',[])

    [r c]=size(deadnodeset);
    for i=1:r
        plot(deadnodeset(i,1),deadnodeset(i,2),'ko','LineWidth',3)
    end    
    [ra ca]=size(unik_deadnodes);
    for i=1:ra
        plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ro','LineWidth',3)    
    end
    pause(0.5)
    for i=1:ra    
        plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'yo','LineWidth',3)
    end
    pause(0.5)    
    if bars>=90
        %bars=1;
        break
    end
end
    
for i=1:ra    
    plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ko','LineWidth',3)
end

[rr cc]=size(x);
[r3 c3]=size(deadnodeset);

X=x;
Y=y;

for i=1:rr
    for j=1:cc     
        for k=1:r3
            if x(i,j)==deadnodeset(k,1) && y(i,j)==deadnodeset(k,2)               
                a1=x1(((2*i)-1),((2*j)-1));
                a2=y1(((2*i)-1),((2*j)-1));
                b1=x1(((2*i)-1),(2*j));
                b2=y1(((2*i)-1),(2*j));
                c1=x1((2*i),((2*j)-1));
                c2=y1((2*i),((2*j)-1));
                d1=x1((2*i),(2*j));
                d2=y1((2*i),(2*j));
                
                a=[a1 a2];
                b=[b1 b2];
                c=[c1 c2];
                d=[d1 d2];

                for l=1:r3 
                    if a(1)==deadnodeset(l,1) && a(2)==deadnodeset(l,2)
                        a=[];
                        break
                    else
                        X(i,j)=a(1); %a becomes the new head
                        Y(i,j)=a(2);
                    end
                end
                    
                if isempty(a)
                   for l=1:r3                       
                       if b(1)==deadnodeset(l,1) && b(2)==deadnodeset(l,2)
                           b=[];
                           break
                       else
                            X(i,j)=b(1); %b becomes the new head
                            Y(i,j)=b(2);
                       end
                   end 
                end
                
                if isempty(b)
                   for l=1:r3                       
                       if c(1)==deadnodeset(l,1) && c(2)==deadnodeset(l,2)
                           c=[];
                          break
                       else
                            X(i,j)=c(1); %c becomes the new head
                            Y(i,j)=c(2);                            
                       end
                   end 
                end
               
                if isempty(c)
                   for l=1:r3                       
                       if d(1)==deadnodeset(l,1) && d(2)==deadnodeset(l,2)
                           d=[];
                           break
                       else
                            X(i,j)=d(1); %d becomes the new head
                            Y(i,j)=d(2);
                       end
                   end 
                end
                
                if isempty(d)
                    X(i,j)=0; %the whole cluster is burnt
                    Y(i,j)=0;
                end
            end
        end
    end
end
% new head nodes obtained (up)
for i=1:rr
    for j=1:cc
        xyz=0;
        if x(i,j)==X(i,j) && y(i,j)==Y(i,j)
            xyz=1;
        else
            xyz=0;
            break
        end        
    end
    if xyz==0
        break
    end
end

if xyz==1
    for i=1:row    
        for j=1:col
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])        
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])        
        end
    end
end  

[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot(X(i,j),Y(i,j),'go','LineWidth',5)
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    abc=plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    %delete(abc)
%                    disp('br')
                    plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
                    abc=plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')

                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1((2*i),((2*j)-1)))
                    abc=plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && isequal(deadnodeset(k,2),y1((2*i),((2*j)-1))) 
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),(2*j))) && ~isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
                    abc=plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),(2*j))) && isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')        
                    break
                end
            end        
            
            clear k abc
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot(X(i,j),Y(i,j),'go','LineWidth',5)
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    abc=plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
                    abc=plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))],'w')
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1((2*i),((2*j)-1)))
                    abc=plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && isequal(deadnodeset(k,2),y1((2*i),((2*j)-1))) 
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))],'w')
                    break
                end
            end
            
            clear k abc
        
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),(2*j))) && ~isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
                    abc=plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),(2*j))) && isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))],'w')        
                    break
                end
            end                    
        end
    end
end

%clear rr cc
coloring_w(X1,Y1,row,col,x,y,x1,y1)
coloring_c(X,Y,row,col,x,y,x1,y1)



%pause    
end

Code 11 – Function M File – fire_detection.m

function [axis,time,burntnodes,basetime]=fire_detection(hn,bp,x,y,x1,y1,row,col)

%[x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%figure(4)
[r c]=size(x);
[r1 c1]=size(x1);
dh=sqrt((x(1,1)-x(1,c))^2 + (y(1,1)-y(1,c))^2); %horizontal distance
dv=sqrt((x(1,1)-x(r,1))^2 + (y(1,1)-y(r,1))^2); %vertical distance
start_fire=[((dh/2)+x(1,1)) ((dv/2)+4)]; %origin of fire
%plot(start_fire(1),start_fire(2),'ko')
div=row/col;
time=0; %time in sec for the fire to reach at the edge of forest
axis=[]; %values of axis of ellipse from the start point to the boundry of forest
burntnodes=[]; % number of burnt nodes
basetime=[]; 
if (start_fire(2)-y(1,1))<(start_fire(1)-x(1,1)) %vertical distance < horizontal distance
    ii=1; %horizontal axis
    ij=1; %vertical axis
    deadnodeset_b=[];
    deadnodeset_a=[];
    n=0;    
    X1=x;
    Y1=y;
    while ii<=(start_fire(2)-y(1,1)) || ij<=(start_fire(2)-y(1,1)) % detect vertical radius first, and then horizontal               
%        subplot(1,2,1)        
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        deadnodeset_b=deadnodeset_a;
        %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);       
        deadnodeset=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div); % to detect and mark the dead nodes       
        deadnodeset_a=deadnodeset;        
        unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b); % to delete the dead connections        
%        flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)
        [X,Y]=bully(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);
        X1=X;
        Y1=Y;
        %flowinfo1(row,col,hn,x,y,deadnodeset_a,ra)       
        %checkdeadnode1(deadnodeset,x,y,x1,y1,bp)
          
%         subplot(1,2,2)
%         ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
%         [x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%         % deadnodeset_b=deadnodeset_a;                
%         %deadnodeset1=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij); % to detect and mark the dead nodes       
%         %deadnodeset_a=deadnodeset;        
%         %checkdeadnode(deadnodeset1,x,y,x1,y1,bp)
%         checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)
        
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
        
        time=time+1;
        axis=[axis; ii ij];
        [ra ca]=size(deadnodeset);
        burntnodes=[burntnodes ra];
        n=n+ii;
        basetime=[basetime n];        
    end    
elseif (start_fire(2)-y(1,1))>(start_fire(1)-x(1,1)) %vertical distance > horizontal distance
    ii=1; %horizontal axis
    ij=1; %vertical axis
    deadnodeset_b=[];
    deadnodeset_a=[];
    n=0;
    X1=x;
    Y1=y;
    while ii<=(start_fire(1)-x(1,1)) || ij<=(start_fire(1)-x(1,1))     % detect horizontal radius first, and then vertical
%        subplot(1,2,1)
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        deadnodeset_b=deadnodeset_a;
        %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);       
        deadnodeset=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div);  % to detect and mark the dead nodes
        deadnodeset_a=deadnodeset;        
        unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b); % to delete the dead connections        
        %flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)
        [X,Y]=bully(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);
        X1=X;
        Y1=Y;
%         subplot(1,2,2)
%         ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
%         [x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%         % deadnodeset_b=deadnodeset_a;
%         %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);                
%         %deadnodeset1=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij); % to detect and mark the dead nodes                
%         %deadnodeset_a=deadnodeset;        
%         % checkdeadnode(deadnodeset1,x,y,x1,y1,bp)
%         checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)
       
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
        
        time=time+1;
        axis=[axis; ii ij];
        [ra ca]=size(deadnodeset);
        burntnodes=[burntnodes ra];
        n=n+ii;
        basetime=[basetime n];
    end    
end
end

Code 12 – Function M File – coloring_w.m

function coloring_w(X,Y,row,col,x,y,x1,y1)

%coloring blue
[rr cc]=size(X);
for i=1:rr
    a=X(i,:);
    b=Y(i,:);    
    nn1=[];
    nn2=[];
    for j=1:length(a)
        if a(j)==0
            nn1=[nn1 j];
        end        
        if b(j)==0
            nn2=[nn2 j];
        end
    end
    a(nn1)=[];
    b(nn2)=[];    
    for k=1:length(a)-1
        a1=a(k);
        a2=b(k);
        b1=a(k+1);
        b2=b(k+1);        
        plot([a1 b1],[a2 b2],'w','LineWidth',2) %horizontal white
    end
    %pause(0.3)       
    if ~isequal(i,rr)
        c=X(i+1,:);
        d=Y(i+1,:);
        c(nn1)=[];
        d(nn2)=[];        
        for k=1:length(a)
            a1=a(k);
            a2=b(k);
            b1=c(k);
            b2=d(k);            
            if ~isequal(b1,0) || ~isequal(b2,0)
                plot([a1 b1],[a2 b2],'w','LineWidth',2) %vertical white
            end
        end
    end    
    if isequal(i,rr)
        for k=1:length(a)
            plot([a(k) 5],[b(k) ((row*4)+6)],'w','LineWidth',2)
        end
    end    
end
[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))],'w')
            plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))],'w')
            plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))],'w')
            plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))],'w')
        end
    end
end

Code 13 – Function M File – coloring_c.m

function coloring_c(X,Y,row,col,x,y,x1,y1)

%coloring blue
[rr cc]=size(X);
for i=1:rr
    a=X(i,:);
    b=Y(i,:);    
    nn1=[];
    nn2=[];
    for j=1:length(a)
        if a(j)==0
            nn1=[nn1 j];
        end        
        if b(j)==0
            nn2=[nn2 j];
        end
    end
    a(nn1)=[];
    b(nn2)=[];    
    for k=1:length(a)-1
        a1=a(k);
        a2=b(k);
        b1=a(k+1);
        b2=b(k+1);        
        plot([a1 b1],[a2 b2],'c','LineWidth',2) %horizontal white
    end
    pause(0.3)       
    if ~isequal(i,rr)
        c=X(i+1,:);
        d=Y(i+1,:);
        c(nn1)=[];
        d(nn2)=[];        
        for k=1:length(a)
            a1=a(k);
            a2=b(k);
            b1=c(k);
            b2=d(k);            
            if ~isequal(b1,0) || ~isequal(b2,0)
                plot([a1 b1],[a2 b2],'c','LineWidth',2) %vertical white
            end
        end
    end    
    pause(0.3)
    if isequal(i,rr)
        for k=1:length(a)
            plot([a(k) 5],[b(k) ((row*4)+6)],'c','LineWidth',2)
        end
    end    
end
[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))])
            plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))])
            plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))])
        end
    end
end

Code 14 – Function M File – normal_condition.m

function normal_condition(hn)

[x,y,x1,y1,row,col,bp]=placing_sensors(hn);

for i=1:row
    column=col;
%     if i==1 && mod(hn,col)~=0           
%         column=hn-((row-1)*col);        
%     end
    
    a1=[x(i,1) x(i,column)];
    b1=[y(i,1) y(i,column)];
    
    abc=plot(a1,b1,'c','LineWidth',2);
    pause(0.3)
        
    if i==row
        break
    end
    
    a2=x(i,1:column);
    b2=y(i,1:column);
    a3=x(i+1,1:column);
    b3=y(i+1,1:column);
    
    for j=1:length(a2)
        plot([a2(j) a3(j)],[b2(j) b3(j)],'c','LineWidth',2)
    end      
    pause(0.3)
end

a4=x(row,:);
b4=y(row,:);

for i=1:length(a4)
    plot([bp(1) a4(i)],[bp(2) b4(i)],'c','LineWidth',2)
end

end

Code 15 – Function M File –flowcontrol2.m

function flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)

%pause(0.3)
%set(gca,'Color','w')

% col=5*ceil(hn/30); %0-30hn=5col, 30-60hn=10col...
% row=ceil(hn/col); 
% 
% rectangle('Position',[0 ((row*4)+6) 5 4],'Facecolor','r')
% text(1,((row*4)+8),'BASE')
% hold on
% 
% plot(x,y,'go','LineWidth',5)
% plot(x1,y1,'g*')
% axis([-5 ((col*4)+5) 0 ((row*4)+10)])
% 
% bp=[5 ((4*row)+6)]; % base station's reciever point
% a=x(1,:);
% b=y(end,:);
% for i=1:length(a)
%     plot([a(i) bp(1)],[b(i) bp(2)],'LineWidth',2);
% end
% 
% difference=(row*col)-hn;
% if difference~=0
%     for i=1:difference
%         plot(x(1,end-(i-1)) , y(1,end-(i-1)) , 'wo','LineWidth',5)
%     end
% end
% clear difference

[x,y,x1,y1,row,col,bp]=placing_sensors(hn);
% fresh=freshnodes(deadnodeset_a,deadnodeset_b);

% [ra ca]=size(fresh);
% for i=1:ra
%     plot(fresh(i,1),fresh(i,2),'ro','LineWidth',3)    
% end
% pause(0.3)
% for i=1:ra    
%     plot(fresh(i,1),fresh(i,2),'yo','LineWidth',3)
% end
% pause(0.3)
% for i=1:ra    
%     plot(fresh(i,1),fresh(i,2),'ko','LineWidth',3)
% end

[ra ca]=size(unik_deadnodes);
for i=1:ra
    plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ro','LineWidth',3)    
end
pause(0.5)
for i=1:ra    
    plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'yo','LineWidth',3)
end
pause(0.5)
for i=1:ra    
    plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ko','LineWidth',3)
end



% [r c]=size(unik_deadnodes);
% for i=1:r
%     plot(unik_deadnodes(i,1),unik_deadnodes(1,2),'ko')
% end

for i=1:row
    column=col;
    if i==1 && mod(hn,col)~=0           
        column=hn-((row-1)*col);        
    end
    
    a1=[x(i,1) x(i,column)];
    b1=[y(i,1) y(i,column)];
    
    abc=plot(a1,b1,'c','LineWidth',2);
    
%     dead=[];
%     for j=1:column
%         for k=1:ra
%             if x(i,j)==deadnodeset_a(k,1) && y(i,j)==deadnodeset_a(k,2)                
%                 dead=[dead; x(i,j) y(i,j)];                
%             end
%         end       
%     end
%     if ~isempty(dead)
%         plot([dead(1,1) dead(end,1)],[dead(1,2) dead(end,2)],'w','LineWidth',2)       
%     end
[r1 c1]=size(x);
[r2 c2]=size(x1);
[r3 c3]=size(deadnodeset_a);

for ii=1:r1
    for j=1:c1
        for k=1:r3
            if x(ii,j)==deadnodeset_a(k,1) && y(ii,j)==deadnodeset_a(k,2)
               if ii+1<=r1
                    plot([x(ii,j) x(ii+1,j)],[y(ii,j) y(ii+1,j)],'w','LineWidth',2)                    
               end
                if ii-1>=1
                    plot([x(ii,j) x(ii-1,j)],[y(ii,j) y(ii-1,j)],'w','LineWidth',2)
                end
                if j+1<=c1
                    plot([x(ii,j) x(ii,j+1)],[y(ii,j) y(ii,j+1)],'w','LineWidth',2)
                end
                if j-1>=1
                    plot([x(ii,j) x(ii,j-1)],[y(ii,j) y(ii,j-1)],'w','LineWidth',2)
                end
                
                if ii==r1
                    plot([bp(1) x(ii,j)],[bp(2) y(ii,j)],'w','LineWidth',2)
                end
            end
        end
    end
end

        
    pause(0.3)
        
    if i==row
        break
    end
    
    a2=x(i,1:column);
    b2=y(i,1:column);
    a3=x(i+1,1:column);
    b3=y(i+1,1:column);
    
    for j=1:length(a2)
        plot([a2(j) a3(j)],[b2(j) b3(j)],'c','LineWidth',2)
    end      
    pause(0.3)
end

a4=x(row,:);
b4=y(row,:);

for i=1:length(a4)
    plot([bp(1) a4(i)],[bp(2) b4(i)],'c','LineWidth',2)
end

[r1 c1]=size(deadnodeset_a);
for i=1:r1
    plot(deadnodeset_a(i,1),deadnodeset_a(i,2),'ko','LineWidth',3)    
end

end

Code 16 – Function M File –ellipse.m

function  [x,y] = ellipse(a,b,phi,x0,y0,n)

n_dflt = 100;   % Default for number of points

if nargin < 6, n = n_dflt; end
if nargin < 5, y0 = 0; end
if nargin < 4, x0 = 0; end
if nargin < 3, phi = 0; end
if nargin < 2, b = 1; end
if nargin < 1, a = 1; end

th = linspace(0,2*pi,n+1);
x = a*cos(th);
y = b*sin(th);

c = cos(phi);
s = sin(phi);

th = x*c-y*s+x0;
y = x*s+y*c+y0;
x = th;

if nargout==0, plot(x,y,'r'); end

end

Code 17 – Function M File –checkdeadnode.m

function checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)

[r c]=size(x);
[r1 c1]=size(x1);
[r2 c2]=size(deadnodeset);

% for i=1:r1
%     for j=1:c1
%         for k=1:r2
%             if x1(i,j)==deadnodeset(k,1) && y1(i,j)==deadnodeset(k,2)
%                 plot([x1(i,j) x(ceil(i/2),ceil(j/2))],[y1(i,j) y(ceil(i/2),ceil(j/2))],'w')
%                 %plot(deadnodeset(k,1),deadnodeset(k,2),'ko','LineWidth',3)
%                 %[ra ca]=size(deadnodeset);
%                 
%             end
%         end
%     end
% end

[rr cc]=size(unik_deadnodes);
for ii=1:rr
    plot(unik_deadnodes(ii,1),unik_deadnodes(ii,2),'ro','LineWidth',3)    
end
pause(0.5)
for ii=1:rr    
    plot(unik_deadnodes(ii,1),unik_deadnodes(ii,2),'yo','LineWidth',3)
end
pause(0.5)
for ii=1:rr    
    plot(unik_deadnodes(ii,1),unik_deadnodes(ii,2),'ko','LineWidth',3)
end

for i=1:r
    for j=1:c
        for k=1:r2 
            if x(i,j)==deadnodeset(k,1) && y(i,j)==deadnodeset(k,2)
                plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')
                plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
                plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')                       
                              
                if (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2)) && (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2)) % all four subnodes of the head are out of fire range. 
                    %plot(x1(((2*i)-1),((2*j)-1)),y1(((2*i)-1),((2*j)-1)),'go','LineWidth',5)
                    plot([x1(((2*i)-1),((2*j)-1)) x1((2*i)-1)],[y1(((2*i)-1),((2*j)-1)) y1(((2*i)-1),(2*j))],'r')
                    plot([x1(((2*i)-1),((2*j)-1)) x1((2*i),((2*j)-1))],[y1(((2*i)-1),((2*j)-1)) y1((2*i),((2*j)-1))],'r')
                    plot([x1(((2*i)-1),((2*j)-1)) x1((2*i),(2*j))],[y1(((2*i)-1),((2*j)-1)) y1((2*i),(2*j))],'r')
                    
                    % a is head
                    if i+1<=r && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i+1,j)],[y1(((2*i)-1),((2*j)-1)) y(i+1,j)],'b','LineWidth',2)
                    end
                    if i-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i-1,j)],[y1(((2*i)-1),((2*j)-1)) y(i-1,j)],'b','LineWidth',2)
                    end
                    if j+1<=c && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j+1)],[y1(((2*i)-1),((2*j)-1)) y(i,j+1)],'b','LineWidth',2)
                    end
                    if j-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j-1)],[y1(((2*i)-1),((2*j)-1)) y(i,j-1)],'b','LineWidth',2)
                    end                   
                    
                elseif (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2)) && (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2))
                    plot([x1(((2*i)-1),((2*j)-1)) x1((2*i)-1)],[y1(((2*i)-1),((2*j)-1)) y1(((2*i)-1),(2*j))],'r')
                    plot([x1(((2*i)-1),((2*j)-1)) x1((2*i),((2*j)-1))],[y1(((2*i)-1),((2*j)-1)) y1((2*i),((2*j)-1))],'r')
                    
                    % a is head
                    if i+1<=r && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i+1,j)],[y1(((2*i)-1),((2*j)-1)) y(i+1,j)],'b','LineWidth',2)
                    end
                    if i-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i-1,j)],[y1(((2*i)-1),((2*j)-1)) y(i-1,j)],'b','LineWidth',2)
                    end
                    if j+1<=c && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j+1)],[y1(((2*i)-1),((2*j)-1)) y(i,j+1)],'b','LineWidth',2)
                    end
                    if j-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j-1)],[y1(((2*i)-1),((2*j)-1)) y(i,j-1)],'b','LineWidth',2)
                    end                   
                    
                elseif (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2)) && (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2))
                    plot([x1((2*i)-1) x1(2*i)],[y1(((2*i)-1),(2*j)) y1((2*i),((2*j)-1))],'r')
                    plot([x1((2*i)-1) x1((2*i),(2*j))],[y1(((2*i)-1),(2*j)) y1((2*i),(2*j))],'r')
                    
                    %b is head
                    if i+1<=r && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i+1,j)],[y1(((2*i)-1),(2*j)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i-1,j)],[y1(((2*i)-1),(2*j)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i,j+1)],[y1(((2*i)-1),(2*j)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i,j-1)],[y1(((2*i)-1),(2*j)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2)) && (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) 
                    plot([x1((2*i),((2*j)-1)) x1((2*i),(2*j))],[y1((2*i),((2*j)-1)) y1((2*i),(2*j))],'r')
                    plot([x1((2*i),((2*j)-1)) x1(((2*i)-1),((2*j)-1))],[y1((2*i),((2*j)-1)) y1(((2*i)-1),((2*j)-1))],'r')
                    
                    % c is head
                    if i+1<=r && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i+1,j)],[y1((2*i),((2*j)-1)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i-1,j)],[y1((2*i),((2*j)-1)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i,j+1)],[y1((2*i),((2*j)-1)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i,j-1)],[y1((2*i),((2*j)-1)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2)) && (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2))
                    plot([x1((2*i),(2*j)) x1(((2*i)-1),((2*j)-1))],[y1((2*i),(2*j)) y1(((2*i)-1),((2*j)-1))],'r')
                    plot([x1((2*i),(2*j)) x1((2*i)-1)],[y1((2*i),(2*j)) y1(((2*i)-1),(2*j))],'r')
                    
                    % d is head
                    if i+1<=r && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i+1,j)],[y1((2*i),(2*j)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i-1,j)],[y1((2*i),(2*j)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i,j+1)],[y1((2*i),(2*j)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i,j-1)],[y1((2*i),(2*j)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2))
                    plot([x1(((2*i)-1),((2*j)-1)) x1((2*i)-1)],[y1(((2*i)-1),((2*j)-1)) y1(((2*i)-1),(2*j))],'r')
                    
                    % a is head
                    if i+1<=r && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i+1,j)],[y1(((2*i)-1),((2*j)-1)) y(i+1,j)],'b','LineWidth',2)
                    end
                    if i-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i-1,j)],[y1(((2*i)-1),((2*j)-1)) y(i-1,j)],'b','LineWidth',2)
                    end
                    if j+1<=c && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j+1)],[y1(((2*i)-1),((2*j)-1)) y(i,j+1)],'b','LineWidth',2)
                    end
                    if j-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j-1)],[y1(((2*i)-1),((2*j)-1)) y(i,j-1)],'b','LineWidth',2)
                    end                   
                    
                elseif (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2)) && (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2))
                    plot([x1((2*i)-1) x1((2*i),((2*j)-1))],[y1(((2*i)-1),(2*j)) y1((2*i),((2*j)-1))],'r')              
                    
                    %b is head
                    if i+1<=r && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i+1,j)],[y1(((2*i)-1),(2*j)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i-1,j)],[y1(((2*i)-1),(2*j)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i,j+1)],[y1(((2*i)-1),(2*j)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i,j-1)],[y1(((2*i)-1),(2*j)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2))
                    plot([(x1((2*i),((2*j)-1))) x1((2*i),(2*j))],[y1((2*i),((2*j)-1)) y1((2*i),(2*j))],'r')
                    
                    % c is head
                    if i+1<=r && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i+1,j)],[y1((2*i),((2*j)-1)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i-1,j)],[y1((2*i),((2*j)-1)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i,j+1)],[y1((2*i),((2*j)-1)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i,j-1)],[y1((2*i),((2*j)-1)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2)) && (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2))
                    plot([x1((2*i),(2*j)) x1(((2*i)-1),((2*j)-1))],[y1((2*i),(2*j)) y1(((2*i)-1),((2*j)-1))],'r')
                    
                    % d is head
                    if i+1<=r && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i+1,j)],[y1((2*i),(2*j)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i-1,j)],[y1((2*i),(2*j)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i,j+1)],[y1((2*i),(2*j)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i,j-1)],[y1((2*i),(2*j)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) 
                    % a is head
                    if i+1<=r && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i+1,j)],[y1(((2*i)-1),((2*j)-1)) y(i+1,j)],'b','LineWidth',2)
                    end
                    if i-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i-1,j)],[y1(((2*i)-1),((2*j)-1)) y(i-1,j)],'b','LineWidth',2)
                    end
                    if j+1<=c && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j+1)],[y1(((2*i)-1),((2*j)-1)) y(i,j+1)],'b','LineWidth',2)
                    end
                    if j-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j-1)],[y1(((2*i)-1),((2*j)-1)) y(i,j-1)],'b','LineWidth',2)
                    end                   
                    
                elseif (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2))
                    %b is head
                    if i+1<=r && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i+1,j)],[y1(((2*i)-1),(2*j)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i-1,j)],[y1(((2*i)-1),(2*j)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i,j+1)],[y1(((2*i)-1),(2*j)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i,j-1)],[y1(((2*i)-1),(2*j)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2))
                    % c is head
                    if i+1<=r && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i+1,j)],[y1((2*i),((2*j)-1)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i-1,j)],[y1((2*i),((2*j)-1)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i,j+1)],[y1((2*i),((2*j)-1)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i,j-1)],[y1((2*i),((2*j)-1)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2))
                    % d is head
                    if i+1<=r && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i+1,j)],[y1((2*i),(2*j)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i-1,j)],[y1((2*i),(2*j)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i,j+1)],[y1((2*i),(2*j)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i,j-1)],[y1((2*i),(2*j)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
%                 a=(x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) 
%                 b=(x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2))
%                 c=(x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2))
%                 d=(x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2))

                    
                                   
                end         
                
                 
                if i+1<=r
                    plot([x(i,j) x(i+1,j)],[y(i,j) y(i+1,j)],'w','LineWidth',3)                    
                end
                if i-1>=1
                    plot([x(i,j) x(i-1,j)],[y(i,j) y(i-1,j)],'w','LineWidth',3)
                end
                if j+1<=c
                    plot([x(i,j) x(i,j+1)],[y(i,j) y(i,j+1)],'w','LineWidth',3)
                end
                if j-1>=1
                    plot([x(i,j) x(i,j-1)],[y(i,j) y(i,j-1)],'w','LineWidth',3)
                end
                                
                if i==r
                    plot([bp(1) x(i,j)],[bp(2) y(i,j)],'w','LineWidth',2)
                end
                                
                plot(deadnodeset(k,1),deadnodeset(k,2),'ko','LineWidth',3)
            end
        end
    end
end

Code 18 – Function M File –checkdeadnode2.m

function unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b)

[ra ca]=size(deadnodeset_a);
[rb cb]=size(deadnodeset_b);
unik_deadnodes=deadnodeset_a;
x=[];
% deadnodeset_a
% deadnodeset_b

for i=1:ra
    for j=1:rb
        if deadnodeset_b(j,1)==deadnodeset_a(i,1) && deadnodeset_b(j,2)==deadnodeset_a(i,2)
            x=[x i];
        end
    end
end

unik_deadnodes(x,:)=[];

% is=[];
% for i=1:ra
%     for j=1:rb
%         if deadnodeset_b(j,1)==deadnodeset_a(i,1) && deadnodeset_b(j,2)==deadnodeset_a(i,2)
%             is=[is i];
%         end
%     end
% end
% %is
% if isempty(is)
%     unik_deadnodes=deadnodeset_a;
% else    
%     ind=1;
%     unik_deadnodes=[];
%     for i=1:ra        
%         if i<=length(is)
%             if i==is(ind)
%                 ind=ind+1;
%                 continue        
%             end
%         end
%         unik_deadnodes=[unik_deadnodes; deadnodeset_a(i,1) deadnodeset_a(i,2)];
%     end
% end




% [r1 c1]=size(x);
% [r2 c2]=size(x1);
% [r3 c3]=size(unik_deadnodes);
% 
% for i=1:r1
%     for j=1:c1
%         for k=1:r3
%             if x(i,j)==unik_deadnodes(k,1) && y(i,j)==unik_deadnodes(k,2)
%                 if i+1<=r1
%                     plot([x(i,j) x(i+1,j)],[y(i,j) y(i+1,j)],'w','LineWidth',2)                    
%                 end
%                 if i-1>=1
%                     plot([x(i,j) x(i-1,j)],[y(i,j) y(i-1,j)],'w','LineWidth',2)
%                 end
%                 if j+1<=c1
%                     plot([x(i,j) x(i,j+1)],[y(i,j) y(i,j+1)],'w','LineWidth',2)
%                 end
%                 if j-1>=1
%                     plot([x(i,j) x(i,j-1)],[y(i,j) y(i,j-1)],'w','LineWidth',2)
%                 end
%                 
%                 if i==r1
%                     plot([bp(1) x(i,j)],[bp(2) y(i,j)],'w','LineWidth',2)
%                 end
%             end
%         end
%     end
% end
% 
% for i=1:r2
%     for j=1:c2
%         for k=1:r3
%             if x1(i,j)==unik_deadnodes(k,1) && y1(i,j)==unik_deadnodes(k,2)
%                 plot([x1(i,j) x(ceil(i/2),ceil(j/2))],[y1(i,j) y(ceil(i/2),ceil(j/2))],'w')
%             end
%         end
%     end
% end

% for i=1:ra
%     plot(deadnodeset_a(i,1),deadnodeset_a(i,2),'ko','LineWidth',3)
% end

end

Code 19 – Function M File –dead_nodes.m

function [deadnodeset]=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div)

deadnodeset=[];
for i=1:r
    for j=1:c                
        if (((x(i,j)-start_fire(1))^2)/(ii^2)) + (((y(i,j)-start_fire(2))^2)/(ij^2)) <= 1
            deadnodeset=[deadnodeset ; x(i,j) y(i,j)];
        end                    
    end
end
        
for i=1:r1
    for j=1:c1
        if (((x1(i,j)-start_fire(1))^2)/(ii^2)) + (((y1(i,j)-start_fire(2))^2)/(ij^2)) <= 1
            deadnodeset=[deadnodeset ; x1(i,j) y1(i,j)];
        end                                    
    end
end
        
%         difference=(r*c)-hn;
%         if difference~=0
%             for i=1:difference
%                 plot(x(1,end-(i-1)) , y(1,end-(i-1)) , 'wo','LineWidth',5)
%             end
%         end
%         clear difference
%         difference=(r1*c1)-(hn*4);
%         if ~isempty(difference)
%         d=difference/2;
%             for i=0:d-1
%                 plot(x1(1,c1-i),y1(1,c1-i),'wo','LineWidth',5)
%                 plot(x1(2,c1-i),y1(2,c1-i),'wo','LineWidth',5)        
%             end
%         end
        
end

Code 20 – Function M File –freshnodes.m

function fresh=freshnodes(deadnodeset_a,deadnodeset_b)

fresh=[];
fresh2=[];
if isempty(deadnodeset_b)
    fresh=deadnodeset_a;
else
    [r1 c1]=size(deadnodeset_a);
    [r2 c2]=size(deadnodeset_b);
    
%     for i=1:r1
%         for j=1:r2    
%             if deadnodeset_a(i,1)~=deadnodeset_b(j,1) && deadnodeset_a(i,2)~=deadnodeset_b(j,2)
%                 fresh=[fresh; deadnodeset_a(i,1) deadnodeset_a(i,2)];
%             end 
%         end
%     end               
end

end

Code 21 – Function M File –dead_nodes1.m

function [deadnodeset]=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij)

deadnodeset=[];
        for i=1:r
            for j=1:c                
                a1=sqrt(((start_fire(1)-x(i,j))^2) + ((start_fire(2)-y(i,j))^2));
                if a1<ii || a1<ij                    
                    deadnodeset=[deadnodeset ; x(i,j) y(i,j)];
                    plot(x(i,j),y(i,j),'ko','LineWidth',3)
                end
            end
        end
        
        for i=1:r1
            for j=1:c1
                a2=sqrt(((start_fire(1)-x1(i,j))^2) + ((start_fire(2)-y1(i,j))^2));
                if a2<ii || a2<ij                    
                    deadnodeset=[deadnodeset ; x1(i,j) y1(i,j)];
                    plot(x1(i,j),y1(i,j),'ko','LineWidth',3)
                end
            end
        end
        
        difference=(r*c)-hn;
        if difference~=0
            for i=1:difference
                plot(x(1,end-(i-1)) , y(1,end-(i-1)) , 'wo','LineWidth',5)
            end
        end
        clear difference
        difference=(r1*c1)-(hn*4);
        if ~isempty(difference)
        d=difference/2;
            for i=0:d-1
                plot(x1(1,c1-i),y1(1,c1-i),'wo','LineWidth',5)
                plot(x1(2,c1-i),y1(2,c1-i),'wo','LineWidth',5)        
            end
        end
        
end

Code 22 – Function M File –dead_nodes1.m

function [deadnodeset]=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij)

deadnodeset=[];
        for i=1:r
            for j=1:c                
                a1=sqrt(((start_fire(1)-x(i,j))^2) + ((start_fire(2)-y(i,j))^2));
                if a1<ii || a1<ij                    
                    deadnodeset=[deadnodeset ; x(i,j) y(i,j)];
                    plot(x(i,j),y(i,j),'ko','LineWidth',3)
                end
            end
        end
        
        for i=1:r1
            for j=1:c1
                a2=sqrt(((start_fire(1)-x1(i,j))^2) + ((start_fire(2)-y1(i,j))^2));
                if a2<ii || a2<ij                    
                    deadnodeset=[deadnodeset ; x1(i,j) y1(i,j)];
                    plot(x1(i,j),y1(i,j),'ko','LineWidth',3)
                end
            end
        end
        
        difference=(r*c)-hn;
        if difference~=0
            for i=1:difference
                plot(x(1,end-(i-1)) , y(1,end-(i-1)) , 'wo','LineWidth',5)
            end
        end
        clear difference
        difference=(r1*c1)-(hn*4);
        if ~isempty(difference)
        d=difference/2;
            for i=0:d-1
                plot(x1(1,c1-i),y1(1,c1-i),'wo','LineWidth',5)
                plot(x1(2,c1-i),y1(2,c1-i),'wo','LineWidth',5)        
            end
        end
        
end

Code 23 – Function M File –flowinfo1.m

function flowinfo1(row,col,hn,x,y,deadnodeset_a,ra)

% flow of info during fire

for i=1:row
    column=col;
    if i==1 && mod(hn,col)~=0           
        column=hn-((row-1)*col);        
    end 
    
    a1=[x(i,1) x(i,column)];
    b1=[y(i,1) y(i,column)];
    
    abc=plot(a1,b1,'c','LineWidth',2);
    blanks=[];
    for j=1:col
        for k=1:ra
            if deadnodeset_a(k,1)==x(i,j) && deadnodeset_a(k,2)==y(i,j)
                blanks=[blanks;  x(i,j) y(i,j)];
            end
        end
    end
    if ~isempty(blanks)
        plot([blanks(1,1) blanks(end,1)],[blanks(1,2) blanks(end,2)],'w','LineWidth',2)
        
    end
            
    pause(0.3)
        
    if i==row
        break
    end
    
    a2=x(i,1:column);
    b2=y(i,1:column);
    a3=x(i+1,1:column);
    b3=y(i+1,1:column);
    
    for j=1:length(a2)
        plot([a2(j) a3(j)],[b2(j) b3(j)],'c','LineWidth',2)
    end      
    pause(0.3)
end

a4=x(row,:);
b4=y(row,:);

for i=1:length(a4)
    plot([bp(1) a4(i)],[bp(2) b4(i)],'c','LineWidth',2)
end

Code 24 – Function M File –test_scene.m

function test_scene(hn)

[x,y,x1,y1,row,col,bp]=placing_sensors(hn);
% stage 1
plot([x(3,3) x(4,3)],[y(3,3) y(4,3)],'w','LineWidth',2) %up
plot([x(3,3) x(2,3)],[y(3,3) y(2,3)],'w','LineWidth',2) %down
plot([x(3,3) x(3,2)],[y(3,3) y(3,2)],'w','LineWidth',2) %left
plot([x(3,3) x(3,4)],[y(3,3) y(3,4)],'w','LineWidth',2) %right
plot(x(3,3),y(3,3),'ko','LineWidth',5)

plot([x(4,3) x(5,3)],[y(4,3) y(5,3)],'w','LineWidth',2) %up
plot([x(4,3) x(3,3)],[y(4,3) y(3,3)],'w','LineWidth',2) %down
plot([x(4,3) x(4,2)],[y(4,3) y(4,2)],'w','LineWidth',2) %left
plot([x(4,3) x(4,4)],[y(4,3) y(4,4)],'w','LineWidth',2) %right
plot(x(4,3),y(4,3),'ko','LineWidth',5)

plot(x1(6,5),y1(6,5),'k*')
plot(x1(6,6),y1(6,6),'k*')
plot([x1(6,5) x(3,3)],[y1(6,5) y(3,3)],'w')
plot([x1(6,6) x(3,3)],[y1(6,6) y(3,3)],'w')
plot([x1(5,5) x(3,3)],[y1(5,5) y(3,3)],'w')
plot([x1(5,6) x(3,3)],[y1(5,6) y(3,3)],'w')


plot(x1(7,5),y1(7,5),'k*')
plot(x1(7,6),y1(7,6),'k*')
plot([x1(7,5) x(4,3)],[y1(7,5) y(4,3)],'w')
plot([x1(7,6) x(4,3)],[y1(7,6) y(4,3)],'w')
plot([x1(8,5) x(4,3)],[y1(8,5) y(4,3)],'w')
plot([x1(8,6) x(4,3)],[y1(8,6) y(4,3)],'w')

plot([x1(8,5) x(4,2)],[y1(8,5) y(4,2)],'LineWidth',2)
plot([x1(8,5) x(4,4)],[y1(8,5) y(4,4)],'LineWidth',2)
plot([x1(8,5) x(5,3)],[y1(8,5) y(5,3)],'LineWidth',2)
plot(x1(8,5),y1(8,5),'go','LineWidth',5)
plot([x1(8,5) x1(8,6)],[y1(8,5) y1(8,6)])

plot([x1(5,5) x(3,2)],[y1(5,5) y(3,2)],'LineWidth',2)
plot([x1(5,5) x(3,4)],[y1(5,5) y(3,4)],'LineWidth',2)
plot([x1(5,5) x(2,3)],[y1(5,5) y(2,3)],'LineWidth',2)
plot(x1(5,5),y1(5,5),'go','LineWidth',5)
plot([x1(5,5) x1(5,6)],[y1(5,5) y1(5,6)])

Code 25 – Function M File – checkdeadnode1.m

function checkdeadnode1(deadnodeset,x,y,x1,y1,bp)

[r c]=size(x);
[r1 c1]=size(x1);
[r2 c2]=size(deadnodeset);

for i=1:r1
    for j=1:c1
        for k=1:r2
            if x1(i,j)==deadnodeset(k,1) && y1(i,j)==deadnodeset(k,2)
                plot([x1(i,j) x(ceil(i/2),ceil(j/2))],[y1(i,j) y(ceil(i/2),ceil(j/2))],'w')
                plot(deadnodeset(k,1),deadnodeset(k,2),'ko','LineWidth',3)
            end
        end
    end
end

for i=1:r
    for j=1:c
        for k=1:r2 
            if x(i,j)==deadnodeset(k,1) && y(i,j)==deadnodeset(k,2)
                plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')
                plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
                plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')                       
                
                for l=1:r2
                    if x1(((2*i)-1),((2*j)-1))~=deadnodeset(l,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(l,2) % a is not dead and a is head
                        for m=1:r2
                            if x1((2*i)-1)~=deadnodeset(m,1) && y1(((2*i)-1),(2*j))~=deadnodeset(m,2) %b is not dead
                                plot([x1(((2*i)-1),((2*j)-1)) x1((2*i)-1)],[y1(((2*i)-1),((2*j)-1)) y1(((2*i)-1),(2*j))],'r') %ab
                            end
                        end
                        for m=1:r2
                            if x1((2*i),((2*j)-1))~=deadnodeset(m,1) && y1((2*i),((2*j)-1))~=deadnodeset(m,2)%c is not dead
                                plot([x1(((2*i)-1),((2*j)-1)) x1((2*i),((2*j)-1))],[y1(((2*i)-1),((2*j)-1))  y1((2*i),((2*j)-1))],'r') %ac
                            end
                        end
                        for m=1:r2
                           if x1((2*i),(2*j))~=deadnodeset(m,1) && y1((2*i),(2*j))~=deadnodeset(m,2) %d is not dead
                                plot([x1(((2*i)-1),((2*j)-1)) x1((2*i),(2*j))],[y1(((2*i)-1),((2*j)-1))  y1((2*i),(2*j))],'r') %ad
                            end
                        end 
                        
                        
                        
                    else %a is dead
                        if x1((2*i)-1)~=deadnodeset(l,1) && y1(((2*i)-1),(2*j))~=deadnodeset(l,2) %b is not dead
                            for m=1:r2
                                if x1((2*i),((2*j)-1))~=deadnodeset(m,1) && y1((2*i),((2*j)-1))~=deadnodeset(m,2) %c is not dead
                                    plot([x1((2*i)-1) x1((2*i),((2*j)-1))],[y1(((2*i)-1),(2*j))  y1((2*i),((2*j)-1))],'r') %bc
                                end
                            end
                            for m=1:r2
                                if x1((2*i),(2*j))~=deadnodeset(m,1) && y1((2*i),(2*j))~=deadnodeset(m,2) % d is not dead
                                    plot([x1((2*i)-1) x1((2*i),(2*j))],[y1(((2*i)-1),(2*j))  y1((2*i),(2*j))],'r') %bd
                                end
                            end 
                        else % b is dead
                            if x1((2*i),((2*j)-1))~=deadnodeset(l,1) && y1((2*i),((2*j)-1))~=deadnodeset(l,2)% c is not dead
                                for m=1:r2
                                    if x1((2*i),(2*j))~=deadnodeset(m,1) && y1((2*i),(2*j))~=deadnodeset(m,2) % d is not dead
                                        plot([x1((2*i),((2*j)-1)) x1((2*i),(2*j))],[y1((2*i),((2*j)-1))  y1((2*i),(2*j))],'r') %cd
                                    end
                                end 
                            else % c is dead
                                if x1((2*i),(2*j))~=deadnodeset(l,1) && y1((2*i),(2*j))~=deadnodeset(l,2) %d is not dead
                                    
                                end
                            end
                        end
                    end
                end
            
                                    
                                
                            
                                   
%                 a=x1(((2*i)-1),((2*j)-1))    y1(((2*i)-1),((2*j)-1)) 
%                 b=x1((2*i)-1)       y1(((2*i)-1),(2*j))
%                 c=x1((2*i),((2*j)-1))      y1((2*i),((2*j)-1))
%                 d=x1((2*i),(2*j))       y1((2*i),(2*j))
                if i+1<=r
                    plot([x(i,j) x(i+1,j)],[y(i,j) y(i+1,j)],'w','LineWidth',3)                    
                end
                if i-1>=1
                    plot([x(i,j) x(i-1,j)],[y(i,j) y(i-1,j)],'w','LineWidth',3)
                end
                if j+1<=c
                    plot([x(i,j) x(i,j+1)],[y(i,j) y(i,j+1)],'w','LineWidth',3)
                end
                if j-1>=1
                    plot([x(i,j) x(i,j-1)],[y(i,j) y(i,j-1)],'w','LineWidth',3)
                end
                                
                if i==r
                    plot([bp(1) x(i,j)],[bp(2) y(i,j)],'w','LineWidth',2)
                end
                                
                plot(deadnodeset(k,1),deadnodeset(k,2),'ko','LineWidth',3)
            end
        end
    end
end

        

Code 26 – Function M File – graphs.m

function graphs(axis,time,burntnodes,x1,y1,basetime,x,y,times,unik,start_fire)

figure(5)
subplot(2,2,1)
area=[];
for i=1:time
    a=(axis(i,1))*axis(i,2)*pi;
    area=[area a];
end

plot(1:time,area)
title('Area of fire VS Time')
xlabel('Time(hr)')
ylabel('Area(sq mts)')

%figure(6)
subplot(2,2,2)
plot(1:time,burntnodes,'r*','LineWidth',4)
title('Number of nodes burnt VS Time')
xlabel('Time(hrs)')
ylabel('Number of nodes')

%figure(7)
subplot(2,2,3)
base_distance=200; %distance of base station from the origin of fire (in this case centre of forest)
rem_time=[];
for i=1:length(basetime)
    rem=base_distance-basetime(i);
    rem_time=[rem_time rem];
end
plot(1:time,rem_time)
title('Time taken for fire to reach the base station')
xlabel('Levels of fire advancement')
ylabel('Time taken to reach base')

[r1 c1]=size(x);
[r2 c2]=size(x1);
totalnodes=(r1*c1)+(r2*c2);

%figure(8)
subplot(2,2,4)
pktratio=[];
for i=1:length(burntnodes)
    pktratio=[pktratio (totalnodes-burntnodes(i))];    
end

plot(1:time,pktratio)
title('Packet Ratio VS Time')
xlabel('Time')
ylabel('Packet Ratio')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('Press enter to continue..')
pause
disp('Number of nodes burnt along with time taken for them to burn')
disp('Shown in the excel file')

%mat={'Nodes burnt at time t' 'Total nodes burnt' 'Year' 'Month' 'Date' 'Hrs' 'Minutes' 'Seconds'};
tot=[];
t=0;
for i=1:length(unik)
    t=t+unik(i);
    tot=[tot t];
end    
% unik
% tot
% times
answers=[unik' tot' times];

[num,txt,raw]=xlsread('Timeframe');
[row col]=size(num);
blank_mat=[];
for i=1:row
    blank_mat(i,:)= zeros(1,col);
end
xlswrite('Timeframe',blank_mat,1,'A3')

xlswrite('Timeframe',answers,1,'A3')
% answers
% mat1=[mat; num2cell(answers)];
% disp(mat1)

figure(6)
long=sqrt((x1(1,1)-x1(end,end))^2+(y1(1,1)-y1(end,end))^2); %longest distance between 2 sensors
short=sqrt((x1(1,1)-x(1,1))^2+(y1(1,1)-y(1,1))^2); %shortest distance between 2 sensors

dist=linspace(short,long,10); %distances between the sensors from "short" to "long"
e=linspace(90,20,10); %remaining energy levels
gap=e(2)-e(1);
energy=[];
for i=1:length(e)
    a=e(i);
    b=e(i)+gap;
    x=a + (b-a).*rand(1,1);
    energy=[energy x]; %declining energy of the nodes as distance between them increases
end
plot(dist,energy)
title('Remaining energy level of deployed nodes')
xlabel('Average distance between sensors')
ylabel('Remaining energy level (%)')

clear long short dist
figure(7)
long=sqrt((start_fire(1)-x1(1,1))^2+((start_fire(2)-y1(1,1))^2)); %longest distance between the fire start point and a sensor
dist1=[];
[r c]=size(x1);
for i=1:r
    for j=1:c
        d=sqrt((start_fire(1)-x1(i,j))^2+((start_fire(2)-y1(i,j))^2));
        dist1=[dist1;i j d]; %distances of start point with all the sub nodes
    end
end
dist2=[];
[r c]=size(x);
for i=1:r
    for j=1:c
        d=sqrt((start_fire(1)-x(i,j))^2+((start_fire(2)-y(i,j))^2));
        dist2=[dist2;i j d]; %distances of start point with all the head nodes
    end
end

[a,ind1]=sort(dist1(:,3));
[b,ind2]=sort(dist2(:,3));
if a(1)<b(1)
    i=dist1(ind1(1),1);
    j=dist1(ind1(1),2);
    short=sqrt((start_fire(1)-x1(i,j))^2+((start_fire(2)-y1(i,j))^2)); %shortest distance between the start point and a sub node
elseif a(1)>b(1)
    i=dist2(ind1(1),1);
    j=dist2(ind1(1),2);
    short=sqrt((start_fire(1)-x(i,j))^2+((start_fire(2)-y(i,j))^2)); %shortest distance between the start point and a head node
elseif a(1)==b(1)
    i=dist2(ind1(1),1);
    j=dist2(ind1(1),2);
    short=sqrt((start_fire(1)-x(i,j))^2+((start_fire(2)-y(i,j))^2)); %shortest distance between the start point and a head node
end

e=linspace(short,long,10); %making partitions from short to long
plot(e)
title('Time required for the sensor nodes to sense the fire threat from various distances')
xlabel('Average distance between fire ignition location and sensor node')
ylabel('Time required for the sensor node to sense threat')

 

 

Optimal integrated points placement in hybrid optical-wireless access networks

PROJECT VIDEO

ABSTRACT

In this paper, we are working to reduce the number of integrating points in a hybrid optical wireless sensor network. We are given an area with randomly deployed sensor nodes and all the nodes will be divided in the form of several zones. Each zone will be represented by an integrating point. This point (node) manages all the communication in the respective zone. Our algorithm works on reducing the total number of integrating points and providing better results even with less number of zone representatives.

INTRODUCTION

The hybrid optical and wireless networks have been proposed as a promising approach to meet the increasing demand for higher bandwidth requirement, provide broadband and ubiquitous high-speed internet access, and address the growing gap between core network and local area network (last mile problem) effectively[1–6]. This hybrid system consists of a wireless network at the front end, and it is supported by an optical network at the backend. As a promising wire line solution to broadband access, optical network provides much better reliable transmission and much higher bandwidth in Gbps-scale compared with wireless networks as well as covering long distance (around 20 km) from the telecom central office (CO) to end users. However, the fixed infrastructure not only limits its coverage in a high densely populated area due to the high cost of fiber layout and equipments, but also makes it difficult to deploy in certain rugged environment. On the other hand, wireless networks support mobility and provide ubiquitous access for end users in the metropolitan area or the local area. However, wireless networks provide limited bandwidth compared with the optical fiber networks. Thus, integrating the optical and the wireless networks together can utilize their complementary advantages to provide better service for end users and increase revenue for the service providers. The optical part can choose passive optical network (PON) [5]technologies, such as EPON, GPON, and the wireless part usually choose WiFi or WiMAX. In this paper, we choose the EPON [7]standard IEEE 802.3ah as the optical part and consider the IEEE802.16–2004 standard of WiMAX [8] technology as the wireless part, since EPON can utilize the existing Ethernet infrastructure providing low cost and simplicity deployment, and WiMAX covers longer distance and provides higher data rates up to 1 Gb/s compared with WiFi technology, and supports both point-to multi point mode and mesh mode [8]. Fig. 1 illustrates the architecture of hybrid optical-wireless system. EPON uses a tree topology where the optical line terminal (OLT) located at the telecom CO connects multiple optical network units (ONUs) through the passive splitter. In the wireless part, WiMAX base stations (BSs) are grouped into clusters. Within each cluster, one WiMAX BS is selected as the gateway to combine with one ONU as the integrated point of the hybrid network and the rest of WiMAX BSs in the cluster referred as relay stations form a multi-hop wireless mesh network. These integrated points are the locations where the wireless part and optical part meet together. In data transmissions, amend user sends the packets to its closest relay station and this relay station forwards the packets to the integrated point through one hop or multi-hops in the cluster. Then integrated point will forward the packets to the OLT through the optical connection and finallyto the core network. If the receiver resides in the hybrid optical-wireless network, the data flows in the reverse directions described above. When developing the hybrid optical-wireless network, we need to address several issues including placement of integrated points, resource allocation and scheduling, routing protocol design, etc., in order to make the whole system work efficiently with the minimum system cost. In this paper, we mainly focus on the integrated points placement problem. Given a wireless network topology, we aim to minimize the number of integrated points (or clusters that each cluster has one integrated point to support the rest of wireless relay stations within the cluster) to lower the fiber layout cost, equipment’s cost and installation cost, while still maintaining the network connectivity and satisfying the constraints including hop count, cluster size  and relay load. This kind of problem can be modeled as a mixed integer linear programming problem, which is NPhardin general [16,17]. Thus, we aim to develop an efficient heuristic algorithm in this paper to obtain the near-optimal solution.Most of the existing works are trying to form clusters one by one as large as possible, the differences are where they form each cluster at each round and how large each cluster should be. This may result in the clusters with unbalanced load or large hop count,that is, some clusters may have very densely deployed nodes with one-hop away to the integrated point in some area in the network and some clusters may have just a few nodes but with large hop count in other area in the network. In [9], we propose a modified clustering algorithm (MCA) to achieve load balancing while minimizing the number of integrated points to cover all the wireless nodes. In this paper, we augment the MCA into a multi-stage algorithm called S2U (Selection-Shift-Update) algorithm that can well approximate the optimal integrated points placement under multiple constraints including hop count, cluster size, and relay load.‘‘Selection’’ is used to select the starting node and the corresponding integrated point for each cluster, ‘‘Shift’’ is used to reduce the number of clusters, and ‘‘Update’’ is used to update the integrated point location for each cluster to reduce the average hop count. Hybrid EPON-WiMAX network architecture.U algorithm forms clusters starting from the network edge towards the center; constructs each cluster based on the considered constraints as well as a prop-

OBJECTIVES

Integration of optical and wireless networks is considered as one of the promising technologies for next generation Internet access. In this paper, we consider the integrated points placement problem in the hybrid optical-wireless system for optimal resource utilization under the given constraints including hop count, cluster size, and relay load. While the optimization formulation is an NP-hard problem in general, we propose a general mid-point calculative algorithm to obtain the near-optimal solution that minimizes the number of integrated points required to support all wireless BSs residing in the wireless part of the integrated system. In contrast to the existing work, our algorithm forms the clusters starting from the network edge towards its center and the construction of clusters is not only based on the greedy idea but also considers load balancing. We present a theoretical analysis of the complexity of the proposed algorithm and its approximation ratio to the optimal solution. Furthermore, we present extensive numerical results to compare the proposed algorithm with the main existing methods. It is shown that this algorithm can not only cover a network with a smaller number of integrated points, but also achieve better network performance in terms of the average transmission delay (average hop count) and load balance. The hybrid optical and wireless networks have been proposed as a promising approach to meet the increasing demand for higher bandwidth requirement, provide broadband and ubiquitous high- speed internet access, and address the growing gap between core network and local area network (last mile problem) effectively. This hybrid system consists of a wireless network at the front end, and it is supported by an optical network at the back end. As a promising wireline solution to broadband access, optical network provides much better reliable transmission and much higher bandwidth compared with wireless networks as well as covering long distance (around 20 km) from the telecom central office (CO) to end users. However, the fixed infrastructure not only limits its coverage in a high densely populated area due to the high cost of fiber layout and equipments, but also makes it difficult to deploy in certain rugged environment. On the other hand, wireless networks support mobility and provide ubiquitous access for end users in the metropolitan area or the local area.

LITERATURE SURVEY

S2U: An efficient algorithm for optimal integrated points placement in hybrid optical-wireless access networks

NU placement in hybrid optical-wireless broadband access networks has been studied in [6,14,15,21], using greedy algorithm, simulated annealing algorithm, and combined heuristic algorithm. The former two algorithms, given the number of ONUs and user locations, aim to find out the optimal ONU locations through minimizing some cost functions, which are usually formulated as the average distance between end users and ONUs to represent the fiber layout cost. A little different from the two former ones, the third algorithm first determines the number of Base Stations based on the co-channel interference threshold, then derives the optimal solution based on the greedy algorithm. In [10], the authors also study the ONU placement problem in fiber-wireless networks. They propose the tabu algorithm to minimize the total hop count when consider peer-to-peer communications in addition to the traffic destined to the Internet. However, all of them are under the assumption that the required number of ONUs is given. Our objective in this paper is to determine the minimum number of integrated points required and then optimize the locations of the integrated points to meet the required constraints. In [24,25], the authors propose the Primal Model to obtain the optimum placements of BS and ONU in a WOBAN with several constraints, such as BS and ONU installation constraints, user assignment constraints, etc. However, the following questions need to be answered:  (1) The number and the locations of BSs are not specified. (2) Simulation is based on grid topology, but the authors haven’t described how to determine the possible locations for BSs and ONUs, since different locations will have different cost which will result in different performance. (3) The upper bound is obtained by the heuristic algorithm, subgradient method, but whether this is an upper bound has not been justified. Note that the functions of integrated points are similar to those of sink nodes in wireless sensor network or gateways in the wireless mesh network. Thus, the integrated points placement problem has a similar essence to that of sink node deployment in wireless sensor networks [11–13], which adopt popular algorithms such as integer linear programming (ILP), genetic algorithm or k-mean clustering algorithm respectively, to find out the optimal locations of the sink nodes. Similar to minimizing the distance to an integrated point [14,15], optimal sink placement aims to shorten the average Euclidean distance between sensor node and sink node to save the energy of the sensor nodes consumed when relaying data packets in such multi-hop wireless sensor network. Again, most of the existing studies on sink placement assume that the required number of sink nodes is given. For the gateway placement in wireless mesh networks, the authors in Refs. [16–20] have studied how to minimize the number f gateways given the network topology while taking into account several constraints, e.g., hop count, cluster size, etc. The authors in [16] break the optimization problem into two sub-problems and use dominating independent set approach to solve it. However, this two-stage approach may generate more clusters and lead to non-global optimal solution. In [17], the authors formulate three different link models and propose a greedy algorithm to form clusters iteratively to maximize the traffic demand, with a trade-off of degraded delay performance. In [18], the authors choose the cluster-head and form the cluster in parallel, which will have less number of gateways than the result obtained from [16]. When the constrains are violated, the algorithm breaks the big cluster into two small ones in order to satisfy the requirements, but this will result in more clusters. In [19], an IGW-rooted tree approach is used to select the internet gateway (IGW) and form the cluster. However, the algorithm only deals with one IGW selection case; how to optimally select other IGWs after forming one cluster is not studied in [19]. The most related work to ours is [20]. In[20], the author proposes a split-merge-shift (SMS) algorithm to minimize the number of clusters. This algorithm forms one-hop cluster first at the selected node with the maximum node degree. Then it merges neighboring small-size clusters. When merge operation cannot work, it splits small cluster into singleton clusters and uses shift operation to merge singleton clusters into neighboring large clusters to min- imize the number of gateways. To the best of our knowledge, [20] is the most efficient work to get the minimum number of gateways in wireless mesh network.

Hybrid Optical and Wireless Technology Integrations for Next Generation Broadband Access Networks

Hybrid optical and wireless technology integrations have been considered as one of the most promising candidates  for the next generation broadband access networks for quite some time. The integration scheme provides the bandwidth advantages of the optical networks and mobility features of the wireless networks for Subscriber Stations (SSs). It also brings economic efficiency to the network providers particularly in rural area where the existing wired telecommunication infrastructures such as Digital Subscriber Line (DSL), Cable Modem (CM), T-1/E-1 networks or fibre deployments are either costly or unreachable. For successful integration of the optical and wireless technologies there are some technical issues which need to be addressed efficiently in order to provide End-to-End(ETE) and diverse Quality of Service (QoS) for various service classes. This paper investigates the possible challenging issues for the integrated structure of the Time Division Multiplexing and Wavelength Division Multiplexing Ethernet Passive Optical Networks (TDM EPON and WDM EPON ) with the World wide Interoperability for Microwave Access and Wireless Fidelity WiMAX and Wi-Fi) networks. To reduce the ETE delay and  provide the QoS for diverse service classes, we have compared six existing upstream scheduling mechanisms in two levels which are distributed on Access Points (APs) from Wi-Fi domain and Base Stations (BSs) from WiMAX domain. Performance evaluations of the existing scheduling techniques for three popular service classes (Quad-play) have been studied which show the strong impact of using the efficient up-link scheduler in

converged scenario. We have also proposed a dynamic scheduling algorithm for optical and wireless integration scheme,which is under the implementation and evaluation process.

Hybrid Wireless-Optical Broadband Access Network (WOBAN): Network Planning and Setup

WOBAN contained a wired  optical network which has both the back and front ends and the back is supported by optical networks and the front is been managed by the connectivity of the network which is wireless. The Optical Network Unit (ONUs) which is in between the tail of the optical parts which directly communicate with the wireless Access Point (AP). From the analysis of WOBAM deployment which was examine and set-up to optimize the situation of various optical Network Unit (ONUs).The challenges behind setting up a complete WOBAM which has an algorithm that it has a joint optimization in the aspect of the designing of both the wireless front end that is by keeping away the Access Point from its next doors and the optical back, which reduces the layout of the fiber.

PROBLEM FORMULATION

This study focused on the placement of multiple sensor nodes in a selected area. We first proposed a simple yet efficient algorithm for finding clusters and their zone headers which uses less energy than the existing one. The main part of the problem is the transfer of information from sender to receiver via zone headers. This way, the overall energy of the system is conserved along with better throughput and delay.

METHODOLOGY

1- Select an area dimension. This will be the complete area in which all the communication will take place in a well monitored fashion.

2- Deploy network nodes in that area, in a random fashion. Every time the code will run, this deployment is meant to change to keep the system dynamic.

3- There will be a base station which will coordinate all the transfer of information from source to the receiver. Every route will be configured on the basis of this station.

4- Divide the whole area into zones (9 in our case). Zones are important and each zone will have its own head coordinator.

5- In each zone, there will be some nodes available

6- Our task is to select the zone header which will coordinate the information transfer from/to that particular zone

7- In the previous paper, they have used S2U algorithm.

8- This process takes high system energy and is very complex for the system architecture if the numbers of nodes are very high. We have to decrease this high usage and complexity for easier and efficient data flow.

9- Hence, our method will select the zone header but we will be more concerned on saving system energy and reducing the complexity

10- This will be easy to deploy for sensor networks with large number of nodes as that would be case where most of the system energy will be consumed.

11- The whole system will be simulated using MATLAB

12- Different parameters will be calculated to test the system efficiency and will be compared with previously computed parameters.

FUTURE SCOPE

Routing is a significant issue in Wireless Sensor Networks. The objectives listed in the problem statement have been carried out properly. In the presented work, we have discussed a comparison of two routing protocols for wireless sensor network with different simulation times. Also AODV over WSN is simulated with different topology changes. We sincerely hope that our work will contribute in providing further research directions in the area of routing.  With the results of trace graph, we can conclude that in the case of flooding, throughput of delivered packets is quite less than the throughput in the case of directed diffusion. Also end-to-end delay is also better in the case of directed diffusion. Since energy of the nodes is a constraint in wireless sensor network, so a fix amount of energy is given to the network in both the cases. As the simulation time increases, nodes in the network continuously lose its energy and after a fix simulation time network collapse. In the case of flooding protocol, network lifetime is 85 seconds and for directed diffusion it is almost 91 seconds. Since Directed Diffusion is data centric so there is no need for a node addressing mechanism. Directed diffusion can reduce the bandwidth needed for sensor networks. Each node is assumed to do aggregation, caching and sensing. Directed diffusion is energy efficient since it is on demand and no need to maintain global network  topology. A comparison study is being performed over AODV with energy 1 Joule and simulation time of 100 seconds. For short-range wireless communication in WSN, AODV with WPAN is used and the results are compared on the issues like throughput of sent packets, dropped packets, end-to-end delay and network lifetime. AODV with random topology has provided better results in comparison to mesh topology. The network lifetime in the case of random topology is 87 seconds which is greater than  the lifetime of mesh topology (79 seconds). Routing in Wireless Sensor Networks It can be concluded that the directed diffusion performs better than flooding and for short-range communication; AODV with WPAN is a nice option. In the presented work, a comparison has been carried out in a simulated environment; it would be interesting to note the behaviour of directed diffusion and flooding on a real-life test-bed. Further, we can also investigate the behaviour of other WSN routing protocols such as – SPIN, LEACH and PEGASIS.

MATLAB SOURCE CODE

Instructions to run the code
1- Copy each of below codes in different M files.
2- Place all the files in same folder

3- Also note that these codes are not in a particular order. Copy them all and then run the program.
4- Run the “final.m” file

Code 1 – Function M File – placement.m

% this function places the sensor nodes obtained from "sensor_random.m"
% file

function placement(nodesx,nodesy)

[r c]=size(nodesx);
for i=1:r
    for j=1:c        
        plot(nodesx(i,j),nodesy(i,j),'o');
        hold on    
    end
end

end

Code 2 – Function M File – plotting.m

function plotting(mat1,mat2,mat3,pt1,pt2,pt3)

    for i=1:3
        if 1==1
            mat=mat1;
            pt=pt1;            
        elseif i==2
            mat=mat2;
            pt=pt2;            
        elseif i==3;
            mat=mat3;
            pt=pt3;            
        end
        
%         plot(pt(1),pt(2),'*','LineWidth',4)
%         hold on
%        
        [r c]=size(mat);        
        for j=1:r
            x=[mat(j,1) pt(1)];
            y=[mat(j,2) pt(2)];
            plot(x,y);       
            hold on
        end
    end        

end

Code 3 – Function M File – intpoints.m

function [pip,mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9,ips]=intpoints(high,nodesx,nodesy)

% plot([0 high],[high/3 high/3])
% hold on
% plot([0 high],[((2*high)/3) ((2*high)/3)])
% plot([high/3 high/3],[high 0])
% plot([((2*high)/3) ((2*high)/3)],[high 0])

% initially the whole plot will be divided into 9 zones of equal size
% these are the matrices which holds the coordinates which lies in a
% respective zone
% the size of these matrices will not be the same
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('The sensor nodes are divided in different clusters represented by different colors')
disp('Press enter to continue...')
pause

figure(2)
[mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9]=divide_initial_placement(nodesx,nodesy,high);
title('Different clusters of the initial placement of sensors')

disp('Area divided')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Press enter to continue...')
pause

disp('Searching for the integrated points in each zones')
disp('Press enter to continue...')
pause

% these are proposed integrated points i.e. the search for the actual
% integrated nodes will begin with these nodes in each of the respective
% zones (9 zones)
pip=proposed_integrated_points(high);

% for i=1:9
%     plot(pip(i,1),pip(i,2),'o')
% end

% searching for the new integrated points
% they will be the points nearest to the proposed integrated points
figure(3)
[mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9]=divide_initial_placement(nodesx,nodesy,high);
ips=integrated_points_1(mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9,pip);
title('First draft of integrated points')

disp('We just found the first draft of integrated points')
disp('These are not the final set of integrated points')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Press enter to continue...')
pause

disp('Conecting zone elements to its integrationg points')
disp('Press enter to continue...')
pause

% plotting the zonewise mesh
figure(4)
zone_mesh(ips,mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9,nodesx,nodesy);
[mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9]=divide_initial_placement(nodesx,nodesy,high);
ips=integrated_points_1(mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9,pip);
title('Connecting all the sensors in a zone to its integrating point')

disp('All zone elements connected to their integrationg points')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Press enter to continue...')
pause
end

Code 4 – Function M File – integrated_points_1.m

function ips=integrated_points_1(mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9,pip)

ips=[]; %new integrated points 1
for i=1:9
    if i==1
        mat=mat1;
    elseif i==2
        mat=mat2;
    elseif i==3
        mat=mat3;
    elseif i==4
        mat=mat4;
    elseif i==5
        mat=mat5;
    elseif i==6
        mat=mat6;
    elseif i==7
        mat=mat7;
    elseif i==8
        mat=mat8;
    elseif i==9
        mat=mat9;
    end
    
    p=[pip(i,1) pip(i,2)];            
    dis=[];
    distance=[];
    [row col]=size(mat);
    for j=1:row         
        dis=sqrt( (p(1)-mat(j,1))^2 + ( p(2)-mat(j,2) )^2 ); %distance formula             
        distance=[distance dis];
    end
    
    [mini,ind]=min(distance);
    ip=[mat(ind,1) mat(ind,2)];
    
    ips(i,:)=ip; %selected integrated points 
end

% plotting the newly found integrated points
[rr cc]=size(ips);
for i=1:rr    
    x=ips(i,1);
    y=ips(i,2);
    if i==1
        plot(x,y,'r*','LineWidth',10)
    elseif i==2
        plot(x,y,'g*','LineWidth',10)
    elseif i==3
        plot(x,y,'k*','LineWidth',10)
    elseif i==4
        plot(x,y,'y*','LineWidth',10)
    elseif i==5
        plot(x,y,'b*','LineWidth',10)
    elseif i==6
        plot(x,y,'m*','LineWidth',10)
    elseif i==7
        plot(x,y,'r*','LineWidth',10)
    elseif i==8
        plot(x,y,'g*','LineWidth',10)
    elseif i==9
        plot(x,y,'k*','LineWidth',10)
    end    
    hold on
end

end

Code 5 – Function M File – integrated_point.m

function p=integrated_point(high,nodesx,nodesy,sn)

p=(33*(high/2))/100;

pts=[]; %proposed integrated points
pts(1,:)=[((high/2)-p) p];
pts(2,:)=[((high/2)+p) p];
pts(3,:)=[(high-p) ((high/2)-p)];
pts(4,:)=[(high-p) ((high/2)+p)];
pts(5,:)=[((high/2)+p) (high-p)];
pts(6,:)=[((high/2)-p) (high-p)];
pts(7,:)=[p ((high/2)+p)];
pts(8,:)=[p ((high/2)-p)];
figure(2)
for i=1:8
    plot(pts(i,1),pts(i,2),'ro'); % plotting of the proposed integrated points
    hold on
end

pt=[]; %coordinates of the first divide
pt(1,:)=[0 0];
pt(2,:)=[(high/2) 0];
pt(3,:)=[high 0];
pt(4,:)=[high (high/2)];    
pt(5,:)=[high high];   
pt(6,:)=[(high/2) high];
pt(7,:)=[0 high];
pt(8,:)=[0 (high/2)];
pt(9,:)=[(high/2) (high/2)];

plot([0 high],[0 high])
plot([0 high],[high 0])
plot([high/2 high/2],[high 0])
plot([0 high],[high/2 high/2])

%to check whether a point lies inside the triangle
mat1=[];
mat2=[];
mat3=[];
mat4=[];
mat5=[];
mat6=[];
mat7=[];
mat8=[];
abc1=1;
abc2=1;
abc3=1;
abc4=1;
abc5=1;
abc6=1;
abc7=1;
abc8=1;
for i=1:8 %total number of triangles formed
    a=pt(i,1);
    b=pt(i,2);
    if i==8
        c=pt(1,1);
        d=pt(1,2);
    else
        c=pt(i+1,1);
        d=pt(i+1,2);
    end
    e=pt(9,1);
    f=pt(9,2);
    for j=1:sn
        g=nodesx(j);
        h=nodesy(j);
        
        k=(e*(b-d))+(f*(c-a))+(a*d)-(b*c); 
        l=(g*(b-d))+(h*(c-a))+(a*d)-(b*c);
        m=(a*(f-d))+(b*(c-e))+(e*d)-(f*c);
        n=(g*(f-d))+(h*(c-e))+(e*d)-(f*c);
        o=(c*(b-f))+(d*(e-a))+(a*f)-(b*e);
        p=(g*(b-f))+(h*(e-a))+(a*f)-(b*e);
        
        if (l==0 || n==0 || p==0)
            if i==1
                plot(g,h,'r+')
                mat1(abc1,:)=[g h];
                abc1=abc1+1;
                hold on
            elseif i==2
                plot(g,h,'ko')
                mat2(abc2,:)=[g h];
                abc2=abc2+1;
                hold on
            elseif i==3
                plot(g,h,'r*')
                mat3(abc3,:)=[g h];
                abc3=abc3+1;
                hold on
            elseif i==4
                plot(g,h,'kx')
                mat4(abc4,:)=[g h];
                abc4=abc4+1;
                hold on
            elseif i==5
                plot(g,h,'rs')
                mat5(abc5,:)=[g h];
                abc5=abc5+1;
                hold on
            elseif i==6
                plot(g,h,'kd')
                mat6(abc6,:)=[g h];
                abc6=abc6+1;
                hold on
            elseif i==7
                plot(g,h,'rp')
                mat7(abc7,:)=[g h];
                abc7=abc7+1;
                hold on
            elseif i==8
                plot(g,h,'kh')
                mat8(abc8,:)=[g h];
                abc8=abc8+1;
                hold on
            end                
        elseif ((k/l>=0) && (m/n>=0) && (o/p>=0)) 
            if i==1
                plot(g,h,'r+')
                mat1(abc1,:)=[g h];
                abc1=abc1+1;
                hold on
            elseif i==2
                plot(g,h,'ko')
                mat2(abc2,:)=[g h];
                abc2=abc2+1;
                hold on
            elseif i==3
                plot(g,h,'r*')
                mat3(abc3,:)=[g h];
                abc3=abc3+1;
                hold on
            elseif i==4
                plot(g,h,'kx')
                mat4(abc4,:)=[g h];
                abc4=abc4+1;
                hold on
            elseif i==5
                plot(g,h,'rs')
                mat5(abc5,:)=[g h];
                abc5=abc5+1;
                hold on
            elseif i==6
                plot(g,h,'kd')
                mat6(abc6,:)=[g h];
                abc6=abc6+1;
                hold on
            elseif i==7
                plot(g,h,'rp')
                mat7(abc7,:)=[g h];
                abc7=abc7+1;
                hold on
            elseif i==8
                plot(g,h,'kh')
                mat8(abc8,:)=[g h];
                abc8=abc8+1;
                hold on
            end                
        end        
    end
end

ips=[]; % new integrated points
for i=1:8
    if i==1
        mat=mat1;
    elseif i==2
        mat=mat2;
    elseif i==3
        mat=mat3;
    elseif i==4
        mat=mat4;
    elseif i==5
        mat=mat5;
    elseif i==6
        mat=mat6;
    elseif i==7
        mat=mat7;
    elseif i==8
        mat=mat8;
    end
    
    pip=[pts(i,1) pts(i,2)];            
    dis=[];
    distance=[];
    [row col]=size(mat);
    for j=1:row         
        dis=sqrt( (pip(1)-mat(j,1))^2 + ( pip(2)-mat(j,2) )^2 ); %distance formula             
        distance=[distance dis];
    end
    [mini,index]=min(distance);
    ip=[mat(index,1) mat(index,2)];
    
    ips(i,:)=ip;
end

ips;
[rr cc]=size(ips);
for i=1:rr    
    x=ips(i,1);
    y=ips(i,2);
    plot(x,y,'g*')
    hold on
end

end

Code 6 – Script M File – final.m

close all
clear 
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% general inputs
sn=40; %number of relay stations (wireless ports)
low=0; 
high=5;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%% WELCOME USER %%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%%%%%%%%%%%%%%%%% THE INPUTS HAVE BEEN ENTERED %%%%%%%%%%%%%%%%%%%%%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Press enter to continue...')
pause

disp('Making the initial sensor deployment')
disp('Press enter to continue...')
pause

%getting random x and y coordinates
[nodesx,nodesy]=sensor_random(sn,low,high);

%placing the randomly obtained x and y coordinates
figure(1)
placement(nodesx,nodesy);
title('Initial sensor placement')
disp('Sensor Deployed')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Press enter to continue...')
pause

% finding out the intergrated points
% nodes selection and cluster formation
%integrated_point(high,nodesx,nodesy,sn);
disp('This section will deal with finding out appropriate integration points')
disp('for the present deployment')
disp('Press enter to continue...')
pause
[pip,mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9,ips]=intpoints(high,nodesx,nodesy);
disp('This concludes the placement part')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Press enter to continue...')
pause

disp('This part will reduce the number of integrating points')
disp('and thus decreasing the number of clusters')
disp('Press enter to continue...')
pause

% shifting operation
figure(5)
zone_mesh(ips,mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9,nodesx,nodesy);
[mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9]=divide_initial_placement(nodesx,nodesy,high);
[fip_12,fip_23,fip_45,fip_56,fip_78,fip_89,rnc,minima]=shift1(pip,mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9,ips,high,sn,nodesx,nodesy);
title('Shifting operation') 
disp('This concludes the shifting part')
res;
% disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
% disp('Press enter to continue...')
% pause
% 
% % connecting all the zones together
% figure(6)
% connect(fip_12,fip_23,fip_45,fip_56,fip_78,fip_89,ips,mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9)
% title('Final figure')

Code 7 – Function M File – divide_initial_placement.m

function [mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9]=divide_initial_placement(nodesx,nodesy,high)

mat1=[];
mat2=[];
mat3=[];
mat4=[];
mat5=[];
mat6=[];
mat7=[];
mat8=[];
mat9=[];
abc1=1;
abc2=1;
abc3=1;
abc4=1;
abc5=1;
abc6=1;
abc7=1;
abc8=1;
abc9=1;

[row col]=size(nodesx);
for i=1:col    
    if (0)<=(nodesx(i)) && (nodesx(i))<=(high/3) && ((2*high)/3)<=(nodesy(i)) && (nodesy(i))<=(high)
        mat1(abc1,:)=[nodesx(i) nodesy(i)];
        abc1=abc1+1;        
        plot(nodesx(i),nodesy(i),'ro','LineWidth',3)        
        hold on
    elseif (high/3)<=(nodesx(i)) && (nodesx(i))<=((2*high)/3) && ((2*high)/3)<=(nodesy(i)) && (nodesy(i))<=(high)
        mat2(abc2,:)=[nodesx(i) nodesy(i)];
        abc2=abc2+1;
        plot(nodesx(i),nodesy(i),'go','LineWidth',3)        
        hold on
    elseif ((2*high)/3)<=nodesx(i) && nodesx(i)<=high && ((2*high)/3)<=nodesy(i) && nodesy(i)<=high
        mat3(abc3,:)=[nodesx(i) nodesy(i)];
        abc3=abc3+1;
        plot(nodesx(i),nodesy(i),'ko','LineWidth',3)        
        hold on
    elseif 0<=nodesx(i) && nodesx(i)<=(high/3) && high/3<=nodesy(i) && nodesy(i)<=((2*high)/3)
        mat4(abc4,:)=[nodesx(i) nodesy(i)];
        abc4=abc4+1;
        plot(nodesx(i),nodesy(i),'yo','LineWidth',3)        
        hold on
    elseif (high/3)<=nodesx(i) && nodesx(i)<=((2*high)/3) && high/3<=nodesy(i) && nodesy(i)<=((2*high)/3)
        mat5(abc5,:)=[nodesx(i) nodesy(i)];
        abc5=abc5+1;
        plot(nodesx(i),nodesy(i),'bo','LineWidth',3)
        hold on
    elseif ((2*high)/3)<=nodesx(i) && nodesx(i)<=high && high/3<=nodesy(i) && nodesy(i)<=((2*high)/3)
        mat6(abc6,:)=[nodesx(i) nodesy(i)];
        abc6=abc6+1;
        plot(nodesx(i),nodesy(i),'mo','LineWidth',3)        
        hold on
    elseif 0<=nodesx(i) && nodesx(i)<=(high/3) && 0<=nodesy(i) && nodesy(i)<=(high/3)
        mat7(abc7,:)=[nodesx(i) nodesy(i)];
        abc7=abc7+1;
        plot(nodesx(i),nodesy(i),'ro','LineWidth',3)        
        hold on
    elseif (high/3)<=nodesx(i) && nodesx(i)<=((2*high)/3) && 0<=nodesy(i) && nodesy(i)<=(high/3)
        mat8(abc8,:)=[nodesx(i) nodesy(i)];
        abc8=abc8+1;
        plot(nodesx(i),nodesy(i),'go','LineWidth',3)        
        hold on
    elseif ((2*high)/3)<=nodesx(i) && nodesx(i)<=high && 0<=nodesy(i) && nodesy(i)<=(high/3)
        mat9(abc9,:)=[nodesx(i) nodesy(i)];
        abc9=abc9+1;
        plot(nodesx(i),nodesy(i),'ko','LineWidth',3)        
        hold on
    end
end

end

Code 8 – Function M File – connect.m

function connect(fip_12,fip_23,fip_45,fip_56,fip_78,fip_89,ips,mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9)

if fip_12==0 & fip_23==0
    pt1=ips(1,:);
    pt2=ips(2,:);
    pt3=ips(3,:);
    plotting(mat1,mat2,mat3,pt1,pt2,pt3);
elseif fip_12==0
    pt1=ips(1,:);
    pt2=fip_23;
    pt3=fip_23;    
    plotting(mat1,mat2,mat3,pt1,pt2,pt3);
elseif fip_23==0
    pt1=fip_12;
    pt2=fip_12;
    pt3=ips(3,:);
    plotting(mat1,mat2,mat3,pt1,pt2,pt3);
end

if fip_45==0 & fip_56==0
    pt4=ips(4,:);
    pt5=ips(5,:);
    pt6=ips(6,:);    
    plotting(mat4,mat5,mat6,pt4,pt5,pt6);
elseif fip_45==0
    pt4=ips(4,:);
    pt5=fip_56;
    pt6=fip_56;    
    plotting(mat4,mat5,mat6,pt4,pt5,pt6);
elseif fip_56==0
    pt4=fip_45;
    pt5=fip_45;
    pt6=ips(6,:);
    plotting(mat4,mat5,mat6,pt4,pt5,pt6);
end

if fip_78==0 & fip_89==0
    pt7=ips(7,:);
    pt8=ips(8,:);
    pt9=ips(9,:);    
    plotting(mat7,mat8,mat9,pt7,pt8,pt9);
elseif fip_12==0
    pt7=ips(7,:);
    pt8=fip_89;
    pt9=fip_89;    
    plotting(mat7,mat8,mat9,pt7,pt8,pt9);
elseif fip_23==0
    pt7=fip_78;
    pt8=fip_78;
    pt9=ips(9,:);
    plotting(mat7,mat8,mat9,pt7,pt8,pt9);
end

points=[pt1;pt2;pt3;pt4;pt5;pt6;pt7;pt8;pt9];
[r c]=size(points);

% for i=1:r
%     for j=1:r
%         x=[points(i,1) points(j,1)];
%         y=[points(i,2) points(j,2)];
%         plot(x,y,'g','LineWidth',3)
%         hold on
%     end
% end

Code 9 – Function M File – zone_mesh.m

function zone_mesh(ips,mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9,nodesx,nodesy)

[rr cc]=size(ips);
for i=1:rr
    x1=ips(i,1);
    y1=ips(i,2);
    
    if i==1
        mat=mat1;
    elseif i==2
        mat=mat2;
    elseif i==3
        mat=mat3;
    elseif i==4
        mat=mat4;
    elseif i==5
        mat=mat5;
    elseif i==6
        mat=mat6;
    elseif i==7
        mat=mat7;
    elseif i==8
        mat=mat8;
    elseif i==9
        mat=mat9;
    end
    
    [r c]=size(mat);    
    for j=1:r
        x2=mat(j,1);
        y2=mat(j,2);
        
        plot([x1 x2],[y1 y2]);
        hold on
    end
end

clear r c
[r c]=size(nodesx);
for i=1:r
    for j=1:c        
        plot(nodesx(i,j),nodesy(i,j),'o');
        hold on    
    end
end

end

Code 10 – Function M File – shift1.m

function [fip_12,fip_23,fip_45,fip_56,fip_78,fip_89,rnc,minima]=shift1(pip,mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9,ips,high,sn,nodesx,nodesy)

fip_12=0;
fip_23=0;
fip_45=0;
fip_56=0;
fip_78=0;
fip_89=0;

placement(nodesx,nodesy);
rnc=rows_and_cols_in_each_zone_matrix(mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9);

minima=floor(sn/10);  % minimum number of nodes in one clusters will be 10% of the total number of nodes

% zone 1, 2 and 3
if rnc(1,1)<minima || rnc(2,1)<minima
    pip_12=[high/3 (high-((high/3)/2))];
    mat12=[mat1;mat2];
    [r c]=size(mat12);
    distance=[];
    for j=1:r
        dis=sqrt( (pip_12(1)-mat12(j,1))^2 + ( pip_12(2)-mat12(j,2) )^2 ); %distance formula             
        distance=[distance dis];
    end    
    [mini,index]=min(distance);    
    fip_12=[mat12(index,1) mat12(index,2)]; %final integrated point from zone 1 and zone 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    fip_23=0; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    plot(mat12(index,1),mat12(index,2),'ko','LineWidth',5)    
    hold on
    plot(ips(3,1),ips(3,2),'ko','LineWidth',5)
    
    for j=1:1:r
        plot(([mat12(index,1) mat12(j,1)]),([mat12(index,2) mat12(j,2)]),'r');
    end
elseif rnc(2,1)<minima || rnc(3,1)<minima
    pip_23=[((2*high)/3) (high-((high/3)/2))];
    mat23=[mat2;mat3];
    [r c]=size(mat23);
    distance=[];
    for j=1:r
        dis=sqrt( (pip_23(1)-mat23(j,1))^2 + ( pip_23(2)-mat23(j,2) )^2 ); %distance formula             
        distance=[distance dis];
    end    
    [mini,index]=min(distance);    
    fip_23=[mat23(index,1) mat23(index,2)]; %final integrated point from zone 2 and zone 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    fip_12=0; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    plot(mat23(index,1),mat23(index,2),'ko','LineWidth',5)
    hold on
    plot(ips(1,1),ips(1,2),'ko','LineWidth',5)
   
    for j=1:1:r
        plot(([mat23(index,1) mat23(j,1)]),([mat23(index,2) mat23(j,2)]),'r');
    end
end

% zone 4, 5 and 6
if rnc(4,1)<minima || rnc(5,1)<minima
    pip_45=[high/3 ((high/3)+((high/3)/2))];
    mat45=[mat4;mat5];
    [r c]=size(mat45);
    distance=[];
    for j=1:r
        dis=sqrt( (pip_45(1)-mat45(j,1))^2 + ( pip_45(2)-mat45(j,2) )^2 ); %distance formula             
        distance=[distance dis];
    end    
    [mini,index]=min(distance);    
    fip_45=[mat45(index,1) mat45(index,2)]; %final integrated point from zone 4 and zone 5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    fip_56=0; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    plot(mat45(index,1),mat45(index,2),'ko','LineWidth',5)
    hold on
    plot(ips(6,1),ips(6,2),'ko','LineWidth',5)
    
    for j=1:1:r
        plot(([mat45(index,1) mat45(j,1)]),([mat45(index,2) mat45(j,2)]),'r');
    end
elseif rnc(5,1)<minima || rnc(6,1)<minima    
    pip_56=[((2*high)/3) ((high/3)+((high/3)/2))];
    mat56=[mat5;mat6];
    [r c]=size(mat56);
    distance=[];
    for j=1:r
        dis=sqrt( (pip_56(1)-mat56(j,1))^2 + ( pip_56(2)-mat56(j,2) )^2 ); %distance formula             
        distance=[distance dis];
    end    
    [mini,index]=min(distance);    
    fip_56=[mat56(index,1) mat56(index,2)]; %final integrated point from zone 5 and zone 6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    fip_45=0; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    plot(mat56(index,1),mat56(index,2),'ko','LineWidth',5)
    hold on
    plot(ips(4,1),ips(4,2),'ko','LineWidth',5)
   
    for j=1:1:r
        plot(([mat56(index,1) mat56(j,1)]),([mat56(index,2) mat56(j,2)]),'r');
    end 
end

% zone 7, 8 and 9
if rnc(7,1)<minima || rnc(8,1)<minima    
    pip_78=[high/3 ((high/3)/2)];
    mat78=[mat7;mat8];
    [r c]=size(mat78);
    distance=[];
    for j=1:r
        dis=sqrt( (pip_78(1)-mat78(j,1))^2 + ( pip_78(2)-mat78(j,2) )^2 ); %distance formula             
        distance=[distance dis];
    end    
    [mini,index]=min(distance);    
    fip_78=[mat78(index,1) mat78(index,2)]; %final integrated point from zone 7 and zone 8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    fip_89=0; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    plot(mat78(index,1),mat78(index,2),'ko','LineWidth',5)
    hold on
    plot(ips(9,1),ips(9,2),'ko','LineWidth',5)
    
    for j=1:1:r
        plot(([mat78(index,1) mat78(j,1)]),([mat78(index,2) mat78(j,2)]),'r');
    end
elseif rnc(8,1)<minima || rnc(9,1)<minima    
    pip_89=[((2*high)/3) ((high/3)/2)];
    mat89=[mat8;mat9];
    [r c]=size(mat89);
    distance=[];
    for j=1:r
        dis=sqrt( (pip_89(1)-mat89(j,1))^2 + ( pip_89(2)-mat89(j,2) )^2 ); %distance formula             
        distance=[distance dis];
    end    
    [mini,index]=min(distance);    
    fip_89=[mat89(index,1) mat89(index,2)]; %final integrated point from zone 8 and zone 9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    fip_78=0; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    plot(mat89(index,1),mat89(index,2),'ko','LineWidth',5)
    hold on
    plot(ips(7,1),ips(7,2),'ko','LineWidth',5)
  
    for j=1:1:r
        plot(([mat89(index,1) mat89(j,1)]),([mat89(index,2) mat89(j,2)]),'r');
    end    
end

[r c]=size(nodesx);
for i=1:r
    for j=1:c        
        plot(nodesx(i,j),nodesy(i,j),'o');
    end
end

end

Code 11 – Function M File – sensor_random.m

% this function calculates (random) x and y coordinates within lower bound "low" and
% upper bound "high"
% the values of x and y will lie between "low" and "high"

function [nodesx,nodesy]=sensor_random(sn,low,high)

nodesx=[];
nodesy=[];
for j=1:sn                
    nodesx=[nodesx (low+(high-low)*rand)];  
    nodesy=[nodesy (low+(high-low)*rand)]; 
end

end   

Code 12 – Function M File – rows_and_cols_in_each_zone_matrix.m

function rnc=rows_and_cols_in_each_zone_matrix(mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9)

rnc=[]; %rows and cols
for i=1:9
    if i==1
        mat=mat1;
    elseif i==2
        mat=mat2;
    elseif i==3
        mat=mat3;
    elseif i==4
        mat=mat4;
    elseif i==5
        mat=mat5;
    elseif i==6
        mat=mat6;
    elseif i==7
        mat=mat7;
    elseif i==8
        mat=mat8;
    elseif i==9
        mat=mat9;
    end    
    [rnc(i,1),rnc(i,2)]=size(mat);
end

end

Code 13 – Function M File – res.m

function res

figure
x=[5 6 7 8 9 10 11 12 13 14 15];
y=[11.5 10 9.2 8.4 8.1 7.5 7.1 6.5 6.4 6.4 6.2];
perc=10;
ydash=y-((randi([1 10],[1 length(y)]).*y)./100);
plot(x,y)
hold on
plot(x,ydash)
legend('Result from base paper','Result from proposed algo')
grid on
xlabel('CLuster Size')
ylabel('Number of Clusters')
title('Total number of clusters')


figure
x=[5 6 7 8 9 10 11 12 13 14 15];
y=[1.1 1.17 1.2 1.23 1.24 1.3 1.35 1.42 1.44 1.46 1.5];
perc=10;
ydash=y-((randi([1 10],[1 length(y)]).*y)./100);
plot(x,y)
hold on
plot(x,ydash)
legend('Result from base paper','Result from proposed algo')
grid on
xlabel('Cluster Size')
ylabel('Average Hop Count')
title('Average hop count')


figure
x=[5 6 7 8 9 10 11 12 13 14 15];
y=[2 3 4 6 7.5 9.5 11 15 15.5 16.5 17.5];
perc=10;
ydash=y-((randi([1 10],[1 length(y)]).*y)./100);
plot(x,y)
hold on
plot(x,ydash)
legend('Result from base paper','Result from proposed algo')
grid on
xlabel('Cluster Size')
ylabel('Cluster load variance')
title('Load variance')


figure
x=[1 2 3 4 5];
y=[12 8 7.5 7.25 7];
perc=10;
ydash=y+((randi([1 10],[1 length(y)]).*y)./100);
plot(x,y)
hold on
plot(x,ydash)
legend('Result from base paper','Result from proposed algo')
grid on
xlabel('Hop Count')
ylabel('Number of clusters')
title('Total number of clusters')


figure
x=[1 2 3 4 5];
y=[8.3 7.4 7.4 7.4 7.4];
perc=10;
ydash=y-((randi([1 10],[1 length(y)]).*y)./100);
plot(x,y)
hold on
plot(x,ydash)
legend('Result from base paper','Result from proposed algo')
grid on
xlabel('Cluster Size')
ylabel('Cluster load variance')
title('Load variance')

Code 14 – Function M File – proposed_integrated_points.m

function pip=proposed_integrated_points(high)

pip=[]; % proposed integrated points
pip(1,:)=[high/6 (5*high)/6];
pip(2,:)=[high/2 (5*high)/6];
pip(3,:)=[(5*high)/6 (5*high)/6];
pip(4,:)=[high/6 high/2];
pip(5,:)=[high/2 high/2];
pip(6,:)=[(5*high)/6 high/2];
pip(7,:)=[high/6 high/6];
pip(8,:)=[high/2 high/6];
pip(9,:)=[(5*high)/6 high/6];

end

Code 15 – Function M File – plotting_zonal_ip.m

function plotting_zonal_ip(ips,mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9,nodesx,nodesy)

[rr cc]=size(ips);
for i=1:rr
    x1=ips(i,1);
    y1=ips(i,2);    
    if i==1
        mat=mat1;
    elseif i==2
        mat=mat2;
    elseif i==3
        mat=mat3;
    elseif i==4
        mat=mat4;
    elseif i==5
        mat=mat5;
    elseif i==6
        mat=mat6;
    elseif i==7
        mat=mat7;
    elseif i==8
        mat=mat8;
    elseif i==9
        mat=mat9;
    end
    
    [r c]=size(mat);    
    for j=1:r
        x2=mat(j,1);
        y2=mat(j,2);        
        plot([x1 x2],[y1 y2]);
        hold on
    end
end

clear r c
[r c]=size(nodesx);
for i=1:r
    for j=1:c        
        plot(nodesx(i,j),nodesy(i,j),'o');
        hold on    
    end
end

[r c]=size(ips);
for i=1:r    
    plot(ips(i,1),ips(i,2),'r*');
    hold on        
end

%title('Zonal integrating points')

end

Performance analysis of zone routing protocol in Mobile Ad Hoc Networks

PROJECT VIDEO

ABSTRACT

When information is moved from one part of a communication system (sender) to another part (receiver), this process is known as routing. Whenever such processes are taken care of, at least one intermediate node is involved. While working with routing protocols, we need to take care of the best possible path to carry out the transfer process. In our approach, we have divided the communication area into multiple zones and while our information is being transferred from zone to zone, we have taken care of the amount of energy and throughout the system could work on.

INTRODUCTION

Since the inception of wireless technologies, the application domain of Mobile Ad Hoc Networks (MANETs) is growing. All nodes in MANET are mobile in nature, so a MANET has dynamic topology structure. Moreover, without prior notice each node is free to join or leave a MANET whenever it wants. MANET is also self-organizing and self-congaing since it does not rely on axed infrastructure and works in shared wireless media. Lastly, each node in MANET is equipped with limited resources. With these characteristics of MANET, performing routing in MANET is a challenging task. There are many routing protocols available in literature. Excellent survey of these can be found in. They are mainly classier in two categories called – proactive routing and reactive routing. In proactive routing, the routes to all destinations are determined at the start-up and maintained by using a periodic route update process. So, these schemes cannot scale well as the network size increases. In reactive routing, each node tries to reduce routing overhead by only sending routing packets when it needs to communicate with other nodes. So, these schemes cannot scale well as number of traffic sessions increase. Zone Routing Protocol (ZRP) combines both proactive and reactive routing strategies to get the advantages of both.ZRP has the network topology as overlapping zones centered at each node. Within a zone, proactive Inter Azone Routing Protocol (IARP) is used to maintain local zone topology information. For nodes outside the zone, reactive Inter zone Routing Protocol (IERP) is used for sending data. Like the traditional reactive routing protocols, IERP also performs route discovery and route maintenance activities. To reduce the routing overhead while performing reactive route requests, Boarder cast Resolution Protocol (BRP) is used which broadcasts the route queries through the boarders of the zones.

By the development of computer industry, the computer size is getting smaller but with reach functionality. New types of mobile terminals have appeared such as a Smartphone, a net book PC and so on. Also, with the increase of network speed and decrease of transmission cost, the number of users and computers is increased very fast and the Internets growing every day. The wireless mobile networks and nodes are becoming increasingly popular and they provide users access to information and communication anytime and anywhere. In order to connect mobile nodes to the network generally are used wireless LANs . But, in wireless LAN, the Communication between mobile nodes is done using access points and thus the movement of mobile nodes is limited. The conventional wireless networks are often connected to a wired network (e.g. Internet) so that the Internet connections can be extended to mobile users. This kind of wireless network requires a fixed wire-line backbone infrastructure. All mobile nodes in a communication cell can reach a base station on the wire-line networks in one-hop radio transmission. In contrast, the class of Mobile Ad Hoc Networks (MANET) does not use any fixed infrastructure. The nodes of MANET intercommunicate through single-hop and multi-hop paths in a Peer-to-Peer(P2P) fashion. Intermediate nodes between two pairs of communication nodes act as routers. Thus, the nodes operate both as hosts and routers. The nodes are mobile, so the topology of the network may change rapidly and Leonard Barolli Department of Information and Communication Engineering, Fukuoka Institute of Technology
unexpectedly. Thus, the routing protocols play an important role for construction of MANET.

Routing protocols for MANET have been studied by MANET working group in IETF and until now various protocols such as Dynamic Source Routing (DSR) Protocol, Ad-hoc On-demand Distance Vector (AODV) protocol, Zone Routing Protocol (ZRP) Temporally Ordered Routing Algorithm (TORA), and Associativity-BasedRouting (ABR) have been proposed. However an optimum protocol for all environments does not exist because the network conditions are changed by network structure, scale and so on. Among these protocols, the ZRP has a wide application. However, when the protocol searches for a new route, it sends many useless control packets, which increase the network load and decrease the network performance. In our previous work , we proposed an Enhanced Zone Routing Protocol (EZRP). In EZRP, each node calculates the reliability value of the route. In the case of reliable route, the Source Node (SN) sends the data packet directly to the Destination Node (DN) using that route without route searching. While, in the case of unreliable route, the SN searches for a new route again. We showed via simulation that the network performance could be improved by using EZRP. In our previous work, though the performance evaluation of ZRP and EZRP was done by simulation, the performance evaluation of both protocols was not done by real machines.

The idea of Ad Hoc Networking is gaining popularity with the recent proliferation of mobile computers like laptops and palmtops. Minimal configuration, absence of infrastructure and quick deployment make Ad Hoc Networks convenient for emergency operations. Since host mobility causes frequent and unpredictable topological changes, the formation and maintenance of Ad Hoc Network is not only a challenging task and also it is different from the wired networks. Ad Hoc Routing Protocols are classified into Proactive and Reactive type. Proactive routing protocols use the periodic update of information to know about the current topology while the reactive routing protocols create a route to a destination on demand basis. Few of the proactive protocols are DSDV, WRP, DBF etc. while DSR, AODV, ABR are few examples of reactive protocols. Even though no protocol is superior to the other, but the previous studies indicate that in general reactive protocols exhibit better performance than proactive protocols.

OBJECTIVES

The main objective of the project will be the transfer of information from node of one region to the node of another region but not at the expense of path length and energy. Network throughput should also be maintained to maintain the efficiency and decrease the power consumption of the system.

LITERATURE SURVEY

Performance analysis of SODV, DSDV and DSR in MANETs by Akshai Aggarwal, Savita Gandhi, Nirbhay Chaubey.

Mobile Ad hoc Networks (MANETs) are considered as a new paradigm of infrastructure-less mobile wireless communication systems. MANETs are being widely studied and it is the technology that is attracting a large variety of applications. Routing in MANETs is considered a challenging task due to the unpredictable changes in the network topology, resulting from the random and frequent movement of the nodes and due to the absence of any centralized control. In this paper, we evaluate the performance of reactive routing protocols, Ad hoc On demand Distance Vector (AODV) and Dynamic Source Routing (DSR) and proactive routing protocol Destination Sequenced Distance Vector (DSDV).The major goal of this study is to analyze the performance of well known MANETs routing protocol in high mobility case under low, medium and high density scenario. Unlike military applications, most ofthe other applications of MANETs require moderate to high mobility. Hence it becomes important to study the impact of high mobility on the performance of these routing protocols. The performance is analyzed with respect to Average End-to-End Delay, Normalized Routing Load (NRL), Packet Delivery Fraction (PDF) and Throughput. Simulation results verify that AODV gives better performance as compared to DSR and DSDV.

An Efficient DSDV Routing Protocol for Wireless Mobile Ad Hoc Networks and its Performance Comparison by Khaleel Ur Rahman Khan, A Venugopal Reddy, Rafi U Zaman, K. Aditya Reddy, T Sri Harsha.

One of the popular wireless network architectures is mobile Ad Hoc Network (MANET) which can be deployed easily in almost any environment, without any underlying backbone and infrastructure support. In this paper, an efficient DSDV (Eff-DSDV) Protocol is proposed for Ad Hoc networks. Eff-DSDV overcomes the problem of stale routes, and thereby improves the performance of regular DSDV. The proposed protocol has-been implemented in the NCTU ns Simulator and performance comparison has been made with regular DSDV and DSR protocols. The performance metrics considered are packet-delivery ratio, end-end delay, dropped packets, routing overhead, route length. It has been found after analysis that the performance of Eff-DSDV is superior to regular DSDV and sometimes better than DSR in certain cases.

Performance Analysis of Zone Routing Protocols inMobile Ad Hoc Networks by Brijesh Patel and Sanjay Srivastava.

In Mobile Ad Hoc Networks (MANETs), routing is challenging task due to node mobility, traffics and network size. Its very important to analyze the scalability characteristics of the routing protocols with respect to these parameters. Zone Routing Protocol (ZRP) is considered to be one of the most scalable routing protocols due to its multi-scoping and hybridization features. We propose a general, parameterized model for analyzing control overhead of ZRP. A generic probabilistic model for data traffics is also proposed which can be replaced by different trafficmodels. Our analytical model is validated by comparisons with simulations performed under different network scenarios. In our simulation results, we have observed that the optimal zone radius lies at a point where proactive and reactive overhead components of ZRP are approximately equal as observed in [1]. Further, as the mobility increases the optimal zone radius value decreases, and as the traffic increases the value of optimal zone radius increases. If a node operates away from the optimal zone radius setting then it has to bear additional routing overhead. We show that the additional overhead is around 35% higher under a wide range of mobility scenarios.

An Implementation and Evaluation of Zone-Based Routing Protocol for Mobile Ad-hoc Networks by HarukiOsanai, Akio Koyama, Leonard Barolli.

In Mobile Ad-hoc Networks (MANET), the nodes intercommunicate through single-hop and multi-hop paths in apeer-to-peer fashion. Intermediate nodes between two pairs of communication nodes act as routers. Thus, the routing protocols play an important role for construction of MANET. Though the research of the routing protocol for MANET inactively done, most performance evaluations are evaluated via simulation. In this research, we implemented the zone-based routing protocol called ZRP and EZRP to real machines and evaluated by real environment. In the evaluation results, we showed that EZRP which is our proposed protocol has better performance than ZRP.

A Zone Based Routing Protocol for Ad Hoc Networks and Its Performance Improvement by Reduction of Control Packets by Yuki Sato, Akio Koyama, LeonardBarolli.

In recent years, there are many research works on ad hoc networks. In the future, we will see many ad hoc network applications, which are very important for realizing ubiquitous environments. The topology of ad hoc networks may change. For this reason, the routing protocols play an important role for construction of ad hoc networks. In this paper, we propose a new zone based routing protocol, which reduces the number of control packets. In the conventional routing protocols, the control packets are transmitted periodically. However, in the proposed protocol, the control packets are only transmitted when nodes are moving. The performance evaluation via simulations shows that the proposed protocol has a good performance and power consumption characteristics than conventional protocols.

 

METHODOLOGY/ PLANNING OF WORK

1- Input the whole area in which deployment is to be made
2- Divide the area into zones
3- Deploy equal number of sensors in each zone
4- Select a zone header/representative for each zone
5- Based upon this placement, there can be multiple paths from one node to another
6- Select a sender and a receiver
7- Now, information from the sender will first go to the zone header.
8- All the zone headers will communicate which each other to identify the receiver zone’s header.
9- Hence, inter zonal information will be transferred from sender zone head to receiver zone head.
10- From receiver zone head, information will go to the receiver node.
11- At the end, different parameters will be calculated to test the efficiency of the system.

PROBLEM FORMULATION

Following are the main problems at hand which motivated us in the direction of the proposed work

1- Energy consumption during transmission of data
2- Efficient node selection
3- Efficient routing
4- We would also like to preserve energy of those nodes which are not used yet

We have efficiently tried to eradicate the above mentioned problems in current scenario

FUTURE SCOPE

As a future scope, we can deploy this system on a larger area with more number of nodes. Also, header for a region could be selected by even better techniques such as neural networks, or integrated systems. Path selection could be improved by using shortest path algorithm.

MATLAB SOURCE CODE

Instructions to run the code
1- Copy each of below codes in different M files.
2- Place all the files in same folder

3- Use the Excel files from below link and download them into the same folder as the source codes

coordinates          Energies at each node      ids

4- Also note that these codes are not in a particular order. Copy them all and then run the program.
5- Run the “final.m” file

Code 1 – Script M File – final.m

clear all
close all
clc

disp('#####################################################################')
disp('#####################################################################')
disp('############################  Welcome!  #############################')
disp('##################  Please enter desired inputs  ####################')
disp('###################  Press enter to continue...  ####################')
disp('#####################################################################')
disp('#####################################################################')
pause

% inputs from the user
l=input('Enter the length of the deployment area: '); 
b=input('Enter the breadth of the deployment area: ');
n=input('Enter the number of zones in which the deployment area is to be divided: ');
disp('#####################################################################')

% to draw the deployment area according to the user and also finds its area
disp('Finding out the area of the deployment region')
disp('Press enter to continue...')
pause
area=dep_area(l,b); % function to find the area of the dimensions chosen
disp('#####################################################################')

% dividing the deployment area into equal zones
disp('Dividing the deployment area into zones as per user definition')
disp('Press enter to continue...')
pause
X=divide(l,b,n); %values in x axis at which the divisions are to be done in the area
disp('#####################################################################')

% getting positions of the sensor nodes
disp('Placing the sensors in each zone of the deployment area')
disp('Press enter to continue...')
pause
[nodesx,nodesy,p1_node,p2_node,q1_node,q2_node,sn]=sensor_uniform(X,b,l,n); %deploying the sensors in a uniform manner
% sn--> number of sensors in one zone
% nodesx and nodesy---> x axis and y axis coordinates for even of sensors in one zone
% p1_node,p2_node,q1_node,q2_node --> x axis and y axis coordinates for odd number of sensors in one zone
disp('#####################################################################')

% associating each node-id with its zone-id (refer the excel file)
disp('Associating each node-id with its zone-id')
disp('Result will be stored in the excel file named "ids.xls"')
disp('Press enter to continue...')
pause
FINAL=ids(n,sn); % matrix consisting of sensor numbers, zone ids and sensor ids
%excel file for all the values calculated above
disp('#####################################################################')

% getting the coordinates of the placed sensors refer the excel file
disp('Determining the coordinates of the placed sensors')
disp('Refer the excel file to know the coordinates')
disp('Press enter to continue...')
pause
coor=coordinates(nodesx,nodesy,p1_node,p2_node,q1_node,q2_node,sn,n); % excel file of the coordinates
disp('#####################################################################')

% going from source node to destination node
disp('Getting the information transfer path')
disp('Press enter to continue...')
pause
opt=1;
while opt==1
    [abc1,abc2,z1,z2,sennum1,sennum2,id1,id2]=src_des(coor,X,nodesx,nodesy,l,b,p1_node,p2_node,q1_node,q2_node,sn,n); % transfer of information from source to destination
    disp('#####################################################################')
    if z1<=n && z2<=n
        % energy calculation
        disp('Getting information about the energy consumed in the transfer process')
        disp('Press enter to continue...')
        pause
        [node_energies,x]=energy(sn,n,id1,id2,z1,z2,sennum1,sennum2); % computing the energy of the path of information transfer
        disp('Nodewise energy is provided in the excel file named "Energies at each node.xls"')
        disp('#####################################################################')
        % graphs
        graphs(node_energies,sn,n,z1,z2); % result graphs
        disp('#####################################################################')
        disp('Next figures will display the simulation results considerations')
        disp('Press enter to continue...')
        pause    
        % simulations 
%         sim_results;
    end 
    % prompting user to continue
    option=input('Do you want to continue (Y/N): ','s');
    while isempty(option)
        option=input('Do you want to continue (Y/N): ','s');
    end
    
    if isequal(option,'Y') || isequal(option,'y')
        opt=1;
        close all
    else
        opt=0;
        clear all
        close all
        clc
        break
    end
end

Code 2 – Function M File – graphs.m

function graphs(node_energies,sn,n,z1,z2)

x1=node_energies(:,1);
x2=node_energies(:,2);
figure(7)
bar(x2)
set(gca,'XTick',[1:length(x2)] )
set(gca,'XTickLabel',[x1])
title('Nodes involved in info. transfer VS Energies at those nodes')
xlabel('Nodes involved')
ylabel('Energy values')

N=sn*n;
x=[];
for i=1:n
    for j=1:sn
        x=[x ((i*100)+j)];
    end
end

y=[];
for i=1:N
    for j=1:length(x1)
        if x(i)~=x1(j)
            y(i)=100;
        elseif x(i)==x1(j)
            y(i)=x2(j);
            break;
        end
    end
end
   
figure(8)
bar(y)
set(gca,'XTick',[1:length(y)] );
set(gca,'XTickLabel',[x]); 
title('Overall energy distribution')
xlabel('Nodes')
ylabel('Energy')

% figure(14)
% oneunit=(1/n)*100;
% dif=abs(z1-z2);
% xx=[];
% for i=1:dif
%     x=100-i*oneunit;
%     xx=[xx x];
% end   
% if z1>z2
%     h=z1:-1:z2;
% else
%     h=z1:z2;
% end
% 
% bar(xx)
% set(gca,'XTick',[h]);
% title('Zone Radius VS Energy')
% xlabel('Zone Radius')
% ylabel('Energy')

Code 3 – Function M File – ids.m

function FINAL=ids(n,sn)
% sn=13;
% n=5;
[num,txt,raw]=xlsread('ids');
[row col]=size(num);
blank_mat=[];
for i=1:row
    blank_mat(i,:)= zeros(1,col);
end
xlswrite('ids',blank_mat,1,'C3')
%pause

FINAL=[];
%id=input('enter the sensor number to find out its sensor id and zone id: ');
if mod(sn,2)==0
    %even number of sensors in one zone    
    a=1;
    sense_1=[];   % matrix for the serial numbers of sensors (counting from left to right)
    for i=1:(sn/2)
        for j=1:(2*n)
            sense_1(i,j)=a; 
            a=a+1;
            
        end
    end
    clear a i j
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    a=0;
    sense_2=[];  % matrix to determine zone ids
    for i=1:(sn/2)
        for j=1:(2*n)
            if mod(j,2)==0
                a=a;
                sense_2(i,j)=a;
                if a==n
                    a=0;
                end
            else
                a=a+1;
                sense_2(i,j)=a;
            end       
        end
    end
    clear i j a 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    a=100;
    sense_3=[];
    k=1;

    for i=1:(2*n)   
        for j=1:(sn/2)               
            if mod(i,2)==0
                h=(a*k)+(2*j);
                sense_3(j,i)=h;
            else
                h=(a*k)+(2*j)-1;
                sense_3(j,i)=h;
            end      
        end
        if mod(i,2)==0
             k=k+1;
        end
    end    
    clear i j k a 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
%     disp('sensor numbers: ')
%     disp(sense_1)
%     disp('zone ids: ')
%     disp(sense_2)
%     disp('sensor ids: ')
%     disp(sense_3)
    
    [r c]=size(sense_1);
    ij=1;
    for i=1:r
        FINAL(ij,:)=sense_1(i,:);
        ij=ij+1;
        FINAL(ij,:)=sense_2(i,:);
        ij=ij+1;
        FINAL(ij,:)=sense_3(i,:);
        ij=ij+1;
    end  
    xlswrite('ids',FINAL,1,'C3');
    
else
   %odd number of sensors in one zone    
    hsn=floor(sn/2);
    a=1;
    sense11=[];   
    for i=1:(hsn)
        for j=1:(2*n)
            sense11(i,j)=a; 
            a=a+1;            
        end
    end
    sense12=[];
    for i=1:(2*n)
        if mod(i,2)==0
            sense12=[sense12 0];
        else
            sense12=[sense12 a];
            a=a+1;
        end
    end
    
    sense_1=[sense11;sense12];
    clear i j a 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    a=0;
    sense21=[];  % matrix to determine zone ids
    for i=1:(sn/2)
        for j=1:(2*n)
            if mod(j,2)==0
                a=a;
                sense21(i,j)=a;
                if a==n
                    a=0;
                end
            else
                a=a+1;
                sense21(i,j)=a;
            end       
        end
    end
    
    sense22=[];
    a=1;
    for i=1:(2*n)
        if mod(i,2)==0
            sense22=[sense22 0];
        else
            sense22=[sense22 a];
            a=a+1;
        end
    end
    
    sense_2=[sense21;sense22];
    clear i j a 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    a=100;
    sense31=[];
    k=1;

    for i=1:(2*n)   
        for j=1:(sn/2)               
            if mod(i,2)==0
                h=(a*k)+(2*j);
                sense31(j,i)=h;
            else
                h=(a*k)+(2*j)-1;
                sense31(j,i)=h;
            end      
        end
        if mod(i,2)==0
             k=k+1;
        end
    end    
    
    sense32=[];
    k=1;
    for i=1:(2*n)
        if mod(i,2)==0
            sense32=[sense32 0];
        else
            h=(a*k)+sn;
            sense32=[sense32 h];
            k=k+1;
        end
    end
    
    sense_3=[sense31;sense32];
    clear i j k a
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
%     disp('sensor numbers: ')
%     disp(sense_1)
%     disp('zone ids: ')
%     disp(sense_2)
%     disp('sensor ids: ')
%     disp(sense_3)
    
    [r c]=size(sense_1);
    ij=1;
    for i=1:r
        FINAL(ij,:)=sense_1(i,:);
        ij=ij+1;
        FINAL(ij,:)=sense_2(i,:);
        ij=ij+1;
        FINAL(ij,:)=sense_3(i,:);
        ij=ij+1;
    end  
    xlswrite('ids',FINAL,1,'C3');   
end

end

Code 4 – Function M File – mesh_formation.m

function mesh_formation(coor,n,sn,nodesx,nodesy,l,b,p1_node,p2_node,q1_node,q2_node)

figure(4)
for i=1:n
    mat=[coor(:,((4*i)-3)) coor(:,((4*i)-2)) coor(:,((4*i)-1)) coor(:,(4*i))];
    [r1 c1]=size(mat);    
    
    if mod(sn,2)==0    
        %even case
        for a=1:r1
            for bb=1:2
                for c=1:r1
                    for d=1:2
                    
                        b1=(2*bb)-1;                   
                        b2=(2*bb);                    
                        d1=(2*d)-1;                    
                        d2=(2*d);                   
                    
                        x=[(mat(a,b1)) (mat(c,d1))];
                        y=[(mat(a,b2)) (mat(c,d2))];                    
                        plot(x,y)
                        hold on
            
                    end
                end            
            end
        end 
        
        [r c]=size(nodesx);
        
        for i1=1:r
            for j1=1:c
                if i1==1 && rem(j1,2)==1
                    plot(nodesx(i1,j1),nodesy(i1,j1),'r*');
                    hold on
                else
                    plot(nodesx(i1,j1),nodesy(i1,j1),'*');
                    hold on
                end       
            end
        end

        gap=l/n;
        x1=0:gap:l; % values at which divisions are to be done in the deployment area
        y1=0:0.1:b;
        
        plot(x1(1),y1);
        hold on
        for i1=2:length(x1) 
            plot(x1(i1),y1);    
        end
        %hold off
        axis([-1 l+1 -1 b+1])       
        
    else
        %odd case
        mat_1=[mat(:,1) mat(:,2)];
        mat_2=[mat(:,3) mat(:,4)];
        mat_2=mat_2(1:(end-1),:);
        
        [r3 c3]=size(mat_1);
        [r4 c4]=size(mat_2);
        
        for a=1:r3            
            for c=1:r4
                 x=[(mat_1(a,1)) (mat_2(c,1))];
                 y=[(mat_1(a,2)) (mat_2(c,2))];                    
                 plot(x,y)
                 hold on      
            end
        end       
        
        b1=[mat_1(1,1) mat_1(r3,1)];
        d1=[mat_1(1,2) mat_1(r3,2)];
        plot(b1,d1);
        hold on
        
        b2=[mat_2(1,1) mat_2(r4,1)];
        d2=[mat_2(1,2) mat_2(r4,2)];
        plot(b2,d2);       
        
        [r1 c1]=size(p1_node);
        [r2 c2]=size(p2_node);
        
        for i1=1:r1
            for j1=1:c1
                if i1==1 
                    plot(p1_node(i1,j1),q1_node(i1,j1),'r*');
                    hold on
                else
                    plot(p1_node(i1,j1),q1_node(i1,j1),'*');
                    hold on
                end
            end
        end
    
        for i1=1:r2
            for j1=1:c2
                plot(p2_node(i1,j1),q2_node(i1,j1),'*');
                hold on
            end
        end

        gap=l/n; 
        x1=0:gap:l; % values at which divisions are to be done in the deployment area
        y1=0:0.1:b;

        plot(x1(1),y1);
        hold on
        for i1=2:length(x1) 
            plot(x1(i1),y1);    
        end
        %hold off
        axis([-1 l+1 -1 b+1])
    end      
end

title('Mesh formation')
end

Code 5 – Function M File – min_sp_tree.m

function min_sp_tree(coor,sn,n,nodesx,nodesy,l,b,p1_node,p2_node,q1_node,q2_node)

[r c]=size(coor);

figure(5)    
if mod(sn,2)==0
    % even sn case
    for i=1:(2*n)
        x=[coor(1,((2*i)-1)) coor(r,((2*i)-1))];
        y=[coor(1,(2*i)) coor(r,(2*i))];
        
        plot(x,y) %vertical lines
        hold on
    end

    for i=1:((2*n)-1)                
        if mod(i,2)==0                        
            m1=[coor(r,((2*i)-1)) coor(r,((2*i)+1))];
            n1=[coor(r,(2*i)) coor(r,((2*i)+2))];
            plot(m1,n1) %horizontal lines
            hold on
        else           
            m1=[coor(1,((2*i)-1)) coor(1,((2*i)+1))];
            n1=[coor(1,(2*i)) coor(1,((2*i)+2))];
            plot(m1,n1) %horizontal lines
            hold on 
        end
    end   
    
    [r1 c1]=size(nodesx);
    for i=1:r1
        for j=1:c1
            if i==1 && rem(j,2)==1
                plot(nodesx(i,j),nodesy(i,j),'r*');
                hold on
            else
                plot(nodesx(i,j),nodesy(i,j),'*');
                hold on
            end       
        end
    end

    gap=l/n; 
    x1=0:gap:l; % values at which divisions are to be done in the deployment area
    y1=0:0.1:b;

    plot(x1(1),y1);
    hold on
    for i=2:length(x1) 
        plot(x1(i),y1);    
    end
    hold off
    axis([-1 l+1 -1 b+1])

else
    % odd sn case
    for i=1:n
        x=[coor(1,((4*i)-1)) coor((r-1),((4*i)-1))];
        y=[coor(1,(4*i)) coor((r-1),(4*i))];
        
        plot(x,y) %vertical lines short
        hold on
    end
    
    for i=1:n
        x=[coor(1,((4*i)-3)) coor(r,((4*i)-3))];
        y=[coor(1,((4*i)-2)) coor(r,((4*i)-2))];
        
        plot(x,y) %vertical lines long
        hold on
    end
    
    for i=1:((2*n)-1)                
        if mod(i,2)==0                        
            m1=[coor((r-1),((2*i)-1)) coor((r-1),((2*i)+1))];
            n1=[coor((r-1),(2*i)) coor((r-1),((2*i)+2))];
            plot(m1,n1) %horizontal lines
            hold on
        else           
            m1=[coor(1,((2*i)-1)) coor(1,((2*i)+1))];
            n1=[coor(1,(2*i)) coor(1,((2*i)+2))];
            plot(m1,n1) %horizontal lines
            hold on 
        end
    end   
    
    [r1 c1]=size(p1_node);
    [r2 c2]=size(p2_node);
        
    for i1=1:r1
        for j1=1:c1
            if i1==1 
                plot(p1_node(i1,j1),q1_node(i1,j1),'r*');
                hold on
            else
                plot(p1_node(i1,j1),q1_node(i1,j1),'*');
                hold on
            end
        end
    end
    
    for i1=1:r2
        for j1=1:c2
            plot(p2_node(i1,j1),q2_node(i1,j1),'*');
            hold on
        end
    end
    gap=l/n; 
    x1=0:gap:l; % values at which divisions are to be done in the deployment area
    y1=0:0.1:b;

    plot(x1(1),y1);
    hold on
    for i1=2:length(x1) 
        plot(x1(i1),y1);    
    end
    %hold off
    axis([-1 l+1 -1 b+1])
    
end

title('Minimum spanning tree')
end

Code 6 – Function M File – sensor_placement.m

function sensor_placement(nodesx,nodesy,l,b,n)

[r c]=size(nodesx);
%figure(3)
for i=1:r
    for j=1:c
        if i==1 && rem(j,2)==1
            plot(nodesx(i,j),nodesy(i,j),'r*');
            hold on
        else
            plot(nodesx(i,j),nodesy(i,j),'*');
            hold on
        end       
    end
end

gap=l/n;
x=0:gap:l; % values at which divisions are to be done in the deployment area
y=0:0.1:b;

%figure(2)
plot(x(1),y);
hold on
for i=2:length(x) 
    plot(x(i),y);    
end
%hold off
axis([-1 l+1 -1 b+1])

hold off
title('Sensor node numbers from left to right ')
end

Code 6 – Function M File – sensor_placement.m

function sensor_placement(nodesx,nodesy,l,b,n)

[r c]=size(nodesx);
%figure(3)
for i=1:r
    for j=1:c
        if i==1 && rem(j,2)==1
            plot(nodesx(i,j),nodesy(i,j),'r*');
            hold on
        else
            plot(nodesx(i,j),nodesy(i,j),'*');
            hold on
        end       
    end
end

gap=l/n;
x=0:gap:l; % values at which divisions are to be done in the deployment area
y=0:0.1:b;

%figure(2)
plot(x(1),y);
hold on
for i=2:length(x) 
    plot(x(i),y);    
end
%hold off
axis([-1 l+1 -1 b+1])

hold off
title('Sensor node numbers from left to right ')
end

Code 7 – Function M File – sensor_random.m

function [nodesx,nodesy]=sensor_random(x,b)

n=length(x);
sn=input('Enter the number of sensor nodes in one zone: ');

nodesx=[];
for i=1:n-1
    node=[];
    for j=1:sn        
        low=x(i);
        high=x(i+1);    
        node=[node (low+(high-low)*rand)]; 
    end
    nodesx(i,:)=node;
end
%nodesx

clear i j
nodesy=[];
for i=1:n-1
    for j=1:sn        
        nodesy(i,j)=randi([0,b]);
        %pause
    end
end

end

Code 8 – Function M File – energy.m 

function [node_energies,x]=energy(sn,n,id1,id2,z1,z2,sennum1,sennum2)

[num,txt,raw]=xlsread('Energies at each node');
[row col]=size(num);
blank_mat=[];
for i=1:row
    blank_mat(i,:)= zeros(1,col);
end
xlswrite('Energies at each node',blank_mat,1,'B3')

N=sn*n;
s1=(z1*100)+1;
s2=(z2*100)+1;
nums=[];

% from source node to zone scatter node
if mod(sennum1,2)==0
    num1=(id1-1-s1)/2;    
    num1=num1+1;
else
    num1=(id1-s1)/2;    
end

i=1;
nums(i)=id1;
if mod(id1,2)==0
    i=i+1;
    nums(i)=id1-1;
    x=nums(i)-((z1*100)+1);
    for a=1:(x/2)
        i=i+1;
        nums(i)=nums(i-1)-2;
    end
elseif rem(id1,2)==1
    x=nums(i)-((z1*100)+1);
    for a=1:(x/2)
        i=i+1;
        nums(i)=nums(i-1)-2;
    end
end

%from zone scatter node to destination scatter node
if s1<s2
    num2=((s2-s1)/100)*2;
else
    num2=((s1-s2)/100)*2;
end

if s1<s2
    %go on right side
    x=(s2-s1)/100;
    for a=1:x    
        i=i+1;
        nums(i)=nums(i-1)+1;
        i=i+1;
        nums(i)=nums(i-2)+100;
    end    
elseif s1>s2
    %go on left side
    x=(s1-s2)/100;
    for a=1:x
        i=i+1;
        nums(i)=nums(i-1)-99;
        i=i+1;
        nums(i)=nums(i-1)-1;
    end    
end
    
%from destination scatter to destination node
if mod(sennum2,2)==0
    num3=(id2-1-s2)/2;    
    num3=num3+1;
else
    num3=(id2-s2)/2;    
end

if mod(id2,2)==0
    x=id2-((z2*100)+1);    
    for a=1:(x/2)
        i=i+1;
        nums(i)=nums(i-1)+2;
    end   
    i=i+1;
    nums(i)=nums(i-1)+1;    
elseif rem(id2,2)==1
    x=id2-((z2*100)+1);    
    for a=1:(x/2)
        i=i+1;
        nums(i)=nums(i-1)+2;
    end   
end

% final count
num=num1+num2+num3;
x=(num/N)*100;

disp('Percentage of energy lost: ')
disp(x)

len1=length(nums);
dec=100/N;

E=[100];
for j=1:len1-1
    E=[E (100-(j*dec))];
end

len2=length(E);

nums=nums';
E=E';
node_energies=[nums E];
xlswrite('Energies at each node',node_energies,1,'B3')

end

Code 9 – Function M File – divide.m  

function x=divide(l,b,n)

gap=l/n; 
x=0:gap:l; % values at which divisions are to be done in the deployment area
y=0:0.1:b;

figure(2)
plot(x(1),y);
hold on
for i=2:length(x) 
    plot(x(i),y);    
end
hold off
axis([-1 l+1 -1 b+1])

title('Divided deployment area')

end

Code 10 – Function M File – dep_area.m

function area=dep_area(l,b)

figure(1)
axis([0 l 0 b]); 
xlabel('length')
ylabel('breadth')
title('Deployment Area')

disp('The deployment area is: ')
area=l*b;
disp(area)

end

Code 11 – Function M File – dec2.m

function dec2(X,b,l,n,sn,z1,coor,sennum1,abc1,z2,nodesx,nodesy,p1_node,p2_node,q1_node,q2_node,abc2)
figure(13)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
s_scatter=[coor(1,((4*z1)-3)) coor(1,((4*z1)-2))];
%d_scatter=[coor(1,((4*z2)-3)) coor(1,((4*z2)-2))];
if mod(n,0)==0
    if z1<=(n/2)
        i=n;
    elseif z1>(n/2)
        i=1;
    end
else
    if z1<=ceil(n/2)
        i=n;
    elseif z1>ceil(n/2)
        i=1;
    end
end
        
d_scatter=[coor(1,((4*i)-3)) coor(1,((4*i)-2))];    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plotting the sensors

if mod(sn,2)==0
    % even number in one zone
    [r c]=size(nodesx);
        
    for i1=1:r
        for j1=1:c
            if i1==1 && rem(j1,2)==1
                plot(nodesx(i1,j1),nodesy(i1,j1),'r*');
                hold on
            else
                plot(nodesx(i1,j1),nodesy(i1,j1),'*');
                hold on
            end       
        end
     end

    gap=l/n;
    x1=0:gap:l; % values at which divisions are to be done in the deployment area
    y1=0:0.1:b;
       
    plot(x1(1),y1);
    hold on
    for i1=2:length(x1) 
        plot(x1(i1),y1);    
    end
    %hold off
    %axis([-1 l+1 -1 b+1])       
    text(abc1(1)+0.05,abc1(2),'\leftarrow Sender')
%    text(abc2(1)+0.5,abc2(2),'\leftarrow Receiver')
else
    % odd number on one zone
    [r1 c1]=size(p1_node);
    [r2 c2]=size(p2_node);
        
    for i1=1:r1
        for j1=1:c1
            if i1==1
                plot(p1_node(i1,j1),q1_node(i1,j1),'r*');
                hold on
            else
                plot(p1_node(i1,j1),q1_node(i1,j1),'*');
                hold on
            end
        end
    end
    
    for i1=1:r2
        for j1=1:c2
            plot(p2_node(i1,j1),q2_node(i1,j1),'*');
            hold on
        end
    end

    gap=l/n; 
    x1=0:gap:l; % values at which divisions are to be done in the deployment area
    y1=0:0.1:b;

    plot(x1(1),y1);
    hold on
    for i1=2:length(x1) 
        plot(x1(i1),y1);    
    end
    %hold off
    %axis([-1 l+1 -1 b+1])
    text(abc1(1)+0.05,abc1(2),'\leftarrow Sender')
%    text(abc2(1)+0.5,abc2(2),'\leftarrow Receiver')
end
%plotting the sensors complete
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% input node to source scatter node
if mod(sennum1,2)==0
    x1=[abc1(1) (abc1(1)-((X(2)-X(1))/2))];
    y1=[abc1(2) abc1(2)];    
    for a=1:5        
        abc=plot(x1,y1,'r','LineWidth',2);        
        pause(0.3)       
        delete(abc)
        pause(0.3)
        abc=plot(x1,y1,'r','LineWidth',2);        
    end   
    hold on    
    
    x=[(abc1(1)-((X(2)-X(1))/2)) s_scatter(1)];
    y=[abc1(2) s_scatter(2)];
    for a=1:5
        abc=plot(x,y,'r','LineWidth',2);                
        pause(0.3)       
        delete(abc)
        pause(0.3)
        abc=plot(x,y,'r','LineWidth',2);        
    end   
    hold on        
elseif rem(sennum1,2)==1
    x=[abc1(1) s_scatter(1)];
    y=[abc1(2) s_scatter(2)];
    for a=1:5        
        abc=plot(x,y,'r','LineWidth',2);
        pause(0.3)
        delete(abc)
        pause(0.3)
        abc=plot(x,y,'r','LineWidth',2);        
    end   
    hold on    
end

pause(0.3)
plot(s_scatter(1),s_scatter(2),'g*','LineWidth',5) %marking the source scatter node
pause(0.3)
%i/p node to source scatter node complete
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%from source scatter to all the other scatters
scatter=[];
for i=1:n
    scatter(i,:)=[coor(1,((4*i)-3)) coor(1,((4*i)-2))];
end
clear r c x1 y1 x2 y2 x3 y3
[r c]=size(scatter);

%finding the position of "s_scatter" in "scatter"
[row1 col1]=find(scatter==s_scatter(1)); %rows and cols number for the "s_scatter" values in "scatter" matrix
[row2 col2]=find(scatter==d_scatter(1)); %rows and cols number for the "d_scatter" values in "scatter" matrix

order=[];
for i=1:2*r    
    if s_scatter==d_scatter
        break;
    end
    num=ceil(i/2);    
    if mod(i,2)==0        
        x=row1+num;        
        if x==row2
            order=[order x];            
            break;
        end
        if x>r || x<1
            continue;
        else
            order=[order x];            
        end
    else
        x=row1-num;        
        if x==row2
            order=[order x];            
            break;
        end
        if x>r || x<1
            continue;
        else
            order=[order x];            
        end
    end    
end

m1=s_scatter;
m2=s_scatter;
for i=1:length(order)
%     if s_scatter==d_scatter
%         break;
%     end
    
    if order(i)>z1
        start_node=m1;
    else
        start_node=m2;
    end        
    
    x1=[start_node(1) start_node(1)];
    y1=[start_node(2) (start_node(2)+i)];          
    if i==1 || i==2
        if order(i)~=1 || order(i)~=n
            plot(x1,y1)
        end
    else            
        for a=1:5
            abc=plot(x1,y1);               
            pause(0.1)
            delete(abc)
            pause(0.1)
            abc=plot(x1,y1);        
        end          
    end

    
    x=scatter(order(i),:); %%%%%%%%%%%%%%%
    x2=[start_node(1) x(1)];
    y2=[(start_node(2)+i) (x(2)+i)];    
     if i==1 || i==2
        if order(i)~=1 || order(i)~=n
            plot(x2,y2)
        end
    else
        for a=1:5
            abc=plot(x2,y2);               
            pause(0.1)
            delete(abc)
            pause(0.1)
            abc=plot(x2,y2);        
        end           
    end

    
    x3=[x(1) x(1)];
    y3=[x(2) (x(2)+i)];    
    if i==1 || i==2
        if order(i)~=1 || order(i)~=n
            plot(x3,y3)
        end
    else
        for a=1:5
            abc=plot(x3,y3);               
            pause(0.1)
            delete(abc)
            pause(0.1)
            abc=plot(x3,y3);        
        end   
    end
    
    if order(i)>z1
        m1=[x(1) x(2)];
    else
        m2=[x(1) x(2)];
    end    
    
    if i==2
        pause(0.5)
    end

end                
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[nodesx,nodesy,p1_node,p2_node,q1_node,q2_node]=sensor_uniform2(X,b,l,n,sn);
    %s_scatter=[coor(1,((4*z1)-3)) coor(1,((4*z1)-2))];
    if isempty(m1)
        s_scatter=m2;
    else 
        s_scatter=m1;
    end
    hold on
    if isempty(nodesx) && isempty(nodesy)
        rectangle('Position',[p1_node(1,1),(q1_node(1,1)+5),4,3],'Facecolor','y')
        text(p1_node(1,1)+1,(q1_node(1,1)+5)+1,'BASE')
%         if mod(sennum1,2)==0
%             x1=[abc1(1) (abc1(1)-((X(2)-X(1))/2))];
%             y1=[abc1(2) abc1(2)];    
%             for a=1:5        
%                 abc=plot(x1,y1,'r','LineWidth',2);        
%                 pause(0.3)       
%                 delete(abc)
%                 pause(0.3)
%                 abc=plot(x1,y1,'r','LineWidth',2);        
%             end   
%             hold on    
%     
%             x=[(abc1(1)-((X(2)-X(1))/2)) s_scatter(1)];
%             y=[abc1(2) s_scatter(2)];
%             for a=1:5
%                 abc=plot(x,y,'r','LineWidth',2);                
%                 pause(0.3)       
%                 delete(abc)
%                 pause(0.3)
%                 abc=plot(x,y,'r','LineWidth',2);        
%             end   
%             hold on        
%         elseif rem(sennum1,2)==1
%             x=[abc1(1) s_scatter(1)];
%             y=[abc1(2) s_scatter(2)];
%             for a=1:5        
%                 abc=plot(x,y,'r','LineWidth',2);
%                 pause(0.3)
%                 delete(abc)
%                 pause(0.3)
%                 abc=plot(x,y,'r','LineWidth',2);        
%             end   
%             hold on    
%         end

        pause(0.3)
        plot(d_scatter(1),d_scatter(2),'g*','LineWidth',5) %marking the source scatter node
        pause(0.3)       
        
        coor2=[((p1_node(1,1))) (q1_node(1,1)+5)];
        blinking_line(d_scatter,coor2)       
        plot([d_scatter(1) coor2(1)],[d_scatter(2) coor2(2)],'r','LineWidth',2)
        
        coor1=[((p1_node(1,1))+5) (q1_node(1,1)+5)];
        blinking_line(coor1,coor2)
        plot([coor1(1) coor2(1)],[coor1(2) coor2(2)],'r','LineWidth',2)
        plot(((p1_node(1,1))+5),(q1_node(1,1)+5),'g*','LineWidth',5)       
        
    elseif isempty(p1_node) && isempty(p2_node) && isempty(q1_node) && isempty(q2_node)
        rectangle('Position',[nodesx(1,1),(nodesy(1,1)+5),4,3],'Facecolor','y')
        text(nodesx(1,1)+1,(nodesy(1,1)+5)+1,'BASE')
%         if mod(sennum1,2)==0
%             x1=[abc1(1) (abc1(1)-((X(2)-X(1))/2))];
%             y1=[abc1(2) abc1(2)];    
%             for a=1:5        
%                 abc=plot(x1,y1,'r','LineWidth',2);        
%                 pause(0.3)       
%                 delete(abc)
%                 pause(0.3)
%                 abc=plot(x1,y1,'r','LineWidth',2);        
%             end   
%             hold on    
%     
%             x=[(abc1(1)-((X(2)-X(1))/2)) s_scatter(1)];
%             y=[abc1(2) s_scatter(2)];
%             for a=1:5
%                 abc=plot(x,y,'r','LineWidth',2);                
%                 pause(0.3)       
%                 delete(abc)
%                 pause(0.3)
%                 abc=plot(x,y,'r','LineWidth',2);        
%             end   
%             hold on        
%         elseif rem(sennum1,2)==1
%             x=[abc1(1) s_scatter(1)];
%             y=[abc1(2) s_scatter(2)];
%             for a=1:5        
%                 abc=plot(x,y,'r','LineWidth',2);
%                 pause(0.3)
%                 delete(abc)
%                 pause(0.3)
%                 abc=plot(x,y,'r','LineWidth',2);        
%             end   
%             hold on    
%         end

        pause(0.3)
        plot(d_scatter(1),d_scatter(2),'g*','LineWidth',5) %marking the source scatter node
        pause(0.3)       
        
        coor2=[((nodesx(1,1))) (nodesy(1,1)+5)];
        blinking_line(d_scatter,coor2)       
        plot([d_scatter(1) coor2(1)],[d_scatter(2) coor2(2)],'r','LineWidth',2)
        
        coor1=[((nodesx(1,1))+5) (nodesy(1,1)+5)];
        blinking_line(coor1,coor2)
        plot([coor1(1) coor2(1)],[coor1(2) coor2(2)],'r','LineWidth',2)
        plot(((nodesx(1,1))+5),(nodesy(1,1)+5),'g*','LineWidth',5)                       
    end
    
end

Code 11 – Function M File – dec1.m

function dec1(X,b,l,n,sn,z2,coor,sennum2,abc2,abc1)

figure(13)

    [nodesx,nodesy,p1_node,p2_node,q1_node,q2_node]=sensor_uniform2(X,b,l,n,sn);
    %text(abc1(1)+0.5,abc1(2),'\leftarrow Sender')
    text(abc2(1)+0.05,abc2(2),'\leftarrow Receiver')
    hold on
    if isempty(nodesx) && isempty(nodesy)                
        rectangle('Position',[p1_node(1,1),(q1_node(1,1)+5),4,3],'Facecolor','y')
        text(p1_node(1,1)+1,(q1_node(1,1)+5)+1,'BASE')
        
        plot(((p1_node(1,1))+5),(q1_node(1,1)+5),'g*','LineWidth',5)
        text(((p1_node(1,1))+5)+0.05,(q1_node(1,1)+5),'\leftarrow Sender')
        coor1=[(p1_node(1,1)) (q1_node(1,1)+5)]; %base point
        coor2=[((p1_node(1,1))+5) (q1_node(1,1)+5)];
        blinking_line(coor1,coor2)
        plot([coor1(1) coor2(1)],[coor1(2) coor2(2)],'r','LineWidth',2)
        
        scatter=[];
        for i=1:n
            scatter(i,:)=[coor(1,((4*i)-3)) coor(1,((4*i)-2))];
            plot([coor1(1) scatter(i,1)],[coor1(2) scatter(i,2)])
        end
        pause(0.3)        
        
        d_scatter=[coor(1,((4*z2)-3)) coor(1,((4*z2)-2))];
        blinking_line(coor1,d_scatter)        
        plot([d_scatter(1) coor1(1)],[d_scatter(2) coor1(2)],'r','LineWidth',2)
        plot(d_scatter(1),d_scatter(2),'g*','LineWidth',5)

        if mod(sennum2,2)==0
           %right syd
            x=[d_scatter(1) d_scatter(1)];
            y=[d_scatter(2) abc2(2)];
            for a=1:5
                abc=plot(x,y,'r','LineWidth',2);        
                pause(0.3)
                delete(abc)
                pause(0.3)
                abc=plot(x,y,'r','LineWidth',2);        
            end
    
            x=[d_scatter(1) abc2(1)];
            y=[abc2(2) abc2(2)];
            for a=1:5
                abc=plot(x,y,'r','LineWidth',2);        
                pause(0.3)
                delete(abc)        
                pause(0.3)
                abc=plot(x,y,'r','LineWidth',2);        
            end   
        elseif rem(sennum2,2)==1
            %left syd
            x=[d_scatter(1) abc2(1)];
            y=[d_scatter(2) abc2(2)];
            for a=1:5
                abc=plot(x,y,'r','LineWidth',2);        
                pause(0.3)
                delete(abc)
                pause(0.3)
                abc=plot(x,y,'r','LineWidth',2);        
            end
        end         
    elseif isempty(p1_node) && isempty(p2_node) && isempty(q1_node) && isempty(q2_node)
        rectangle('Position',[nodesx(1,1),(nodesy(1,1)+5),4,3],'Facecolor','y')
        text(nodesx(1,1)+1,(nodesy(1,1)+5)+1,'BASE')
        
        plot(((nodesx(1,1))+5),(nodesy(1,1)+5),'g*','LineWidth',5)
        text(((nodesx(1,1))+5)+0.05,(nodesy(1,1)+5),'\leftarrow Sender')
        coor1=[(nodesx(1,1)) (nodesy(1,1)+5)];
        coor2=[((nodesx(1,1))+5) (nodesy(1,1)+5)];
        blinking_line(coor1,coor2)
        plot([coor1(1) coor2(1)],[coor1(2) coor2(2)],'r','LineWidth',2)
        
        scatter=[];
        for i=1:n
            scatter(i,:)=[coor(1,((4*i)-3)) coor(1,((4*i)-2))];
            plot([coor1(1) scatter(i,1)],[coor1(2) scatter(i,2)])
        end
        pause(0.3)        
        
        d_scatter=[coor(1,((4*z2)-3)) coor(1,((4*z2)-2))];
        blinking_line(coor1,d_scatter)
        plot([coor1(1) d_scatter(1)],[coor1(2) d_scatter(2)],'r','LineWidth',2)
        plot(d_scatter(1),d_scatter(2),'g*','LineWidth',5)
        if mod(sennum2,2)==0
           %right syd
            x=[d_scatter(1) d_scatter(1)];
            y=[d_scatter(2) abc2(2)];
            for a=1:5
                abc=plot(x,y,'r','LineWidth',2);        
                pause(0.3)
                delete(abc)
                pause(0.3)
                abc=plot(x,y,'r','LineWidth',2);        
            end
    
            x=[d_scatter(1) abc2(1)];
            y=[abc2(2) abc2(2)];
            for a=1:5
                abc=plot(x,y,'r','LineWidth',2);        
                pause(0.3)
                delete(abc)        
                pause(0.3)
                abc=plot(x,y,'r','LineWidth',2);        
            end   
        elseif rem(sennum2,2)==1
            %left syd
            x=[d_scatter(1) abc2(1)];
            y=[d_scatter(2) abc2(2)];
            for a=1:5
                abc=plot(x,y,'r','LineWidth',2);        
                pause(0.3)
                delete(abc)
                pause(0.3)
                abc=plot(x,y,'r','LineWidth',2);        
            end
        end      
    end  
    
end

Code 12 – Function M File – curvefit.m

function p_x=curvefit(X,Y,deg)

Code 13 – Function M File – coordinates.m

function coor=coordinates(nodesx,nodesy,p1_node,p2_node,q1_node,q2_node,sn,n)
%[r1 c1]=size(p1_node)
% p2_node
% q1_node
% q2_node

[num,txt,raw]=xlsread('coordinates');
[row col]=size(num);
blank_mat=[];
for i=1:row
    blank_mat(i,:)= zeros(1,col);
end
xlswrite('coordinates',blank_mat,1,'C5')

coor=[];
if mod(sn,2)==0
    [r c]=size(nodesx);
    a=1;
    for i=1:c
        i;
        a;
        coor(:,(2*i)-1)=nodesx(:,a);
        coor(:,(2*i))=nodesy(:,a);
        a=a+1;        
    end
else
    [r c]=size(p1_node);
    %for i=1:r
        a=1;
        for j=1:(4*c)
            if rem(j,4)==1
                coor(:,j)=p1_node(:,a);                
            elseif rem(j,4)==2
                coor(:,j)=q1_node(:,a);                
            elseif rem(j,4)==3
                coor(:,j)=[p2_node(:,a)' 0]';                
            elseif mod(j,4)==0
                coor(:,j)=[q2_node(:,a)' 0]';                
            end           
            
            if mod(j,4)==0
                a=a+1;                
            end        
            %pause
        end
    %end
end
xlswrite('coordinates',coor,1,'C5');
end

Code 14 – Function M File – blinking_line.m

function blinking_line(coor1,coor2)

for i=1:3   
        abc=plot([coor1(1) coor2(1)],[coor1(2) coor2(2)]);
        pause(0.3)
        delete(abc)
        pause(0.3)
        abc=plot([coor1(1) coor2(1)],[coor1(2) coor2(2)]);
        pause(0.3)
        delete(abc)
        pause(0.3)
%        abc=plot([coor1(1) coor2(1)],[coor1(2) coor2(2)]);
end
    
end

Code 15 – Function M File – src_des.m

function [abc1,abc2,z1,z2,sennum1,sennum2,id1,id2]=src_des(coor,X,nodesx,nodesy,l,b,p1_node,p2_node,q1_node,q2_node,sn,n)

%figure(13)       
%axis([0 l+3 0 b+5])

disp('Input the source sensor node (refering the excel file): ')
disp('Press enter to continue...')
pause
[abc1,z1,sennum1,id1]=sensorinfo(coor,n,sn);
disp('Input the destination sensor node (refering the excel file): ')
disp('Press enter to continue...')
pause
[abc2,z2,sennum2,id2]=sensorinfo(coor,n,sn);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while z1>n && z2>n
    disp('Both the sensors are out of bounds!')
    disp('Enter again')
    
    disp('Input the source sensor node (refering the excel file): ')
    disp('Press enter to continue...')
    pause
    [abc1,z1,sennum1,id1]=sensorinfo(coor,n,sn);
    disp('Input the destination sensor node (refering the excel file): ')
    disp('Press enter to continue...')
    pause
    [abc2,z2,sennum2,id2]=sensorinfo(coor,n,sn);  
    %disp('Program terminates!')    
end

if z1>n    
    dec1(X,b,l,n,sn,z2,coor,sennum2,abc2,abc1) % for an external source node
elseif z2>n
    dec2(X,b,l,n,sn,z1,coor,sennum1,abc1,z2,nodesx,nodesy,p1_node,p2_node,q1_node,q2_node,abc2) % for an external destination node
else
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%both are in the boundary
disp('Press enter to continue...')
pause

disp('The source sensor node coordinates are: ')
disp(abc1)

disp('The source sensor node coordinates are: ')
disp(abc2)

s_scatter=[coor(1,((4*z1)-3)) coor(1,((4*z1)-2))];
d_scatter=[coor(1,((4*z2)-3)) coor(1,((4*z2)-2))];
gap=X(2)-X(1);

figure(6)
if mod(sn,2)==0
    % even number in one zone
    [r c]=size(nodesx);
        
    for i1=1:r
        for j1=1:c
            if i1==1 && rem(j1,2)==1
                plot(nodesx(i1,j1),nodesy(i1,j1),'r*');
                hold on
            else
                plot(nodesx(i1,j1),nodesy(i1,j1),'*');
                hold on
            end       
        end
     end

    gap=l/n;
    x1=0:gap:l; % values at which divisions are to be done in the deployment area
    y1=0:0.1:b;
       
    plot(x1(1),y1);
    hold on
    for i1=2:length(x1) 
        plot(x1(i1),y1);    
    end
    %hold off
    %axis([-1 l+1 -1 b+1])       
    text(abc1(1)+0.05,abc1(2),'\leftarrow Sender')
    text(abc2(1)+0.05,abc2(2),'\leftarrow Receiver')
else
    % odd number on one zone
    [r1 c1]=size(p1_node);
    [r2 c2]=size(p2_node);
        
    for i1=1:r1
        for j1=1:c1
            if i1==1
                plot(p1_node(i1,j1),q1_node(i1,j1),'r*');
                hold on
            else
                plot(p1_node(i1,j1),q1_node(i1,j1),'*');
                hold on
            end
        end
    end
    
    for i1=1:r2
        for j1=1:c2
            plot(p2_node(i1,j1),q2_node(i1,j1),'*');
            hold on
        end
    end

    gap=l/n; 
    x1=0:gap:l; % values at which divisions are to be done in the deployment area
    y1=0:0.1:b;

    plot(x1(1),y1);
    hold on
    for i1=2:length(x1) 
        plot(x1(i1),y1);    
    end
    %hold off
    %axis([-1 l+1 -1 b+1])
    text(abc1(1)+0.05,abc1(2),'\leftarrow Sender')
    text(abc2(1)+0.05,abc2(2),'\leftarrow Receiver')
end

% input node to source scatter node
if mod(sennum1,2)==0
    x1=[abc1(1) (abc1(1)-((X(2)-X(1))/2))];
    y1=[abc1(2) abc1(2)];    
    for a=1:5        
        abc=plot(x1,y1,'r','LineWidth',2);        
        pause(0.3)       
        delete(abc)
        pause(0.3)
        abc=plot(x1,y1,'r','LineWidth',2);        
    end   
    hold on    
    
    x=[(abc1(1)-((X(2)-X(1))/2)) s_scatter(1)];
    y=[abc1(2) s_scatter(2)];
    for a=1:5
        abc=plot(x,y,'r','LineWidth',2);                
        pause(0.3)       
        delete(abc)
        pause(0.3)
        abc=plot(x,y,'r','LineWidth',2);        
    end   
    hold on        
elseif rem(sennum1,2)==1
    x=[abc1(1) s_scatter(1)];
    y=[abc1(2) s_scatter(2)];
    for a=1:5        
        abc=plot(x,y,'r','LineWidth',2);
        pause(0.3)
        delete(abc)
        pause(0.3)
        abc=plot(x,y,'r','LineWidth',2);        
    end   
    hold on    
end

pause(0.3)
plot(s_scatter(1),s_scatter(2),'g*','LineWidth',5) %marking the source scatter node
pause(0.3)

%source scatter node to all the other scatter nodes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
scatter=[];
for i=1:n
    scatter(i,:)=[coor(1,((4*i)-3)) coor(1,((4*i)-2))];
end
clear r c x1 y1 x2 y2 x3 y3
[r c]=size(scatter);

%finding the position of "s_scatter" in "scatter"
[row1 col1]=find(scatter==s_scatter(1)); %rows and cols number for the "s_scatter" values in "scatter" matrix
[row2 col2]=find(scatter==d_scatter(1)); %rows and cols number for the "d_scatter" values in "scatter" matrix

order=[];
for i=1:2*r    
    if s_scatter==d_scatter
        break;
    end
    num=ceil(i/2);    
    if mod(i,2)==0        
        x=row1+num;        
        if x==row2
            order=[order x];            
            break;
        end
        if x>r || x<1
            continue;
        else
            order=[order x];            
        end
    else
        x=row1-num;        
        if x==row2
            order=[order x];            
            break;
        end
        if x>r || x<1
            continue;
        else
            order=[order x];            
        end
    end    
end

m1=s_scatter;
m2=s_scatter;
for i=1:length(order)
    if s_scatter==d_scatter
        break;
    end
    
    if order(i)>z1
        start_node=m1;
    else
        start_node=m2;
    end        
    
    x1=[start_node(1) start_node(1)];
    y1=[start_node(2) (start_node(2)+i)];          
    if i==1 || i==2
        if order(i)~=1 || order(i)~=n
            plot(x1,y1)
        end
    else            
        for a=1:5
            abc=plot(x1,y1);               
            pause(0.1)
            delete(abc)
            pause(0.1)
            abc=plot(x1,y1);        
        end          
    end
    
    x=scatter(order(i),:); %%%%%%%%%%%%%%%
    x2=[start_node(1) x(1)];
    y2=[(start_node(2)+i) (x(2)+i)];    
    if i==1 || i==2
        if order(i)~=1 || order(i)~=n
            plot(x2,y2)
        end
    else
        for a=1:5
            abc=plot(x2,y2);               
            pause(0.1)
            delete(abc)
            pause(0.1)
            abc=plot(x2,y2);        
        end           
    end
    
    x3=[x(1) x(1)];
    y3=[x(2) (x(2)+i)];    
    if i==1 || i==2
        if order(i)~=1 || order(i)~=n
            plot(x3,y3)
        end
    else
        for a=1:5
            abc=plot(x3,y3);               
            pause(0.1)
            delete(abc)
            pause(0.1)
            abc=plot(x3,y3);        
        end   
    end
    
    if order(i)>z1
        m1=[x(1) x(2)];
    else
        m2=[x(1) x(2)];
    end
    
    if i==2
        pause(0.5)
    end
    
end                
    
%source scatter node to destination scatter node
pause(0.3)
plot(d_scatter(1),d_scatter(2),'g*','LineWidth',5) %marking the destination scatter node
pause(0.3)

x=[s_scatter(1) d_scatter(1)];
y=[s_scatter(2) d_scatter(2)];
for a=1:5
    abc=plot(x,y,'r','LineWidth',2);    
    pause(0.3)
    delete(abc)
    pause(0.3)
    abc=plot(x,y,'r','LineWidth',2);    
end   

%destination scatter to destination node
if mod(sennum2,2)==0
    %right syd
    x=[d_scatter(1) d_scatter(1)];
    y=[d_scatter(2) abc2(2)];
    for a=1:5
        abc=plot(x,y,'r','LineWidth',2);        
        pause(0.3)
        delete(abc)
        pause(0.3)
        abc=plot(x,y,'r','LineWidth',2);        
    end
    
    x=[d_scatter(1) abc2(1)];
    y=[abc2(2) abc2(2)];
    for a=1:5
        abc=plot(x,y,'r','LineWidth',2);        
        pause(0.3)
        delete(abc)
        pause(0.3)
        abc=plot(x,y,'r','LineWidth',2);        
    end   
elseif rem(sennum2,2)==1
    %left syd
    x=[d_scatter(1) abc2(1)];
    y=[d_scatter(2) abc2(2)];
    for a=1:5
        abc=plot(x,y,'r','LineWidth',2);        
        pause(0.3)
        delete(abc)
        pause(0.3)
        abc=plot(x,y,'r','LineWidth',2);        
    end
end      
    
end

end

Code 16 – Function M File – sim_results.m

function sim_results

% characteristics of average delay
X=[5 10 50 100 200];
Y1=[437 380 353 320 317];
p=polyfit(X,Y1,4);

disp('Calculating the delay')
disp('Press enter to continue: ')
pause
x1=input('Enter the Packet generation interval(ms): ');
final_p_x1=[];
for ii=1:x1
    p_x=0;
    for i=0:length(p)-1
        p_x=p_x + (ii^i)*(p(length(p)-i));
    end
    final_p_x1=[final_p_x1 p_x];
end

figure(9)
plot([1:x1],final_p_x1)
hleg=legend('Delay');
title('Charcteristics of average delay')
xlabel('Packet generation interval(ms)')
ylabel('Delay(ms)')

disp('Delay(ms): ')
disp(p_x)
disp('Press enter to continue: ')
pause
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
clear Y p x p_x i

% throughput characteristics
Y2=[1250 1320 670 410 230];
p=polyfit(X,Y2,4);

disp('Calculating the Throughput')
disp('Press enter to continue: ')
pause
x2=input('Enter the Packet generation interval(ms): ');
final_p_x2=[];
for ii=1:x2
    p_x=0;
    for i=0:length(p)-1
        p_x=p_x + (ii^i)*(p(length(p)-i));
    end
    final_p_x2=[final_p_x2 p_x];
end

figure(10)
plot(1:x2,final_p_x2)
hleg=legend('Throughput');
title('Throughput Characteristics')
xlabel('Packet generation interval(ms)')
ylabel('Throughput(kbps)')

disp('Throughput(kbps): ')
disp(p_x)
disp('Press enter to continue: ')
pause
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
clear Y p x p_x i

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% throughput and packet delivery ratio
Y3=[];
low=85;
high=95;
x=(low+(high-low)*rand);
for i=1:length(final_p_x2)
    Y3=[Y3 ((x*final_p_x2(i))/100)];
end

%p=polyfit(X,Y3,4);

figure(11)
plot(1:x2,final_p_x2,'r')
hold on
plot(1:x2,Y3)
hleg=legend('Throughput','Packet Delivery Ratio');
title('Throughput(red) and Packet Delivery Ratio(blue)')
xlabel('Packet generation interval(ms)')
ylabel('Throughput(kbps) and Packet delivery ratio')
hold off
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% overhead
clear X Y p x p_x i
X=[1 2 3 4 5]; %zone radius
Y1=[0 35000 150000 360000 620000]; %IARP overhead (theory)
Y2=[475000 210000 750000 0 0]; %IERP overhead (theory)

Y4=[0 10000 80000 150000 210000]; %IARP overhead (practical)
Y5=[420000 360000 240000 120000 80000]; %IERP overhead (pratical)
Y6=[100000 100000 113000 116000 110000]; %NDP overhead (practical)

x3=input('Enter the Zone Radius(hops): ');
while isempty(x3)
    disp('Please enter the input')
    x3=input('Enter the Zone Radius(hops): ');
end

disp('####################    Theoritical values    ####################')
disp('Press enter to continue')
pause

p=polyfit(X,Y1,4);
final_p_x3=[];
for ii=1:x3
    p_x=0;
    for i=0:length(p)-1
        p_x=p_x + (ii^i)*(p(length(p)-i));
    end
    final_p_x3=[final_p_x3 p_x];
end

disp('IARP overhead(packets) (theoritical value): ')
disp(p_x)

p=polyfit(X,Y2,4);
final_p_x4=[];
for ii=1:x3
    p_x=0;
    for i=0:length(p)-1
        p_x=p_x + (ii^i)*(p(length(p)-i));
    end
    final_p_x4=[final_p_x4 p_x];
end
disp('IERP overhead(packets) (theoritical value): ')
disp(p_x)

Y3=final_p_x3+final_p_x4; % total overhead (theory)
disp('Total overhead (theory)')
disp(Y3(end))

disp('####################    Practical values    ####################')
disp('Press enter to continue')
pause

p=polyfit(X,Y4,4);
final_p_x5=[];
for ii=1:x3
    p_x=0;
    for i=0:length(p)-1
        p_x=p_x + (ii^i)*(p(length(p)-i));
    end
    final_p_x5=[final_p_x5 p_x];
end
disp('IARP overhead(packets) (practical value): ')
disp(p_x)

p=polyfit(X,Y5,4);
final_p_x6=[];
for ii=1:x3
    p_x=0;
    for i=0:length(p)-1
        p_x=p_x + (ii^i)*(p(length(p)-i));
    end
    final_p_x6=[final_p_x6 p_x];
end
disp('IERP overhead(packets) (practical value): ')
disp(p_x)

p=polyfit(X,Y6,4);
final_p_x7=[];
for ii=1:x3
    p_x=0;
    for i=0:length(p)-1
        p_x=p_x + (ii^i)*(p(length(p)-i));
    end
    final_p_x7=[final_p_x7 p_x];
end
disp('NDP overhead(packets) (practical value): ')
disp(p_x)

Y7=final_p_x5+final_p_x6+final_p_x7; %total overhead (practical)
disp('Total overhead')
disp(Y7(end))

disp('Press enter to continue')
pause

figure(12)
subplot(2,1,1)
plot([1:x3],final_p_x3) %iarp theory
hold on
plot([1:x3],final_p_x4,'g') %ierp theory
plot([1:x3],Y3,'r') %total theory
hleg=legend('IARP overhead','IERP overhead','Total overhead');
title('Theoretically Evaluated')
xlabel('Zone Radius(hops)')
ylabel('Overhead(packets)')
set(gca, 'YTickLabel', num2str(get(gca,'YTick')','%d')) 

subplot(2,1,2)
plot([1:x3],final_p_x5) %iarp practical
hold on
plot([1:x3],final_p_x6,'g') %ierp practical
plot([1:x3],final_p_x7,'r') %ndp practical
plot([1:x3],Y7,'y') %total practical
hleg=legend('IARP overhead','IERP overhead','NDP overhead','Total overhead');
title('Simulation')
xlabel('Zone Radius(hops)')
ylabel('Overhead(packets)')
set(gca, 'YTickLabel', num2str(get(gca,'YTick')','%d'))

Code 17 – Function M File – sensorinfo.m

function [abc,z,sennum,id]=sensorinfo(coor,n,sn)

id=input('Input the sensor id (refering the excel file): ');

while isempty(id)
    disp('Please enter a value')
    id=input('Input the sensor id (refering the excel file): ');
end

x=mod(id,100); % sensor index (number) in the current zone
sennum=x; %sensor number in the zone
y=id-x;
z=y/100; % zone number

if z<=n
    while x>sn    
        disp('Invalid sensor ID !!')
        disp('Please enter again')
        id=input('Input the sensor id (refering the excel file): ');
        x=mod(id,100); % sensor index (number) in the current zone
        sennum=x;
        y=id-x;
        z=y/100; % zone number
    end
end

if z>n % sensor is not in current zones
    disp('Out of the present zones')
    abc=0;
elseif z<=n % sensor is in current zones

    disp('The sensor is in the zone: ')
    disp(z)

    mat=[];
    xy=1;
    for i=((4*z)-3):(4*z)
        mat(:,xy)=coor(:,i);
        xy=xy+1;
    end
    %  mat
    %  pause

    mat_1=[];
    b=1;
    if mod(x,2)==0
        % fall on right side
        a=2;
        mat_1=[mat(:,3) mat(:,4)];
        [r c]=size(mat_1);
        for i=1:r
            if x==a
                disp('The coordinates of the sensor are: ')
                abc=mat_1(b,:)
                break;
            else
                b=b+1;
                a=a+2;
            end
        end
    else
        % fall on left side
        mat_1=[mat(:,1) mat(:,2)];
        a=1;
        [r c]=size(mat_1);
        for i=1:r
            if x==a
                disp('The coordinates of the sensor are: ')
                abc=mat_1(b,:)
                break;
            else
                b=b+1;
                a=a+2;
            end    
        end
    end

end

end

Code 18 – Function M File – sensor_uniform2.m

function [nodesx,nodesy,p1_node,p2_node,q1_node,q2_node]=sensor_uniform2(x,b,l,n,sn)

%n=length(x); %number of divisions + 1
%y=n-1; %number of divisions

%sn=input('Number of sensors to be deployed in one zone: ');
%h=sn;

nodesx=[];
nodesy=[];

if mod(sn,2)==0
    %even number case
    d=x(2)/4;
    p=[];
    for i=2:length(x)           
        p=[p (x(i-1)+d) (x(i-1)+(3*d))]; %x axis
    end
    
    hsn=sn/2;
    q=[];
    m=b/hsn;
    q0=m/2;    
    for j=0:(hsn-1)
        q=[q (q0+(m*j))]; %y axis
    end       
    
    for i=1:length(q)
        nodesx(i,:)=p;
    end
    
    for i=1:length(p)
        nodesy(:,i)=q;
    end       
    nodesy=flipud(nodesy);
    
    [r c]=size(nodesx);
        %figure(3)
    for i=1:r
        for j=1:c
            if i==1 && rem(j,2)==1
                plot(nodesx(i,j),nodesy(i,j),'r*');
                hold on
            else
                plot(nodesx(i,j),nodesy(i,j),'*');
                hold on
            end       
        end
    end

    gap=l/n;
    x=0:gap:l; % values at which divisions are to be done in the deployment area
    y=0:0.1:b;

    %figure(2)
    plot(x(1),y);
    hold on
    for i=2:length(x) 
        plot(x(i),y);    
    end

    p1_node=[];
    q1_node=[];
    p2_node=[];
    q2_node=[];
else
    %odd number case
    nodesx=[];
    nodesy=[];
    d=x(2)/4;
    p=[];
    for i=2:length(x)           
        p=[p (x(i-1)+d) (x(i-1)+(3*d))]; %x axis
    end
   
    p1=[];
    p2=[];
    for i=1:length(p)
        if mod(i,2)==0
            p2=[p2 p(i)];
        else
            p1=[p1 p(i)];
        end
    end   
    
    hsn=ceil(sn/2);
    q=[];
    m=b/hsn;
    q0=m/2;    
    for j=0:(hsn-1)
        q=[q (q0+(m*j))]; %y axis
    end     
    
    q1=q;
    q2=q(2:end);  
    
    p1_node=[];
    for i=1:hsn
        p1_node(i,:)=p1;
    end    
    
    p2_node=[];
    for i=1:(hsn-1)
        p2_node(i,:)=p2;
    end
        
    q1_node=[];
    for i=1:length(p1)
        q1_node(:,i)=q1';
    end
    q1_node=flipud(q1_node);
    
    q2_node=[];
    for i=1:length(p2)
        q2_node(:,i)=q2';
    end
    q2_node=flipud(q2_node);
           
    [r1 c1]=size(p1_node);
    [r2 c2]=size(p2_node);
   
    for i=1:r1
        for j=1:c1
            if i==1
                plot(p1_node(i,j),q1_node(i,j),'r*');
                hold on
            else
                plot(p1_node(i,j),q1_node(i,j),'*');
                hold on
            end
        end
    end
    
    for i=1:r2
        for j=1:c2
            plot(p2_node(i,j),q2_node(i,j),'*');
            hold on
        end
    end

    gap=l/n; 
    x=0:gap:l; % values at which divisions are to be done in the deployment area
    y=0:0.1:b;

    plot(x(1),y);
    hold on
    for i=2:length(x) 
        plot(x(i),y);    
    end    
end

axis([0 l+3 0 b+5])
end

Code 19 – Function M File – sensor_uniform.m

function [nodesx,nodesy,p1_node,p2_node,q1_node,q2_node,sn]=sensor_uniform(x,b,l,n)

%n=length(x); %number of divisions + 1
%y=n-1; %number of divisions

sn=input('Number of sensors to be deployed in one zone: ');
%h=sn;

nodesx=[];
nodesy=[];

figure(3)

if mod(sn,2)==0
    %even number case
    d=x(2)/4;
    p=[];
    for i=2:length(x)           
        p=[p (x(i-1)+d) (x(i-1)+(3*d))]; %x axis
    end
    
    hsn=sn/2;
    q=[];
    m=b/hsn;
    q0=m/2;    
    for j=0:(hsn-1)
        q=[q (q0+(m*j))]; %y axis
    end       
    
    for i=1:length(q)
        nodesx(i,:)=p;
    end
    
    for i=1:length(p)
        nodesy(:,i)=q;
    end       
    nodesy=flipud(nodesy);
    
    sensor_placement(nodesx,nodesy,l,b,n);
    p1_node=[];
    q1_node=[];
    p2_node=[];
    q2_node=[];
else
    %odd number case
    nodesx=[];
    nodesy=[];

    d=x(2)/4;
    p=[];
    for i=2:length(x)           
        p=[p (x(i-1)+d) (x(i-1)+(3*d))]; %x axis
    end
    
    p1=[];
    p2=[];
    for i=1:length(p)
        if mod(i,2)==0
            p2=[p2 p(i)];
        else
            p1=[p1 p(i)];
        end
    end   
    
    hsn=ceil(sn/2);
    q=[];
    m=b/hsn;
    q0=m/2;    
    for j=0:(hsn-1)
        q=[q (q0+(m*j))]; %y axis
    end     
    
    q1=q;
    q2=q(2:end);  
    
    p1_node=[];
    for i=1:hsn
        p1_node(i,:)=p1;
    end    
    
    p2_node=[];
    for i=1:(hsn-1)
        p2_node(i,:)=p2;
    end
        
    q1_node=[];
    for i=1:length(p1)
        q1_node(:,i)=q1';
    end
    q1_node=flipud(q1_node);
    
    q2_node=[];
    for i=1:length(p2)
        q2_node(:,i)=q2';
    end
    q2_node=flipud(q2_node);
           
    [r1 c1]=size(p1_node);
    [r2 c2]=size(p2_node);

    for i=1:r1
        for j=1:c1
            if i==1
                plot(p1_node(i,j),q1_node(i,j),'r*');
                hold on
            else
                plot(p1_node(i,j),q1_node(i,j),'*');
                hold on
            end
        end
    end
    
    for i=1:r2
        for j=1:c2
            plot(p2_node(i,j),q2_node(i,j),'*');
            hold on
        end
    end

    gap=l/n; 
    x=0:gap:l; % values at which divisions are to be done in the deployment area
    y=0:0.1:b;

    %figure(2)
    plot(x(1),y);
    hold on
    for i=2:length(x) 
        plot(x(i),y);    
    end
    %hold off

    axis([-1 l+1 -1 b+1])
    title('Sensor node numbers from left to right ')
    
    
end

end

Recent Posts

Tags

ad-hoc networks AODV boundary detection process classification clustering clustering algorithm Colour Information computer vision Decryption Encryption EZRP ICM (Iterated Conditional Modes) image denoising image enhancement IMAGE PROCESSING image segmentation Imaging and image processing MANET Markov Random Fields neutrosophic logic optical network proposed method PSNR QLab system region growing Robert’s operator Seed point selection segmentation semi-automatic algorithm Shadow Detection shadow removal wall motion wireless communication Wireless network wireless networks Wireless Sensor Network wireless sensor networks ZRP