MATLAB Answers

Naime
0

find cycle in array

Asked by Naime
on 22 Oct 2018
Latest activity Edited by Sim
on 9 Oct 2019
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]}

  6 Comments

Sim
on 7 Oct 2019
Hi Naime! I have your exact problem... Did you find any solution to share ?
Guillaume
on 8 Oct 2019
@simone, have you looked at the answers provided? In particular, I linked to two fileexchange submissions in my answer.
Sim
on 9 Oct 2019
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:

Sign in to comment.

Tags

2 Answers

KSSV
Answer by KSSV
on 22 Oct 2018

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

  0 Comments

Sign in to comment.


Guillaume
Answer by Guillaume
on 22 Oct 2018

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)

  0 Comments

Sign in to comment.