图的连通性判断

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

基于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

% 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);

相关文档
最新文档