Exemple #1
0
import matplotlib.pyplot as plt
from networkx import nx

z = [
    98, 95, 95, 94, 92, 89, 89, 89, 87, 87, 86, 85, 85, 84, 83, 81, 80, 79, 77,
    75, 71, 70, 70, 70, 69, 68, 68, 67, 65, 64, 63, 62, 61, 59, 58, 55, 54, 54,
    53, 53, 53, 53, 51, 49, 47, 46, 45, 45, 45, 43, 42, 42, 41, 39, 38, 38, 37,
    36, 35, 35, 35, 35, 35, 34, 33, 30, 30, 29, 28, 28, 27, 25, 23, 22, 22, 21,
    19, 19, 19, 18, 17, 15, 15, 15, 13, 13, 12, 12, 12, 11, 11, 10, 10, 5, 5,
    4, 4, 2, 1, 1
]

print(nx.is_graphical(z))

print("Configuration model")
G = nx.configuration_model(z)  # configuration model
print(G)
degree_sequence = [d for n, d in G.degree()]  # degree sequence
print("Degree sequence %s" % degree_sequence)
print("Degree histogram")
hist = {}
for d in degree_sequence:
    if d in hist:
        hist[d] += 1
    else:
        hist[d] = 1
print("degree #nodes")
for d in hist:
    print('%d %d' % (d, hist[d]))

nx.draw(G)
                return False
        d = degree_sequence.pop(0)
        if d == 0:
            print("There exists a graph G with this degree sequence")
            return True
        if d > len(degree_sequence):
            print(str(d) + " dgree is too large for sequence!")
            return False
        for i in range(0, d):
            degree_sequence[i] -= 1
        print("Popped: " + str(d))
    return False


random_sequence = []
n = random.randint(0, 100)

print("the length of the degree sequence is {0}".format(n))
for x in range(n):
    random_sequence.append(random.randint(0, n - 1))
print("the degree sequence is {0}".format(random_sequence))

start_time = time.time()

x = havelHakimi(random_sequence)
print("--- %s seconds ---" % (time.time() - start_time))

if x == True:
    G = nx.configuration_model(random_sequence)
    nx.draw(G)
    plt.show()
Degree Sequence
===============

Random graph from given degree sequence.
"""
import matplotlib.pyplot as plt
from networkx import nx

# Specify seed for reproducibility
seed = 668273

z = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
print(nx.is_graphical(z))

print("Configuration model")
G = nx.configuration_model(
    z, seed=seed)  # configuration model, seed for reproduciblity
degree_sequence = [d for n, d in G.degree()]  # degree sequence
print(f"Degree sequence {degree_sequence}")
print("Degree histogram")
hist = {}
for d in degree_sequence:
    if d in hist:
        hist[d] += 1
    else:
        hist[d] = 1
print("degree #nodes")
for d in hist:
    print(f"{d:4} {hist[d]:6}")

pos = nx.spring_layout(G, seed=seed)  # Seed layout for reproducibility
nx.draw(G, pos=pos)
#    Copyright (C) 2004-2018 by
#    Aric Hagberg <*****@*****.**>
#    Dan Schult <*****@*****.**>
#    Pieter Swart <*****@*****.**>
#    All rights reserved.
#    BSD license.

import matplotlib.pyplot as plt
from networkx import nx

z = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
print(nx.is_graphical(z))

print("Configuration model")
G = nx.configuration_model(z)  # configuration model
degree_sequence = [d for n, d in G.degree()]  # degree sequence
print("Degree sequence %s" % degree_sequence)
print("Degree histogram")
hist = {}
for d in degree_sequence:
    if d in hist:
        hist[d] += 1
    else:
        hist[d] = 1
print("degree #nodes")
for d in hist:
    print('%d %d' % (d, hist[d]))

nx.draw(G)
plt.show()
def get_graph_based_degree_sequence(z=None, file_location="./Output/"):
    if not z:
        z = [5, 3, 3, 3, 3, 2, 2, 4, 1, 1, 3]
    g = nx.configuration_model(z)
    nx.write_pajek(g, file_location + "randomWithSequence.net")
    return g