Ejemplo n.º 1
0
 def test_from_adj(self):
   m = Graph.from_adj_matrix(ADJ)
   self.assertEqual(m.num_edges(), 5)
   self.assertEqual(m.num_vertices(), 4)
   m = Graph.from_adj_matrix(csr_matrix(ADJ))
   self.assertEqual(m.num_edges(), 5)
   self.assertEqual(m.num_vertices(), 4)
Ejemplo n.º 2
0
 def test_from_adj(self):
     m = Graph.from_adj_matrix(ADJ)
     self.assertEqual(m.num_edges(), 5)
     self.assertEqual(m.num_vertices(), 4)
     m = Graph.from_adj_matrix(csr_matrix(ADJ))
     self.assertEqual(m.num_edges(), 5)
     self.assertEqual(m.num_vertices(), 4)
Ejemplo n.º 3
0
 def test_from_pairs(self):
   g = Graph.from_edge_pairs(PAIRS)
   self.assertEqual(g.num_edges(), 5)
   self.assertEqual(g.num_vertices(), 4)
   g = Graph.from_edge_pairs(PAIRS, num_vertices=10)
   self.assertEqual(g.num_edges(), 5)
   self.assertEqual(g.num_vertices(), 10)
   g = Graph.from_edge_pairs(PAIRS, symmetric=True)
   self.assertEqual(g.num_edges(), 8)
   self.assertEqual(g.num_vertices(), 4)
Ejemplo n.º 4
0
 def test_from_pairs(self):
     g = Graph.from_edge_pairs(PAIRS)
     self.assertEqual(g.num_edges(), 5)
     self.assertEqual(g.num_vertices(), 4)
     g = Graph.from_edge_pairs(PAIRS, num_vertices=10)
     self.assertEqual(g.num_edges(), 5)
     self.assertEqual(g.num_vertices(), 10)
     g = Graph.from_edge_pairs(PAIRS, symmetric=True)
     self.assertEqual(g.num_edges(), 8)
     self.assertEqual(g.num_vertices(), 4)
Ejemplo n.º 5
0
  def test_from_pairs_weighted(self):
    w = np.array([1,1,0.1,2,1,2,3.1,4])
    p = [[0,1],[1,2],[2,3],[3,4],[1,0],[2,1],[3,2],[4,3]]
    expected = [[0,1,0,0,0],[1,0,1,0,0],[0,2,0,0.1,0],[0,0,3.1,0,2],[0,0,0,4,0]]
    G = Graph.from_edge_pairs(p, weights=w, num_vertices=5)
    assert_array_almost_equal(G.matrix(dense=True), expected)

    # weighted + symmetric
    expected = [[0,1,2,0],[1,3,4,0],[2,4,0,0],[0,0,0,5]]
    G = Graph.from_edge_pairs(PAIRS, symmetric=True, weights=np.arange(1,6))
    assert_array_equal(G.matrix(dense=True), expected)
Ejemplo n.º 6
0
 def test_from_pairs_empty(self):
   g = Graph.from_edge_pairs([])
   self.assertEqual(g.num_edges(), 0)
   self.assertEqual(g.num_vertices(), 0)
   ii, jj = g.pairs().T
   assert_array_equal(ii, [])
   assert_array_equal(jj, [])
   # Make sure ii and jj have indexable dtypes
   PAIRS[ii,jj]
   # Make sure num_vertices is set correctly
   g = Graph.from_edge_pairs([], num_vertices=5)
   self.assertEqual(g.num_edges(), 0)
   self.assertEqual(g.num_vertices(), 5)
Ejemplo n.º 7
0
 def test_from_pairs_empty(self):
     g = Graph.from_edge_pairs([])
     self.assertEqual(g.num_edges(), 0)
     self.assertEqual(g.num_vertices(), 0)
     ii, jj = g.pairs().T
     assert_array_equal(ii, [])
     assert_array_equal(jj, [])
     # Make sure ii and jj have indexable dtypes
     PAIRS[ii, jj]
     # Make sure num_vertices is set correctly
     g = Graph.from_edge_pairs([], num_vertices=5)
     self.assertEqual(g.num_edges(), 0)
     self.assertEqual(g.num_vertices(), 5)
Ejemplo n.º 8
0
    def test_from_pairs_weighted(self):
        w = np.array([1, 1, 0.1, 2, 1, 2, 3.1, 4])
        p = [[0, 1], [1, 2], [2, 3], [3, 4], [1, 0], [2, 1], [3, 2], [4, 3]]
        expected = [[0, 1, 0, 0, 0], [1, 0, 1, 0, 0], [0, 2, 0, 0.1, 0],
                    [0, 0, 3.1, 0, 2], [0, 0, 0, 4, 0]]
        G = Graph.from_edge_pairs(p, weights=w, num_vertices=5)
        assert_array_almost_equal(G.matrix('dense'), expected)

        # weighted + symmetric
        w = np.arange(1, 6)
        expected = [[0, 1, 2, 0], [1, 3, 4, 0], [2, 4, 0, 0], [0, 0, 0, 5]]
        G = Graph.from_edge_pairs(PAIRS, symmetric=True, weights=w)
        assert_array_equal(G.matrix('dense'), expected)
        G = Graph.from_edge_pairs(PAIRS[::-1], symmetric=True, weights=w[::-1])
        assert_array_equal(G.matrix('dense'), expected)
Ejemplo n.º 9
0
 def test_from_pairs_weighted(self):
   w = np.array([1,1,0.1,2,1,2,3.1,4])
   p = [[0,1],[1,2],[2,3],[3,4],[1,0],[2,1],[3,2],[4,3]]
   expected = [[0,1,0,0,0],[1,0,1,0,0],[0,2,0,0.1,0],[0,0,3.1,0,2],[0,0,0,4,0]]
   G = Graph.from_edge_pairs(p, weights=w, num_vertices=5)
   assert_array_almost_equal(G.matrix(dense=True), expected)
   # weighted + symmetric is NYI for now
   self.assertRaises(NotImplementedError, Graph.from_edge_pairs, PAIRS,
                     symmetric=True, weights=w)
Ejemplo n.º 10
0
 def test_from_pairs_floating(self):
   g = Graph.from_edge_pairs(PAIRS.astype(float))
   p = g.pairs()
   self.assertTrue(np.can_cast(p, PAIRS.dtype, casting='same_kind'),
                   "Expected integral dtype, got %s" % p.dtype)
   assert_array_equal(p, PAIRS)
Ejemplo n.º 11
0
                        if len(self.result[self.path]) > 1:
                            self.result.append([self.start])
                            self.path += 1
                        else:
                            self.result[self.path] = [self.start]
                        self.visited = set()

    def construct_all_paths(self):
        self.__compute_iterative(self.start)
        self.__compute(self.start)
        self.result.pop()
        return self.result


if __name__ == '__main__':
    g = Graph()
    a = g.insert_vertex('A')
    b = g.insert_vertex('B')
    c = g.insert_vertex('C')
    d = g.insert_vertex('D')
    g.insert_edge(a, b, 'ab_bi')
    g.insert_edge(a, d, 'ad_bi')
    g.insert_edge(b, d, 'bd_bi')
    g.insert_edge(d, c, 'dc_bi')
    g.insert_edge(a, c, 'ca_bi')

    forest = DFS_complete(g)
    print(forest)
    bfs_forest = BFS_complete(g)
    print(bfs_forest)
Ejemplo n.º 12
0
 def test_from_pairs_floating(self):
     g = Graph.from_edge_pairs(PAIRS.astype(float))
     p = g.pairs()
     self.assertTrue(np.can_cast(p, PAIRS.dtype, casting='same_kind'),
                     "Expected integral dtype, got %s" % p.dtype)
     assert_array_equal(p, PAIRS)