Example #1
0
    def test_periodic(self):
        G = nx.grid_2d_graph(0, 0, periodic=True)
        assert_equal(dict(G.degree()), {})

        for m, n, H in [(2, 2, nx.cycle_graph(4)), (1, 7, nx.cycle_graph(7)),
                        (7, 1, nx.cycle_graph(7)),
                        (2, 5, nx.circular_ladder_graph(5)),
                        (5, 2, nx.circular_ladder_graph(5)),
                        (2, 4, nx.cubical_graph()),
                        (4, 2, nx.cubical_graph())]:
            G = nx.grid_2d_graph(m, n, periodic=True)
            assert_true(nx.could_be_isomorphic(G, H))
Example #2
0
    def test_periodic(self):
        G = nx.grid_2d_graph(0, 0, periodic=True)
        assert_equal(dict(G.degree()), {})

        for m, n, H in [(2, 2, nx.cycle_graph(4)), (1, 7, nx.cycle_graph(7)),
                        (7, 1, nx.cycle_graph(7)),
                        (2, 5, nx.circular_ladder_graph(5)),
                        (5, 2, nx.circular_ladder_graph(5)),
                        (2, 4, nx.cubical_graph()),
                        (4, 2, nx.cubical_graph())]:
            G = nx.grid_2d_graph(m, n, periodic=True)
            assert_true(nx.could_be_isomorphic(G, H))
Example #3
0
def display_topology(list_red,list_green):
    '''
    This function creates the topology showing the compliant routers in red and the con-compliant routers in red.
    Each router is labelled with its respective management IP. The spring nature of topology places the routers in
    the network in a random manner such that router 1 is connected to router 2, which in turn is connected to router 3.
    '''

    print "Trying to Generate Graph"
    G=nx.cubical_graph()
    pos=nx.spring_layout(G) # positions for all nodes

    # nodes
    nx.draw_networkx_nodes(G,pos,
                           nodelist=list_green,
                           node_color='g',
                           node_size=500)
    nx.draw_networkx_nodes(G,pos,
                           nodelist=list_red,
                           node_color='r',
                           node_size=500)
    nx.draw_networkx_edges(G,pos,
                       edgelist=[(1,2),(2,3)],
                       width=5,alpha=0.5,edge_color='b')


    labels= {}
    labels[1]=ip_list[0]
    labels[2]=ip_list[1]
    labels[3]=ip_list[2]

    nx.draw_networkx_labels(G,pos,labels,font_size=16)
    plt.savefig("/home/netman/graphs/photo.png")
    plt.show()
Example #4
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert list(nx.clustering(G).values()) == [0, 0, 0, 0, 0, 0, 0, 0]
     assert nx.clustering(G, 1) == 0
     assert list(nx.clustering(G, [1, 2]).values()) == [0, 0]
     assert nx.clustering(G, 1) == 0
     assert nx.clustering(G, [1, 2]) == {1: 0, 2: 0}
    def CutOffLessThan5D(self, pairs, dataTbl, cmc):

        # build matrix from pairs
        sna.loadGraphFromCsv(foMF + ".pairs", " ")
        sna.runMeasure("totaldegreeCentrality")

        print("\n display results .\n")
        sna.displayResults("totaldegreeCentrality")
        print("\n")
        try:
            print("Loading network from file\n(%s)" % (foMF + ".pairs"))
            self.graph = networkx.read_edgelist(foMF + ".pairs", delimiter=" ")
            self.graph.name = "Social Network"

            print("Attempt to draw graph.\n")
            G = networkx.cubical_graph()

            print("created G")
            networkx.draw(G)

            plt.show()  # display

            print("Degree centrality")
            d = degree_centrality(G)
            for v in G.nodes():
                print("%0.2d %5.3f" % (v, d[v]))

        except:
            print("Unable to open file:", filename)

        print("cutoff < 5 degrees end method.\n")
