这里,在下面的代码中,我得到了分配给顶点的颜色,但是对于框的长度没有意义' l'
我想要将颜色分配给w.r.t.框的长度和功能应该输出网络中不同颜色的数量,这样我就可以用它来确定现实世界网络的分形维数,依据法律,
N B ~l B -d B
请帮助我实现这一目标。
文件dolphin包含两列,每列代表一条边的两端。
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
colors = ['c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10', 'c11', 'c12']
f = open("real_network/dolphin")
a = [int(n) for n in f.read().split()]
G.add_nodes_from(a)
i = 0
b = []
while i<len(a):
b.append((a[i],a[i+1]))
i = i+2
#print b
G.add_edges_from(b)
colors_of_nodes={}
def coloring(node, color):
for neighbor in G.neighbors(node):
color_of_neighbor = colors_of_nodes.get(neighbor, None)
if color_of_neighbor == color:
return False
return True
def get_color_for_node(node):
for color in colors:
if coloring(node, color):
return color
def main():
for node in G.nodes():
colors_of_nodes[node] = get_color_for_node(node)
print colors_of_nodes
main()