如何使用python中的networkx来生成一个图
python networkx来生成一个图
使用python提供的第三方的库networkx,networkx是专门用来生成图论和网络科学里面各种图及其各种计算函数的。
(a).如果已知一个图的图形,如何将其生成对应的邻接矩阵,这个在networkx里面提供了函数nx.to_numpy_matrix(G)来完成
(b).如果已知一个图的邻接矩阵,如何将其转化成对应的图形
代码如下:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 16 20:13:42 2019
@author: Administrator
"""
"""
这个函数的作用是将一个矩阵给转换成一个图,
矩阵以多维列表的形式存在,即列表的列表
此处的转换是针对无向图
根据邻接矩阵得到图之后,我们就可以调用networkx
里面的各种函数来分析图的性质,比如度分布,
平均路径程度,聚类系数等一系列图的拓扑性质
"""
import networkx as nx
def matrix_to_graph():
G = nx.Graph()
#matrix为邻接矩阵,以多维列表的形式存在
matrix = [[0, 1, 1],[1,0,1],[1,1,0]]
nodes = range(len(matrix))
G.add_nodes_from(nodes)
for i in range(len(matrix)):
for j in range(len(matrix)):
if(matrix[i][j] == 1):
G.add_edge(i, j)
position = nx.circular_layout(G)
nx.draw_networkx_nodes(G,position, nodelist=nodes, node_color="r")
nx.draw_networkx_edges(G,position)
nx.draw_networkx_labels(G,position)
print(nx.to_numpy_matrix(G))
matrix_to_graph()
运行结果如下:

networkx随机图生成
导入包
import networkx as nx #导入networkx包 import random #导入random包 import matplotlib.pyplot as plt #导入画图工具包
新建图
G = nx.Graph() #建立无向图 H = nx.path_graph(100) #添加节点 G.add_nodes_from(H) #添加节点
随机概率添加边的函数
def rand_edge(vi,vj,p=0.2): #默认概率p=0.1 probability =random.random()#生成随机小数 if(probability<p): #如果小于p G.add_edge(vi,vj) #连接vi和vj节点
添加边
i=0 while (i<100): j=0 while(j<i): rand_edge(i,j) #调用rand_edge() j +=1 i +=1
matplotlib画图

连通子图
number_components = nx.number_connected_components(G)
largest_components = max(nx.connected_components(G), key=len)
print("最大连通子图:" + str(largest_components))
print("最大连通子图长度:"+ str(len(largest_components)))
print("连通子图个数: "+str(nx.number_connected_components(G)))节点的度
nx.degree(G) DVweight = G.degree() degree_sum = sum(span for n, span in DVweight) #各节点度数之和 degree_max = max(span for n, span in DVweight) #节点最大度数
代码
import networkx as nx #导入networkx包
import random #导入random包
import matplotlib.pyplot as plt
G = nx.Graph()
H = nx.path_graph(100)
G.add_nodes_from(H)
def rand_edge(vi,vj,p=0.2):
probability =random.random()
if(probability<p):
G.add_edge(vi,vj)
i=0
while (i<100):
j=0
while(j<i):
rand_edge(i,j)
j +=1
i +=1
number_components = nx.number_connected_components(G)
largest_components = max(nx.connected_components(G), key=len)
nx.degree(G)
DVweight = G.degree()
degree_sum = sum(span for n, span in DVweight) #各节点度数之和
degree_max = max(span for n, span in DVweight) #节点最大度数
print("度数之和: " + str(degree_sum))
print("节点最大度数:" + str(degree_max))
print("最大连通子图:" + str(largest_components))
print("最大连通子图长度:"+ str(len(largest_components)))
print("连通子图个数: "+str(nx.number_connected_components(G)))
nx.draw_networkx(G, with_labels=True)
plt.show()
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Python与Appium实现手机APP自动化测试的示例代码
本文主要介绍了Python与Appium实现手机APP自动化测试的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2022-02-02
编写Python脚本把sqlAlchemy对象转换成dict的教程
这篇文章主要介绍了编写Python脚本把sqlAlchemy对象转换成dict的教程,主要是基于Python的model类构建一个转换的方法,需要的朋友可以参考下2015-05-05
Jupyter安装拓展nbextensions及解决官网下载慢的问题
这篇文章主要介绍了Jupyter安装拓展nbextensions及解决官网下载慢的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-03-03
python中pandas nlargest()的详细用法小结
df.nlargest()是一个DataFrame的方法,用于返回DataFrame中最大的n个值所在的行,通过调用nlargest()方法,我们返回了分数最高的三个行,并按照降序排列,本文结合实例代码给大家介绍的非常详细,需要的朋友参考下吧2023-10-10
python中CURL 和python requests的相互转换实现
本文主要介绍了python中CURL 和python requests的相互转换实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-03-03
keras的ImageDataGenerator和flow()的用法说明
这篇文章主要介绍了keras的ImageDataGenerator和flow()的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-07-07


最新评论