Пример #1
0
 def test_null_graph(self):
     null = nx.null_graph()
     assert_equal(list(nx.edge_boundary(null, [])), [])
     assert_equal(list(nx.edge_boundary(null, [], [])), [])
     assert_equal(list(nx.edge_boundary(null, [1, 2, 3])), [])
     assert_equal(list(nx.edge_boundary(null, [1, 2, 3], [4, 5, 6])), [])
     assert_equal(list(nx.edge_boundary(null, [1, 2, 3], [3, 4, 5])), [])
Пример #2
0
def test_strong_product_null():
    null = nx.null_graph()
    empty10 = nx.empty_graph(10)
    K3 = nx.complete_graph(3)
    K10 = nx.complete_graph(10)
    P3 = nx.path_graph(3)
    P10 = nx.path_graph(10)
    # null graph
    G = nx.strong_product(null, null)
    assert_true(nx.is_isomorphic(G, null))
    # null_graph X anything = null_graph and v.v.
    G = nx.strong_product(null, empty10)
    assert_true(nx.is_isomorphic(G, null))
    G = nx.strong_product(null, K3)
    assert_true(nx.is_isomorphic(G, null))
    G = nx.strong_product(null, K10)
    assert_true(nx.is_isomorphic(G, null))
    G = nx.strong_product(null, P3)
    assert_true(nx.is_isomorphic(G, null))
    G = nx.strong_product(null, P10)
    assert_true(nx.is_isomorphic(G, null))
    G = nx.strong_product(empty10, null)
    assert_true(nx.is_isomorphic(G, null))
    G = nx.strong_product(K3, null)
    assert_true(nx.is_isomorphic(G, null))
    G = nx.strong_product(K10, null)
    assert_true(nx.is_isomorphic(G, null))
    G = nx.strong_product(P3, null)
    assert_true(nx.is_isomorphic(G, null))
    G = nx.strong_product(P10, null)
    assert_true(nx.is_isomorphic(G, null))
Пример #3
0
def layered_tree(width, height, branch_factor=3):
    """
        Creates layered tree that is circular to ensure the
        graph is connected
    """
    G = nx.null_graph()

    # Create the first layer
    for i in range(width):
        # Label nodes as tuples of form (layer, index)
        G.add_node((0, i))

    choices = np.arange(width)
    for layer in range(height - 1):
        # Generate the nodes for the next layer
        for i in range(width):
            G.add_node((layer + 1, i))

        # Add random edges
        for u in range(width):
            connections = np.random.choice(choices,
                                           size=branch_factor,
                                           replace=False)
            for v in connections:
                G.add_edge((layer, u), (layer + 1, v))

    # Create circular connection
    for u in range(width):
        connections = np.random.choice(choices,
                                       size=branch_factor,
                                       replace=False)
        for v in connections:
            G.add_edge((height - 1, u), (0, v))

    return G
def test_complement():
    null = nx.null_graph()
    empty1 = nx.empty_graph(1)
    empty10 = nx.empty_graph(10)
    K3 = nx.complete_graph(3)
    K5 = nx.complete_graph(5)
    K10 = nx.complete_graph(10)
    P2 = nx.path_graph(2)
    P3 = nx.path_graph(3)
    P5 = nx.path_graph(5)
    P10 = nx.path_graph(10)
    # complement of the complete graph is empty

    G = nx.complement(K3)
    assert nx.is_isomorphic(G, nx.empty_graph(3))
    G = nx.complement(K5)
    assert nx.is_isomorphic(G, nx.empty_graph(5))
    # for any G, G=complement(complement(G))
    P3cc = nx.complement(nx.complement(P3))
    assert nx.is_isomorphic(P3, P3cc)
    nullcc = nx.complement(nx.complement(null))
    assert nx.is_isomorphic(null, nullcc)
    b = nx.bull_graph()
    bcc = nx.complement(nx.complement(b))
    assert nx.is_isomorphic(b, bcc)
Пример #5
0
def self_repetition(G, n=1, linknode=0):
    new_graph = nx.null_graph()
    new_graph.add_node(0)
    for i in range(n):
        new_graph = nx.disjoint_union(new_graph, G.copy())
        new_graph.add_edge(0, i * len(G) + linknode + 1)
    return new_graph
Пример #6
0
 def test_null(self):
     dimacs = """\
     p cnf 0 0
     """
     graph=nx.null_graph()
     F = TseitinFormula(graph)
     self.assertCnfEqualsDimacs(F,dimacs)
Пример #7
0
def line(n):
  test_graph = nx.null_graph()
  for i in range(n):
    test_graph.add_node(i)
    if i > 0:
      test_graph.add_edge(i, i - 1)
  return test_graph
