Search code examples
matlabgraphedges

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?


Solution

  • 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.