Example #6
0
def main() -> None:
    """Simple environment sandbox"""

    # Create a toy graph
    graph = nx.DiGraph()
    graph.add_edges_from([('a', 'b'), ('b', 'c')])
    print(graph)

    # create a random graph
    graph = nx.cubical_graph()

    graph = model.assign_random_labels(graph)

    vulnerabilities = actions_test.SAMPLE_VULNERABILITIES

    model.setup_yaml_serializer()

    # Define an environment from this graph
    env = model.Environment(network=graph,
                            vulnerability_library=vulnerabilities,
                            identifiers=actions_test.ENV_IDENTIFIERS)

    model_test.check_reserializing(env)

    model_test.check_reserializing(vulnerabilities)

    # Save the environment to file as Yaml
    with open('./simpleenv.yaml', 'w') as file:
        yaml.dump(env, file)
    print(yaml.dump(env))
Example #7
0
 def test_labels_and_colors(self):
     G = nx.cubical_graph()
     pos = nx.spring_layout(G)  # positions for all nodes
     # nodes
     nx.draw_networkx_nodes(G, pos,
                            nodelist=[0, 1, 2, 3],
                            node_color='r',
                            node_size=500,
                            alpha=0.8)
     nx.draw_networkx_nodes(G, pos,
                            nodelist=[4, 5, 6, 7],
                            node_color='b',
                            node_size=500,
                            alpha=0.8)
     # edges
     nx.draw_networkx_edges(G, pos, width=1.0, alpha=0.5)
     nx.draw_networkx_edges(G, pos,
                            edgelist=[(0, 1), (1, 2), (2, 3), (3, 0)],
                            width=8, alpha=0.5, edge_color='r')
     nx.draw_networkx_edges(G, pos,
                            edgelist=[(4, 5), (5, 6), (6, 7), (7, 4)],
                            width=8, alpha=0.5, edge_color='b')
     # some math labels
     labels = {}
     labels[0] = r'$a$'
     labels[1] = r'$b$'
     labels[2] = r'$c$'
     labels[3] = r'$d$'
     labels[4] = r'$\alpha$'
     labels[5] = r'$\beta$'
     labels[6] = r'$\gamma$'
     labels[7] = r'$\delta$'
     nx.draw_networkx_labels(G, pos, labels, font_size=16)
     plt.show()
    def CutOffLessThan5D(self, pairs, dataTbl, cmc):

        # build matrix from pairs
        sna.loadGraphFromCsv(foMF + ".pairs", " ")
        sna.runMeasure("totaldegreeCentrality")

        print("\n display results .\n")
        sna.displayResults("totaldegreeCentrality")
        print("\n")
        try:
            print("Loading network from file\n(%s)" % (foMF + ".pairs"))
            self.graph = networkx.read_edgelist(foMF + ".pairs", delimiter=" ")
            self.graph.name = "Social Network"

            print("Attempt to draw graph.\n")
            G = networkx.cubical_graph()

            print("created G")
            networkx.draw(G)

            plt.show()  # display

            print("Degree centrality")
            d = degree_centrality(G)
            for v in G.nodes():
                print("%0.2d %5.3f" % (v, d[v]))

        except:
            print("Unable to open file:", filename)

        print("cutoff < 5 degrees end method.\n")
Example #9
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert_equal(list(nx.triangles(G).values()), [0, 0, 0, 0, 0, 0, 0, 0])
     assert_equal(nx.triangles(G, 1), 0)
     assert_equal(list(nx.triangles(G, [1, 2]).values()), [0, 0])
     assert_equal(nx.triangles(G, 1), 0)
     assert_equal(nx.triangles(G, [1, 2]), {1: 0, 2: 0})
Example #10
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert_equal(list(nx.square_clustering(G).values()),
                  [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5])
     assert_equal(list(nx.square_clustering(G,[1,2]).values()),[0.5, 0.5])
     assert_equal(nx.square_clustering(G,[1])[1],0.5)
     assert_equal(nx.square_clustering(G,[1,2]),{1: 0.5, 2: 0.5})