Пример #8
0
 def test_null(self):
     dimacs = """\
     p cnf 0 0
     """
     graph = nx.null_graph()
     F = TseitinFormula(graph)
     self.assertCnfEqualsDimacs(F, dimacs)
Пример #9
0
 def test_null_graph(self):
     null = nx.null_graph()
     assert_equal(list(nx.edge_boundary(null, [])), [])
     assert_equal(list(nx.edge_boundary(null, [], [])), [])
     assert_equal(list(nx.edge_boundary(null, [1, 2, 3])), [])
     assert_equal(list(nx.edge_boundary(null, [1, 2, 3], [4, 5, 6])), [])
     assert_equal(list(nx.edge_boundary(null, [1, 2, 3], [3, 4, 5])), [])
Пример #10
0
    def test_path_graph(self):
        p = nx.path_graph(0)
        assert is_isomorphic(p, nx.null_graph())

        p = nx.path_graph(1)
        assert is_isomorphic(p, nx.empty_graph(1))

        p = nx.path_graph(10)
        assert nx.is_connected(p)
        assert sorted(
            d for n, d in p.degree()) == [1, 1, 2, 2, 2, 2, 2, 2, 2, 2]
        assert p.order() - 1 == p.size()

        dp = nx.path_graph(3, create_using=nx.DiGraph)
        assert dp.has_edge(0, 1)
        assert not dp.has_edge(1, 0)

        mp = nx.path_graph(10, create_using=nx.MultiGraph)
        assert_edges_equal(mp.edges(), p.edges())

        G = nx.path_graph("abc")
        assert len(G) == 3
        assert G.size() == 2
        g = nx.path_graph("abc", nx.DiGraph)
        assert len(g) == 3
        assert g.size() == 2
        assert g.is_directed()
Пример #11
0
def sample_subgraph(G, target_size = 20, max_size = 24, start = None):
    subgraph = nx.null_graph()
    initial_node = start
    if not start or start not in G.nodes():
        initial_node = np.random.choice(list(G.nodes()))

    node_queue = queue.Queue()
    node_queue.put((initial_node, 1))
    prob_sum = 1
    seen = set()
    seen.add(initial_node)

    ## sample nodes
    while not node_queue.empty():
        new_node = node_queue.get()
        prob_sum -= new_node[1]
        roll = random.random()
        if roll > new_node[1]:
            continue
        subgraph.add_node(new_node[0])
        if len(subgraph) >= max_size:
            break
        for neighbor in set(G.neighbors(new_node[0])) - seen:
            seen.add(neighbor)
            select_prob = max(0, 1 - ((len(subgraph) + prob_sum) / target_size))
            node_queue.put((neighbor, select_prob))
            prob_sum += select_prob

    ## add edges
    for edge in set(permutations(list(subgraph.nodes()), 2)).intersection(set(G.edges())):
        subgraph.add_edge(*edge)

    return subgraph
Пример #12
0
    def test_wheel_graph(self):
        for n, G in [
            (0, nx.null_graph()),
            (1, nx.empty_graph(1)),
            (2, nx.path_graph(2)),
            (3, nx.complete_graph(3)),
            (4, nx.complete_graph(4)),
        ]:
            g = nx.wheel_graph(n)
            assert is_isomorphic(g, G)

        g = nx.wheel_graph(10)
        assert sorted(
            d for n, d in g.degree()) == [3, 3, 3, 3, 3, 3, 3, 3, 3, 9]

        pytest.raises(nx.NetworkXError,
                      nx.wheel_graph,
                      10,
                      create_using=nx.DiGraph)

        mg = nx.wheel_graph(10, create_using=nx.MultiGraph())
        assert_edges_equal(mg.edges(), g.edges())

        G = nx.wheel_graph("abc")
        assert len(G) == 3
        assert G.size() == 3
Пример #13
0
def test_cartesian_product_null():
    null=nx.null_graph()
    empty10=nx.empty_graph(10)
    K3=nx.complete_graph(3)
    K10=nx.complete_graph(10)
    P3=nx.path_graph(3)
    P10=nx.path_graph(10)
    # null graph
    G=cartesian_product(null,null)
    assert_true(nx.is_isomorphic(G,null))
    # null_graph X anything = null_graph and v.v.
    G=cartesian_product(null,empty10)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(null,K3)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(null,K10)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(null,P3)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(null,P10)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(empty10,null)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(K3,null)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(K10,null)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(P3,null)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(P10,null)
    assert_true(nx.is_isomorphic(G,null))
