Пример #1
0
 def test_graph_toposort_visitor(self):
     graph = Graph(range(6))
     graph.add_edges(5, 2, 0)
     graph.add_edges(4, 0, 1)
     graph.add_edge(2, 3)
     graph.add_edge(3, 1)
     actual = GraphToposortVisitor.visit(graph)
     expect = [5, 4, 2, 3, 1, 0]
     self.assertEquals(actual, expect)
Пример #2
0
    def test_graph_edges(self):
        graph = Graph([1,2,3,4], name='graph', weight=1)
        self.assertRaises(KeyError, lambda: graph.add_edge(5, 4))
        self.assertRaises(KeyError, lambda: graph.add_edge(4, 5))

        graph.add_node(4, name='node(4)')
        self.assertEquals(graph.get_node(4), {'name' : 'node(4)'})

        graph.add_edge(2, 4, name='edge(2-4)')
        self.assertEquals(graph.get_edge(2, 4), {'name' : 'edge(2-4)'})
Пример #3
0
class GraphTraversalTest(unittest.TestCase):

    def setUp(self):
        self.graph = Graph(['a', 'b', 'c', 'd', 'e'])
        self.graph.add_edge('a', 'b')
        self.graph.add_edge('a', 'c')
        self.graph.add_edge('b', 'c')
        self.graph.add_edge('b', 'd')
        self.graph.add_edge('b', 'e')
        self.graph.add_edge('d', 'e')

    def test_graph_bfs_visit(self):
        visitor = GraphPathExistsVisitor('e')
        graph_bfs_visit(self.graph, visitor, 'a')
        self.assertTrue(visitor.path_exists)

        visitor = GraphPathExistsVisitor('a')
        graph_bfs_visit(self.graph, visitor, 'b')
        self.assertFalse(visitor.path_exists)

    def test_graph_dfs_visit(self):
        visitor = GraphPathExistsVisitor('e')
        graph_dfs_visit(self.graph, visitor, 'a')
        self.assertTrue(visitor.path_exists)

        visitor = GraphPathExistsVisitor('a')
        graph_dfs_visit(self.graph, visitor, 'b')
        self.assertFalse(visitor.path_exists)
Пример #4
0
    def test_graph_copy(self):
        source = Graph(['a', 'b', 'c', 'd', 'e'])
        source.add_edge('a', 'b')
        source.add_edge('a', 'c')
        source.add_edge('b', 'c')
        source.add_edge('b', 'd')
        source.add_edge('b', 'e')
        source.add_edge('d', 'e')
        output = copy_graph(source, 'a')

        expect = [ (s, d.keys()) for s,d in source.edges.items() if d.values()]
        actual = [ (s, d.keys()) for s,d in output.edges.items() if d.values()]
        self.assertEqual(expect, actual)
Пример #5
0
class GraphTraversalTest(unittest.TestCase):

    def setUp(self):
        self.graph = Graph(['a', 'b', 'c', 'd', 'e'])
        self.graph.add_edge('a', 'b')
        self.graph.add_edge('a', 'c')
        self.graph.add_edge('b', 'c')
        self.graph.add_edge('b', 'd')
        self.graph.add_edge('b', 'e')
        self.graph.add_edge('d', 'e')

    def test_graph_dfs_path_exists(self):
        self.assertTrue(graph_dfs_path_exists(self.graph, 'a', 'e'))
        self.assertFalse(graph_dfs_path_exists(self.graph, 'b', 'a'))
Пример #6
0
from bashwork.structure.graph import Graph

def is_shared(shared_root, find_node, root, graph):
    shares = set(graph.get_edges(shared_root))
    stack  = [ [root] ]

    while stack:
        path = stack.pop()
        if path[-1] == find_node:
            return any(p in shares for p in path)

        for node in graph.get_edges(path[-1]):
            stack.append(path + [node])
    return False

if __name__ == "__main__":
    graph = Graph(nodes=list('abcdefghijk'))
    graph.add_edge('a', 'b')
    graph.add_edge('a', 'c')
    graph.add_edge('a', 'd')
    graph.add_edge('b', 'e')
    graph.add_edge('d', 'f')
    graph.add_edge('d', 'g')
    graph.add_edge('g', 'h')
    graph.add_edge('g', 'i')
    graph.add_edge('j', 'g')
    graph.add_edge('j', 'k')
    graph.add_edge('k', 'b')

    print("is node 'h' shared in 'j': %s" % is_shared('j', 'h', 'a', graph))