Ejemplo n.º 1
0
 def test_topo_sort1(self):
     g = graphs.Graph(directed=True)
     g.add_edges((1, 0), (2, 1))
     exists, path = algorithms.topo_sort(g)
     print "Path1: ", path
     g = graphs.Graph(directed=True)
     g.add_edges((0, 1), (1, 2))
     exists, path = algorithms.topo_sort(g)
     print "Path2: ", path
     self.assertTrue(exists)
Ejemplo n.º 2
0
    def test_nodes_and_edges(self):
        g = graphs.Graph()
        nodes = [1, 2, 3, 4]
        g.add_nodes(*nodes)
        g.add_edges((1, 2), (2, 3), (3, 4), (4, 5))

        for k in nodes:
            n = g.nodes[k]
            self.assertTrue(k in g.nodes)
        self.assertTrue(5 in g.nodes)

        self.assertTrue(isinstance(g.get_edge(1, 2), graphs.Edge))
        self.assertTrue(isinstance(g.get_edge(2, 3), graphs.Edge))
        self.assertTrue(isinstance(g.get_edge(3, 4), graphs.Edge))
        self.assertTrue(isinstance(g.get_edge(4, 5), graphs.Edge))

        self.assertTrue(isinstance(g.get_edge(2, 1), graphs.Edge))
        self.assertTrue(isinstance(g.get_edge(3, 2), graphs.Edge))
        self.assertTrue(isinstance(g.get_edge(4, 3), graphs.Edge))
        self.assertTrue(isinstance(g.get_edge(5, 4), graphs.Edge))

        # Ensure a -> b and b -> a is the same edge instance
        self.assertEqual(id(g.get_edge(2, 1)), id(g.get_edge(1, 2)))
        self.assertEqual(id(g.get_edge(3, 2)), id(g.get_edge(2, 3)))
        self.assertEqual(id(g.get_edge(4, 3)), id(g.get_edge(3, 4)))
        self.assertEqual(id(g.get_edge(5, 4)), id(g.get_edge(4, 5)))
Ejemplo n.º 3
0
    def test_islands(self):
        g = graphs.Graph()
        g.add_edges((1, 2), (2, 3), (3, 4))
        g.add_edges((10, 20), (20, 30), (30, 40))
        g.add_edges((100, 200), (200, 300), (300, 400))

        comps = algorithms.connected_components(g)
        self.assertEqual(len(set(comps.values())), 3)
Ejemplo n.º 4
0
    def test_topo_sort_no_cycles_disconnected(self):
        g = graphs.Graph(directed=True)
        g.add_edges((1, 2), (1, 3), (1, 4))
        g.add_edges((10, 20), (10, 30), (10, 40))

        exists, path = algorithms.topo_sort(g)
        print "Path: ", path
        self.assertTrue(exists)
        self.assertEquals(path, [4, 3, 2, 1, 40, 30, 20, 10])
Ejemplo n.º 5
0
    def test_topo_sort_no_cycles(self):
        g = graphs.Graph(directed=True)
        g.add_edges((1, 2), (1, 9), (2, 9), (2, 3), (9, 3), (3, 4), (4, 5),
                    (4, 8), (5, 6), (6, 8), (6, 7), (8, 7))

        exists, path = algorithms.topo_sort(g)
        print "Path: ", path
        self.assertTrue(exists)
        self.assertEquals(path, [7, 8, 6, 5, 4, 3, 9, 2, 1])