Пример #14
0
def __create_centroids(gen_fun,
                       centroids: Tuple[int, int],
                       nodes_per_centroid: Tuple[int, int],
                       centroid_connectivity: float,
                       centroid_extra: Dict[str, Any] = None,
                       n_nodes=None,
                       **kwargs):

    if centroid_extra:
        raise NotImplementedError()

    graphs = [nx.null_graph()]
    n_centroids = random.randint(*centroids)
    for _ in range(n_centroids):
        n_nodes_centroid = random.randint(*nodes_per_centroid)
        graphs.append(gen_fun(n_nodes_centroid, **kwargs))

    graph = nx.union_all(graphs,
                         rename=map(lambda i: str(i) + "-",
                                    range(len(graphs))))

    central_nodes = filter(lambda name: name.split("-")[1] == "0", graph)
    centroid_edges = itertools.combinations(central_nodes, 2)

    for node_1, node_2 in centroid_edges:
        if random.random() < centroid_connectivity:
            graph.add_edge(node_1, node_2)

    return graph
Пример #15
0
def classic_graphs():
    print("Balanced Tree")
    BG = nx.balanced_tree(3, 2)
    draw_graph(BG)
    print("Barbell Graph")
    BBG = nx.barbell_graph(3, 2)
    draw_graph(BBG)
    print("Complete Graph")
    CG = nx.complete_graph(10)
    draw_graph(CG)
    print("Complete Multipartite Graph")
    CMG = nx.complete_multipartite_graph(1, 2, 10)
    print([CMG.node[u]['block'] for u in CMG])
    print(CMG.edges(0))
    print(CMG.edges(2))
    print(CMG.edges(4))
    draw_graph(CMG)
    print("Circular Ladder Graph")
    CLG = nx.circular_ladder_graph(5)
    draw_graph(CLG)
    print("Dorogovtsev Goltsev Mendes Graph")
    DGMG = nx.dorogovtsev_goltsev_mendes_graph(3)
    draw_graph(DGMG)
    print("Empty Graph")
    EG = nx.empty_graph(5, create_using=nx.DiGraph())
    draw_graph(EG)
    print("Grid 2D Graph")
    G2DG = nx.grid_2d_graph(5, 6)
    draw_graph(G2DG)
    print("Grid Graph")
    GDG = nx.grid_graph(dim=[5, 2])
    draw_graph(GDG)
    print("Hypercube Graph")
    HG = nx.hypercube_graph(3)
    draw_graph(HG)
    print("Ladder Graph")
    LG = nx.ladder_graph(8)
    draw_graph(LG)
    print("Ladder Graph")
    LG = nx.ladder_graph(8)
    draw_graph(LG)
    print("Lollipop Graph")
    LPG = nx.lollipop_graph(n=6, m=4)
    draw_graph(LPG)
    print("Null Graph")
    NG = nx.null_graph()
    draw_graph(NG)
    print("Path Graph")
    PG = nx.path_graph(16)
    draw_graph(PG)
    print("Star Graph")
    SG = nx.star_graph(16)
    draw_graph(SG)
    print("Trivial Graph")
    TG = nx.trivial_graph()
    draw_graph(TG)
    print("Wheel Graph")
    WG = nx.wheel_graph(n=18)
    draw_graph(WG)
Пример #16
0
 def test_null_graph(self):
     """Tests that the null graph has empty node boundaries."""
     null = nx.null_graph()
     assert_equal(nx.node_boundary(null, []), set())
     assert_equal(nx.node_boundary(null, [], []), set())
     assert_equal(nx.node_boundary(null, [1, 2, 3]), set())
     assert_equal(nx.node_boundary(null, [1, 2, 3], [4, 5, 6]), set())
     assert_equal(nx.node_boundary(null, [1, 2, 3], [3, 4, 5]), set())
Пример #17
0
 def test_null_graph(self):
     """Tests that the null graph has empty node boundaries."""
     null = nx.null_graph()
     assert_equal(nx.node_boundary(null, []), set())
     assert_equal(nx.node_boundary(null, [], []), set())
     assert_equal(nx.node_boundary(null, [1, 2, 3]), set())
     assert_equal(nx.node_boundary(null, [1, 2, 3], [4, 5, 6]), set())
     assert_equal(nx.node_boundary(null, [1, 2, 3], [3, 4, 5]), set())
Пример #18
0
def triangle():
  test_graph = nx.null_graph()
  test_graph.add_node(0)
  test_graph.add_node(1)
  test_graph.add_node(2)
  test_graph.add_edge(0, 2)
  test_graph.add_edge(0, 1)
  test_graph.add_edge(1, 2)
  return test_graph
Пример #19
0
 def test_special_cases(self):
     for n, H in [
         (0, nx.null_graph()),
         (1, nx.path_graph(2)),
         (2, nx.cycle_graph(4)),
         (3, nx.cubical_graph()),
     ]:
         G = nx.hypercube_graph(n)
         assert nx.could_be_isomorphic(G, H)
