Demo entry 6785672

py

   

Submitted by liu on Mar 18, 2019 at 09:12
Language: Python 3. Code size: 1.3 kB.

import random
import matplotlib.pyplot as plt

scale = 1000
p = 0.1
avd = 0  # average degree
avc = 0  # averageS clustering coefficient
Pk = [0]*scale  # degree distribution

"""生成矩阵"""
net = [[0]*scale for i in range(scale)]
for i in range(0, scale-1):
    for j in range(i+1, scale):
        if random.uniform(0, 1) < p:
            value = 1
        else:
            value = 0
        net[i][j] = value
        net[j][i] = value

""""计算网络的度"""
td = 0
for i in range(0, scale):
    d = 0
    for j in range(0, scale):
        d = d + net[i][j]
    Pk[d] = Pk[d] + 1
    td = td + d
Pk = map(lambda x: x / scale, Pk)
Pk = list(Pk)
avd = td / scale
print("average degree is: ", avd)

"""计算网络的聚类系数"""
tc = 0
for i in range(0, scale):
    a = []
    e = 0
    t = 0
    for index, value in enumerate(net[i]):
        if value == 1:
            a.append(index)
    if len(a) != 0 and len(a) != 1:
        t = len(a)*(len(a)-1)/2
        for j in a:
            for k in a:
                if net[j][k] == 1:
                    e = e + 1
        tc = tc + e/(2*t)

avc = tc / scale
print("average clustering coefficient is :", avc)

plt.plot(Pk)
plt.xlabel('Degree')
plt.ylabel('Probability')
plt.show()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).