|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import division |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import networkx as nx |
| 7 | +import scipy |
| 8 | + |
| 9 | +from typing import List |
| 10 | + |
| 11 | + |
| 12 | +def sum_all_pair_shortest_path_length_adjacency_matrix(g: nx.Graph) -> int: |
| 13 | + """ Computes the sum of shortest path length over all the pairs of nodes in g |
| 14 | +
|
| 15 | + Args: |
| 16 | + g: a networkx graph representing the connection between switches. |
| 17 | +
|
| 18 | + Returns: |
| 19 | + total_sp: sum of shortest path length over all the pairs of nodes in g |
| 20 | + """ |
| 21 | + |
| 22 | + num_node = len(g.nodes()) |
| 23 | + A = nx.to_numpy_matrix(g, order='F', dtype=np.float32) |
| 24 | + B = np.eye(num_node, order='F', dtype=np.float32) |
| 25 | + total_sp = num_node * (num_node - 1) - num_node |
| 26 | + C = np.zeros(np.shape(A), order='F', dtype=np.float32) |
| 27 | + for k in range(num_node - 1): |
| 28 | + B = scipy.linalg.blas.sgemm(1, B, A) |
| 29 | + # B = np.matmul(B, A) |
| 30 | + C = np.add(C, B) |
| 31 | + num = np.count_nonzero(C == 0) |
| 32 | + if num == 0: |
| 33 | + break |
| 34 | + total_sp += num |
| 35 | + |
| 36 | + return total_sp |
| 37 | + |
| 38 | + |
| 39 | +def all_pair_shortest_path_length_adjacency_matrix(g: nx.Graph, tor_list: List = None) -> np.array: |
| 40 | + """ Returns the length of the shortest path between all pairs of ToRs |
| 41 | +
|
| 42 | + Args: |
| 43 | + g: a networkx graph representing the connection between switches. |
| 44 | + tor_list: a list of tors such that the output represents the shortest path length among pairs |
| 45 | + with both ends in tor_list. In case tor_list = None, this function returns the shortest |
| 46 | + path length between all the pairs. |
| 47 | + Returns: |
| 48 | + shortest_path_np_array: |
| 49 | + """ |
| 50 | + |
| 51 | + num_node = len(g.nodes()) |
| 52 | + A = nx.to_numpy_matrix(g, order='F', dtype=np.float32) |
| 53 | + B = np.eye(num_node, order='F', dtype=np.float32) |
| 54 | + |
| 55 | + C = np.eye(num_node, order='F', dtype=np.float32) |
| 56 | + shortest_path_np_array = np.ones(np.shape(A), order='F', dtype=np.float32) |
| 57 | + |
| 58 | + for k in range(num_node - 1): |
| 59 | + B = scipy.linalg.blas.sgemm(1, B, A) |
| 60 | + C = np.add(C, B) |
| 61 | + if np.count_nonzero(C == 0) == 0: |
| 62 | + break |
| 63 | + |
| 64 | + add_np_array = np.subtract(np.ones(np.shape(C), order='F', dtype=np.float32), C) |
| 65 | + add_np_array = np.where(add_np_array < 0, 0, add_np_array) |
| 66 | + shortest_path_np_array = np.add(shortest_path_np_array, add_np_array) |
| 67 | + del add_np_array |
| 68 | + |
| 69 | + shortest_path_np_array = np.subtract(shortest_path_np_array, np.eye(num_node, order='F', dtype=np.float32)) |
| 70 | + delete_index_list = list() |
| 71 | + if tor_list: |
| 72 | + for index, node in enumerate(g.nodes()): |
| 73 | + if node not in tor_list: |
| 74 | + delete_index_list.append(index) |
| 75 | + shortest_path_np_array = np.delete(shortest_path_np_array, delete_index_list, axis=1) |
| 76 | + shortest_path_np_array = np.delete(shortest_path_np_array, delete_index_list, axis=0) |
| 77 | + |
| 78 | + del A |
| 79 | + del B |
| 80 | + del C |
| 81 | + return shortest_path_np_array |
0 commit comments