#################本文为学习《图论算法及其MATLAB实现》的学习笔记#################
将图中任意两点间均连通的子图化为块,称为该图的连通块。
将简单图的连通的顶点分为一类的过程称为判断图的连通性。
算法用途
不仅判断图的连通性,而且程序还能求出该图的连通块数,并指出每个顶点分别属于哪一个连通块。
程序参数说明
G: 邻接矩阵
S: 顶点所分的块数
Q: 每个顶点所在的块号
算法的matlab程序详解
%图的连通性计算
function [ S,Q ] = concom( G )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% 输入: G: 邻接矩阵
%%%%%%%%% 输出: S: 顶点所分的块数
%%%%%%%%% Q: 每个顶点所在的块号
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n = size(G,1); % 计算顶点数
m = sum(sum(G))/2; % 计算总边数
S = 0;
j = 1;
C = 1;
Q = zeros(n,1);
for i = 1:n
for j = (i+1):n
if G(i,j) == 1 % 两点之间有边
if Q(i) == Q(j) % 若两点之间有边,则属于同一块
if Q(i) == 0
Q(i) = C;
Q(j) = C;
C = C+1; % 更新块号
S = S+1; % 总块数+1
end
else
if Q(i) == 0 % 若为标记i,则与j在同一块
Q(i) = Q(j);
elseif Q(j) == 0 % 若为标记j,则与i在同一块
Q(j) = Q(i);
else % 若两者相连,但标记为不同的块,则合并
for k = 1:n
if Q(k) == Q(i)
Q(k) = Q(j);% 将两块合并
end
end
S = S+1;
end
end
end
end
end
S;
Q;