Originally Posted by

**htata123** I ran the code and it seems to display all values of K, not the minimum. So the algorithm I'm trying to implement is like this;

for all values of k from 1 to n (size of square matrix),

compute A^k.

Now in the new set of matrices A^k, I need to find the smallest k such that A^k does not have a zero diagonal. So what I did for that was I compared the diagonal for each A^k with the diagonal of the zero matrix. If that was equal then that value of k is not valid. The code does that. The output when I ran the code is like 1, 2, 3, 4, 5 which is fine, but I only need it to display 1. If there are no such values of K, i.e. all powers of A^k have zero diagonals then it should display no graphs exist.

Try this:

Code:

function sma(A)
[n,n] = size(A);
for k = 1:n;
P = A^k;
for k = 1:n;
if diag(A^k) == diag(zeros(n));
disp('no cycles for this graph exist');
else
k
break
end
end
end

CB