def main():
    G = nx.cubical_graph()
    pos = nx.spring_layout(G)

    nx.draw_networkx_nodes(G, pos,
                            nodelist=[0,1,2,3],
                            node_color='r',
                            node_size=500,
                            alpha =0.8)
    nx.draw_networkx_nodes(G, pos,
                            nodelist=[4,5,6,7],
                            node_color='b',
                            node_size=500,
                            alpha=0.8)

    nx.draw_networkx_edges(G, pos, width=1.0, alpha=0.5)
    nx.draw_networkx_edges(G, pos,
                            edgelist=[(0,1),(1,2),(2,3),(3,0)],
                            width=8, alpha=0.5, edge_color='b')

    labels={}
    labels[0]=r'$a$'
    labels[1]=r'$b$'
    labels[2]=r'$c$'
    labels[3]=r'$d$'
    labels[4]=r'$\alpha$'
    labels[5]=r'$\beta$'
    labels[6]=r'$\gamma$'
    labels[7]=r'$\delta$'
    nx.draw_networkx_labels(G, pos,labels,font_size=16)
    nx.write_graphml(G, 'ex.graphml')
    plt.axis('off')
    plt.show()
Example #12
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert (list(nx.triangles(G).values()) == [0, 0, 0, 0, 0, 0, 0, 0])
     assert nx.triangles(G, 1) == 0
     assert list(nx.triangles(G, [1, 2]).values()) == [0, 0]
     assert nx.triangles(G, 1) == 0
     assert nx.triangles(G, [1, 2]) == {1: 0, 2: 0}
Example #13
0
 def test_labels_and_colors(self):
     G = nx.cubical_graph()
     pos = nx.spring_layout(G)  # positions for all nodes
     # nodes
     nx.draw_networkx_nodes(G,
                            pos,
                            nodelist=[0, 1, 2, 3],
                            node_color="r",
                            node_size=500,
                            alpha=0.75)
     nx.draw_networkx_nodes(
         G,
         pos,
         nodelist=[4, 5, 6, 7],
         node_color="b",
         node_size=500,
         alpha=[0.25, 0.5, 0.75, 1.0],
     )
     # edges
     nx.draw_networkx_edges(G, pos, width=1.0, alpha=0.5)
     nx.draw_networkx_edges(
         G,
         pos,
         edgelist=[(0, 1), (1, 2), (2, 3), (3, 0)],
         width=8,
         alpha=0.5,
         edge_color="r",
     )
     nx.draw_networkx_edges(
         G,
         pos,
         edgelist=[(4, 5), (5, 6), (6, 7), (7, 4)],
         width=8,
         alpha=0.5,
         edge_color="b",
     )
     nx.draw_networkx_edges(
         G,
         pos,
         edgelist=[(4, 5), (5, 6), (6, 7), (7, 4)],
         min_source_margin=0.5,
         min_target_margin=0.75,
         width=8,
         edge_color="b",
     )
     # some math labels
     labels = {}
     labels[0] = r"$a$"
     labels[1] = r"$b$"
     labels[2] = r"$c$"
     labels[3] = r"$d$"
     labels[4] = r"$\alpha$"
     labels[5] = r"$\beta$"
     labels[6] = r"$\gamma$"
     labels[7] = r"$\delta$"
     nx.draw_networkx_labels(G, pos, labels, font_size=16)
     nx.draw_networkx_edge_labels(G, pos, edge_labels=None, rotate=False)
     nx.draw_networkx_edge_labels(G, pos, edge_labels={(4, 5): "4-5"})
     plt.show()
Example #14
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert_equal(list(nx.clustering(G,weight='weight').values()),
                  [0, 0, 0, 0, 0, 0, 0, 0])
     assert_equal(nx.clustering(G,1),0)
     assert_equal(list(nx.clustering(G,[1,2],weight='weight').values()),[0, 0])
     assert_equal(nx.clustering(G,1,weight='weight'),0)
     assert_equal(nx.clustering(G,[1,2],weight='weight'),{1: 0, 2: 0})
Example #15
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert_equal(nx.triangles(G).values(),
                  [0, 0, 0, 0, 0, 0, 0, 0])
     assert_equal(nx.triangles(G,1),0)
     assert_equal(nx.triangles(G,[1,2]).values(),[0, 0])
     assert_equal(nx.triangles(G,1),0)
     assert_equal(nx.triangles(G,[1,2]),{1: 0, 2: 0})
