Search code examples
matlabadjacency-matrix

Convert edge list to adjacency matrix


if i have the following code in matlab

function adj=edgeL2adj(el)
nodes=sort(unique([el(:,1) el(:,2)])); % get all nodes, sorted
adj=zeros(numel(nodes));   % initialize adjacency matrix
% across all edges
for i=1:size(el,1);adj(find(nodes==el(i,1)),find(nodes==el(i,2)))=el(i,3);
end

that convert edge list m x 3 to adjacency list n x n but i have a matrix of edge list m x 2 so what is the required change in previous code that give me true result.

example:

if edge list =[1 2;2 3;2 4] then adjacency matrix=[0 1 0 0;0 0 1 1;0 0 0 0; 0 0 0 0]

Solution

  • I would personally do away with your loop code and take advantage of sparse then convert back to a full matrix if desired. The first column consists of the source node and the second column consists of the target node. You simply set all of these entries in the sparse matrix to 1. However, judging from your code, the third column of your edge list is also the corresponding weight, and so I'll write code that will assume both cases. Also, make sure you filter out duplicate rows using unique:

    Example with weights just being 1

    edgelist = [1 2;2 3;2 4];
    edgelist = unique(edgelist, 'rows');
    sz = max(edgelist(:));
    A = sparse(edgelist(:,1), edgelist(:,2), 1, sz, sz);
    

    The first line of code denotes your edge list where each row pair consists of two nodes incident with each other (i.e. they are connected by an edge). The second line removes any duplicate rows from the edge list. The third line determines how big the adjacency matrix should be. We need to figure out what the largest node ID is so that we can allocate a N x N sparse matrix where N is the largest node ID. The last line of code simply uses the first column and second column of the edge list to populate the entries in the sparse matrix, we set them to one and ensure that the size of the matrix is N x N.

    We get this:

    >> A
    
    A =
    
       (1,2)        1
       (2,3)        1
       (2,4)        1
    

    You can optionally convert the matrix to full by using the full function:

    >> full(A)
    
    ans =
    
         0     1     0     0
         0     0     1     1
         0     0     0     0
         0     0     0     0
    

    As you can see, this corresponds with your desired result.

    Example with weights in the third column

    edgelist = [1 2 0.1;2 3 0.2;2 4 0.3];
    edgelist = unique(edgelist, 'rows');
    sz = max(max(edgelist(:, 1:2)));
    A = sparse(edgelist(:,1), edgelist(:,2), edgelist(:,3), sz, sz);
    

    Same code as before, but you're changing the third parameter to sparse with the third column of edgelist.

    This is what we get:

    >> A
    
    A =
    
       (1,2)       0.1000
       (2,3)       0.2000
       (2,4)       0.3000
    
    >> full(A)
    
    ans =
    
             0    0.1000         0         0
             0         0    0.2000    0.3000
             0         0         0         0
             0         0         0         0