Ejemplo n.º 1
0
    def setUp(self):
        self.n1 = Node("n1", {})
        self.n2 = Node("n2", {})
        self.n3 = Node("n3", {})
        self.n4 = Node("n4", {})
        self.n5 = Node("n5", {})
        self.e1 = Edge.undirected("e1", start_node=self.n1, end_node=self.n2)
        self.e2 = Edge.undirected("e2", start_node=self.n2, end_node=self.n3)
        self.e3 = Edge.undirected("e3", start_node=self.n3, end_node=self.n4)
        self.e4 = Edge.undirected("e4", start_node=self.n1, end_node=self.n4)

        self.graph = BaseGraph(
            "g1",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=set([self.n1, self.n2, self.n3, self.n4]),
            edges=set([self.e1, self.e2]),
        )

        # intialize profiler
        self.prof = cProfile.Profile()
        self.prof.enable()
        self.verbose = False
Ejemplo n.º 2
0
 def setUp(self):
     """ """
     self.n1 = Node("n1", {})
     self.n2 = Node("n2", {})
     self.n3 = Node("n3", {})
     self.e1 = Edge(
         "e1",
         start_node=self.n1,
         end_node=self.n2,
         edge_type=EdgeType.DIRECTED,
     )
     self.e2 = Edge(
         "e2",
         start_node=self.n2,
         end_node=self.n3,
         edge_type=EdgeType.DIRECTED,
     )
     self.q = PriorityQueue(is_min=True)
     self.q.insert(2, self.n1)
     self.q.insert(5, self.n2)
     self.q.insert(1, self.n3)
     self.qm = PriorityQueue(is_min=False)
     self.qm.insert(2, self.n1)
     self.qm.insert(5, self.n2)
     self.qm.insert(1, self.n3)
Ejemplo n.º 3
0
 def test_edges_by_end(self):
     """"""
     n1 = Node("n1", {})
     n2 = Node("n2", {})
     e1 = Edge(
         "e1", start_node=n1, end_node=n2, edge_type=EdgeType.UNDIRECTED
     )
     e2 = Edge(
         "e2", start_node=n1, end_node=n1, edge_type=EdgeType.UNDIRECTED
     )
     g = BaseGraph("g", nodes=set([n1, n2]), edges=set([e1, e2]))
     self.assertEqual(BaseGraphOps.edges_by_end(g, n2), set([e1]))
Ejemplo n.º 4
0
 def test_is_node_incident(self):
     """"""
     n1 = Node("n1", {})
     n2 = Node("n2", {})
     e1 = Edge(
         "e1", start_node=n1, end_node=n2, edge_type=EdgeType.UNDIRECTED
     )
     e2 = Edge(
         "e2", start_node=n1, end_node=n1, edge_type=EdgeType.UNDIRECTED
     )
     self.assertTrue(Graph.is_node_incident(n1, e1))
     self.assertFalse(Graph.is_node_incident(n2, e2))
Ejemplo n.º 5
0
 def test__add__g(self):
     """"""
     n = Node("n646", {})
     n1 = Node("n647", {})
     n2 = Node("n648", {})
     e = Edge(
         "e8", start_node=self.n1, end_node=n, edge_type=EdgeType.UNDIRECTED
     )
     gg = Graph(gid="temp", data={}, nodes=set([n, n1, n2]), edges=set([e]))
     g = self.graph + gg
     self.assertEqual(
         BaseGraphOps.nodes(g),
         set([self.n1, self.n2, self.n3, self.n4, n, n1, n2]),
     )
     self.assertEqual(BaseGraphOps.edges(g), set([e, self.e1, self.e2]))
Ejemplo n.º 6
0
 def test_equal(self):
     n1 = Node("n1", {})
     n2 = Node("n2", {})
     n3 = Node("n3", {})
     n4 = Node("n4", {})
     e1 = Edge(
         "e1", start_node=n1, end_node=n2, edge_type=EdgeType.UNDIRECTED
     )
     e2 = Edge(
         "e2", start_node=n2, end_node=n3, edge_type=EdgeType.UNDIRECTED
     )
     graph = Graph(
         "g1", data={}, nodes=set([n1, n2, n3, n4]), edges=set([e1, e2])
     )
     self.assertEqual(graph, self.graph)
Ejemplo n.º 7
0
 def test__sub__n(self):
     """"""
     n = Node("n646", {})
     g = self.graph - n
     self.assertEqual(
         BaseGraphOps.nodes(g), set([self.n1, self.n2, self.n3, self.n4])
     )
Ejemplo n.º 8
0
 def test_has_self_loop(self):
     """"""
     n1 = Node("n1", {})
     n2 = Node("n2", {})
     e1 = Edge("e1",
               start_node=n1,
               end_node=n2,
               edge_type=EdgeType.UNDIRECTED)
     e2 = Edge("e2",
               start_node=n1,
               end_node=n1,
               edge_type=EdgeType.UNDIRECTED)
     g1 = BaseGraph("graph", nodes=set([n1, n2]), edges=set([e1, e2]))
     g2 = BaseGraph("graph", nodes=set([n1, n2]), edges=set([e1]))
     self.assertTrue(BaseGraphAnalyzer.has_self_loop(g1))
     self.assertFalse(BaseGraphAnalyzer.has_self_loop(g2))
Ejemplo n.º 9
0
 def test_subtract_g(self):
     n = Node("n646", {})
     n1 = Node("n647", {})
     n2 = Node("n648", {})
     e = Edge(
         "e8", start_node=self.n1, end_node=n, edge_type=EdgeType.UNDIRECTED
     )
     gg = BaseGraph(
         gid="temp",
         data={},
         nodes=set([n, n1, n2]),
         edges=set([e, self.e1]),
     )
     g = BaseGraphAlgOps.subtract(self.graph, gg)
     self.assertEqual(BaseGraphOps.edges(g), set([]))
     self.assertEqual(BaseGraphOps.nodes(g), set([self.n3, self.n4]))
Ejemplo n.º 10
0
 def test_union_e(self):
     n = Node("n646", {})
     e = Edge(
         "e8", start_node=self.n1, end_node=n, edge_type=EdgeType.UNDIRECTED
     )
     eset = BaseGraphSetOps.union(self.graph, set([e]))
     self.assertEqual(eset, set([e, self.e1, self.e2]))
Ejemplo n.º 11
0
 def test__sub__e(self):
     """"""
     n = Node("n646", {})
     e = Edge(
         "e8", start_node=self.n1, end_node=n, edge_type=EdgeType.UNDIRECTED
     )
     g = self.graph - e
     self.assertEqual(BaseGraphOps.edges(g), set([self.e1, self.e2]))
Ejemplo n.º 12
0
 def setUp(self):
     """"""
     n1 = Node("m1", {})
     n2 = Node("m2", {})
     self.dedge = Edge(
         edge_id="medge",
         start_node=n1,
         end_node=n2,
         edge_type=EdgeType.DIRECTED,
         data={"my": "data"},
     )
     self.uedge = Edge(
         edge_id="uedge",
         start_node=n1,
         end_node=n2,
         edge_type=EdgeType.UNDIRECTED,
         data={"my": "data"},
     )
Ejemplo n.º 13
0
    def test_is_trivial_2(self):
        """"""
        n = Node("n646", {})
        e = Edge("e8", start_node=n, end_node=n, edge_type=EdgeType.UNDIRECTED)
        check = False
        try:
            BaseGraph(gid="temp", data={}, nodes=set([n]), edges=set([e]))
        except ValueError:
            check = True

        self.assertTrue(check)
Ejemplo n.º 14
0
 def test_add_node(self):
     n = Node("n646", {})
     g = BaseGraphAlgOps.add(self.graph, n)
     self.assertEqual(
         BaseGraphOps.nodes(g), set([self.n1, self.n2, self.n3, self.n4, n])
     )
Ejemplo n.º 15
0
    def setUp(self):
        self.n1 = Node("n1", {})
        self.n2 = Node("n2", {})
        self.n3 = Node("n3", {})
        self.n4 = Node("n4", {})
        self.n5 = Node("n5", {})
        self.e1 = Edge(
            "e1",
            start_node=self.n1,
            end_node=self.n2,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.e2 = Edge(
            "e2",
            start_node=self.n2,
            end_node=self.n3,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.e3 = Edge(
            "e3",
            start_node=self.n3,
            end_node=self.n4,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.e4 = Edge(
            "e4",
            start_node=self.n1,
            end_node=self.n4,
            edge_type=EdgeType.UNDIRECTED,
        )

        self.graph = BaseGraph(
            "g1",
            data={"my": "graph", "data": "is", "very": "awesome"},
            nodes=set([self.n1, self.n2, self.n3, self.n4]),
            edges=set([self.e1, self.e2]),
        )
        self.graph_2 = BaseGraph(
            "g2",
            data={"my": "graph", "data": "is", "very": "awesome"},
            nodes=set([self.n1, self.n2, self.n3, self.n4]),
            edges=set([self.e1, self.e2, self.e3]),
        )
        #
        self.a = Node("a", {})  # b
        self.b = Node("b", {})  # c
        self.f = Node("f", {})  # d
        self.e = Node("e", {})  # e
        self.ae = Edge(
            "ae",
            start_node=self.a,
            end_node=self.e,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.ab = Edge(
            "ab",
            start_node=self.a,
            end_node=self.b,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.af = Edge(
            "af",
            start_node=self.a,
            end_node=self.f,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.be = Edge(
            "be",
            start_node=self.b,
            end_node=self.e,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.ef = Edge(
            "ef",
            start_node=self.e,
            end_node=self.f,
            edge_type=EdgeType.UNDIRECTED,
        )

        # undirected graph
        self.ugraph2 = BaseGraph(
            "ug2",
            data={"my": "graph", "data": "is", "very": "awesome"},
            nodes=set([self.a, self.b, self.e, self.f]),
            edges=set(
                [
                    self.ae,
                    self.ab,
                    self.af,
                    self.be,
                    self.ef,
                ]
            ),
        )
        # ugraph2 :
        #   +-----+
        #  /       \
        # a -- b -- e
        #  \       /
        #   +-----f

        self.ugraph3 = BaseGraph(
            "ug3",
            data={"my": "graph", "data": "is", "very": "awesome"},
            nodes=set([self.a, self.b, self.e, self.f]),
            edges=set(
                [
                    self.ab,
                    # self.af,
                    self.be,
                ]
            ),
        )
        # ugraph3 :
        #
        #
        # a -- b -- e
        #  \
        #   +-----f

        self.ugraph4 = BaseGraph(
            "ug4",
            data={"my": "graph", "data": "is", "very": "awesome"},
            nodes=set(BaseGraphOps.nodes(self.ugraph2)).union(
                BaseGraphOps.nodes(self.graph_2)
            ),
            edges=BaseGraphOps.edges(self.ugraph2).union(
                BaseGraphOps.edges(self.graph_2)
            ),
        )
        # ugraph 4
        #   +-----+     n1 -- n2 -- n3 -- n4
        #  /       \     \                /
        # a -- b -- e     +--------------+
        #  \       /
        #   +-----f

        # make some directed edges
        self.bb = Node("bb", {})
        self.cc = Node("cc", {})
        self.dd = Node("dd", {})
        self.ee = Node("ee", {})

        self.bb_cc = Edge(
            "bb_cc",
            start_node=self.bb,
            end_node=self.cc,
            edge_type=EdgeType.DIRECTED,
        )
        self.cc_dd = Edge(
            "cc_dd",
            start_node=self.cc,
            end_node=self.dd,
            edge_type=EdgeType.DIRECTED,
        )
        self.dd_ee = Edge(
            "dd_ee",
            start_node=self.dd,
            end_node=self.ee,
            edge_type=EdgeType.DIRECTED,
        )
        self.ee_bb = Edge(
            "ee_bb",
            start_node=self.ee,
            end_node=self.bb,
            edge_type=EdgeType.DIRECTED,
        )
        self.bb_dd = Edge(
            "bb_dd",
            start_node=self.bb,
            end_node=self.dd,
            edge_type=EdgeType.DIRECTED,
        )
Ejemplo n.º 16
0
    def setUp(self):
        #
        self.n1 = Node("n1", {})
        self.n2 = Node("n2", {})
        self.n3 = Node("n3", {})
        self.n4 = Node("n4", {})
        self.n5 = Node("n5", {})
        self.e1 = Edge(
            "e1",
            start_node=self.n1,
            end_node=self.n2,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.e2 = Edge(
            "e2",
            start_node=self.n2,
            end_node=self.n3,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.e3 = Edge(
            "e3",
            start_node=self.n3,
            end_node=self.n4,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.e4 = Edge(
            "e4",
            start_node=self.n1,
            end_node=self.n4,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.graph_2 = UndiGraph(
            "g2",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=set([self.n1, self.n2, self.n3, self.n4]),
            edges=set([self.e1, self.e2, self.e3]),
        )

        self.a = Node("a", {})  # b
        self.b = Node("b", {})  # c
        self.c = Node("c", {})
        self.d = Node("d", {})
        self.f = Node("f", {})  # d
        self.e = Node("e", {})  # e
        self.g = Node("g", {})
        self.h = Node("h", {})
        self.ae = Edge(
            "ae",
            start_node=self.a,
            end_node=self.e,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.ab = Edge(
            "ab",
            start_node=self.a,
            end_node=self.b,
            data={"w": 1},
            edge_type=EdgeType.UNDIRECTED,
        )
        self.af = Edge(
            "af",
            start_node=self.a,
            end_node=self.f,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.ah = Edge(
            "ah",
            start_node=self.a,
            end_node=self.h,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.bh = Edge(
            "bh",
            start_node=self.b,
            end_node=self.h,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.be = Edge(
            "be",
            start_node=self.b,
            end_node=self.e,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.ef = Edge(
            "ef",
            data={"w": 5},
            start_node=self.e,
            end_node=self.f,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.de = Edge(
            "de",
            data={"w": 4},
            start_node=self.d,
            end_node=self.e,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.df = Edge(
            "df",
            data={"w": 8},
            start_node=self.d,
            end_node=self.f,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.cd = Edge(
            "cd",
            data={"w": 3},
            start_node=self.c,
            end_node=self.d,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.cg = Edge(
            "cg",
            start_node=self.c,
            end_node=self.g,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.gd = Edge(
            "gd",
            data={"w": 7},
            start_node=self.g,
            end_node=self.d,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.bg = Edge(
            "bg",
            data={"w": 6},
            start_node=self.b,
            end_node=self.g,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.fg = Edge(
            "fg",
            start_node=self.f,
            end_node=self.g,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.bc = Edge(
            "bc",
            start_node=self.b,
            end_node=self.c,
            data={"w": 2},
            edge_type=EdgeType.UNDIRECTED,
        )

        # undirected graph
        self.ugraph1 = UndiGraph(
            "ug1",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=set([self.a, self.b, self.e, self.f]),
            edges=set([
                self.ae,
                # self.ab,
                self.af,
                # self.be,
                self.ef,
            ]),
        )
        # ugraph1:
        #   +-----+
        #  /       \
        # a    b    e
        #  \       /
        #   +-----f

        self.ugraph2 = UndiGraph(
            "ug2",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=set([self.a, self.b, self.e, self.f]),
            edges=set([
                self.ae,
                self.ab,
                self.af,
                self.be,
                self.ef,
            ]),
        )
        # ugraph2 :
        #   +-----+
        #  /       \
        # a -- b -- e
        #  \       /
        #   +-----f

        self.ugraph3 = UndiGraph(
            "ug3",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=set([self.a, self.b, self.e, self.f]),
            edges=set([
                self.ab,
                self.af,
                self.be,
            ]),
        )
        # ugraph3 :
        #
        #
        # a -- b -- e
        #  \
        #   +-----f

        self.ugraph4 = UndiGraph(
            "ug4",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=BaseGraphOps.nodes(self.ugraph2).union(
                BaseGraphOps.nodes(self.graph_2)),
            edges=BaseGraphOps.edges(self.ugraph2).union(
                BaseGraphOps.edges(self.graph_2)),
        )
        # ugraph 4
        #   +-----+     n1 -- n2 -- n3 -- n4
        #  /       \     \                /
        # a -- b -- e     +--------------+
        #  \       /
        #   +-----f

        self.ugraph5 = UndiGraph(
            "ug5",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=set([self.a, self.b, self.c, self.d, self.e, self.f,
                       self.g]),
            edges=set([
                self.ab,
                self.bc,
                self.bg,
                self.cd,
                self.gd,
                self.df,
                self.de,
                self.ef,
            ]),
        )
        # ugraph 5
        #        +----c---+   +--e
        #       /  2     3 \ / 4 |
        # a --- b           d    | 5
        #    1   \ 6     7 / \ 8 |
        #         +---g---+   +--f

        self.ugraph6 = UndiGraph(
            "ug6",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=set([
                self.a,
                self.b,
                self.c,
                self.d,
                self.e,
                self.f,
                self.g,
                self.h,
            ]),
            edges=set([
                self.ab,
                self.ah,
                self.bc,
                self.bh,
                self.cd,
                self.de,
                self.df,
                self.cg,
                self.fg,
            ]),
        )
        # ugraph 6
        # a--+   e----d
        # |   \      / \
        # |    b----c   f
        # |   /      \ /
        # h--+        g
        self.ad = Edge(
            "ad",
            start_node=self.a,
            end_node=self.d,
            edge_type=EdgeType.UNDIRECTED,
        )
        #
        self.ugraph7 = UndiGraph(
            "ug7",
            nodes=set([self.a, self.b, self.c, self.d]),
            edges=set([self.ab, self.bc, self.cd, self.ad]),
        )
Ejemplo n.º 17
0
 def test_is_endvertice_true(self):
     """"""
     positive = self.uedge.is_endvertice(Node("m1", {}))
     self.assertEqual(positive, True)
Ejemplo n.º 18
0
 def setUp(self):
     """"""
     self.a = Node("a")
     self.b = Node("b")
     self.c = Node("c")
     self.d = Node("d")
     self.e = Node("e")
     self.f = Node("f")
     self.g = Node("g")
     self.h = Node("h")
     self.j = Node("j")
     self.k = Node("k")
     self.m = Node("m")
     #
     #    +--a --+
     #    |      |
     #    b      c
     #    |       \
     # +--+--+     g
     # |  |  |     |
     # d  e  f     h -- j
     #       |
     #    +--+---+
     #    |      |
     #    k      m
     #
     #
     self.ab = Edge(edge_id="ab", start_node=self.a, end_node=self.b)
     self.ac = Edge(edge_id="ac", start_node=self.a, end_node=self.c)
     self.bd = Edge(edge_id="bd", start_node=self.b, end_node=self.d)
     self.be = Edge(edge_id="be", start_node=self.b, end_node=self.e)
     self.bf = Edge(edge_id="bf", start_node=self.b, end_node=self.f)
     self.fk = Edge(edge_id="fk", start_node=self.f, end_node=self.k)
     self.fm = Edge(edge_id="fm", start_node=self.f, end_node=self.m)
     self.cg = Edge(edge_id="cg", start_node=self.c, end_node=self.g)
     self.gh = Edge(edge_id="gh", start_node=self.g, end_node=self.h)
     self.hj = Edge(edge_id="hj", start_node=self.h, end_node=self.j)
     self.gtree = Tree(
         gid="t",
         edges=set(
             [
                 self.ab,
                 self.ac,
                 self.bd,
                 self.be,
                 self.bf,
                 self.fk,
                 self.fm,
                 self.cg,
                 self.gh,
                 self.hj,
             ]
         ),
     )
Ejemplo n.º 19
0
 def setUp(self):
     """"""
     self.node = Node(node_id="mnode", data={"my": "data"})
Ejemplo n.º 20
0
 def test_hash(self):
     mstr = "mnode--my-data::is-awesome"
     n1 = Node("mnode", {"my": "data", "is": "awesome"})
     self.assertEqual(hash(n1), hash(mstr))
Ejemplo n.º 21
0
 def test_str(self):
     n1 = Node("mnode", {"my": "data", "is": "awesome"})
     mstr = "mnode--my-data::is-awesome"
     self.assertEqual(str(n1), mstr)
Ejemplo n.º 22
0
 def test_equal(self):
     self.assertEqual(self.node, Node("mnode", {"my": "data"}))
Ejemplo n.º 23
0
    def setUp(self):
        #
        self.n1 = Node("n1", {})
        self.n2 = Node("n2", {})
        self.n3 = Node("n3", {})
        self.n4 = Node("n4", {})
        self.n5 = Node("n5", {})
        self.e1 = Edge.directed("e1", start_node=self.n1, end_node=self.n2)
        self.e2 = Edge.directed("e2", start_node=self.n2, end_node=self.n3)
        self.e3 = Edge.directed("e3", start_node=self.n3, end_node=self.n4)
        self.e4 = Edge.directed("e4", start_node=self.n1, end_node=self.n4)
        self.graph_2 = DiGraph(
            "g2",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=set([self.n1, self.n2, self.n3, self.n4]),
            edges=set([self.e1, self.e2, self.e3, self.e4]),
        )
        #
        # n1 → n2 → n3 → n4
        # |              ↑
        # +--------------+

        self.a = Node("a", {})  # b
        self.b = Node("b", {})  # c
        self.c = Node("c", {})
        self.d = Node("d", {})
        self.f = Node("f", {})  # d
        self.e = Node("e", {})  # e
        self.g = Node("g", {})
        self.h = Node("h", {})
        self.ae = Edge.directed("ae", start_node=self.a, end_node=self.e)
        self.ab = Edge.directed("ab", start_node=self.a, end_node=self.b)
        self.af = Edge.directed("af", start_node=self.a, end_node=self.f)
        self.ah = Edge.directed("ah", start_node=self.a, end_node=self.h)
        self.bh = Edge.directed("bh", start_node=self.b, end_node=self.h)
        self.be = Edge.directed("be", start_node=self.b, end_node=self.e)
        self.ef = Edge.directed("ef", start_node=self.e, end_node=self.f)
        self.de = Edge.directed("de", start_node=self.d, end_node=self.e)
        self.df = Edge.directed("df", start_node=self.d, end_node=self.f)
        self.cd = Edge.directed("cd", start_node=self.c, end_node=self.d)
        self.cg = Edge.directed("cg", start_node=self.c, end_node=self.g)
        self.gd = Edge.directed("gd", start_node=self.g, end_node=self.d)
        self.bg = Edge.directed("bg", start_node=self.b, end_node=self.g)
        self.fg = Edge.directed("fg", start_node=self.f, end_node=self.g)
        self.bc = Edge.directed("bc", start_node=self.b, end_node=self.c)

        # directed graph
        self.dgraph1 = DiGraph(
            "dg1",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=set([self.a, self.b, self.e, self.f]),
            edges=set([
                self.ae,
                # self.ab,
                self.af,
                # self.be,
                self.ef,
            ]),
        )
        # dgraph1:
        #
        #
        # a --------> e  b
        # |           |
        # +---> f <---+
        #

        self.dgraph2 = DiGraph(
            "dg2",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=set([self.a, self.b, self.e, self.f]),
            edges=set([
                self.ae,
                self.ab,
                self.af,
                self.be,
                self.ef,
            ]),
        )
        # dgraph2 :
        #
        # a -> b -> e -> f
        # |         ↑    ↑
        # +---------+----+

        self.dgraph3 = DiGraph(
            "dg3",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=set([self.a, self.b, self.e, self.f]),
            edges=set([
                self.ab,
                self.af,
                self.be,
            ]),
        )
        # dgraph3 :
        #
        # a -> b -> e
        #  \
        #   +---> f

        self.dgraph4 = DiGraph(
            "dg4",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=BaseGraphOps.nodes(self.dgraph2).union(
                BaseGraphOps.nodes(self.graph_2)),
            edges=BaseGraphOps.edges(self.dgraph2).union(
                BaseGraphOps.edges(self.graph_2)),
        )
        # dgraph 4
        #
        # a -> b -> e -> f   n1 -> n2 -> n3 -> n4
        # |         ↑    ↑   |                  ↑
        # +---------+----+   +------------------+

        self.e_n = Edge.directed("en", start_node=self.e, end_node=self.n1)

        self.dgraph5 = DiGraph(
            "dg5",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=set([self.a, self.b, self.c, self.d, self.e, self.f,
                       self.g]),
            edges=set([
                self.ab,
                self.bc,
                self.bg,
                self.cd,
                self.gd,
                self.df,
                self.de,
                self.ef,
            ]),
        )
        # dgraph 5
        #        +--> c        +---> e
        #       /      \      /      |
        # a -> b        +--> d       |
        #       \      /      \      v
        #        +--> g        +---> f

        self.dgraph6 = DiGraph(
            "dg6",
            data={
                "my": "graph",
                "data": "is",
                "very": "awesome"
            },
            nodes=set([
                self.a,
                self.b,
                self.c,
                self.d,
                self.e,
                self.f,
                self.g,
                self.h,
            ]),
            edges=set([
                self.ab,
                self.ah,
                self.bc,
                self.bh,
                self.cd,
                self.de,
                self.df,
                self.cg,
                self.fg,
            ]),
        )
Ejemplo n.º 24
0
 def test_start(self):
     self.assertEqual(self.uedge.start(), Node("m1", {}))
Ejemplo n.º 25
0
 def test_end(self):
     self.assertEqual(self.uedge.end(), Node("m2", {}))
Ejemplo n.º 26
0
    def test_intersection_v(self):
        n = Node("n646", {})

        vset = BaseGraphSetOps.intersection(self.graph, set([self.n1, n]))
        self.assertEqual(vset, set([self.n1]))
Ejemplo n.º 27
0
 def test_is_endvertice_false(self):
     """"""
     negative = self.uedge.is_endvertice(Node("m3", {}))
     self.assertEqual(negative, False)
Ejemplo n.º 28
0
 def test_union_v(self):
     n = Node("n646", {})
     vset = BaseGraphSetOps.union(self.graph, set([n]))
     self.assertEqual(vset, set([self.n1, self.n2, self.n3, self.n4, n]))
Ejemplo n.º 29
0
    def setUp(self):
        """"""
        # From Diestel 2017, p. 7, fig. 1.3.1
        self.n1 = Node("a", {})  # b
        self.n2 = Node("b", {})  # c
        self.n3 = Node("f", {})  # d
        self.n4 = Node("e", {})  # e
        self.n5 = Node("g", {})  # e
        self.n6 = Node("h", {})  # e
        self.n7 = Node("j", {})  # e

        self.e1 = Edge(
            "e1",
            start_node=self.n1,
            end_node=self.n2,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.e2 = Edge(
            "e2",
            start_node=self.n2,
            end_node=self.n3,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.e3 = Edge(
            "e3",
            start_node=self.n3,
            end_node=self.n4,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.e4 = Edge(
            "e4",
            start_node=self.n4,
            end_node=self.n5,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.e5 = Edge(
            "e5",
            start_node=self.n5,
            end_node=self.n6,
            edge_type=EdgeType.UNDIRECTED,
        )
        self.e6 = Edge(
            "e6",
            start_node=self.n6,
            end_node=self.n7,
            edge_type=EdgeType.UNDIRECTED,
        )
        # n1 - n2 - n3 - n4 - n5 - n6 - n7
        #
        #
        #

        self.path = Path(
            gid="mpath",
            data={},
            edges=[self.e1, self.e2, self.e3, self.e4, self.e5, self.e6],
        )
        # tree
        self.a = Node("a")
        self.b = Node("b")
        self.c = Node("c")
        self.d = Node("d")
        self.e = Node("e")
        self.f = Node("f")
        self.g = Node("g")
        self.h = Node("h")
        self.j = Node("j")
        self.k = Node("k")
        self.m = Node("m")
        #
        #    +--a --+
        #    |      |
        #    b      c
        #    |       \
        # +--+--+     g
        # |  |  |     |
        # d  e  f     h -- j
        #       |
        #    +--+---+
        #    |      |
        #    k      m
        #
        #
        self.ab = Edge(edge_id="ab", start_node=self.a, end_node=self.b)
        self.ac = Edge(edge_id="ac", start_node=self.a, end_node=self.c)
        self.bd = Edge(edge_id="bd", start_node=self.b, end_node=self.d)
        self.be = Edge(edge_id="be", start_node=self.b, end_node=self.e)
        self.bf = Edge(edge_id="bf", start_node=self.b, end_node=self.f)
        self.fk = Edge(edge_id="fk", start_node=self.f, end_node=self.k)
        self.fm = Edge(edge_id="fm", start_node=self.f, end_node=self.m)
        self.cg = Edge(edge_id="cg", start_node=self.c, end_node=self.g)
        self.gh = Edge(edge_id="gh", start_node=self.g, end_node=self.h)
        self.hj = Edge(edge_id="hj", start_node=self.h, end_node=self.j)
        self.gtree = Graph.from_edgeset(edges=set([
            self.ab,
            self.ac,
            self.bd,
            self.be,
            self.bf,
            self.fk,
            self.fm,
            self.cg,
            self.gh,
            self.hj,
        ]), )
Ejemplo n.º 30
0
    def setUp(self):
        self.verbose = False
        #
        # Alan Gibbons, Algorithmic graph theory 1985, p. 22, fig. 1.16
        # depth first undirected graph

        # nodes
        self.n1 = Node("n1", data={})
        self.n2 = Node("n2", data={})
        self.n3 = Node("n3", data={})
        self.n4 = Node("n4", data={})
        self.n5 = Node("n5", data={})
        self.n6 = Node("n6", data={})
        self.n7 = Node("n7", data={})
        self.n8 = Node("n8", data={})
        self.n9 = Node("n9", data={})
        self.n10 = Node("n10", data={})
        self.n11 = Node("n11", data={})
        self.n12 = Node("n12", data={})
        self.n13 = Node("n13", data={})

        # edges
        self.e1u = Edge.undirected(
            "n1n4", start_node=self.n1, end_node=self.n4, data={}
        )
        self.e2u = Edge.undirected(
            "n1n3", start_node=self.n1, end_node=self.n3, data={}
        )
        self.e3u = Edge.undirected(
            "n1n2", start_node=self.n1, end_node=self.n2, data={}
        )
        self.e4u = Edge.undirected(
            "n1n5", start_node=self.n1, end_node=self.n5, data={}
        )
        self.e5u = Edge.undirected(
            "n1n6", start_node=self.n1, end_node=self.n6, data={}
        )
        self.e6u = Edge.undirected(
            "n1n7", start_node=self.n1, end_node=self.n7, data={}
        )
        self.e7u = Edge.undirected(
            "n1n8", start_node=self.n1, end_node=self.n8, data={}
        )
        self.e8u = Edge.undirected(
            "n8n2", start_node=self.n8, end_node=self.n2, data={}
        )
        self.e9u = Edge.undirected(
            "n9n10", start_node=self.n9, end_node=self.n10, data={}
        )
        self.e10u = Edge.undirected(
            "n9n13", start_node=self.n9, end_node=self.n13, data={}
        )
        self.e11u = Edge.undirected(
            "n10n11", start_node=self.n10, end_node=self.n11, data={}
        )
        self.e12u = Edge.undirected(
            "n10n12", start_node=self.n10, end_node=self.n12, data={}
        )
        self.ugraph = BaseGraph(
            "ugraph",
            nodes=set(
                [
                    self.n1,
                    self.n2,
                    self.n3,
                    self.n4,
                    self.n5,
                    self.n6,
                    self.n7,
                    self.n8,
                    self.n9,
                    self.n10,
                    self.n11,
                    self.n12,
                    self.n13,
                ]
            ),
            edges=set(
                [
                    self.e1u,
                    self.e2u,
                    self.e3u,
                    self.e4u,
                    self.e5u,
                    self.e6u,
                    self.e7u,
                    self.e8u,
                    self.e9u,
                    self.e10u,
                    self.e11u,
                    self.e12u,
                ]
            ),
            data={},
        )

        # tree
        self.a = Node("a")
        self.b = Node("b")
        self.c = Node("c")
        self.d = Node("d")
        self.e = Node("e")
        self.f = Node("f")
        self.g = Node("g")
        self.h = Node("h")
        self.j = Node("j")
        self.k = Node("k")
        self.m = Node("m")
        #
        #    +--a --+
        #    |      |
        #    b      c
        #    |       \
        # +--+--+     g
        # |  |  |     |
        # d  e  f     h -- j
        #       |
        #    +--+---+
        #    |      |
        #    k      m
        #
        #
        self.ab = Edge(edge_id="ab", start_node=self.a, end_node=self.b)
        self.ac = Edge(edge_id="ac", start_node=self.a, end_node=self.c)
        self.bd = Edge(edge_id="bd", start_node=self.b, end_node=self.d)
        self.be = Edge(edge_id="be", start_node=self.b, end_node=self.e)
        self.bf = Edge(edge_id="bf", start_node=self.b, end_node=self.f)
        self.fk = Edge(edge_id="fk", start_node=self.f, end_node=self.k)
        self.fm = Edge(edge_id="fm", start_node=self.f, end_node=self.m)
        self.cg = Edge(edge_id="cg", start_node=self.c, end_node=self.g)
        self.gh = Edge(edge_id="gh", start_node=self.g, end_node=self.h)
        self.hj = Edge(edge_id="hj", start_node=self.h, end_node=self.j)
        self.gtree = BaseGraph.from_edgeset(
            edges=set(
                [
                    self.ab,
                    self.ac,
                    self.bd,
                    self.be,
                    self.bf,
                    self.fk,
                    self.fm,
                    self.cg,
                    self.gh,
                    self.hj,
                ]
            ),
        )

        # initialize profiler
        self.prof = cProfile.Profile()
        self.prof.enable()