def test_dfs(self):
     graph = Graph(directed=False)
     graph.add_edge(data1='a', data2=123, weight=3)
     graph.add_edge(data1='b', data2=123, weight=2)
     graph.add_edge(data1='c', data2=123, weight=1)
     graph.add_edge(data1='c', data2='a', weight=1)
     with self.assertRaises(TypeError) as error:
         t = graph.dfs(node='c')
 def test_dfs(self):
     graph = Graph(directed=True)
     graph.add_edge(data1='a', data2='b')
     graph.add_edge(data1='a', data2='c')
     graph.add_edge(data1='b', data2='d')
     graph.add_edge(data1='c', data2='e')
     t = graph.dfs(node='a')
     self.assertNotEqual(t.index('b'), 1)
     self.assertNotEqual(t.index('b'), 3)
     self.assertNotEqual(t.index('c'), 1)
     self.assertNotEqual(t.index('c'), 3)
     self.assertNotEqual(t.index('d'), 0)
     self.assertNotEqual(t.index('d'), 2)
     self.assertNotEqual(t.index('e'), 0)
     self.assertNotEqual(t.index('e'), 2)
Exemple #3
0
def earliest_ancestor(ancestors, starting_node):
    graph = Graph()
    for vertex_1, vertex_2 in ancestors:
        graph.add_vertex(vertex_1)
        graph.add_vertex(vertex_2)
        # add edges
    for vertex_1, vertex_2 in ancestors:
        graph.add_edge(vertex_1, vertex_2)

    targetVertex = None
    longestPath = 1
    for vertex in graph.vertices:
        path = graph.dfs(vertex, starting_node)
        if path:
            if len(path) > longestPath:
                longestPath = len(path)
                targetVertex = vertex
        elif not path and longestPath == 1:
            targetVertex = -1

    return targetVertex
 def test_dfs_empty_graph(self):
     graph = Graph(directed=True)
     t = graph.dfs(node='c')
     self.assertIsNone(t)
Exemple #5
0
 def graphTest(self):
     graphConstruct = GraphConstruct()
     adjList = graphConstruct.getAdjacencyList([[0, 1], [0, 2], [1, 2],
                                                [2, 0], [2, 3], [3, 3]])
     graph = Graph(adjList)
     graph.dfs()