Construct adjacency list from an adjacency matrix

18 visualizaciones (últimos 30 días)
Saurabh
Saurabh el 4 de Mzo. de 2014
Comentada: Alec Jacobson el 17 de Mayo de 2021
I have an adjacency matrix :-
E.g A =[0 1 1;1 0 0; 1 0 0];
output :
2 3
1
1
I want to create an adjacency list from this matrix.

Respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 4 de Mzo. de 2014
A =[0 1 1;1 0 0; 1 0 0]
B=cellfun(@(x) find(x),num2cell(A,2),'un',0)
celldisp(B)
  1 comentario
Alec Jacobson
Alec Jacobson el 17 de Mayo de 2021
This will be very slow for large graphs even if your adjacency matrix A is sparse: num2cell effectively creates a dense matrix.
As a drop-in replacement you could use:
B = arrayfun(@(i) find(A(:,i))',1:size(A,2),'UniformOutput',false)';
However, depending on how you're using this adjacency list it might be better to just use find(A(:,i) any time you need i's neighbors: the compressed column storage of matlab's sparse matrices is tantamount to storing an adjacency list anyway.

Iniciar sesión para comentar.

Categorías

Más información sobre Graph and Network Algorithms en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by