Example #16
0
 def test_cubical_multigraph(self):
     g = networkx.cubical_graph(networkx.MultiGraph)
     out_graph = retworkx.networkx_converter(g)
     self.assertIsInstance(out_graph, retworkx.PyGraph)
     self.assertEqual(out_graph.nodes(), list(g.nodes))
     self.assertEqual(out_graph.weighted_edge_list(),
                      list(g.edges(data=True)))
     self.assertEqual(out_graph.multigraph, g.is_multigraph())
Example #17
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert (list(nx.square_clustering(G).values()) == [
         0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5
     ])
     assert list(nx.square_clustering(G, [1, 2]).values()) == [0.5, 0.5]
     assert nx.square_clustering(G, [1])[1] == 0.5
     assert nx.square_clustering(G, [1, 2]) == {1: 0.5, 2: 0.5}
Example #18
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert_equal(list(nx.clustering(G).values()),
                  [0, 0, 0, 0, 0, 0, 0, 0])
     assert_equal(nx.clustering(G,1),0)
     assert_equal(list(nx.clustering(G,[1,2]).values()),[0, 0])
     assert_equal(nx.clustering(G,1),0)
     assert_equal(nx.clustering(G,[1,2]),{1: 0, 2: 0})
Example #19
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert (list(nx.clustering(
         G, weight='weight').values()) == [0, 0, 0, 0, 0, 0, 0, 0])
     assert nx.clustering(G, 1) == 0
     assert list(nx.clustering(G, [1, 2],
                               weight='weight').values()) == [0, 0]
     assert nx.clustering(G, 1, weight='weight') == 0
     assert nx.clustering(G, [1, 2], weight='weight') == {1: 0, 2: 0}
Example #20
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)
 def test_cubical(self):
     expected = True
     g = nx.cubical_graph()
     for o in enumerate_dfs_ordering_naively(g):
         @spec_order(o)
         def func(g):
             return is_planar(g)
         actual = func(g)
         self.assertEqual(expected, actual)
Example #22
0
def generate_cubical_graph(sem):
    G = nx.cubical_graph()
    i = 1
    for (e1, e2) in G.edges():
        if i == 1:
            G.edges[e1, e2].update(R=0, no=i, sem=sem)
        else:
            G.edges[e1, e2].update(R=random.randint(1, 5), no=i, sem=0)
        i += 1
    return G
Example #23
0
def test_create_random_environment() -> None:
    graph = nx.cubical_graph()

    graph = model.assign_random_labels(graph)

    env = model.Environment(network=graph,
                            vulnerability_library=vulnerabilities,
                            identifiers=ENV_IDENTIFIERS)
    assert env
    pass
Example #24
0
def test_single_infected_node_initially() -> None:
    # create a random environment
    graph = nx.cubical_graph()
    graph = model.assign_random_labels(graph)
    env = model.Environment(network=graph,
                            vulnerability_library=dict([]),
                            identifiers=ENV_IDENTIFIERS)
    count = sum(1 for i in graph.nodes if env.get_node(i).agent_installed)
    assert count == 1
    return
Example #25
0
def draw_graph():
    G = nx.cubical_graph()
    plt.subplot(121)
    nx.draw(G)
    plt.subplot(122)
    fig, ax = plt.subplots()
    fig.savefig('foo.png')
    plt.show()
    nx.draw(G)
    nx.draw(G, pos=nx.circular_layout(G), node_color='r', edge_color='b')
    fig.savefig('foo.png')
    plt.show()
Example #26
0
def test_cartesian_product_classic():
    # test some classic product graphs
    P2 = nx.path_graph(2)
    P3 = nx.path_graph(3)
    # 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)))
 def test_is_distance_regular(self):
     assert_true(nx.is_distance_regular(nx.icosahedral_graph()))
     assert_true(nx.is_distance_regular(nx.petersen_graph()))
     assert_true(nx.is_distance_regular(nx.cubical_graph()))
     assert_true(nx.is_distance_regular(nx.complete_bipartite_graph(3,3)))
     assert_true(nx.is_distance_regular(nx.tetrahedral_graph()))
     assert_true(nx.is_distance_regular(nx.dodecahedral_graph()))
     assert_true(nx.is_distance_regular(nx.pappus_graph()))
     assert_true(nx.is_distance_regular(nx.heawood_graph()))
     assert_true(nx.is_distance_regular(nx.cycle_graph(3)))
     # no distance regular
     assert_false(nx.is_distance_regular(nx.path_graph(4)))
