简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM