TraumaEnSOes

mcve

Jun 3rd, 2020
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.77 KB | None | 0 0
  1. from typing import List
  2. import numpy as np
  3. import numba as nb
  4. from scipy.sparse import csc_matrix, diags
  5.  
  6. def find_islands(adj: csc_matrix):
  7.     """
  8.    Method to get the islands of a graph
  9.    This is the non-recursive version
  10.    :return: list of islands, where each element is a list of the node indices of the island
  11.    """
  12.  
  13.     node_number = adj.shape[0]
  14.  
  15.     # Mark all the vertices as not visited
  16.     visited = np.zeros(node_number, dtype=bool)
  17.  
  18.     # storage structure for the islands (list of lists)
  19.     islands = list()  # type: List[List[int]]
  20.  
  21.     # set the island index
  22.     island_idx = 0
  23.  
  24.     # go though all the vertices...
  25.     for node in range(node_number):
  26.  
  27.         # if the node has not been visited...
  28.         if not visited[node]:
  29.  
  30.             # add new island, because the recursive process has already visited all the island connected to v
  31.             # if island_idx >= len(islands):
  32.             islands.append(list())
  33.  
  34.             # ------------------------------------------------------------------------------------------------------
  35.             # DFS: store in the island all the reachable vertices from current vertex "node"
  36.             #
  37.             # declare a stack with the initial node to visit (node)
  38.             stack = list()  # type: List[int]
  39.             stack.append(node)
  40.  
  41.             while len(stack) > 0:
  42.  
  43.                 # pick the first element of the stack
  44.                 v = stack.pop(0)
  45.  
  46.                 # if v has not been visited...
  47.                 if not visited[v]:
  48.  
  49.                     # mark as visited
  50.                     visited[v] = True
  51.  
  52.                     # add element to the island
  53.                     islands[island_idx].append(v)
  54.  
  55.                     # Add the neighbours of v to the stack
  56.                     start = adj.indptr[v]
  57.                     end = adj.indptr[v + 1]
  58.                     for i in range(start, end):
  59.                         k = adj.indices[i]  # get the column index in the CSC scheme
  60.                         if not visited[k]:
  61.                             stack.append(k)
  62.             # ------------------------------------------------------------------------------------------------------
  63.  
  64.             # increase the islands index, because all the other connected vertices have been visited
  65.             island_idx += 1
  66.  
  67.     # sort each of the islands to maintain raccord
  68.     for island in islands:
  69.         island.sort()  # the sorting is done in-place
  70.  
  71.     return islands
  72.  
  73. def get_elements_of_the_island( C_element_bus: csc_matrix, island ):
  74.     """
  75.    Get the branch indices of the island
  76.    :param C_branch_bus: CSC elements-buses connectivity matrix with the dimensions: elements x buses
  77.    :param island: array of bus indices of the island
  78.    :return: array of indices of the elements that match that island
  79.    """
  80.  
  81.     # assert isinstance( C_element_bus, csc_matrix )
  82.  
  83.     # faster method
  84.     n_rows = C_element_bus.shape[0]
  85.     visited = np.zeros( n_rows, dtype = bool )
  86.     elm_idx = np.zeros( n_rows, dtype = int )
  87.     n_visited = 0
  88.  
  89.     for k in range( len( island ) ):
  90.  
  91.         j = island[k]  # column index
  92.  
  93.         for l in range( C_element_bus.indptr[j], C_element_bus.indptr[j + 1] ):
  94.  
  95.             i = C_element_bus.indices[l]  # row index
  96.  
  97.             if not visited[i]:
  98.                 visited[i] = True
  99.                 elm_idx[n_visited] = i
  100.                 n_visited += 1
  101.  
  102.     # resize vector
  103.     elm_idx = elm_idx[:n_visited]
  104.  
  105.     return elm_idx
  106.  
  107. dense = np.matrix( [
  108.     [ 2, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  109.     [ 1, 4, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  110.     [ 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  111.     [ 0, 1, 1, 5, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 ],
  112.     [ 1, 1, 0, 1, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0 ],
  113.     [ 0, 0, 0, 0, 1, 4, 0, 0, 0, 0, 1, 1, 1, 0 ],
  114.     [ 0, 0, 0, 1, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0 ],
  115.     [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  116.     [ 0, 0, 0, 1, 0, 0, 1, 0, 4, 1, 0, 0, 0, 1 ],
  117.     [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0 ],
  118.     [ 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 2, 0, 0, 0 ],
  119.     [ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 1, 0 ],
  120.     [ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 3, 1 ],
  121.     [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 2 ]
  122. ] )
  123. sparse = csc_matrix( dense )
  124.  
  125. vectorOfIslands = find_islands( sparse )
  126.  
  127. genDense = np.matrix( [
  128.     [ 1, 0, 0, 0, 0 ],
  129.     [ 0, 1, 0, 0, 0 ],
  130.     [ 0, 0, 1, 0, 0 ],
  131.     [ 0, 0, 0, 0, 0 ],
  132.     [ 0, 0, 0, 0, 0 ],
  133.     [ 0, 0, 0, 1, 0 ],
  134.     [ 0, 0, 0, 0, 0 ],
  135.     [ 0, 0, 0, 0, 1 ],
  136.     [ 0, 0, 0, 0, 0 ],
  137.     [ 0, 0, 0, 0, 0 ],
  138.     [ 0, 0, 0, 0, 0 ],
  139.     [ 0, 0, 0, 0, 0 ],
  140.     [ 0, 0, 0, 0, 0 ],
  141.     [ 0, 0, 0, 0, 0 ]
  142. ] )
  143. genSparse = csc_matrix( genDense )
  144.  
  145. result1 = get_elements_of_the_island( genSparse.transpose( ), vectorOfIslands[0] )
  146.  
  147. print( result1 )
Add Comment
Please, Sign In to add comment