def test_directed_havel_hakimi():
    # Test range of valid directed degree sequences
    n, r = 100, 10
    p = 1.0 / r
    for i in range(r):
        G1 = nx.erdos_renyi_graph(n, p * (i + 1), None, True)
        din1 = list(d for n, d in G1.in_degree())
        dout1 = list(d for n, d in G1.out_degree())
        G2 = nx.directed_havel_hakimi_graph(din1, dout1)
        din2 = list(d for n, d in G2.in_degree())
        dout2 = list(d for n, d in G2.out_degree())
        assert_equal(sorted(din1), sorted(din2))
        assert_equal(sorted(dout1), sorted(dout2))

    # Test non-graphical sequence
    dout = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
    din = [103, 102, 102, 102, 102, 102, 102, 102, 102, 102]
    assert_raises(nx.exception.NetworkXError, nx.directed_havel_hakimi_graph,
                  din, dout)
    # Test valid sequences
    dout = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
    din = [2, 2, 2, 2, 2, 2, 2, 2, 0, 2]
    G2 = nx.directed_havel_hakimi_graph(din, dout)
    dout2 = (d for n, d in G2.out_degree())
    din2 = (d for n, d in G2.in_degree())
    assert_equal(sorted(dout), sorted(dout2))
    assert_equal(sorted(din), sorted(din2))
    # Test unequal sums
    din = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
    assert_raises(nx.exception.NetworkXError, nx.directed_havel_hakimi_graph,
                  din, dout)
    # Test for negative values
    din = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -2]
    assert_raises(nx.exception.NetworkXError, nx.directed_havel_hakimi_graph,
                  din, dout)
Пример #2
0
def test_directed_havel_hakimi():
    # Test range of valid directed degree sequences
    n, r = 100, 10
    p = 1.0 / r
    for i in range(r):
        G1 = nx.erdos_renyi_graph(n, p * (i + 1), None, True)
        din1 = list(d for n, d in G1.in_degree())
        dout1 = list(d for n, d in G1.out_degree())
        G2 = nx.directed_havel_hakimi_graph(din1, dout1)
        din2 = list(d for n, d in G2.in_degree())
        dout2 = list(d for n, d in G2.out_degree())
        assert_equal(sorted(din1), sorted(din2))
        assert_equal(sorted(dout1), sorted(dout2))

    # Test non-graphical sequence
    dout = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
    din = [103, 102, 102, 102, 102, 102, 102, 102, 102, 102]
    assert_raises(nx.exception.NetworkXError,
                  nx.directed_havel_hakimi_graph, din, dout)
    # Test valid sequences
    dout = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
    din = [2, 2, 2, 2, 2, 2, 2, 2, 0, 2]
    G2 = nx.directed_havel_hakimi_graph(din, dout)
    dout2 = (d for n, d in G2.out_degree())
    din2 = (d for n, d in G2.in_degree())
    assert_equal(sorted(dout), sorted(dout2))
    assert_equal(sorted(din), sorted(din2))
    # Test unequal sums
    din = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
    assert_raises(nx.exception.NetworkXError,
                  nx.directed_havel_hakimi_graph, din, dout)
    # Test for negative values
    din = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -2]
    assert_raises(nx.exception.NetworkXError,
                  nx.directed_havel_hakimi_graph, din, dout)
Пример #3
0
class Topology(object, nx.Graph):

    for N in [20, 30, 40]:
        for delta in [2, 4, 8]:
            fmax_vector = []
            for i in range(5):
                nodes = range(N)
                np.random.seed(5)
                degree = [delta for i in xrange(N)]
                G = nx.directed_havel_hakimi_graph(degree, degree)
                G = nx.DiGraph(G)

                bb = nx.edge_betweenness_centrality(G, normalized=False)
                nx.set_edge_attributes(G, 'weight', bb)
                nx.set_edge_attributes(G, 'capacity', bb)

                T_matrix = np.zeros((N, N))

                for s in nodes:
                    for d in nodes:
                        if s != d:
                            flow = np.random.uniform(0.5, 1.5)
                            T_matrix[s, d] = flow
                            if G.has_edge(s, d):
                                G.edge[s][d]['weight'] = flow
                                G.edge[s][d]['capacity'] = np.random.randint(
                                    8, 12)

                f_value = 0
                (p, a) = (0, 0)
                for i in range(N):
                    for j in range(N):
                        edges = nx.shortest_path(G, i, j, weight='weight')
                        for k in range(len(edges) - 1):
                            G.edge[edges[k]][edges[
                                k + 1]]['weight'] += T_matrix[i][j]
                    if i != j:
                        flow_value = nx.maximum_flow_value(G, i, j)
                        if flow_value > f_value:
                            f_value = flow_value
                            (p, a) = (i, j)

                fmax = 0
                (s_f, d_f) = (0, 0)

                for s in G.edge:
                    for d in G.edge[s]:
                        if G.edge[s][d]['weight'] > fmax:
                            fmax = G.edge[s][d]['weight']
                            (s_f, d_f) = (s, d)
                fmax_vector.append(fmax)
                tot_edges = G.number_of_edges()

            np.set_printoptions(precision=3)
            #print T_matrix
            #print tot_edges
            print 'N = ' + str(N) + ' D = ' + str(delta) + 'fmax = ' + str(
                np.mean(fmax_vector))  #+ ' flow = ' + str(flow_value)
