Exemplo n.º 1
0
def test_random_reference():
    G = nx.connected_watts_strogatz_graph(50, 6, 0.1, seed=rng)
    Gr = random_reference(G, niter=1, seed=rng)
    C = nx.average_clustering(G)
    Cr = nx.average_clustering(Gr)
    assert_true(C > Cr)

    assert_raises(nx.NetworkXError, random_reference, nx.Graph())
    assert_raises(nx.NetworkXNotImplemented, random_reference, nx.DiGraph())

    H = nx.Graph(((0, 1), (2, 3)))
    Hl = random_reference(H, niter=1, seed=rng)
def _get_small_world_measures(graph, meas, **kwargs):
    # Compute the mean clustering coefficient and average shortest path length
    # for an equivalent random graph
    nrand = kwargs['nrand']
    niter = kwargs['niter']
    C = meas['C']
    L = meas['L']

    randMetrics = {"Cr": [], "Cl": [], "L": []}
    for _ in range(nrand):
        Gr = nx.random_reference(graph, niter=niter)
        Gl = nx.lattice_reference(graph, niter=niter)
        randMetrics["Cl"].append(nx.transitivity(Gl))
        randMetrics["Cr"].append(nx.transitivity(Gr))
        randMetrics["L"].append(nx.average_shortest_path_length(Gr))

    Cl = np.mean(randMetrics["Cl"])
    Cr = np.mean(randMetrics["Cr"])
    Lr = np.mean(randMetrics["L"])

    omega = (Lr / L) - (C / Cl)
    sigma = (C / Cr) / (L / Lr)

    meas['Omega'] = omega
    meas['Sigma'] = sigma
Exemplo n.º 3
0
for i, C in enumerate((G.subgraph(c).copy() for c in nx.connected_components(G))):
    print(i, nx.average_shortest_path_length(C))
# %%
# so there is actually a built in function to test small-worldness in NetworkX.
# It might be fun to do the calculation myself and compare it to the results of
# built in. The built in is making my machine work... (didn't finish)
# nx.sigma(G)
# nx.omega(G)
# %%
largest_cc = max(nx.connected_components(G), key=len)
S = G.subgraph(largest_cc)
# %%
print(G.number_of_nodes())
print(S.number_of_nodes())
# %%
R = nx.random_reference(S)
# %%
L = nx.lattice_reference(S)
# %%

l = nx.average_shortest_path_length(S)
l_r = nx.average_shortest_path_length(R)
l_l = nx.average_shortest_path_length(L)
# %%
c = nx.average_clustering(S)
c_r = nx.average_clustering(R)
c_l = nx.average_clustering(L)

# %%
s = (c / c_r) / (l / l_r)
s
Exemplo n.º 4
0
listOfWeightedGraphs = []
listOfUnweightedGraphs = []

for i in range(0, 20):
    # Create an empty graph object for erdos renyi algorithm
    g = nx.Graph()
    # Adding nodes
    g.add_nodes_from(range(1, N + 1))
    erdos_renyi(g, P)
    listOfWeightedGraphs.append(g)

    #create new graph for maslov snepen
    g1 = nx.Graph()
    g1.add_nodes_from(range(1, N + 1))
    create_unweighted_random_graph(g1, P)
    g1 = nx.random_reference(g, 1, True, 1)
    listOfUnweightedGraphs.append(g1)

# plot created weighted and unweighted graphs respectively.
nx.draw(g, with_labels=1)
plt.title("Example visualization of Erdos-Renyi graph")
plt.show()

nx.draw(g1, with_labels=1)
plt.title("Example visualization of Maslov-Snepen graph")
plt.show()

list_of_weighted_cms_lists = []
list_of_unweighted_cms_lists = []

# Centrality measurements for weighted graphs