CDS最小支配集产生算法的MATLAB仿真

目录

一、理论基础

二、核心程序

三、仿真结论


一、理论基础

        CDS(Connected Dominating Set)最小支配集产生算法是一种无线传感器网络中常用的集群形成算法,可以有效地减少网络中的冗余节点并提高网络的覆盖率和能源利用率。本文将详细介绍CDS最小支配集产生算法的原理、流程和实现方法。

一、算法原理

        在无线传感器网络中,节点之间通常会建立连接关系,形成连通图。为了提高网络的覆盖率和能源利用率,需要选出一些节点作为集群头节点,其他节点都与集群头节点建立连接关系,从而形成一个支配集。CDS最小支配集产生算法就是为了选出最小的支配集,同时保证网络的连通性和覆盖率。

        CDS最小支配集产生算法的基本思想是:首先选出一些节点作为集群头节点,使得它们能够覆盖整个网络,并且与其他节点建立连接关系。然后,将非集群头节点与其最近的集群头节点建立连接关系,从而使整个网络都与集群头节点相连,形成一个支配集。最后,从支配集中选出最小的子集作为最小支配集。

二、算法流程

CDS最小支配集产生算法的流程如下:

确定集群头节点
         首先,需要选出一些节点作为集群头节点。一般来说,集群头节点应该满足以下几个条件:

能够覆盖整个网络;
         与其他节点建立连接关系,数量尽量小。常用的选取方法包括贪心算法、蚁群算法、遗传算法等。

建立连接关系
         将非集群头节点与其最近的集群头节点建立连接关系,从而使整个网络都与集群头节点相连。常用的连接方法包括单向连接和双向连接。

构建支配集
         将所有集群头节点和与其建立连接的非集群头节点组合起来,构建出一个支配集。该支配集应满足以下几个条件:

能够覆盖整个网络;
与其他节点建立连接关系;
子集数量尽量小。
寻找最小支配集
       从构建好的支配集中选出最小的子集作为最小支配集。常用的选取方法包括贪心算法、模拟退火算法、遗传算法等。

       CDS最小支配集产生算法是一种常用的无线传感器网络集群形成算法,可以有效地减少网络中的冗余节点并提高网络的覆盖率和能源利用率。该算法的实现方法包括贪心算法、蚁群算法、遗传算法等,可以使用各种编程语言实现。
 

二、核心程序

.........................................................
for i=1:Node_Num
    num1=0;
    for j=1:Node_Num
        if i~=j
            dist=(Posxy(i,1)-Posxy(j,1))^2+...
                (Posxy(i,2)-Posxy(j,2))^2;
            dist=sqrt(dist);
            if dist<Radius&&dist>0
                num1=num1+1;
                d1(i,num1+1)=j;
            end
        end
    end
    d1(i,1)=num1;
end

%%
%*********************show the connected picture**************************

hold on
for i=1:Node_Num%draw the line between two nodes
    for j=1:d1(i,1)
        plot([Posxy(i,1),Posxy(d1(i,j+1),1)],...
            [Posxy(i,2),Posxy(d1(i,j+1),2)],'-ok');
        hold on;
    end
end

%%
%************node get the two_hop neighbors information*******************

Max_Degree=max(d1(:,1));
Neighbor_hop2=zeros(Max_Degree,Max_Degree+1,Node_Num);%store two_hop neighbors information
for i=1:Node_Num
   for j=1:d1(i,1)
       Neighbor_hop2(j,1,i)=d1(i,j+1);
       for m=1:d1(d1(i,j+1),1)
           Neighbor_hop2(j,m+1,i)=d1(d1(i,j+1),m+1);
       end
   end
end

%
%**********color the node with gray and white based on the rule**********
%0-white;1-gray;

Color_N=zeros(Node_Num,1);%intial the color of nodes

for i=1:Node_Num
    if d1(i,1)<2
        Color_N(i,1)=0;
    else
        for j=1:d1(i,1)
            color=0;
            for n=j+1:d1(i,1)
                state=0;
                for m=2:d1(Neighbor_hop2(j,1,i),1)+1
                    if Neighbor_hop2(n,1,i)==Neighbor_hop2(j,m,i)
                        state=1;
                        break;
                    end
                end
                if state==0
                    color=1;
                    break;
                end
            end
            if color==1
                break;
            end
        end
        Color_N(i,1)=color;
    end
end

%%
%**********color Black based on the rule**********
%0-white;1-gray;2-balck;3-gray';

%Balck
for i=1:Node_Num
    if Color_N(i,1)==0&&d1(i,1)>0
        temp_degree=0;
        temp_id=0;
        for j=1:d1(i,1)
            if Color_N(d1(i,j+1),1)~=0
                if Color_N(d1(i,j+1),1)~=2
                    Color_N(d1(i,j+1),1)=3;
                end
                if d1(d1(i,j+1),1)>temp_degree
                    temp_degree=d1(d1(i,j+1),1);
                    temp_id=d1(i,j+1);
                elseif d1(d1(i,j+1),1)==temp_degree
                    if temp_id>d1(i,j+1)
                        temp_degree=d1(d1(i,j+1),1);
                        temp_id=d1(i,j+1);
                    end
                end
            end
        end
        if temp_id~=0
            Color_N(temp_id,1)=2;
        end
    end