Пример #20
0
 def test_empty_subgraph(self):
     # Subgraph of an empty graph is an empty graph. test 1 
     nullgraph=nx.null_graph()
     E5=nx.empty_graph(5)
     E10=nx.empty_graph(10)
     H=E10.subgraph([])
     assert_true(nx.is_isomorphic(H,nullgraph))
     H=E10.subgraph([1,2,3,4,5])
     assert_true(nx.is_isomorphic(H,E5))
Пример #21
0
 def test_empty_subgraph(self):
     # Subgraph of an empty graph is an empty graph. test 1
     nullgraph = nx.null_graph()
     E5 = nx.empty_graph(5)
     E10 = nx.empty_graph(10)
     H = E10.subgraph([])
     assert_true(nx.is_isomorphic(H, nullgraph))
     H = E10.subgraph([1, 2, 3, 4, 5])
     assert_true(nx.is_isomorphic(H, E5))
Пример #22
0
def test_strong_product():
    null=nx.null_graph()
    empty1=nx.empty_graph(1)
    empty10=nx.empty_graph(10)
    K2=nx.complete_graph(2)
    K3=nx.complete_graph(3)
    K5=nx.complete_graph(5)
    K10=nx.complete_graph(10)
    P2=nx.path_graph(2)
    P3=nx.path_graph(3)
    P5=nx.path_graph(5)
    P10=nx.path_graph(10)
    # null graph
    G=strong_product(null,null)
    assert_true(nx.is_isomorphic(G,null))
    # null_graph X anything = null_graph and v.v.
    G=strong_product(null,empty10)
    assert_true(nx.is_isomorphic(G,null))
    G=strong_product(null,K3)
    assert_true(nx.is_isomorphic(G,null))
    G=strong_product(null,K10)
    assert_true(nx.is_isomorphic(G,null))
    G=strong_product(null,P3)
    assert_true(nx.is_isomorphic(G,null))
    G=strong_product(null,P10)
    assert_true(nx.is_isomorphic(G,null))
    G=strong_product(empty10,null)
    assert_true(nx.is_isomorphic(G,null))
    G=strong_product(K3,null)
    assert_true(nx.is_isomorphic(G,null))
    G=strong_product(K10,null)
    assert_true(nx.is_isomorphic(G,null))
    G=strong_product(P3,null)
    assert_true(nx.is_isomorphic(G,null))
    G=strong_product(P10,null)
    assert_true(nx.is_isomorphic(G,null))

    G=strong_product(P5,K3)
    assert_equal(nx.number_of_nodes(G),5*3)
    G=strong_product(K3,K5)
    assert_equal(nx.number_of_nodes(G),3*5)

    #No classic easily found classic results for strong product

    G = nx.erdos_renyi_graph(10,2/10.)
    H = nx.erdos_renyi_graph(10,2/10.)
    GH = strong_product(G,H)

    for (u_G,u_H) in GH.nodes_iter():
        for (v_G,v_H) in GH.nodes_iter():
            if (u_G==v_G and H.has_edge(u_H,v_H)) or \
               (u_H==v_H and G.has_edge(u_G,v_G)) or \
               (G.has_edge(u_G,v_G) and H.has_edge(u_H,v_H)):
                assert_true(GH.has_edge((u_G,u_H),(v_G,v_H)))
            else:
                assert_true(not GH.has_edge((u_G,u_H),(v_G,v_H)))
Пример #23
0
 def setUp(self):
     self.null = nx.null_graph()
     self.P1 = cnlti(nx.path_graph(1), first_label=1)
     self.P3 = cnlti(nx.path_graph(3), first_label=1)
     self.P10 = cnlti(nx.path_graph(10), first_label=1)
     self.K1 = cnlti(nx.complete_graph(1), first_label=1)
     self.K3 = cnlti(nx.complete_graph(3), first_label=1)
     self.K4 = cnlti(nx.complete_graph(4), first_label=1)
     self.K5 = cnlti(nx.complete_graph(5), first_label=1)
     self.K10 = cnlti(nx.complete_graph(10), first_label=1)
     self.G = nx.Graph
Пример #24
0
 def setup_class(cls):
     cls.null = nx.null_graph()
     cls.P1 = cnlti(nx.path_graph(1), first_label=1)
     cls.P3 = cnlti(nx.path_graph(3), first_label=1)
     cls.P10 = cnlti(nx.path_graph(10), first_label=1)
     cls.K1 = cnlti(nx.complete_graph(1), first_label=1)
     cls.K3 = cnlti(nx.complete_graph(3), first_label=1)
     cls.K4 = cnlti(nx.complete_graph(4), first_label=1)
     cls.K5 = cnlti(nx.complete_graph(5), first_label=1)
     cls.K10 = cnlti(nx.complete_graph(10), first_label=1)
     cls.G = nx.Graph
