Exemple #1
0
    def setUp(self):
        # G is the example graph in Figure 1 from Batagelj and
        # Zaversnik's paper titled An O(m) Algorithm for Cores
        # Decomposition of Networks, 2003,
        # http://arXiv.org/abs/cs/0310049.  With nodes labeled as
        # shown, the 3-core is given by nodes 1-8, the 2-core by nodes
        # 9-16, the 1-core by nodes 17-20 and node 21 is in the
        # 0-core.
        t1 = nx.convert_node_labels_to_integers(nx.tetrahedral_graph(), 1)
        t2 = nx.convert_node_labels_to_integers(t1, 5)
        G = nx.union(t1, t2)
        G.add_edges_from([(3, 7), (2, 11), (11, 5), (11, 12), (5, 12),
                          (12, 19), (12, 18), (3, 9), (7, 9), (7, 10),
                          (9, 10), (9, 20), (17, 13), (13, 14), (14, 15),
                          (15, 16), (16, 13)])
        G.add_node(21)
        self.G = G

        # Create the graph H resulting from the degree sequence
        # [0, 1, 2, 2, 2, 2, 3] when using the Havel-Hakimi algorithm.

        degseq = [0, 1, 2, 2, 2, 2, 3]
        H = nx.havel_hakimi_graph(degseq)
        mapping = {6: 0, 0: 1, 4: 3, 5: 6, 3: 4, 1: 2, 2: 5}
        self.H = nx.relabel_nodes(H, mapping)
def generate_simple_graph(sfunction, N, avg_degree):
    """generate a simple random graph with sfunction degree sequence"""

    graphical_deg_seq = False
    is_connected_graph = False
    while is_connected_graph == False:
        while graphical_deg_seq == False:
            seq = sfunction(N, avg_degree, seqtype="simple_degree")
            graphical_deg_seq = nx.is_valid_degree_sequence(seq)
        G = nx.havel_hakimi_graph(seq)
        G.remove_edges_from(G.selfloop_edges())

        if not nx.is_connected(G):
            try:
                connect_simple_graph(G)
                is_connected_graph = True
                randomize_graph(G)
            except (IndexError):
                is_connected_graph = False

        if not nx.is_connected(G):
            try:
                connect_simple_graph(G)
                is_connected_graph = True

            except (IndexError):
                is_connected_graph = False
                graphical_deg_seq = False

    return G
Exemple #3
0
def test_graph_by_deg_sequence():
        dataname = "polbooks"               # (105,441)        
    #    dataname = "polblogs"               # (1224,16715) 
        dataname = "as20graph"              # (6474,12572)        config_model: 11500 edges (parallel edges, selfloops removed)
    #    dataname = "wiki-Vote"              # (7115,100762)   
    #    dataname = "ca-HepPh"               # (12006,118489)     
        dataname = "ca-AstroPh"             # (18771,198050)      config_model: 197009 edges  
        dataname = "com_amazon_ungraph"     # (334863,925872)      config_model: 925842 edges (9s)
    #    dataname = "com_dblp_ungraph"       # (317080,1049866)     
        dataname = "com_youtube_ungraph"    # (1134890,2987624)    config_model: 2962400 edges (32s) (7GB mem), havel_hakimi: 2987624 (21s)
        
        print "dataname =", dataname

        filename = "../_data/" + dataname + ".gr"       
        
        print "filename =", filename
        
        G = nx.read_edgelist(filename, '#', '\t', None, nodetype=int)
        print "#nodes =", G.number_of_nodes()
        print "#edges =", G.number_of_edges()
        
        deg_seq = list(G.degree().itervalues())
        start = time.clock()
#        G2 = nx.configuration_model(deg_seq, None, None)
        G2 = nx.havel_hakimi_graph(deg_seq, None)
        print "configuration_model - DONE, elapsed", time.clock() - start
        
        # remove parallel edges and selfloops
        G2 = nx.Graph(G2)
        G2.remove_edges_from(G2.selfloop_edges())
        print "#nodes =", G2.number_of_nodes()
        print "#edges =", G2.number_of_edges()
Exemple #4
0
def test2():
    deg = [3, 2, 2, 1, 0]
    G = nx.havel_hakimi_graph(deg)
    # Add self loops
    for node in G.nodes():
        G.add_edge(node, node)
    Ln = nx.normalized_laplacian(G)
    print Ln
    np.set_printoptions(4)
    print repr(Ln)
def test_havel_hakimi_construction():
    G = nx.havel_hakimi_graph([])
    assert_equal(len(G), 0)

    z = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
    assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z)
    z = ["A", 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
    assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z)

    z = [5, 4, 3, 3, 3, 2, 2, 2]
    G = nx.havel_hakimi_graph(z)
    G = nx.configuration_model(z)
    z = [6, 5, 4, 4, 2, 1, 1, 1]
    assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z)

    z = [10, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2]

    G = nx.havel_hakimi_graph(z)

    assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z,
                  create_using=nx.DiGraph())