end
%end Balck

%%
%%**********color Real_Violet based on the rule**********
%0-white;1-gray;2-balck;3-gray';
%remove the Violet node form to get a dominating set
%1 and 3 组成DS

Count2=0;
for m=1:Node_Num
    if Color_N(m,1)==1
        Count2=Count2+1;
    end
end
while(Count2)%Simulation of distributed computing
    for i=1:Node_Num
        if Color_N(i,1)==1
            temp_degree=d1(i,1);
            state=1;
            for j=1:d1(i,1)
                if Color_N(d1(i,j+1))==1
                    if temp_degree>d1(d1(i,j+1),1)
                        state=1;
                    elseif temp_degree==d1(d1(i,j+1),1)
                        if i<d1(i,j+1)
                            state=1;
                        else
                            state=0;
                            break;
                        end
                    else
                        state=0;
                        break;
                    end
                end
            end
            if state==1
                Color_N(i,1)=4;
                Count2=Count2-1;
                for j=1:d1(i,1)
                    if Color_N(d1(i,j+1))==1
                        Color_N(d1(i,j+1),1)=5;
                        Count2=Count2-1;
                    end
                end
            end
        end
    end
end

%%
%choes some node to form the connect nodes who's color number eqult to 2 or
%4
tempall=1;
temp_self=Neighbor_hop2(:,:,1)*0;%2 hop's information

for i=1:Node_Num

    if Color_N(i,1)==4||Color_N(i,1)==2
        temp_self=temp_self*0;%2 hop's information
        
        %node i formulation the 2_hop connected graph
        for n=1:d1(i,1)
            temp2=d1(d1(i,n+1),1);
            temp_count=1;
            state=1;
            for m=2:temp2+1
                temp_self(n,1)=d1(i,n+1);
                temp=Neighbor_hop2(n,m,i);
                state=1;
                for p=1:d1(i,1)
                    if temp==i
                        state=0;
                        break;
                    elseif temp==d1(i,p+1)
                        state=0;
                        break;
                    end
                end
                if state==1

                    temp_count=temp_count+1;
                    temp_self(n,temp_count)=temp;

                end
            end
        end
        
        
        
        
%%        
        %chose some node in two_hops of node i to connect the2_hop's dominating nodes
        Already_handle=zeros(Max_Degree*Max_Degree,1);
        Already_handle_result=Already_handle;
        handle_count=0;
        for n=1:d1(i,1)
            if temp_self(n,1)==0;
                break;                
            end
            for m=2:Max_Degree+1
                if temp_self(n,m)==0
                    break;
                end
                if Color_N(temp_self(n,m),1)==4||Color_N(temp_self(n,m),1)==2
                    state=0;
                    for p=1:handle_count
                        if Already_handle(p,1)==temp_self(n,m)
                            state=1;
                            if Already_handle_result(p,1)==0
                                break;
                            else
                                if Color_N(temp_self(n,1))==2||Color_N(temp_self(n,1))==4
                                    Already_handle_result(p,1)=0;
                                    break;
                                elseif  d1(temp_self(n,1),1)>d1(Already_handle_result(p,1),1)
                                    Already_handle_result(p,1)=temp_self(n,1);
                                elseif d1(temp_self(n,1),1)==d1(Already_handle_result(p,1),1)
                                    if temp_self(n:1)<Already_handle_result(p,1)
                                        Already_handle_result(p,1)=temp_self(n,1);
                                    end
                                end
                            end
                        end
                    end
                    if state==0;
                        if Color_N(temp_self(n,1))==2||Color_N(temp_self(n,1))==4
                            Already_handle(handle_count+1,1)=temp_self(n,m);
                            Already_handle_result(handle_count+1,1)=0;
                            handle_count=handle_count+1;
                        else
                            Already_handle(handle_count+1,1)=temp_self(n,m);
                            Already_handle_result(handle_count+1,1)=temp_self(n,1);
                            handle_count=handle_count+1;
                        end
                    end
                end
            end
        end
        
        for n=1:handle_count
           if  Already_handle_result(n,1)==0
              
           else
               Color_N(Already_handle_result(n,1))=6;
               
               temp1=Already_handle_result(n,1);
               temp2=Already_handle(n,1);
               plot([Posxy(i,1),Posxy(temp1,1)],[Posxy(i,2),Posxy(temp1,2)],'o-.c')
               plot([Posxy(temp2,1),Posxy(temp1,1)],[Posxy(temp2,2),Posxy(temp1,2)],'o-.c')
               
           end
        end
        