Example #28
0
def test_cartesian_product_classic():
    # test some classic product graphs
    P2 = nx.path_graph(2)
    P3 = nx.path_graph(3)
    # cube = 2-path X 2-path
    G = nx.cartesian_product(P2, P2)
    G = nx.cartesian_product(P2, G)
    assert_true(nx.is_isomorphic(G, nx.cubical_graph()))

    # 3x3 grid
    G = nx.cartesian_product(P3, P3)
    assert_true(nx.is_isomorphic(G, nx.grid_2d_graph(3, 3)))
 def test_is_distance_regular(self):
     assert_true(nx.is_distance_regular(nx.icosahedral_graph()))
     assert_true(nx.is_distance_regular(nx.petersen_graph()))
     assert_true(nx.is_distance_regular(nx.cubical_graph()))
     assert_true(nx.is_distance_regular(nx.complete_bipartite_graph(3, 3)))
     assert_true(nx.is_distance_regular(nx.tetrahedral_graph()))
     assert_true(nx.is_distance_regular(nx.dodecahedral_graph()))
     assert_true(nx.is_distance_regular(nx.pappus_graph()))
     assert_true(nx.is_distance_regular(nx.heawood_graph()))
     assert_true(nx.is_distance_regular(nx.cycle_graph(3)))
     # no distance regular
     assert_false(nx.is_distance_regular(nx.path_graph(4)))
Example #30
0
def test_tensor_product_classic_result():
    K2 = nx.complete_graph(2)
    G = nx.petersen_graph()
    G = nx.tensor_product(G, K2)
    assert_true(nx.is_isomorphic(G, nx.desargues_graph()))

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

    G = nx.tetrahedral_graph()
    G = nx.tensor_product(G, K2)
    assert_true(nx.is_isomorphic(G, nx.cubical_graph()))
Example #31
0
def test_tensor_product_classic_result():
    K2 = nx.complete_graph(2)
    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()))
Example #32
0
def test_environment_is_serializable() -> None:
    # create a random environment
    env = model.Environment(
        network=model.assign_random_labels(nx.cubical_graph()),
        version=model.VERSION_TAG,
        vulnerability_library=dict([]),
        identifiers=ENV_IDENTIFIERS,
        creationTime=datetime.utcnow(),
        lastModified=datetime.utcnow(),
    )
    # Dump the environment as Yaml
    _ = yaml.dump(env)
    assert True
Example #33
0
def test_yaml_serialization_environment() -> None:
    """Test Yaml serialization and deserialization for type Environment"""
    network = model.assign_random_labels(nx.cubical_graph())
    env = model.Environment(network=network,
                            vulnerability_library=vulnerabilities,
                            identifiers=model.infer_constants_from_network(
                                network, vulnerabilities))

    model.setup_yaml_serializer()

    serialized = yaml.dump(env)
    assert (len(serialized) > 100)

    check_reserializing(env)
Example #34
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert list(nx.square_clustering(G).values()) == [
         1 / 3,
         1 / 3,
         1 / 3,
         1 / 3,
         1 / 3,
         1 / 3,
         1 / 3,
         1 / 3,
     ]
     assert list(nx.square_clustering(G, [1, 2]).values()) == [1 / 3, 1 / 3]
     assert nx.square_clustering(G, [1])[1] == 1 / 3
     assert nx.square_clustering(G, [1, 2]) == {1: 1 / 3, 2: 1 / 3}