Пример #25
0
 def setUp(self):
     self.null=nx.null_graph()
     self.P1=cnlti(nx.path_graph(1),first_label=1)
     self.P3=cnlti(nx.path_graph(3),first_label=1)
     self.P10=cnlti(nx.path_graph(10),first_label=1)
     self.K1=cnlti(nx.complete_graph(1),first_label=1)
     self.K3=cnlti(nx.complete_graph(3),first_label=1)
     self.K4=cnlti(nx.complete_graph(4),first_label=1)
     self.K5=cnlti(nx.complete_graph(5),first_label=1)
     self.K10=cnlti(nx.complete_graph(10),first_label=1)
     self.G=nx.Graph
Пример #26
0
 def test_write_path(self):
     # On Windows, we can't reopen a file that is open
     # So, for test we get a valid name from tempfile but close it.
     with tempfile.NamedTemporaryFile() as f:
         fullfilename = f.name
     # file should be closed now, so write_sparse6 can open it
     nx.write_sparse6(nx.null_graph(), fullfilename)
     fh = open(fullfilename, mode='rb')
     assert fh.read() == b'>>sparse6<<:?\n'
     fh.close()
     import os
     os.remove(fullfilename)
Пример #27
0
 def test_write_path(self):
     # On Windows, we can't reopen a file that is open
     # So, for test we get a valid name from tempfile but close it.
     with tempfile.NamedTemporaryFile() as f:
         fullfilename = f.name
     # file should be closed now, so write_sparse6 can open it
     nx.write_sparse6(nx.null_graph(), fullfilename)
     fh = open(fullfilename, mode='rb')
     self.assertEqual(fh.read(), b'>>sparse6<<:?\n')
     fh.close()
     import os
     os.remove(fullfilename)
Пример #28
0
    def test_complete_bipartite_graph(self):
        G = complete_bipartite_graph(0, 0)
        assert nx.is_isomorphic(G, nx.null_graph())

        for i in [1, 5]:
            G = complete_bipartite_graph(i, 0)
            assert nx.is_isomorphic(G, nx.empty_graph(i))
            G = complete_bipartite_graph(0, i)
            assert nx.is_isomorphic(G, nx.empty_graph(i))

        G = complete_bipartite_graph(2, 2)
        assert nx.is_isomorphic(G, nx.cycle_graph(4))

        G = complete_bipartite_graph(1, 5)
        assert nx.is_isomorphic(G, nx.star_graph(5))

        G = complete_bipartite_graph(5, 1)
        assert nx.is_isomorphic(G, nx.star_graph(5))

        # complete_bipartite_graph(m1,m2) is a connected graph with
        # m1+m2 nodes and  m1*m2 edges
        for m1, m2 in [(5, 11), (7, 3)]:
            G = complete_bipartite_graph(m1, m2)
            assert nx.number_of_nodes(G) == m1 + m2
            assert nx.number_of_edges(G) == m1 * m2

        pytest.raises(nx.NetworkXError, complete_bipartite_graph,
                      7, 3, create_using=nx.DiGraph)
        pytest.raises(nx.NetworkXError, complete_bipartite_graph,
                      7, 3, create_using=nx.DiGraph)
        pytest.raises(nx.NetworkXError, complete_bipartite_graph,
                      7, 3, create_using=nx.MultiDiGraph)

        mG = complete_bipartite_graph(7, 3, create_using=nx.MultiGraph)
        assert mG.is_multigraph()
        assert sorted(mG.edges()) == sorted(G.edges())

        mG = complete_bipartite_graph(7, 3, create_using=nx.MultiGraph)
        assert mG.is_multigraph()
        assert sorted(mG.edges()) == sorted(G.edges())

        mG = complete_bipartite_graph(7, 3)  # default to Graph
        assert sorted(mG.edges()) == sorted(G.edges())
        assert not mG.is_multigraph()
        assert not mG.is_directed()

        # specify nodes rather than number of nodes
        G = complete_bipartite_graph([1, 2], ['a', 'b'])
        has_edges = G.has_edge(1, 'a') & G.has_edge(1, 'b') &\
            G.has_edge(2, 'a') & G.has_edge(2, 'b')
        assert has_edges
        assert G.size() == 4
Пример #29
0
def square():
  test_graph = nx.null_graph()
  test_graph.add_node(0)
  test_graph.add_node(1)
  test_graph.add_node(2)
  test_graph.add_node(3)
  test_graph.add_edge(0, 1)
  test_graph.add_edge(0, 2)
  test_graph.add_edge(0, 3)
  test_graph.add_edge(1, 2)
  test_graph.add_edge(1, 3)
  test_graph.add_edge(2, 3)
  return test_graph
