Carlos Mamede
Carlos Mamede

Reputation: 23

The total number of edges given a adjacency matrix

I need to calculate the total number of edges given the adjacency matrix for a undirected simple graph. I was told that I could perform this in MATLAB using the following:

n_edges=sum(sum(Adj))/2;

Can someone explain how it is calculating the edges?

Upvotes: 2

Views: 9043

Answers (1)

eigenchris
eigenchris

Reputation: 5821

Just repeating my comment...

Adj(i,j) = 1 tells you there is an edge connecting nodes i and j. If A(i,j) = 1 then A(j,i) = 1 as well, as these indicate the same edge. Since we count every edge twice, we need to divide the total by 2.

Upvotes: 6

Related Questions