Skip to content

Instantly share code, notes, and snippets.

@USM-F
Last active November 24, 2020 13:16
Show Gist options
  • Save USM-F/31c1d1d4eee593c36247ff1a13eb96d2 to your computer and use it in GitHub Desktop.
Save USM-F/31c1d1d4eee593c36247ff1a13eb96d2 to your computer and use it in GitHub Desktop.
Convert adjacency matrix to adjacency list
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