import numpy as np from scipy.sparse import csr_matrix n = 5 # number of nodes adj_list = [[1, 3], [0, 2, 3], [1, 4], [0, 1], []] # Create the row and column indices arrays rows = [] cols = [] for i in range(n): for j in adj_list[i]: rows.append(i) cols.append(j) # Create the data array (all ones for unweighted graph) data = np.ones(len(rows)) # Create the CSR matrix csr_mat = csr_matrix((data, (rows, cols)), shape=(n, n))