基于MATLAB的实现,此方法可以知道有几个连通域,并且知道各个顶点的归属。
Branches中显示各个节点的归属,同一行的为同一连通分支中的节点。
其第一列为它的分类数。
例如下图,有五个连通分支,1、2、3在同一个连通分支中。
这是上图的邻接矩阵,同一节点间为0。
Branches中的显示内容,第一列为连通分支数,后边跟着的是给连通分支中的节点。
第一行就表示1、2、3为一个连通分支,4自己在一个连通分支中等等。
function [Branches,numBranch]=Net_Branches(ConnectMatrix)
% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
% This program is designed to count the calculate connected components in networks.
% Usage [Cp_Average, Cp_Nodal] = Net_ClusteringCoefficients(ConnectMatrix,Type)
% Input:
% ConnectMatrix --- The connect matrix without self-edges.
% Output:
% Branches --- A matrix, each rows of which represents the
% different connected components.
% numBranch --- The numbers of connected components in network
%
% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ % Refer:
% Ulrik Barandes <A faster algorithm for betweennes centrality>
% Written by Hu Yong, Nov,2010
% E-mail: carrot.hy2010@
% based on Matlab 2008a
% Version (1.0),Copywrite (c) 2010
% Input check-------------------------------------------------------------%
[numNode,I] = size(ConnectMatrix);
if numNode ~= I
error('Pls check your connect matrix');
end
% End check---------------------------------------------------------------%
Node = [1:numNode];
Branches = [];
while any(Node)
Quence = find(Node,1); %find a non-zero number in Node set
subField=[]; %one component
% start search
while ~isempty(Quence)
currentNode = Quence(1);
Quence(1) = []; %dequeue
subField=[subField,currentNode];
Node(currentNode)=0;
neighborNode=find(ConnectMatrix(currentNode,:));
for i=neighborNode
if Node(i) ~= 0 %first found
Quence=[Quence,i];
Node(i)=0;
end
end
end
subField = [subField,zeros(1,numNode-length(subField))];
Branches = [Branches;subField]; %save
end
numBranch = size(Branches,1);。