Example #35
0
def platonic(n):  # n must be 4, 6, 8, 12 or 20
    # returns the matrix for sp2 platonic solid, with alpha = 0 and beta = -1
    n_int = int(n)
    if n_int == 4:
        s = nx.tetrahedral_graph()
    elif n_int == 6:
        s = nx.cubical_graph()
    elif n_int == 8:
        s = nx.octahedral_graph()
    elif n_int == 12:
        s = nx.dodecahedral_graph()
    elif n_int == 20:
        s = nx.icosahedral_graph()
    else:
        print("n must be equal to 4, 6, 8, 12 or 20")

    M = -nx.adjacency_matrix(s)
    return M.todense()
Example #36
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)))
Example #37
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert nx.generalized_degree(G, 0) == {0: 3}
Example #38
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert nx.transitivity(G) == 0.0
Example #39
0
def generate_cubic():
    return nx.cubical_graph()
Example #40
0
import matplotlib.pyplot as plt

import networkx as nx

G=nx.cubical_graph()
pos=nx.spring_layout(G) # positions for all nodes
print(G)
# nodes
nx.draw_networkx_nodes(G,pos,
                       nodelist=[0,1,2,3],
                       node_color='r',
                       node_size=500,
                   alpha=0.8)
nx.draw_networkx_nodes(G,pos,
                       nodelist=[4,5,6,7],
                       node_color='b',
                       node_size=500,
                   alpha=0.8)

# edges
nx.draw_networkx_edges(G,pos,width=1.0,alpha=0.5)
nx.draw_networkx_edges(G,pos,
                       edgelist=[(0,1),(1,2),(2,3),(3,0)],
                       width=8,alpha=0.5,edge_color='r')
nx.draw_networkx_edges(G,pos,
                       edgelist=[(4,5),(5,6),(6,7),(7,4)],
                       width=8,alpha=0.5,edge_color='b')


# some math labels
labels={}
Example #41
0
# -*- coding: utf-8 -*-

import networkx as nx
import matplotlib.pylab as plt
from numpy.random import *
import colorsys

if __name__ == '__main__':
	sample = nx.cubical_graph()

	for i in xrange(0,50):
		sample.add_node(i)

	for i in xrange(0,50):
		sample.add_edge(i,poisson(lam=30))
	#print sample.edges()
	try: print nx.shortest_path(sample, 30, 0)
	except: print u"ぼっち"
    #graph layput
    #circular, random, shell, spring, spectral
    #ncolors = [colorsys.hsv_to_rgb(h / num_nodes, 1.0, 1.0)for h in range(num_nodes)]
	nx.draw(sample, pos=nx.spring_layout(sample), node_color='white',edge_color="g")

	plt.savefig("sample.png")
	nx.write_gexf(sample, "sample.gexf")
import networkx as nx
import matplotlib.pylab as plt
from plot_multigraph import plot_multigraph

graphs = [
  ("bull", nx.bull_graph()),
  ("chvatal", nx.chvatal_graph()),
  ("cubical", nx.cubical_graph()),
  ("desargues", nx.desargues_graph()),
  ("diamond", nx.diamond_graph()),
  ("dodecahedral", nx.dodecahedral_graph()),
  ("frucht", nx.frucht_graph()),
  ("heawood", nx.heawood_graph()),
  ("house", nx.house_graph()),
  ("house_x", nx.house_x_graph()),
  ("icosahedral", nx.icosahedral_graph()),
  ("krackhardt_kite", nx.krackhardt_kite_graph()),
  ("moebius_kantor", nx.moebius_kantor_graph()),
  ("octahedral", nx.octahedral_graph()),
  ("pappus", nx.pappus_graph()),
  ("petersen", nx.petersen_graph()),
  ("sedgewick_maze", nx.sedgewick_maze_graph()),
  ("tetrahedral", nx.tetrahedral_graph()),
  ("truncated_cube", nx.truncated_cube_graph()),
  ("truncated_tetrahedron", nx.truncated_tetrahedron_graph()),
]

plot_multigraph(graphs, 4, 5, node_size=50)
plt.savefig('graphs/small.png')

Example #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)))
Example #44
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))
Example #45
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert_equal(nx.transitivity(G),0.0)
Example #46
0
 def test_cubical(self):
     G = nx.cubical_graph()
     assert_equal(nx.generalized_degree(G,0), {0: 3})