Big O Complexity for Matlab code - euclidean distance between each element in matrix A(n*n)

2 views (last 30 days)
i want to compute Big O complexity for this Matlab code , the challenge i have is how to compute complexity for "sum, size and traspose "of matrix operations
the code is
A=[0 1 1 0 ; 1 0 1 1 ; 1 1 0 1 ; 0 1 1 0];
k=full(sum(A))
num_edges=sum(k)
kkk=full(sum(A,2)');
n = size(A,1) % number of nodes
ff=zeros(n,n);
gg=zeros(n,n);
for i=1:n
for j=1:n
for q=1:n
q;
i,j;
if(kkk(q)==0)
gg(i,j)=A(i,j);
else
gg(i,j)=gg(i,j)+(((A(q,i)-A(q,j)).^2)/(n));
end
end
gg(i,j)=gg(i,j).^0.5;
end
end
gg=gg
Thank you,

Accepted Answer

Walter Roberson
Walter Roberson on 25 Dec 2015
The complexity of the size() operation is O(1)
The cost of sum() is number of items being totaled at a time, multiplied by number of times that has to happen. That is going to come out the same as the number of elements in the matrix. Which makes sense: every element only needs to be examined once.
The cost of a transpose is the same as the number of items being transpose.
Your code does not need to do the transpose.
Your code is not calculating Euclidean distance. Euclidean distance does not divide by n. I also think your check of kkk(q) is wrong -- at least it would be if you could potentially have negative entries.
Calculating Euclidean distance between each two items in the matrix would require n^4 output locations (or half of that for a triangular version that takes advantage of symmetry.)
  4 Comments
Jan
Jan on 25 Dec 2015
Please format your code properly. Remove the blank lines and use the "{} Code" button.
Remove the useless lines "q;" and "i,j;" . And finaly tell us the problem you need help for.

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!