⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 get_community.m

📁 社团划分
💻 M
字号:
function [Community_Nodes,Community_Vertex]=get_community(infname)
%
%Community_Nodes--N*N adjacent,if edge Community_Nodes(i,j) is innter
%                 community edge,Community_Nodes(i,j)=community_ID;else
%                 Community_Nodes(i,j)=-1 for inter-community edge
%Write by Rock on 06.09.27

TEST=0;

if TEST==1
    infname='SCN_Data_topology_connected'
end

%1.read Nodes
Nodes=spconvert(load([infname,'.adj']));
N=length(Nodes);
M=nnz(Nodes);

%2.read Community ID
Community_Vertex=get_community_ID([infname,'-fc_a.groups']);    
community_num=max(Community_Vertex);
community_num

%3.distinguish edge as inner-community and inter_community
[Col,Row,Weight]=find(Nodes);
BridgeVertex_flag=zeros(N,1);%if vertex i is birdge vertex,BridgeVertex_flag(i)=1,else BridgeVertex_flag(i) is 0

for i=1:M
    if  Community_Vertex(Col(i))== Community_Vertex(Row(i))
        Weight(i)=Community_Vertex(Col(i));%inner_community edge
    else
        Weight(i)=-1;%inter_community edge
        BridgeVertex_flag(Community_Vertex(Col(i)))=1;
        BridgeVertex_flag(Community_Vertex(Row(i)))=1;
    end
end

Community_Nodes=sparse(Row,Col,Weight);

%cope with BC
if TEST==1
    EdgeBC=spconvert(load([infname,'.edgeBC']));
    VertexBC=load([infname,'.vertexBC']);

    %inter/inner edge BC
    %[inner/inter_community_edge_num,inner/inter_community_edge_total_BC,inner/inter_community_ave_edge_BC],Community_Edge_info(end) saving inter_community edge info
    Community_Edge_info=zeros(community_num+1,3);
    [Row,Col,Weight]=find(Community_Nodes);
    for i=1:M
        if Weight(i)<0%inner-community edge
            Community_Edge_info(end,1)=Community_Edge_info(end,1)+1;
            Community_Edge_info(end,2)=Community_Edge_info(end,2)+EdgeBC(Row(i),Col(i));
        else
            Community_Edge_info(Weight(i),1)=Community_Edge_info(Weight(i),1)+1;
            Community_Edge_info(Weight(i),2)=Community_Edge_info(Weight(i),2)+EdgeBC(Row(i),Col(i));
        end
    end
    for i=1:community_num+1
        Community_Edge_info(i,3)=Community_Edge_info(i,2)/Community_Edge_info(i,1);
    end
    Community_Edge_info
    
    %inter/inner vertex info
    %[vertex_num,inner_community_edge_num,inner_community_ave_edge,inter_community_edge_num,ave_degree]
    Community_Vertex_info=zeros(community_num,5);
    for i=1:N
        Community_Vertex_info(Community_Vertex(i),5)=Community_Vertex_info(Community_Vertex(i),5)+nnz(Nodes(i,:));%total edge num
    end
    
    for i=1:community_num
         Community_Vertex_info(i,1)=nnz(find(Community_Vertex==i));%vertex num
         Community_Vertex_info(i,2)=nnz(find(Community_Nodes==i));%inner_community_edge_num
         Community_Vertex_info(i,3)=Community_Vertex_info(i,2)/Community_Vertex_info(i,1);
         Community_Vertex_info(i,5)=Community_Vertex_info(i,5)/Community_Vertex_info(i,1);
    end
    [Row,Col,Weight]=find(Community_Nodes);
    for i=1:M
        if Weight(i)<0%inner-community edge
            Community_Vertex_info(Community_Vertex(Row(i)),4)=Community_Vertex_info(Community_Vertex(Row(i)),4)+1;
            Community_Vertex_info(Community_Vertex(Col(i)),4)=Community_Vertex_info(Community_Vertex(Col(i)),4)+1;
        end
    end
    Community_Vertex_info(:,1:end)=Community_Vertex_info(:,1:end)/2;
    
    figure
    plot(Community_Edge_info(1:community_num,3),Community_Vertex_info(1:community_num,3),'ro')
    xlabel('ave inner edgeBC')
    ylabel('ave community degree')
    
    figure
    plot(Community_Vertex_info(:,5),Community_Vertex_info(:,3),'ro')
    xlabel('ave degree')
    ylabel('ave inner degree')
    
    Community_Vertex_info
end

return

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -