def test_equal(self): self.assertEqual(self.node, Node("mnode", {"my": "data"}))
def test_str(self): n1 = Node("mnode", {"my": "data", "is": "awesome"}) mstr = "mnode--my-data::is-awesome" self.assertEqual(str(n1), mstr)
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]), )
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, ]), )
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()
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, ] ), )
def test_is_in_false(self): """""" n = Node("n86", {}) b = BaseGraphOps.is_in(self.graph, n) self.assertFalse(b)
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, ) # self.ugraph1 = BaseGraph( "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, ] ), ) # 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=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, )
class GraphTest(unittest.TestCase): """""" def setUp(self): self.verbose = False 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 = Graph( "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 = Graph( "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.ugraph1 = Graph( "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 = Graph( "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 = Graph( "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 = Graph( "ug4", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set( [ self.a, self.b, self.e, self.f, self.n1, self.n2, self.n3, self.n4, ] ), edges=set( [ self.ab, self.af, self.ae, self.be, self.ef, self.e1, self.e2, self.e3, self.e4, ] ), ) # 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, ) self.dgraph = Graph( "g1", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set([self.bb, self.cc, self.dd, self.ee]), edges=set( [self.bb_cc, self.cc_dd, self.dd_ee, self.ee_bb, self.bb_dd] ), ) # initialize profiler self.prof = cProfile.Profile() self.prof.enable() # print("\n<<<<--------") def tearDown(self): """ """ p = Stats(self.prof) p.sort_stats("cumtime") if self.verbose is True: p.dump_stats("profiles/test_graph.py.prof") p.print_stats() p.strip_dirs() # p.print_stats() # print("\n--------->>>") def test_id(self): return self.assertEqual(self.graph.id(), "g1") def test_from_edgeset(self): """""" eset = set([self.e1, self.e2, self.e3, self.e4]) g = Graph.from_edgeset(eset) self.assertEqual( BaseGraphOps.nodes(g), set([self.n1, self.n2, self.n3, self.n4]) ) self.assertEqual(BaseGraphOps.edges(g), eset) 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)) def test_adjmat_int(self): """""" mat = self.ugraph1.to_adjmat() self.assertEqual( mat, { ("b", "b"): 0, ("b", "e"): 0, ("b", "f"): 0, ("b", "a"): 0, ("e", "b"): 0, ("e", "e"): 0, ("e", "f"): 1, ("e", "a"): 1, ("f", "b"): 0, ("f", "e"): 1, ("f", "f"): 0, ("f", "a"): 1, ("a", "b"): 0, ("a", "e"): 1, ("a", "f"): 1, ("a", "a"): 0, }, ) def test_adjmat_bool(self): """""" mat = self.ugraph1.to_adjmat(vtype=bool) self.assertEqual( mat, { ("b", "b"): False, ("b", "e"): False, ("b", "f"): False, ("b", "a"): False, ("e", "b"): False, ("e", "e"): False, ("e", "f"): True, ("e", "a"): True, ("f", "b"): False, ("f", "e"): True, ("f", "f"): False, ("f", "a"): True, ("a", "b"): False, ("a", "e"): True, ("a", "f"): True, ("a", "a"): False, }, ) def test_transitive_closure_mat(self): """""" mat = self.ugraph1.transitive_closure_matrix() self.assertEqual( mat, { ("a", "b"): True, ("a", "e"): True, ("a", "f"): True, ("b", "a"): False, ("b", "e"): False, ("b", "f"): False, ("e", "a"): True, ("e", "b"): True, ("e", "f"): True, ("f", "a"): True, ("f", "b"): True, ("f", "e"): True, }, ) 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) def test_is_connected_false(self): """""" self.assertEqual(self.graph.is_connected(), False) def test_is_connected_true(self): """""" self.assertTrue(self.graph_2.is_connected()) def test_is_adjacent_of(self): self.assertTrue(self.graph_2.is_adjacent_of(self.e2, self.e3)) def test_is_neighbour_of_true(self): isneighbor = self.graph_2.is_neighbour_of(self.n2, self.n3) self.assertTrue(isneighbor) def test_is_neighbour_of_false(self): isneighbor = self.graph_2.is_neighbour_of(self.n2, self.n2) self.assertFalse(isneighbor) def test_neighbours_of(self): ndes = set( [n.id() for n in BaseGraphOps.neighbours_of(self.graph_2, self.n2)] ) self.assertEqual(ndes, set([self.n1.id(), self.n3.id()])) def test_nb_neighbours_of(self): ndes = BaseGraphAnalyzer.nb_neighbours_of(self.graph_2, self.n2) self.assertEqual(ndes, 2) def test__add__n(self): """""" n = Node("n646", {}) g = self.graph + n self.assertEqual( BaseGraphOps.nodes(g), set([self.n1, self.n2, self.n3, self.n4, n]) ) def test__add__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([e, self.e1, self.e2])) 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])) 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]) ) 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])) def test__sub__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, self.e1]), ) g = self.graph - gg self.assertEqual(BaseGraphOps.edges(g), set([])) self.assertEqual(BaseGraphOps.nodes(g), set([self.n3, self.n4])) def test_visit_graph_dfs_nb_component(self): "test visit graph dfs function" com = self.ugraph1.graph_props.nb_component com2 = self.ugraph2.graph_props.nb_component self.assertEqual(com, 2) self.assertEqual(com2, 1) def test_get_components(self): """""" comps = self.ugraph4.get_components() cs = list(comps) cs0ns = BaseGraphOps.nodes(cs[0]) cs0es = BaseGraphOps.edges(cs[0]) # cs1ns = BaseGraphOps.nodes(cs[1]) cs1es = BaseGraphOps.edges(cs[1]) # # compare graphs # first component u2nodes = set([self.a, self.b, self.e, self.f]) u2edges = set([self.ab, self.af, self.ae, self.be, self.ef]) # second component g2node = set([self.n1, self.n2, self.n3, self.n4]) g2edge = set([self.e1, self.e2, self.e3, self.e4]) # cond1 = u2nodes == cs0ns or u2nodes == cs1ns # cond2 = g2node == cs0ns or g2node == cs1ns # cond3 = u2edges == cs0es or u2edges == cs1es cond4 = g2edge == cs0es or g2edge == cs1es self.assertTrue(cond1) self.assertTrue(cond2) self.assertTrue(cond3) self.assertTrue(cond4) def test_visit_graph_dfs_cycles_false(self): "test visit graph dfs function" c3 = BaseGraphAnalyzer.has_cycles(self.ugraph3) self.assertFalse(c3) def test_visit_graph_dfs_cycles_true(self): "test visit graph dfs function" c3 = BaseGraphAnalyzer.has_cycles(self.ugraph2) self.assertTrue(c3)