您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python—networkx:求图的平均路径长度并画出直方图

5b51 2022/1/14 8:25:26 python 字数 6855 阅读 908 来源 www.jb51.cc/python

<prestyle=\"overflow:auto;line-height:1.5;font-family:Inconsolata,Monaco,Consolas,monospace;background-color:rgb(255,255,255);\"><spanclass=\"c\"style=\"font-style:it

概述

<pre style="overflow:auto;line-height:1.5;font-family:Inconsolata,Monaco,Consolas,monospace;background-color:rgb(255,255,255);"><span class="c" style="font-style:italic;"><span style="font-size:12px;"><h1 style="font-family:'Roboto Slab','ff-tisa-web-pro',Georgia,Arial,sans-serif;color:rgb(64,64,64);background-color:rgb(252,252,252);"><span style="font-size:14px;font-weight:normal;">要绘制一个动态网络,到处找资料,收集相关的networkx绘图资料,计算路径的代码如下:<h1 style="font-family:'Roboto Slab',252);">NetworkX Examples—<span style="font-family:Inconsolata,monospace;line-height:1.5;background-color:rgb(255,255);">BASIC——properties

<code class="language-python">#!/usr/bin/env python
"""
Compute some network properties for the lollipop graph.
"""

Copyright (C) 2004 by

Aric Hagberg hagberg@lanl.gov

Dan Schult dschult@colgate.edu

Pieter Swart swart@lanl.gov

All rights reserved.

BSD license.

from networkx import *
G = lollipop_graph(4,6)
pathlengths=[]

单源最短路径算法求出节点v到图G每个节点的最短路径,存入pathlengths

print("source vertex {target:length,}")
for v in G.nodes():
spl=single_source_shortest_path_length(G,v)
print('%s %s' % (v,spl))
for p in spl.values():
pathlengths.append(p)

取出每条路径,计算平均值。

print('')print("average shortest path length %s" % (sum(pathlengths)/len(pathlengths)))

路径长度直方图,如果路径不存在,设为1,如果已经存在过一次,则原先基础上加1

histogram of path lengths

dist={}
for p in pathlengths:
if p in dist:
dist[p]+=1
else:
dist[p]=1

print('')
print("length #paths")
verts=dist.keys()
for d in sorted(verts):
print('%s %d' % (d,dist[d]))

内嵌函数求图G的多个属性

print("radius: %d" % radius(G))
print("diameter: %d" % diameter(G))
print("eccentricity: %s" % eccentricity(G))
print("center: %s" % center(G))
print("periphery: %s" % periphery(G))
print("density: %s" % density(G))

from networkx import *
G = lollipop_graph(4,6)
pathlengths=[]

print("source vertex {target:length,}")
for v in G.nodes():
spl=single_source_shortest_path_length(G,v)
print('%s %s' % (v,spl))
for p in spl.values():
pathlengths.append(p)

print('')print("average shortest path length %s" % (sum(pathlengths)/len(pathlengths)))

dist={}
for p in pathlengths:
if p in dist:
dist[p]+=1
else:
dist[p]=1

print('')
print("length #paths")
verts=dist.keys()
for d in sorted(verts):
print('%s %d' % (d,dist[d]))

print("radius: %d" % radius(G))
print("diameter: %d" % diameter(G))
print("eccentricity: %s" % eccentricity(G))
print("center: %s" % center(G))
print("periphery: %s" % periphery(G))
print("density: %s" % density(G))

from networkx import *
G = lollipop_graph(4,6)
pathlengths=[]

print("source vertex {target:length,}")
for v in G.nodes():
spl=single_source_shortest_path_length(G,v)
print('%s %s' % (v,spl))
for p in spl.values():
pathlengths.append(p)

print('')print("average shortest path length %s" % (sum(pathlengths)/len(pathlengths)))

dist={}
for p in pathlengths:
if p in dist:
dist[p]+=1
else:
dist[p]=1

print('')
print("length #paths")
verts=dist.keys()
for d in sorted(verts):
print('%s %d' % (d,dist[d]))

print("radius: %d" % radius(G))
print("diameter: %d" % diameter(G))
print("eccentricity: %s" % eccentricity(G))
print("center: %s" % center(G))
print("periphery: %s" % periphery(G))
print("density: %s" % density(G))

总结

以上是编程之家为你收集整理的python—networkx:求图的平均路径长度并画出直方图全部内容,希望文章能够帮你解决python—networkx:求图的平均路径长度并画出直方图所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