Exemplo n.º 1
0
def get_path_cover_multi_src(adj, src_list, dst, bfs_cache=None):
    """Generates a list of nodes that contains all the nodes in between
  multiple sources and a destination (endpoints included).

  Args:
    adj: Adjacency matrix.
    src: Source node index list, 0-based.
    dst: Destination node index, 0-based.

  Returns:
    cover: List of node indices, in traversal order.
  """
    num_nodes = adj.shape[0]
    if bfs_cache is None:
        bfs_cache = np.zeros([num_nodes, num_nodes], dtype=np.int8)
    cover = []
    for src in src_list:
        if src == dst:
            return cover
        if src > dst:
            raise Exception("Source must be smaller than destination.")
        if src >= num_nodes or dst >= num_nodes:
            raise Exception("Node index must be smaller than number of nodes.")
        if not bfs(adj, src, dst, cache=bfs_cache):
            log.error("Source is not connected to destination.")
    cover.extend(src_list)
    start = min(src_list) + 1
    for idx in range(start, dst):
        # If the node is connected to both source and destination, add.
        if idx in src_list:
            continue
        connect_dst = bfs(adj, idx, dst, cache=bfs_cache)
        if not connect_dst:
            continue
        for src in src_list:
            connect_src = bfs(adj, src, idx, cache=bfs_cache)
            if connect_src:
                break
        if connect_src and connect_dst:
            cover.append(idx)
    cover.append(dst)
    return cover
 def test_bfs_performance(self):
   adj = np.random.uniform(0, 1, [1000, 1000])
   adj = (adj > 0.3).astype(np.int8)
   for ii in range(10):
     bfs(adj, 0, 999, None)
 def test_bfs_cache(self):
   adj = np.zeros([3, 3], dtype=np.int8)
   self.assertTrue(bfs(adj, 0, 2, cache=np.ones([3, 3], dtype=np.int8)))
   self.assertFalse(bfs(adj, 0, 2, cache=np.zeros([3, 3], dtype=np.int8)))
 def test_bfs(self):
   adj = np.array([[1, 1, 0], [0, 0, 1], [0, 0, 0]], dtype=np.int8)
   self.assertTrue(bfs(adj, 0, 2, None))
   self.assertFalse(bfs(adj, 2, 0, None))