예제 #1
0
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from path_ring import make_ring_lattice


def path_lengths(G):
    length_iter = nx.shortest_path_length(G)
    for _, dist_map in length_iter:
        for _, dist in dist_map.items():
            yield dist


def characteristic_path_length(G):
    return np.mean(list(path_lengths(G)))


complete = nx.complete_graph(10)
characteristic_path_length(complete)  # 0.9
lattice = make_ring_lattice(1000, 10)
characteristic_path_length(lattice)  # 50.4
예제 #2
0
`clustering_coefficient`为每个节点调用`node_clustering`一次。`node_clustering`是以$k$为单位的二次函数,即邻居数。因此:
- 在完整图中,$k = n-1$,所以`node_clustering`是$O(n^2)$,且`clustering_coefficient`是$O(n^3)$。
- 在环形晶格或其他图中,$k$与$n$不成正比的图,`clustering_coefficient`是$O(k^2 n)$。
'''


def node_clustering(G, u):
    neighbors = G[u]
    k = len(neighbors)
    if k < 2:
        return np.nan

    possible = comb(k, 2)
    exist = 0
    for v, w in all_pairs_undirected(neighbors):
        if G.has_edge(v, w):
            exist += 1
    return exist / possible


lattice = make_ring_lattice(10, 4)
node_clustering(lattice, 1)  # 0.5


def clustering_coefficient(G):
    cu = [node_clustering(G, node) for node in G]
    return np.nanmean(cu)


clustering_coefficient(lattice)  # 0.5
예제 #3
0
def make_ws_graph(n, k, p):
    ws = make_ring_lattice(n, k)
    rewire(ws, p)
    return ws