def test_havel_hakimi_construction():
    G = nx.havel_hakimi_graph([])
    assert len(G) == 0

    z = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
    pytest.raises(nx.NetworkXError, nx.havel_hakimi_graph, z)
    z = ["A", 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
    pytest.raises(nx.NetworkXError, nx.havel_hakimi_graph, z)

    z = [5, 4, 3, 3, 3, 2, 2, 2]
    G = nx.havel_hakimi_graph(z)
    G = nx.configuration_model(z)
    z = [6, 5, 4, 4, 2, 1, 1, 1]
    pytest.raises(nx.NetworkXError, nx.havel_hakimi_graph, z)

    z = [10, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2]

    G = nx.havel_hakimi_graph(z)

    pytest.raises(nx.NetworkXError,
                  nx.havel_hakimi_graph,
                  z,
                  create_using=nx.DiGraph())
Exemple #7
0
def DegreeSequence(deg_sequence):
    """
    Returns a graph with the given degree sequence. Raises a NetworkX
    error if the proposed degree sequence cannot be that of a graph.

    Graph returned is the one returned by the Havel-Hakimi algorithm,
    which constructs a simple graph by connecting vertices of highest
    degree to other vertices of highest degree, resorting the remaining
    vertices by degree and repeating the process. See Theorem 1.4 in
    [1].

    INPUT:


    -  ``deg_sequence`` - a list of integers with each
       entry corresponding to the degree of a different vertex.


    EXAMPLES::

        sage: G = graphs.DegreeSequence([3,3,3,3])
        sage: G.edges(labels=False)
        [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
        sage: G.show()  # long time

    ::

        sage: G = graphs.DegreeSequence([3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3])
        sage: G.show()  # long time

    ::

        sage: G = graphs.DegreeSequence([4,4,4,4,4,4,4,4])
        sage: G.show()  # long time

    ::

        sage: G = graphs.DegreeSequence([1,2,3,4,3,4,3,2,3,2,1])
        sage: G.show()  # long time

    REFERENCE:

    - [1] Chartrand, G. and Lesniak, L. Graphs and Digraphs.
      Chapman and Hall/CRC, 1996.
    """
    import networkx
    return graph.Graph(networkx.havel_hakimi_graph([int(i) for i in deg_sequence]))
Exemple #8
0
def degree_sequence_graphs():
    print("Degree sequence")
    print("Configuration model")
    z = nx.utils.create_degree_sequence(30, powerlaw_sequence)
    G = nx.configuration_model(z)
    draw_graph(G)
    # to remove paralel edges
    G = nx.Graph(G)
    draw_graph(G)
    # to remove self loops
    G.remove_edges_from(G.selfloop_edges())
    draw_graph(G)
    print("Directed configuration model")
    D = nx.DiGraph([(0, 1), (1, 2), (2, 3), (1, 3)])  # directed path graph
    din = list(D.in_degree().values())
    dout = list(D.out_degree().values())
    din.append(1)
    dout[0] = 2
    D = nx.directed_configuration_model(din, dout)
    D = nx.DiGraph(D)  # to remove paralell edges
    D.remove_edges_from(D.selfloop_edges())  # to remove self loops
    draw_graph(D)
    print("Expected degree graphs")
    z = [i for i in range(10)]
    G = nx.expected_degree_graph(z)
    draw_graph(G)
    print("Havel Hakimi graphs")
    z = [2, 4, 5, 6, 7, 3]
    G = nx.havel_hakimi_graph(z)
    draw_graph(G)
    print("Directed Havel Hakimi graphs")
    inds = [2, 4, 5, 6, 7, 3]
    outds = [2, 4, 5, 6, 7, 3]
    G = nx.directed_havel_hakimi_graph(in_deg_sequence=inds,
                                       out_deg_sequence=outds)
    draw_graph(G)
    print("Degree Sequence Tree")
    dg = [1, 2, 3, 4, 2, 3]
    G = nx.degree_sequence_tree(dg)
    draw_graph(G)
    print("Random Degree Sequence")
    sequence = [1, 2, 2, 3]
    G = nx.random_degree_sequence_graph(sequence)
    sorted(G.degree().values())
    draw_graph(G)
    def __init__(self,
                 n_processing_elements,
                 topology_specific_parameters,
                 PEs_positions=None):

        super().__init__(n_processing_elements, topology_specific_parameters,
                         PEs_positions)

        n_neighbours = math.ceil(topology_specific_parameters[
            'number of neighbours as a percentage of the number of PEs'] *
                                 n_processing_elements)

        # the Havel-Hakimi algorithm is used to obtain a simple graph with the requested degrees
        graph = nx.havel_hakimi_graph([n_neighbours] * n_processing_elements)

        self._neighbours = [
            graph.neighbors(i) for i in range(n_processing_elements)
        ]
Exemple #10
0
    def setup_class(cls):
        # G is the example graph in Figure 1 from Batagelj and
        # Zaversnik's paper titled An O(m) Algorithm for Cores
        # Decomposition of Networks, 2003,
        # http://arXiv.org/abs/cs/0310049.  With nodes labeled as
        # shown, the 3-core is given by nodes 1-8, the 2-core by nodes
        # 9-16, the 1-core by nodes 17-20 and node 21 is in the
        # 0-core.
        t1 = nx.convert_node_labels_to_integers(nx.tetrahedral_graph(), 1)
        t2 = nx.convert_node_labels_to_integers(t1, 5)
        G = nx.union(t1, t2)
        G.add_edges_from(
            [
                (3, 7),
                (2, 11),
                (11, 5),
                (11, 12),
                (5, 12),
                (12, 19),
                (12, 18),
                (3, 9),
                (7, 9),
                (7, 10),
                (9, 10),
                (9, 20),
                (17, 13),
                (13, 14),
                (14, 15),
                (15, 16),
                (16, 13),
            ]
        )
        G.add_node(21)
        cls.G = G

        # Create the graph H resulting from the degree sequence
        # [0, 1, 2, 2, 2, 2, 3] when using the Havel-Hakimi algorithm.

        degseq = [0, 1, 2, 2, 2, 2, 3]
        H = nx.havel_hakimi_graph(degseq)
        mapping = {6: 0, 0: 1, 4: 3, 5: 6, 3: 4, 1: 2, 2: 5}
        cls.H = nx.relabel_nodes(H, mapping)
Exemple #11
0
def generate_graph(args):
    num_nodes = args.num_nodes
    while True:

        degrees = np.random.randint(args.min_degree, args.max_degree + 1, size = num_nodes)
        degrees = degrees.tolist()
        try:
            graph = nx.havel_hakimi_graph(degrees)
            print("Generated successfully.")
            break
        except Exception as err:
            print("Error. Generating graph...")
            continue

    if args.visualize:
        sorted_degs = sorted(degrees)
        plt.bar(np.arange(len(degrees)), sorted_degs)
        plt.xlabel('Index')
        plt.ylabel('Degree')
        plt.show()
    return graph
Exemple #12
0
    # most used graphs
    G1 = nx.erdos_renyi_graph(n=24, p=0.3, seed=seed)

    # some cool graphs
    G2 = nx.star_graph(20)
    G3 = nx.path_graph(30)
    G4 = nx.petersen_graph()
    G5 = nx.dodecahedral_graph()
    G6 = nx.house_graph()
    G7 = nx.moebius_kantor_graph()
    G8 = nx.barabasi_albert_graph(5, 4)
    G9 = nx.heawood_graph()
    G10 = nx.icosahedral_graph()
    G11 = nx.sedgewick_maze_graph()
    G12 = nx.havel_hakimi_graph([1, 1])
    G13 = nx.complete_graph(20)
    G14 = nx.bull_graph()

    G = G1  # choose a graph from the list

    gd.draw_custom(G)
    plt.show()

    #exact cut
    print("Time 'local_consistent_max_cut':" +
          str(gt.execution_time(mc.local_consistent_max_cut, 1, G)))
    print('Edges cut: ' + str(gc.cut_edges(G)))
    print('\n')
    print("Time 'lazy_local_consistent_max_cut':" +
          str(gt.execution_time(mc.lazy_local_consistent_max_cut, 1, G)))
Exemple #13
0
def get_C_Havel_Hakimi(degSeq):
    g = nx.havel_hakimi_graph(degSeq)
    C_HH = nx.transitivity(g)
    return C_HH
Exemple #14
0
    # most used graphs
    G1 = nx.erdos_renyi_graph(n=24, p=0.3, seed=seed)

    # some cool graphs
    G2 = nx.star_graph(20)
    G3 = nx.path_graph(30)
    G4 = nx.petersen_graph()
    G5 = nx.dodecahedral_graph()
    G6 = nx.house_graph()
    G7 = nx.moebius_kantor_graph()
    G8 = nx.barabasi_albert_graph(5, 4)
    G9 = nx.heawood_graph()
    G10 = nx.icosahedral_graph()
    G11 = nx.sedgewick_maze_graph()
    G12 = nx.havel_hakimi_graph([1, 1])
    G13 = nx.complete_graph(20)
    G14 = nx.bull_graph()

    G = G1  # choose a graph from the list

    gd.draw_custom(G)
    plt.show()

    #exact cut
    print("Time 'local_consistent_max_cut':" + str(gt.execution_time(mc.local_consistent_max_cut, 1, G)))
    print('Edges cut: ' + str(gc.cut_edges(G)))
    print('\n')
    print("Time 'lazy_local_consistent_max_cut':" + str(gt.execution_time(mc.lazy_local_consistent_max_cut, 1, G)))
    print('Edges cut: ' + str(gc.cut_edges(G)))
    print('\n')