How to select a submatrix from an adjacency list in Python?

I have an adjacency list where each array represents non-zero columns in this row (for example, the 0th array in the list below means that columns 2 and 6 are 1, and everything else is 0).

adj_list = [[2, 6], [1, 3, 24], [2, 4], [3, 5, 21], [4, 6, 10], [1, 5, 7], [6, 8, 9], [7], [7, 10, 14], [5, 9, 11], [10, 12, 18], [11, 13] [12, 14, 15], [9, 13 ], [13, 16, 17], [15], [15], [11, 19, 20], [18], [18], [4, 22, 23], [21], [21], [2, 25, 26], [24], [24]]

Given this adj - list, I would like to select a submatrix that has the same row and column indices that are specified:

submatrix = (0, 1, 2, 5, 22)

Each item in the submatrix indicates a line number.

1) For each row iin the submatrix, I need to get an array ithfrom adj_list(which is equivalent to getting a row ithfrom the adjacency matrix)

2) Then from this array I need to extract the elements that correspond to the submatrix

For example, if I am currently looking at the 3rd element in submatrix, which is 5, then I need to go to the 5th array in adj_list (the equivalent of getting the 5th row adj.matrix), which is [1, 5,7 ], and then I need to look at which elements in [1,5,7] correspond to the submatrix (equivalent to getting the 1st, 5th and 7th columns of the 5th row). In this case, the result for the 5th row should be [0,1,0,1,0], because only 1 and 5 intersect in two arrays).

How can I effectively select this sub-matrix provided adj. list?

+3
1
adj_list = [[2, 6], [1, 3, 24], [2, 4], [3, 5, 21], [4, 6, 10], [1, 5, 7], [6, 8, 9], [7], [7, 10, 14], [5, 9, 11], [10, 12, 18], [11, 13], [12, 14, 15], [9, 13], [13, 16, 17], [15], [15], [11, 19, 20], [18], [18], [4, 22, 23], [21], [21], [2, 25, 26], [24], [24]]

submatrix = (0, 1, 2, 5, 22)

result = [[i in adj_list[sm] for i in submatrix] for sm in submatrix]

; , , , - , , .

+2

All Articles