Pol
Pol

Reputation: 117

Adjacency matrix from Adjacency lists of a graph in matlab

I have Adjacency lists of a graph with 198 vertices and 2472 edges. How can I construct adjacency matrix of graph?

Thanks for any help

Upvotes: 0

Views: 191

Answers (1)

Banghua Zhao
Banghua Zhao

Reputation: 1556

Since you have 198 vertices, the adjacency matrix is 198 by 198, which is not so big. So we can just use a full matrix. Suppose the vertex number starts from 1. Suppose your adjacency lists matrix AL have the following format:

AL(1,:) = [1, 4, 6, -1, ...]
AL(2,:) = [2, 3, 7, 8, ...]
...

Where -1 is used to make the column of matrix AL same size.

Here is the code:

% initialize adjacency matrix
AM = zeros(198, 198)

% construct adjacency matrix 
L = length(A(1,:));
for i = 1:198
    for j = 1:L
        if AL(i,j) > 0
            AM(i,AL(i,j)) = 1;
        end
    end
end

Upvotes: 1

Related Questions