Ejemplo n.º 6
0
    def test_cloning(self):
        """
        Leetcode - https://leetcode.com/problems/clone-graph/
        Complicated case with self-loop and multi edges
        """
        class UndirectedGraphNode:
            def __init__(self, x, neighs = None):
                self.label = x
                self.neighbors = []

        nodes = {}
        for n in [0,1,2,3,4,5]:
            nodes[n] = UndirectedGraphNode(n)

        nodes[0].neighbors = [nodes[1], nodes[5]]
        nodes[1].neighbors = [nodes[2], nodes[5]]
        nodes[2].neighbors = [nodes[3]]
        nodes[3].neighbors = [nodes[4], nodes[4]]
        nodes[4].neighbors = [nodes[5], nodes[5]]
        g = graphs.Graph(multi = True, 
                         key_func = (lambda node: node.label), 
                         neighbors_func = (lambda node: [(n,None) for n in node.neighbors]))

        class MyTraversal(dfs.Traversal):
            def __init__(self, graph):
                dfs.Traversal.__init__(self, graph)
                self.copiedNodes = {}

            def should_process_node(self, node):
                if node.label not in self.copiedNodes:
                    print "Processing Node: ", node.label
                    self.copiedNodes[node.label] = UndirectedGraphNode(node.label)

            def process_edge(self, source, target, edge_data): 
                print "Processing Edge: ", source.label, target.label
                self.should_process_node(source)
                self.should_process_node(target)
                self.copiedNodes[source.label].neighbors.append(self.copiedNodes[target.label])
                # self.copiedNodes[target.label].neighbors.append(self.copiedNodes[source.label])

        mt = MyTraversal(g)
        dfs.dfs(nodes[0], mt)
        self.assertEqual(sorted([n.label for n in mt.copiedNodes[0].neighbors]), [1,5])
        self.assertEqual(sorted([n.label for n in mt.copiedNodes[1].neighbors]), [2,5])
        self.assertEqual(sorted([n.label for n in mt.copiedNodes[2].neighbors]), [3])
        self.assertEqual(sorted([n.label for n in mt.copiedNodes[3].neighbors]), [4,4])
        self.assertEqual(sorted([n.label for n in mt.copiedNodes[4].neighbors]), [5,5])
        self.assertEqual(sorted([n.label for n in mt.copiedNodes[5].neighbors]), [])
Ejemplo n.º 7
0
    def test_basic(self):
        g = graphs.Graph()
        g.add_edges((1, 2), (1, 3), (1, 4), (2, 5), (5, 6))

        tr = bfs.Traversal(g)
        bfs.bfs(1, tr)
        self.assertEqual(tr.node_state[1], 1)
        self.assertEqual(tr.node_state[2], 1)
        self.assertEqual(tr.node_state[3], 1)
        self.assertEqual(tr.node_state[4], 1)
        self.assertEqual(tr.node_state[5], 1)
        self.assertEqual(tr.node_state[6], 1)

        self.assertTrue(1 not in tr.parents)
        self.assertEqual(tr.parents[2], 1)
        self.assertEqual(tr.parents[3], 1)
        self.assertEqual(tr.parents[4], 1)
        self.assertEqual(tr.parents[5], 2)
        self.assertEqual(tr.parents[6], 5)
Ejemplo n.º 8
0
    def test_nodes_and_edges_directed(self):
        g = graphs.Graph(directed=True)
        nodes = [1, 2, 3, 4]
        g.add_nodes(*nodes)
        g.add_edges((1, 2), (2, 3), (3, 4), (4, 5))

        for k in nodes:
            n = g.nodes[k]
            self.assertTrue(k in g.nodes)
        self.assertTrue(5 in g.nodes)

        self.assertTrue(isinstance(g.get_edge(1, 2), graphs.Edge))
        self.assertTrue(isinstance(g.get_edge(2, 3), graphs.Edge))
        self.assertTrue(isinstance(g.get_edge(3, 4), graphs.Edge))
        self.assertTrue(isinstance(g.get_edge(4, 5), graphs.Edge))

        self.assertEqual(g.get_edge(2, 1), None)
        self.assertEqual(g.get_edge(3, 2), None)
        self.assertEqual(g.get_edge(4, 3), None)
        self.assertEqual(g.get_edge(5, 4), None)
Ejemplo n.º 9
0
    def test_basic(self):
        g = graphs.Graph()
        g.add_edges((1, 2), (1, 3), (1, 4), (2, 5), (5, 6))

        comps = algorithms.connected_components(g)
        self.assertEqual(len(set(comps.values())), 1)
Ejemplo n.º 10
0
    def test_no_edges(self):
        g = graphs.Graph()
        g.add_nodes(1, 2, 3, 4, 5)

        comps = algorithms.connected_components(g)
        self.assertEqual(len(set(comps.values())), 5)
Ejemplo n.º 11
0
 def test_creation(self):
     g = graphs.Graph()