现在的库很多了,介绍一个2021版本过后的matlab函数allcycles,可以寻找无向图中所有的循环
如下图

clear;clc;close all;
n = 5;
A = delsq(numgrid('S',n));
G = graph(A,'omitselfloops');
plot(G)
[cycles,edgecycles] = allcycles(G);
tiledlayout flow
for k = 1:length(cycles)
nexttile
highlight(plot(G),cycles{k},'Edges',edgecycles{k},'EdgeColor','r','NodeColor','r')
title("Cycle " + k)
end
输出结果


还可以用邻接矩阵自己构建
clear;clc;close all;
a=[0 0 1 1 0 1 0
0 0 1 0 0 0 0
1 1 0 1 0 0 0
1 0 1 0 0 0 0
0 0 0 0 0 0 1
1 0 0 0 0 0 1
0 0 0 0 1 1 0 ]
G=graph(a)
plot(G)
[cycles,edgecycles] = allcycles(G);
tiledlayout flow
for k = 1:length(cycles)
nexttile
highlight(plot(G),cycles{k},'Edges',edgecycles{k},'EdgeColor','r','NodeColor','r')
title("Cycle " + k)
end
输出结果


1073

被折叠的 条评论
为什么被折叠?