Пример #30
0
def generate_trees(n = 20):
    sequence = np.random.permutation(n)

    a = AVL_Tree()
    b = BST_Tree()
    a_root = None
    b_root = None

    for i in sequence:
        a_root = a.insert(a_root, i)
        b_root = a.insert(b_root, i)

    avl_graph = nx.null_graph()
    avl_queue = queue.Queue()
    avl_queue.put(a_root)
    print(a_root.val)
    while not avl_queue.empty():
        current = avl_queue.get()
        if current.left:
            avl_graph.add_edge(current.val, current.left.val)
            avl_queue.put(current.left)
        if current.right:
            avl_graph.add_edge(current.val, current.right.val)
            avl_queue.put(current.right)

    bst_graph = nx.null_graph()
    bst_queue = queue.Queue()
    bst_queue.put(b_root)
    print(b_root.val)
    while not bst_queue.empty():
        current = bst_queue.get()
        if current.left:
            bst_graph.add_edge(current.val, current.left.val)
            bst_queue.put(current.left)
        if current.right:
            bst_graph.add_edge(current.val, current.right.val)
            bst_queue.put(current.right)

    return (avl_graph, bst_graph)
Пример #31
0
def dblp():
    name_dict = {}
    nodes = open('authorDict.txt', 'r').readlines()
    count = 0
    for node in nodes:
        name_dict[count] = node.replace("\n", "")
        count += 1

    G = nx.null_graph()
    edges = open('dblp_edges.txt', 'r').readlines()
    for edge in edges:
        temp = edge.replace("\n", "").split()
        G.add_edge(name_dict[int(temp[0])], name_dict[int(temp[1])])

    return G
Пример #32
0
def ladder_tree(width, height, branch_factor=2):
    """
        Similar to layered tree except each layer is 
        connected horazontally and cicularly like that 
        of a rung in a cicular ladder
    """
    G = nx.null_graph()

    # Create the first layer
    for i in range(width):
        # Label nodes as tuples of form (layer, index)
        G.add_node((0, i))

        # Connect the 'rung' of the ladder
        if (i > 0):
            G.add_edge((0, i), (0, i - 1))

    # Add cicular edge
    G.add_edge((0, 0), (0, width - 1))

    choices = np.arange(width)
    for layer in range(height - 1):
        # Generate the next layer
        for i in range(width):
            G.add_node((layer + 1, i))
            # Connect the 'rung' of the ladder
            if (i > 0):
                G.add_edge((layer + 1, i), (layer + 1, i - 1))
        # Add cicular edge
        G.add_edge((layer + 1, 0), (layer + 1, width - 1))

        # Add random edges
        for u in range(width):
            connections = np.random.choice(choices,
                                           size=branch_factor,
                                           replace=False)
            for v in connections:
                G.add_edge((layer, u), (layer + 1, v))

    # Create circular connection for entire ladder
    for u in range(width):
        connections = np.random.choice(choices,
                                       size=branch_factor,
                                       replace=False)
        for v in connections:
            G.add_edge((height - 1, u), (0, v))

    return G
Пример #33
0
 def test_subgraph_nbunch(self):
     nullgraph = nx.null_graph()
     K1 = nx.complete_graph(1)
     K3 = nx.complete_graph(3)
     K5 = nx.complete_graph(5)
     # Test G.subgraph(nbunch), where nbunch is a single node
     H = K5.subgraph(1)
     assert_true(nx.is_isomorphic(H, K1))
     # Test G.subgraph(nbunch), where nbunch is a set
     H = K5.subgraph(set([1]))
     assert_true(nx.is_isomorphic(H, K1))
     # Test G.subgraph(nbunch), where nbunch is an iterator
     H = K5.subgraph(iter(K3))
     assert_true(nx.is_isomorphic(H, K3))
     # Test G.subgraph(nbunch), where nbunch is another graph
     H = K5.subgraph(K3)
     assert_true(nx.is_isomorphic(H, K3))
     H = K5.subgraph([9])
     assert_true(nx.is_isomorphic(H, nullgraph))
Пример #34
0
 def test_subgraph_nbunch(self):
     nullgraph=nx.null_graph()
     K1=nx.complete_graph(1)
     K3=nx.complete_graph(3)
     K5=nx.complete_graph(5)
     # Test G.subgraph(nbunch), where nbunch is a single node
     H=K5.subgraph(1)
     assert_true(nx.is_isomorphic(H,K1))
     # Test G.subgraph(nbunch), where nbunch is a set
     H=K5.subgraph(set([1]))
     assert_true(nx.is_isomorphic(H,K1))
     # Test G.subgraph(nbunch), where nbunch is an iterator
     H=K5.subgraph(iter(K3))
     assert_true(nx.is_isomorphic(H,K3))
     # Test G.subgraph(nbunch), where nbunch is another graph
     H=K5.subgraph(K3)
     assert_true(nx.is_isomorphic(H,K3))
     H=K5.subgraph([9])
     assert_true(nx.is_isomorphic(H,nullgraph))