Пример #4
0
    def createRandomTopology(self, N, tsd):
        delta = np.random.randint(4, N / 2)
        degree = [delta for i in xrange(N)]
        G = nx.directed_havel_hakimi_graph(degree, degree)

        for s, d in G.edges():
            G.edge[s][d]['weight'] = tsd[s][d]

        #nx.draw_networkx(G)
        #plt.show()
        return G
def ObtenerParesDeDatos(informacion):
    # descomponer entrada
    red_P = informacion[0]
    recipientes = informacion[1]
    infoRedes_P = recipientes[0]
    cuenta_P = recipientes[1]
    total_P = recipientes[2]
    # variables locales
    orden = 0
    aristas = 0
    inDegreeSequence = []
    outDegreeSequence = []
    conexa = False
    aleatoriaGNM = nx.DiGraph()
    aleatoriaHH = nx.DiGraph()
    datosRed = []
    grafo = nx.DiGraph
    grafoNoDirigido = nx.Graph()
    # obtener datos de la real
    orden = red_P.order()
    aristas = red_P.size()
    inDegreeSequence = [deg for node, deg in red_P.in_degree()]
    outDegreeSequence = [deg for node, deg in red_P.out_degree()]
    grafoNoDirigido = nx.Graph(red_P)
    datosRed = obtenerValores(red_P, grafoNoDirigido)
    infoRedes_P.append((datosRed, ["1", "0"]))
    # generar gnm preservando orden y tamaño
    conexa = False
    while (not conexa):
        aleatoriaGNM = nx.gnm_random_graph(orden, aristas, directed=True)
        grafoNoDirigido = nx.Graph(aleatoriaGNM)
        if (nx.is_connected(grafoNoDirigido)):
            conexa = True
    datosRed = obtenerValores(aleatoriaGNM, grafoNoDirigido)
    infoRedes_P.append((datosRed, ["0", "1"]))
    # generar havel hakimi para preservar secuencias de grado
    conexa = False
    while (not conexa):
        aleatoriaHH = nx.directed_havel_hakimi_graph(inDegreeSequence,
                                                     outDegreeSequence)
        grafoNoDirigido = nx.Graph(aleatoriaHH)
        if (nx.is_connected(grafoNoDirigido)):
            conexa = True
    datosRed = obtenerValores(aleatoriaHH, grafoNoDirigido)
    infoRedes_P.append((datosRed, ["0", "1"]))
    # imprimir avance
    cuenta_P.value = cuenta_P.value + 1
    print(" - avance: " + str(round(
        (cuenta_P.value * 100) / total_P.value, 2)) + " % ...",
          end="\r")
Пример #6
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)
Пример #7
0
    'Cython': 'dijkstra_cython(G, source, target)',
    'Cython Fibonacci': 'dijkstra_cython_fibo(G, source,target)',
    'Openmp': 'dijkstra_openmp(G, source,target)'
}

# Generate a random directed graph using Networkx.
np.random.seed(0)
nb_nodes = 10000
nb_edges = 40000
in_deg, out_deg = np.random.multinomial(
    n=nb_edges - nb_nodes,
    pvals=np.ones(nb_nodes) / nb_nodes,
    size=2,
) + 1
print(in_deg, out_deg)
G = nx.directed_havel_hakimi_graph(in_deg, out_deg)
# Add random weights to the graph.
weights = np.random.randint(1, 1000, size=nb_edges)
for i, (u, v) in enumerate(G.edges()):
    G.edges[u, v]['weight'] = weights[i]

# Randomly choose a source and a target node.
source = np.random.choice(G.nodes)
target = source
while target == source:
    target = np.random.choice(G.nodes)

# Number of run for each tested functions.
N = 10

# import cProfile