k-core
k-core主要用于找出图中具有指定core数的子结构,一般k越大,该结构的范围会越小,知道没有满足条件的子结构,且(k)-core的结果一定是(k-1)-core的子图,关于k-core算法的具体细节,可参考直观理解:k-Core算法。
方法名称 | 用途描述 |
---|---|
core_number (G) |
返回每个顶点的核心数 |
k_core (G[, k, core_number]) |
返回图G的 k-core |
k_shell (G[, k, core_number]) |
返回图G的 k-shell |
k_crust (G[, k, core_number]) |
返回图G的 k-crust |
k_corona (G, k[, core_number]) |
返回图G的 k-corona |
k_truss (G, k) |
返回图G的 k-truss |
onion_layers (G) |
返回图的洋葱分解中每个顶点的层。 |
下面开始用代码演示~~~
import networkx as nx
import networkx.algorithms as algos
import matplotlib.pyplot as plt
#创建karate_club
G = nx.karate_club_graph()
nx.draw(G, with_labels = True)

#返回每个顶点的核心数
print(algos.core_number(G))
#result:{0: 4, 1: 4, 2: 4, 3: 4, 4: 3, 5: 3, 6: 3, 7: 4, 8: 4, 9: 2, 10: 3, 11: 1, 12: 2, 13: 4, 14: 2, 15: 2, 16: 2, 17: 2, 18: 2, 19: 3, 20: 2, 21: 2, 22: 2, 23: 3, 24: 3, 25: 3, 26: 2, 27: 3, 28: 3, 29: 3, 30: 4, 31: 3, 32: 4, 33: 4}
#返回每个顶点的onion_layers
print(algos.onion_layers(G))
#result:{11: 1, 9: 2, 12: 2, 14: 2, 15: 2, 16: 2, 17: 2, 18: 2, 20: 2, 21: 2, 22: 2, 26: 2, 4: 3, 5: 3, 6: 3, 10: 3, 19: 3, 24: 3, 25: 3, 28: 3, 29: 3, 23: 4, 27: 4, 31: 4, 7: 5, 30: 5, 32: 5, 33: 5, 8: 6, 1: 6, 3: 6, 13: 6, 0: 7, 2: 7}
#展示3-core结构
nx.draw(algos.k_core(G, 3), with_labels = True)

#展示4-core结构
nx.draw(algos.k_core(G, 4), with_labels = True)

#展示3-shell结构
nx.draw(algos.k_shell(G, 3), with_labels = True)

#展示3-shell结构
nx.draw(algos.k_shell(G, 4), with_labels = True)

#展示2-crust结构
nx.draw(algos.k_crust(G, 2), with_labels = True)

#展示3-crust结构
nx.draw(algos.k_crust(G, 3), with_labels = True)

#展示3-corona结构
nx.draw(algos.k_corona(G, 3), with_labels = True)

#展示4-corona结构
nx.draw(algos.k_corona(G, 4), with_labels = True)

#展示3-truss结构
nx.draw(algos.k_truss(G, 3), with_labels = True)

#展示4-truss结构
nx.draw(algos.k_truss(G, 4), with_labels = True)

网友评论