%%        
        %node i formulation the 3_hop connected graph
       Already_handle2=zeros(Max_Degree*Max_Degree*Max_Degree,1);
       handle_count2=0;
       Already_handle2_reult=zeros(Max_Degree*Max_Degree*Max_Degree,2);
       
       for n=1:d1(i,1)
           if temp_self(n,1)==0;
                break;                
           end
           for m=2:Max_Degree+1
                if temp_self(n,m)==0
                    break;
                end
                for p=1:d1(temp_self(n,m),1)
                    
                    temp_node=d1(temp_self(n,m),p+1);
                    
                    if Color_N(temp_node)==2||Color_N(temp_node)==4
                        
                        state_in=0;
                        
                        % it is one of 2hops neighboor of nod i if state_in==1;
                        for n2=1:d1(i,1)
                            if temp_self(n2,1)==0
                                break;
                            end
                            if temp_self(n2,1)==temp_node
                                state_in=1;
                                break;
                            end
                            for m2=2:Max_Degree+1
                                if temp_self(n2,m2)==0
                                    break;
                                end
                                if temp_self(n2,m2)==temp_node
                                    state_in=1;
                                    break;
                                end
                            end
                            if state_in==1
                                break;
                            end
                        end
                        
                        % it is one of 2hops neighboor of nod i if
                        % state_in==0, then we get the rout from there.
                        if state_in==0
                            
                            state_in2=0;
                            
                            for q=1:handle_count2
                                
                                if temp_node==Already_handle2(q,1)
                                    
                                    state_in2=1;
                                    
                                    temp1=Already_handle2_reult(q,1);
                                    temp2=Already_handle2_reult(q,2);
                                    temp3=temp_self(n,1);
                                    temp4=temp_self(n,m);
                                    
                                    if temp1==0
                                        break;
                                        %do nothing
                                    else
                                        if Color_N(temp3,1)==2||Color_N(temp3,1)==4||Color_N(temp4,1)==2||Color_N(temp4,1)==4
                                            Already_handle2_reult(q,1)=0;
                                            Already_handle2_reult(q,2)=0;
                                        else
                                            a=d1(temp3,1)+d1(temp4,1);
                                            b=d1(temp1,1)+d1(temp2,1);
                                            if a>b
                                                Already_handle2_reult(q,1)=temp3;
                                                Already_handle2_reult(q,2)=temp4;
                                            elseif a==b
                                                if temp3+temp4<temp1+temp2
                                                    Already_handle2_reult(q,1)=temp3;
                                                    Already_handle2_reult(q,2)=temp4;
                                                else
                                                    %do nothing
                                                end
                                            end
                                        end
                                    end
                                    
                                end
                                
                                if state_in2==1
                                   break; 
                                end
                                
                            end
                            
                            if state_in2==0;
                                handle_count2=handle_count2+1;
                                Already_handle2(handle_count2,1)=temp_node;
                                temp1=temp_self(n,1);
                                temp2=temp_self(n,m);
                                if  Color_N(temp1,1)==2||Color_N(temp1,1)==4||Color_N(temp2,1)==2||Color_N(temp2,1)==4
                                    Already_handle2_reult(handle_count2,1)=0;
                                    Already_handle2_reult(handle_count2,2)=0;
                                else
                                    Already_handle2_reult(handle_count2,1)=temp_self(n,1);
                                    Already_handle2_reult(handle_count2,2)=temp_self(n,m);
                                end
                            end
                            
                        end
                        
                    end
                    
                end
                
           end
       end
       
       for n=1:handle_count2
           if  Already_handle2_reult(n,1)==0

           else
               Color_N(Already_handle2_reult(n,1),1)=7;
               Color_N(Already_handle2_reult(n,2),1)=7;
               
               temp1=Already_handle2_reult(n,1);
               temp2=Already_handle2_reult(n,2);
               temp3=Already_handle2(n,1);
               plot([Posxy(i,1),Posxy(temp1,1)],[Posxy(i,2),Posxy(temp1,2)],'o-.b')
               plot([Posxy(temp2,1),Posxy(temp1,1)],[Posxy(temp2,2),Posxy(temp1,2)],'o-.b')
               plot([Posxy(temp2,1),Posxy(temp3,1)],[Posxy(temp2,2),Posxy(temp3,2)],'o-.b')

           end
       end
       
    end
end

%%
% get result

CDS_count1=0;
CDS_count2=0;
CDS_count3=0;

for i=1:Node_Num%point the node out
    if Color_N(i,1)==4||Color_N(i,1)==2
        CDS_count1=CDS_count1+1;
    elseif Color_N(i,1)==6
        CDS_count2=CDS_count2+1;
    elseif Color_N(i,1)==7
        CDS_count3=CDS_count3+1;
    end
end%画出点

hold on
for i=1:Node_Num%point the node out
    if Color_N(i,1)==4||Color_N(i,1)==2
        plot(Posxy(i,1),Posxy(i,2),'*r')
        for j=1:d1(i,1)
            m=d1(i,j+1);
            if Color_N(m,1)==4||Color_N(m,1)==2
                plot([Posxy(m,1),Posxy(i,1)],[Posxy(m,2),Posxy(i,2)],'o-.r')
            end
        end
    end
end


CDS_count=CDS_count1+CDS_count2+CDS_count3;
disp(CDS_count1);
disp(CDS_count2);
disp(CDS_count3);
disp(CDS_count);
disp(Node_Num);
disp(CDS_count/Node_Num);
UP2121

三、仿真结论

猜你喜欢

转载自blog.csdn.net/ccsss22/article/details/131135909