Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import List
- import numpy as np
- import numba as nb
- from scipy.sparse import csc_matrix, diags
- def find_islands(adj: csc_matrix):
- """
- Method to get the islands of a graph
- This is the non-recursive version
- :return: list of islands, where each element is a list of the node indices of the island
- """
- node_number = adj.shape[0]
- # Mark all the vertices as not visited
- visited = np.zeros(node_number, dtype=bool)
- # storage structure for the islands (list of lists)
- islands = list() # type: List[List[int]]
- # set the island index
- island_idx = 0
- # go though all the vertices...
- for node in range(node_number):
- # if the node has not been visited...
- if not visited[node]:
- # add new island, because the recursive process has already visited all the island connected to v
- # if island_idx >= len(islands):
- islands.append(list())
- # ------------------------------------------------------------------------------------------------------
- # DFS: store in the island all the reachable vertices from current vertex "node"
- #
- # declare a stack with the initial node to visit (node)
- stack = list() # type: List[int]
- stack.append(node)
- while len(stack) > 0:
- # pick the first element of the stack
- v = stack.pop(0)
- # if v has not been visited...
- if not visited[v]:
- # mark as visited
- visited[v] = True
- # add element to the island
- islands[island_idx].append(v)
- # Add the neighbours of v to the stack
- start = adj.indptr[v]
- end = adj.indptr[v + 1]
- for i in range(start, end):
- k = adj.indices[i] # get the column index in the CSC scheme
- if not visited[k]:
- stack.append(k)
- # ------------------------------------------------------------------------------------------------------
- # increase the islands index, because all the other connected vertices have been visited
- island_idx += 1
- # sort each of the islands to maintain raccord
- for island in islands:
- island.sort() # the sorting is done in-place
- return islands
- def get_elements_of_the_island( C_element_bus: csc_matrix, island ):
- """
- Get the branch indices of the island
- :param C_branch_bus: CSC elements-buses connectivity matrix with the dimensions: elements x buses
- :param island: array of bus indices of the island
- :return: array of indices of the elements that match that island
- """
- # assert isinstance( C_element_bus, csc_matrix )
- # faster method
- n_rows = C_element_bus.shape[0]
- visited = np.zeros( n_rows, dtype = bool )
- elm_idx = np.zeros( n_rows, dtype = int )
- n_visited = 0
- for k in range( len( island ) ):
- j = island[k] # column index
- for l in range( C_element_bus.indptr[j], C_element_bus.indptr[j + 1] ):
- i = C_element_bus.indices[l] # row index
- if not visited[i]:
- visited[i] = True
- elm_idx[n_visited] = i
- n_visited += 1
- # resize vector
- elm_idx = elm_idx[:n_visited]
- return elm_idx
- dense = np.matrix( [
- [ 2, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
- [ 1, 4, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
- [ 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
- [ 0, 1, 1, 5, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 ],
- [ 1, 1, 0, 1, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0 ],
- [ 0, 0, 0, 0, 1, 4, 0, 0, 0, 0, 1, 1, 1, 0 ],
- [ 0, 0, 0, 1, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0 ],
- [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
- [ 0, 0, 0, 1, 0, 0, 1, 0, 4, 1, 0, 0, 0, 1 ],
- [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0 ],
- [ 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 2, 0, 0, 0 ],
- [ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 1, 0 ],
- [ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 3, 1 ],
- [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 2 ]
- ] )
- sparse = csc_matrix( dense )
- vectorOfIslands = find_islands( sparse )
- genDense = np.matrix( [
- [ 1, 0, 0, 0, 0 ],
- [ 0, 1, 0, 0, 0 ],
- [ 0, 0, 1, 0, 0 ],
- [ 0, 0, 0, 0, 0 ],
- [ 0, 0, 0, 0, 0 ],
- [ 0, 0, 0, 1, 0 ],
- [ 0, 0, 0, 0, 0 ],
- [ 0, 0, 0, 0, 1 ],
- [ 0, 0, 0, 0, 0 ],
- [ 0, 0, 0, 0, 0 ],
- [ 0, 0, 0, 0, 0 ],
- [ 0, 0, 0, 0, 0 ],
- [ 0, 0, 0, 0, 0 ],
- [ 0, 0, 0, 0, 0 ]
- ] )
- genSparse = csc_matrix( genDense )
- result1 = get_elements_of_the_island( genSparse.transpose( ), vectorOfIslands[0] )
- print( result1 )
Add Comment
Please, Sign In to add comment