Пример #35
0
def k_length_path(G, u, k):
    try:
        bfs_result = bfs.bfs(G, u, k)
        current_node = bfs_result[k-1][0]
        k_path = nx.null_graph()
        k_path.add_node(current_node)
        for i in range(k-2, -1, -1):
            current_neighbors = list(nx.neighbors(G, current_node))
            for node in bfs_result[i]:
                if(current_neighbors.count(node) == 1):
                    k_path.add_node(node, name=node)
                    k_path.add_edge(current_node, node)
                    current_node = node 
                    break
                else:
                    continue
        nx.draw(k_path, with_labels=True)
        plt.show()
    except:
        sys.exit("Invalid Input - KLP")
Пример #36
0
class Topology:
    G = nx.null_graph()

    def __init__(self, gmlFilename=None):
        if gmlFilename != None:
            self.G.read_graphml(gmlFilename)

    def write_to_file(self, filename):
        nx.write_graphml(self.G, filename)

    def add_query(self, trace, verbose=False):
        antecedent = None
        for hop in trace.hops():
            currentVertex = hop[1]['ip']
            self.G.add_node(currentVertex)

            if antecedent != None:
                self.G.add_edge(antecedent, currentVertex)

            antecedent = currentVertex
Пример #37
0
 def test_null_graph(self):
     G = nx.null_graph()
     result = BytesIO()
     nx.write_sparse6(G, result)
     self.assertEqual(result.getvalue(), b'>>sparse6<<:?\n')
Пример #38
0
 def test_complete_0_partite_graph(self):
     """Tests that the complete 0-partite graph is the null graph."""
     G = nx.complete_multipartite_graph()
     H = nx.null_graph()
     assert_nodes_equal(G, H)
     assert_edges_equal(G.edges(), H.edges())
Пример #39
0
 def test_null_graph(self):
     G = nx.null_graph()
     eq_(len(max_clique(G)), 0)
Пример #40
0
 def test_write_path(self):
     with tempfile.NamedTemporaryFile() as f:
         nx.write_sparse6(nx.null_graph(), f.name)
         self.assertEqual(f.read(), b'>>sparse6<<:?\n')
Пример #41
0
 def test_null_graph(self):
     G = nx.null_graph()
     ground_truth = set()
     self._check_communities(G, ground_truth)
Пример #42
0
 def test_null_graph(self):
     result = BytesIO()
     nx.write_graph6(nx.null_graph(), result)
     self.assertEqual(result.getvalue(), b'>>graph6<<?\n')
Пример #43
0
def test_cartesian_product():
    null=nx.null_graph()
    empty1=nx.empty_graph(1)
    empty10=nx.empty_graph(10)
    K3=nx.complete_graph(3)
    K5=nx.complete_graph(5)
    K10=nx.complete_graph(10)
    P2=nx.path_graph(2)
    P3=nx.path_graph(3)
    P5=nx.path_graph(5)
    P10=nx.path_graph(10)
    # null graph
    G=cartesian_product(null,null)
    assert_true(nx.is_isomorphic(G,null))
    # null_graph X anything = null_graph and v.v.
    G=cartesian_product(null,empty10)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(null,K3)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(null,K10)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(null,P3)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(null,P10)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(empty10,null)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(K3,null)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(K10,null)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(P3,null)
    assert_true(nx.is_isomorphic(G,null))
    G=cartesian_product(P10,null)
    assert_true(nx.is_isomorphic(G,null))

    # order(GXH)=order(G)*order(H)
    G=cartesian_product(P5,K3)
    assert_equal(nx.number_of_nodes(G),5*3)
    assert_equal(nx.number_of_edges(G),
                 nx.number_of_edges(P5)*nx.number_of_nodes(K3)+
                 nx.number_of_edges(K3)*nx.number_of_nodes(P5))
    G=cartesian_product(K3,K5)
    assert_equal(nx.number_of_nodes(G),3*5)
    assert_equal(nx.number_of_edges(G),
                 nx.number_of_edges(K5)*nx.number_of_nodes(K3)+
                 nx.number_of_edges(K3)*nx.number_of_nodes(K5))

    # test some classic product graphs
    # cube = 2-path X 2-path
    G=cartesian_product(P2,P2)
    G=cartesian_product(P2,G)
    assert_true(nx.is_isomorphic(G,nx.cubical_graph()))

    # 3x3 grid
    G=cartesian_product(P3,P3)
    assert_true(nx.is_isomorphic(G,nx.grid_2d_graph(3,3)))

    G = nx.erdos_renyi_graph(10,2/10.)
    H = nx.erdos_renyi_graph(10,2/10.)
    GH = cartesian_product(G,H)

    for (u_G,u_H) in GH.nodes_iter():
        for (v_G,v_H) in GH.nodes_iter():
            if (u_G==v_G and H.has_edge(u_H,v_H)) or \
               (u_H==v_H and G.has_edge(u_G,v_G)):
                assert_true(GH.has_edge((u_G,u_H),(v_G,v_H)))
            else:
                assert_true(not GH.has_edge((u_G,u_H),(v_G,v_H)))
