Last active
November 24, 2020 13:16
-
-
Save USM-F/31c1d1d4eee593c36247ff1a13eb96d2 to your computer and use it in GitHub Desktop.
Convert adjacency matrix to adjacency list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def adjacency_matrix_to_adjacency_list(adjacency_matrix): | |
adjacency_list = [] | |
indices = np.argwhere(adjacency_matrix==1) | |
for node in range(len(adjacency_matrix)): | |
adjacency_list.append([]) | |
for index in indices: | |
if index[0] == node: | |
adjacency_list[node].append(index[1]) | |
adjacency_list = [np.asarray(l) for l in adjacency_list] | |
return adjacency_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment