Example #1
0
 def test_starting_from(self):
     graph = lil_matrix((3, 3), dtype='bool')
     graph[1, 2] = True
     longest_path = longest_path_in_tree(graph, 0)
     self.assertArraysEqual(longest_path, np.array([0]))
     longest_path = longest_path_in_tree(graph, 1)
     self.assertArraysEqualOrReversed(longest_path, np.array([1, 2]))
Example #2
0
    def test_complex_tree(self):
        '''
        Network topology:

        0 -> 2 -> 5 -> 8
        |    |
        v    v
        1    4 -> 7 -> 9
        |    |
        v    v
        3    6
        '''
        graph = lil_matrix((10, 10), dtype='bool')
        edges = [(0, 1), (1, 3), (0, 2), (2, 4), (4, 6), (4, 7), (7, 9),
                 (2, 5), (5, 8)]
        for source, target in edges:
            graph[source, target] = True
        longest_path = longest_path_in_tree(graph)
        self.assertArraysEqualOrReversed(longest_path,
                                         np.array([3, 1, 0, 2, 4, 7, 9]))

        # Now move node 9 to be under node 8, instead of node 7. The path
        # should change accordingly.
        graph[7, 9] = False
        graph[8, 9] = True
        longest_path = longest_path_in_tree(graph)
        self.assertArraysEqualOrReversed(longest_path,
                                         np.array([3, 1, 0, 2, 5, 8, 9]))
Example #3
0
 def test_linear_tree(self):
     graph = lil_matrix((5, 5), dtype='bool')
     for i in range(4):
         graph[i, i + 1] = True
     longest_path = longest_path_in_tree(graph)
     # Either order for the path is technically OK.
     self.assertArraysEqualOrReversed(longest_path,
                                      np.array([0, 1, 2, 3, 4]))
Example #4
0
 def test_single_node(self):
     graph = lil_matrix((1, 1), dtype='bool')
     longest_path = longest_path_in_tree(graph)
     self.assertArraysEqual(longest_path, np.array([0]))