網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
python networkx來(lái)生成一個(gè)圖
使用python提供的第三方的庫(kù)networkx,networkx是專門(mén)用來(lái)生成圖論和網(wǎng)絡(luò)科學(xué)里面各種圖及其各種計(jì)算函數(shù)的。
(a).如果已知一個(gè)圖的圖形,如何將其生成對(duì)應(yīng)的鄰接矩陣,這個(gè)在networkx里面提供了函數(shù)nx.to_numpy_matrix(G)來(lái)完成
(b).如果已知一個(gè)圖的鄰接矩陣,如何將其轉(zhuǎn)化成對(duì)應(yīng)的圖形
代碼如下:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 16 20:13:42 2019
@author: Administrator
"""
"""
這個(gè)函數(shù)的作用是將一個(gè)矩陣給轉(zhuǎn)換成一個(gè)圖,
矩陣以多維列表的形式存在,即列表的列表
此處的轉(zhuǎn)換是針對(duì)無(wú)向圖
根據(jù)鄰接矩陣得到圖之后,我們就可以調(diào)用networkx
里面的各種函數(shù)來(lái)分析圖的性質(zhì),比如度分布,
平均路徑程度,聚類系數(shù)等一系列圖的拓?fù)湫再|(zhì)
"""
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()
運(yùn)行結(jié)果如下:
networkx隨機(jī)圖生成
導(dǎo)入包
import networkx as nx ? #導(dǎo)入networkx包
import random?? ??? ??? ?#導(dǎo)入random包
import matplotlib.pyplot as plt #導(dǎo)入畫(huà)圖工具包
新建圖
G = nx.Graph()?? ??? ??? ?#建立無(wú)向圖
H = nx.path_graph(100)?? ?#添加節(jié)點(diǎn)
G.add_nodes_from(H)?? ??? ?#添加節(jié)點(diǎn)
隨機(jī)概率添加邊的函數(shù)
def rand_edge(vi,vj,p=0.2):?? ??? ?#默認(rèn)概率p=0.1
? ? probability =random.random()#生成隨機(jī)小數(shù)
? ? if(probability<p):?? ??? ??? ?#如果小于p
? ? ? ? G.add_edge(vi,vj) ??? ??? ?#連接vi和vj節(jié)點(diǎn)
添加邊
i=0
while (i<100):
? ? j=0
? ? while(j<i):
? ? ? ? ? ? rand_edge(i,j)?? ??? ?#調(diào)用rand_edge()
? ? ? ? ? ? j +=1
? ? i +=1
matplotlib畫(huà)圖
連通子圖
number_components = nx.number_connected_components(G)?
largest_components = max(nx.connected_components(G), key=len)
print("最大連通子圖:" + str(largest_components))
print("最大連通子圖長(zhǎng)度:"+ str(len(largest_components)))
print("連通子圖個(gè)數(shù): "+str(nx.number_connected_components(G)))
節(jié)點(diǎn)的度
nx.degree(G)
DVweight = G.degree()
degree_sum = sum(span for n, span in DVweight) ?? ??? ?#各節(jié)點(diǎn)度數(shù)之和
degree_max = max(span for n, span in DVweight)?? ??? ?#節(jié)點(diǎn)最大度數(shù)
代碼?
import networkx as nx #導(dǎo)入networkx包
import random #導(dǎo)入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) #各節(jié)點(diǎn)度數(shù)之和
degree_max = max(span for n, span in DVweight) #節(jié)點(diǎn)最大度數(shù)
print("度數(shù)之和: " + str(degree_sum))
print("節(jié)點(diǎn)最大度數(shù):" + str(degree_max))
print("最大連通子圖:" + str(largest_components))
print("最大連通子圖長(zhǎng)度:"+ str(len(largest_components)))
print("連通子圖個(gè)數(shù): "+str(nx.number_connected_components(G)))
nx.draw_networkx(G, with_labels=True)
plt.show()
總結(jié)
原文鏈接:https://blog.csdn.net/qq_31960623/article/details/100547277
相關(guān)推薦
- 2023-06-19 python中time模塊指定格式時(shí)間字符串轉(zhuǎn)為時(shí)間戳_python
- 2022-05-19 python?字符串常用方法超詳細(xì)梳理總結(jié)_python
- 2022-05-25 @Service未注入、 @Autowired未自動(dòng)注入
- 2022-05-08 PyTorch實(shí)現(xiàn)多維度特征輸入邏輯回歸_python
- 2022-08-15 centos7 redis5安裝
- 2022-09-21 redis緩存數(shù)據(jù)庫(kù)中數(shù)據(jù)的方法_Redis
- 2022-10-10 GO必知必會(huì)的常見(jiàn)面試題匯總_Golang
- 2022-06-14 全面了解C語(yǔ)言?static?關(guān)鍵字_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支