find cycle in array

조회 수: 7 (최근 30일)
NA
NA 2018년 10월 22일
편집: Matt J 2020년 6월 5일
I have a M M=[1,2 ;1,5; 2,4;2,5;4,5; 4,9;5,6;6,11;9,10;10,11] I want to find a loop in this array. I want this answer A={[1,2,5],[2,4,5],[4,5,6,9,10,11]}
  댓글 수: 7
Sim
Sim 2019년 10월 9일
편집: Sim 2019년 10월 9일
Yes, thank you.. I tried what you suggested:
[1] Count all cycles in simple undirected graph version 1.2.0.0 (5.43 KB) by Jeff Howbert
[2] Count Loops in a Graph version 1.1.0.0 (167 KB) by Joseph Kirk
Unfortunately, both solutions [1] and [2] were not working for my case... but I understood more about the problem and the way to describe it! Thanks a lot, very helpful FileExchange submissions!
Just for information, the solution I was looking for was kindly provided by Matt J here:
Can Chen
Can Chen 2020년 6월 5일
Hi Na, I work at MathWorks on graphs. If you have a few minutes, I would very much appreciate hearing more about your workflow using cycles. Would you please contact me directly?

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2020년 6월 5일
편집: Matt J 2020년 6월 5일
This can be done using the spatialgraph2D submission on the File Exchange,
M = [1,2 ;1,5; 2,4;2,5;4,5; 4,9;5,6;6,11;9,10;10,11]
G = graph(M(:, 1), M(:, 2));
hg=plot(G);
obj=spatialgraph2D(G,hg.XData,hg.YData);
[~,A]=obj.polyshape;
>> A{:}
ans =
1 5 2
ans =
2 5 4
ans =
4 5 6 11 10 9
>> obj.mosaic

추가 답변 (2개)

KSSV
KSSV 2018년 10월 22일
M=[1,2 ;1,5; 2,4;2,5;4,5; 4,9;5,6;6,11;9,10;10,11] ;
A={[1,2,5],[2,4,5],[4,5,6,9,10,11]} ;
[c,ia,ib] = unique(M(:,1)) ;
N = length(c) ;
B = cell(N,1) ;
for i = 1:length(c)
T = M(ib==i,:) ;
B{i} = unique(T(:)) ;
end

Guillaume
Guillaume 2018년 10월 22일
There are no built-in algorithms in matlab to find cycles in graphs (undirected or directed). You'll either have to write your own, eg. using DFS or BFS (both DFS and BFS are implemented in matlab) or some other algorithm that you can find using your favorite search engine, or you'll have to use one of the submissions in the FileExchange ( [1], [2], and probably more)

카테고리

Help CenterFile Exchange에서 Networks에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by