Пример #44
0
 def setUp(self):
     self.null=nx.null_graph()
     self.P10=cnlti(nx.path_graph(10),first_label=1)
     self.K10=cnlti(nx.complete_graph(10),first_label=1)
Пример #45
0
 def test_special_cases(self):
     for n, H in [(0, nx.null_graph()), (1, nx.path_graph(2)),
                  (2, nx.cycle_graph(4)), (3, nx.cubical_graph())]:
         G = nx.hypercube_graph(n)
         assert_true(nx.could_be_isomorphic(G, H))
Пример #46
0
 def test_null(self):
     null = nx.null_graph()
     assert_equal(list(null.degree()), [])
     assert_equal(dict(null.degree()), {})
Пример #47
0
 def test_null_graph(self):
     G = nx.null_graph()
     assert_equal(len(max_clique(G)), 0)
Пример #48
0
 def test_null_graph(self):
     nx.average_shortest_path_length(nx.null_graph())
Пример #49
0
 def test_write_path(self):
     with tempfile.NamedTemporaryFile() as f:
         g6.write_graph6_file(nx.null_graph(), f)
         f.seek(0)
         self.assertEqual(f.read(), b'>>graph6<<?\n')
Пример #50
0
 def test_null_subgraph(self):
     # Subgraph of a null graph is a null graph
     nullgraph = nx.null_graph()
     G = nx.null_graph()
     H = G.subgraph([])
     assert_true(nx.is_isomorphic(H, nullgraph))
Пример #51
0
 def test_result_null_graph(self):
     assert (calc_and_compare(NX.null_graph()))
Пример #52
0
 def test_null_graph(self):
     nx.to_prufer_sequence(nx.null_graph())
Пример #53
0
 def test_null_graph(self):
     G = nx.null_graph()
     assert len(max_clique(G)) == 0
Пример #54
0
def test_tensor_product():
    null=nx.null_graph()
    empty1=nx.empty_graph(1)
    empty10=nx.empty_graph(10)
    K2=nx.complete_graph(2)
    K3=nx.complete_graph(3)
    K5=nx.complete_graph(5)
    K10=nx.complete_graph(10)
    P2=nx.path_graph(2)
    P3=nx.path_graph(3)
    P5=nx.path_graph(5)
    P10=nx.path_graph(10)
    # null graph
    G=tensor_product(null,null)
    assert_true(nx.is_isomorphic(G,null))
    # null_graph X anything = null_graph and v.v.
    G=tensor_product(null,empty10)
    assert_true(nx.is_isomorphic(G,null))
    G=tensor_product(null,K3)
    assert_true(nx.is_isomorphic(G,null))
    G=tensor_product(null,K10)
    assert_true(nx.is_isomorphic(G,null))
    G=tensor_product(null,P3)
    assert_true(nx.is_isomorphic(G,null))
    G=tensor_product(null,P10)
    assert_true(nx.is_isomorphic(G,null))
    G=tensor_product(empty10,null)
    assert_true(nx.is_isomorphic(G,null))
    G=tensor_product(K3,null)
    assert_true(nx.is_isomorphic(G,null))
    G=tensor_product(K10,null)
    assert_true(nx.is_isomorphic(G,null))
    G=tensor_product(P3,null)
    assert_true(nx.is_isomorphic(G,null))
    G=tensor_product(P10,null)
    assert_true(nx.is_isomorphic(G,null))

    G=tensor_product(P5,K3)
    assert_equal(nx.number_of_nodes(G),5*3)
    G=tensor_product(K3,K5)
    assert_equal(nx.number_of_nodes(G),3*5)

    G = nx.petersen_graph()
    G = tensor_product(G,K2)
    assert_true(nx.is_isomorphic(G,nx.desargues_graph()))

    G = nx.cycle_graph(5)
    G = tensor_product(G,K2)
    assert_true(nx.is_isomorphic(G,nx.cycle_graph(10)))

    G = nx.tetrahedral_graph()
    G = tensor_product(G,K2)
    assert_true(nx.is_isomorphic(G,nx.cubical_graph()))

    G = nx.erdos_renyi_graph(10,2/10.)
    H = nx.erdos_renyi_graph(10,2/10.)
    GH = tensor_product(G,H)

    for (u_G,u_H) in GH.nodes_iter():
        for (v_G,v_H) in GH.nodes_iter():
            if H.has_edge(u_H,v_H) and G.has_edge(u_G,v_G):
                assert_true(GH.has_edge((u_G,u_H),(v_G,v_H)))
            else:
                assert_true(not GH.has_edge((u_G,u_H),(v_G,v_H)))