python中networkx函數(shù)的具體使用
1. 介紹
1.1 前言
NetworkX是復(fù)雜網(wǎng)絡(luò)研究領(lǐng)域中的常用Python包。
1.2 圖的類型(Graph Types)
允許以可哈希的object作為節(jié)點(diǎn),任何Python object作為邊屬性。
如何選擇使用哪種圖:
這里解釋一下什么是平行邊:連接一對(duì)頂點(diǎn)的兩條邊叫做平行邊,即,無向圖中,兩個(gè)頂點(diǎn)間有多條邊,他們叫做平行邊,打個(gè)比方,北京和上海直接可以 是公路、鐵路、飛機(jī),那么他們互為平行邊。
1.3 常用方法
創(chuàng)建一個(gè)空的圖
1)無向圖:G = nx.Graph()
2)有向圖:DG = nx.DiGraph()
將有向圖轉(zhuǎn)換為無向圖:G = nx.Graph(DG)
圖是否有向:G.is_directed() 返回布爾值
添加節(jié)點(diǎn)
1)直接添加一個(gè)節(jié)點(diǎn)(任何object都可以作為節(jié)點(diǎn),包括另一個(gè)圖)G.add_node(1)、G.add_node(DG)
2)從任何容器加點(diǎn):a list, dict, set or even the lines from a file or the nodes from another graph…;G.add_nodes_from() 或 nx.path_graph()
添加邊
1)添加一條邊 G.add_edge(u, v)
2)添加一個(gè)邊的列表 G.add_edges_from([(1, 2), (1, 3)])
3)添加一個(gè)邊的collection G.add_edges_from(H.edges)
4)如果添加的邊的點(diǎn)不存在于圖中,會(huì)自動(dòng)添上相應(yīng)節(jié)點(diǎn)而不報(bào)錯(cuò)
屬性attribute
1)圖的節(jié)點(diǎn)/邊/圖都可以在關(guān)聯(lián)的attribute字典中以鍵值對(duì)key/value形式存儲(chǔ)attribute(key一定要是可哈希的)
2)默認(rèn)情況下屬性字典是空的
3)可以通過add_edge() add_node() 方法或直接操作分別名為graph edges nodes的屬性字典來進(jìn)行操作
2. 代碼示例
import networkx as nx import numpy as np #定義圖的節(jié)點(diǎn)和邊 nodes=['0','1','2','3','4','5','a','b','c'] edges=[('0','0',1),('0','1',1),('0','5',1),('0','5',2),('1','2',3),('1','4',5),('2','1',7),('2','4',6),('a','b',0.5),('b','c',0.5),('c','a',0.5)] plt.subplots(1,2,figsize=(10,3)) #定義一個(gè)無向圖和有向圖 G1 = nx.Graph() G1.add_nodes_from(nodes) G1.add_weighted_edges_from(edges) G2 = nx.DiGraph() G2.add_nodes_from(nodes) G2.add_weighted_edges_from(edges) pos1=nx.circular_layout(G1) pos2=nx.circular_layout(G2) #畫出無向圖和有向圖 plt.subplot(121) nx.draw(G1,pos1, with_labels=True, font_weight='bold') plt.title('無向圖',fontproperties=myfont) plt.axis('on') plt.xticks([]) plt.yticks([]) plt.subplot(122) nx.draw(G2,pos2, with_labels=True, font_weight='bold') plt.title('有向圖',fontproperties=myfont) plt.axis('on') plt.xticks([]) plt.yticks([]) plt.show() #控制numpy輸出小數(shù)位數(shù) np.set_printoptions(precision=3) #鄰接矩陣 A = nx.adjacency_matrix(G1) print('鄰接矩陣:\n',A.todense()) 鄰接矩陣: [[0. 0. 0. 0. 5. 0. 0. 0. 6. ] [0. 0. 0. 2. 0. 0. 0. 0. 0. ] [0. 0. 0. 0. 0. 0.5 0.5 0. 0. ] [0. 2. 0. 1. 1. 0. 0. 0. 0. ] [5. 0. 0. 1. 0. 0. 0. 0. 7. ] [0. 0. 0.5 0. 0. 0. 0.5 0. 0. ] [0. 0. 0.5 0. 0. 0.5 0. 0. 0. ] [0. 0. 0. 0. 0. 0. 0. 0. 0. ] [6. 0. 0. 0. 7. 0. 0. 0. 0. ]] #關(guān)聯(lián)矩陣 I = nx.incidence_matrix(G1) print('\n關(guān)聯(lián)矩陣:\n',I.todense()) 關(guān)聯(lián)矩陣: [[1. 1. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 1. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 1. 1. 0. 0. 0. 0.] [0. 0. 1. 0. 0. 1. 0. 0. 0.] [0. 1. 0. 0. 0. 1. 0. 1. 0.] [0. 0. 0. 1. 0. 0. 0. 0. 1.] [0. 0. 0. 0. 1. 0. 0. 0. 1.] [0. 0. 0. 0. 0. 0. 0. 0. 0.] [1. 0. 0. 0. 0. 0. 0. 1. 0.]] #拉普拉斯矩陣 L=nx.laplacian_matrix(G1) print('\n拉普拉斯矩陣:\n',L.todense()) 拉普拉斯矩陣: [[11. 0. 0. 0. -5. 0. 0. 0. -6. ] [ 0. 2. 0. -2. 0. 0. 0. 0. 0. ] [ 0. 0. 1. 0. 0. -0.5 -0.5 0. 0. ] [ 0. -2. 0. 3. -1. 0. 0. 0. 0. ] [-5. 0. 0. -1. 13. 0. 0. 0. -7. ] [ 0. 0. -0.5 0. 0. 1. -0.5 0. 0. ] [ 0. 0. -0.5 0. 0. -0.5 1. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [-6. 0. 0. 0. -7. 0. 0. 0. 13. ]] #標(biāo)準(zhǔn)化的拉普拉斯矩陣 NL=nx.normalized_laplacian_matrix(G1) print('標(biāo)準(zhǔn)化的拉普拉斯矩陣:\n',NL.todense()) 標(biāo)準(zhǔn)化的拉普拉斯矩陣: [[ 1. 0. 0. 0. -0.418 0. 0. 0. -0.502] [ 0. 1. 0. -0.707 0. 0. 0. 0. 0. ] [ 0. 0. 1. 0. 0. -0.5 -0.5 0. 0. ] [ 0. -0.707 0. 0.75 -0.139 0. 0. 0. 0. ] [-0.418 0. 0. -0.139 1. 0. 0. 0. -0.538] [ 0. 0. -0.5 0. 0. 1. -0.5 0. 0. ] [ 0. 0. -0.5 0. 0. -0.5 1. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [-0.502 0. 0. 0. -0.538 0. 0. 0. 1. ]] #有向圖拉普拉斯矩陣 DL=nx.directed_laplacian_matrix(G2) print('\n有向拉普拉斯矩陣:\n',DL) 有向拉普拉斯矩陣: [[ 0.889 -0.117 -0.029 -0.087 -0.319 -0.029 -0.029 -0.129 -0.242] [-0.117 0.889 -0.026 -0.278 -0.051 -0.026 -0.026 -0.114 -0.056] [-0.029 -0.026 0.994 -0.012 -0.009 -0.481 -0.481 -0.025 -0.01 ] [-0.087 -0.278 -0.012 0.757 -0.097 -0.012 -0.012 -0.052 -0.006] [-0.319 -0.051 -0.009 -0.097 0.994 -0.009 -0.009 -0.041 -0.434] [-0.029 -0.026 -0.481 -0.012 -0.009 0.994 -0.481 -0.025 -0.01 ] [-0.029 -0.026 -0.481 -0.012 -0.009 -0.481 0.994 -0.025 -0.01 ] [-0.129 -0.114 -0.025 -0.052 -0.041 -0.025 -0.025 0.889 -0.045] [-0.242 -0.056 -0.01 -0.006 -0.434 -0.01 -0.01 -0.045 0.994]] #拉普拉斯算子的特征值 LS=nx.laplacian_spectrum(G1) print('\n拉普拉斯算子的特征值:\n',LS) 拉普拉斯算子的特征值: [-1.436e-15 0.000e+00 4.610e-16 7.000e-01 1.500e+00 1.500e+00 4.576e+00 1.660e+01 2.013e+01] #鄰接矩陣的特征值 AS=nx.adjacency_spectrum(G1) print('鄰接矩陣的特征值:\n',AS) 鄰接矩陣的特征值: [12.068+0.000e+00j 2.588+0.000e+00j -7.219+0.000e+00j -4.925+0.000e+00j -1.513+0.000e+00j 1. +0.000e+00j -0.5 +2.393e-17j -0.5 -2.393e-17j0. +0.000e+00j] #無向圖的代數(shù)連通性 AC=nx.algebraic_connectivity(G1) print('無向圖的代數(shù)連通性:\n',AC) 無向圖的代數(shù)連通性: 0.0 #圖的光譜排序 SO=nx.spectral_ordering(G1) print('圖的光譜排序:\n',SO) 圖的光譜排序: ['4', '2', '1', '0', '5', 'b', 'c', 'a', '3']
到此這篇關(guān)于python中networkx函數(shù)的具體使用的文章就介紹到這了,更多相關(guān)python networkx使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 如何使用python中的networkx來生成一個(gè)圖
- Python networkx中獲取圖的鄰接矩陣方式
- python?NetworkX庫生成并繪制帶權(quán)無向圖
- Python利用networkx畫圖繪制Les?Misérables人物關(guān)系
- Python networkx包的實(shí)現(xiàn)
- 使用Python的networkx繪制精美網(wǎng)絡(luò)圖教程
- python networkx 根據(jù)圖的權(quán)重畫圖實(shí)現(xiàn)
- python networkx 包繪制復(fù)雜網(wǎng)絡(luò)關(guān)系圖的實(shí)現(xiàn)
- Python 學(xué)習(xí)教程之networkx
相關(guān)文章
anaconda中安裝的python環(huán)境中沒有pip3的問題及解決
這篇文章主要介紹了anaconda中安裝的python環(huán)境中沒有pip3的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02python 基礎(chǔ)學(xué)習(xí)第二彈 類屬性和實(shí)例屬性
本人c程序員,最近開始學(xué)python,深深的被python的強(qiáng)大所吸引,今后也會(huì)把學(xué)到的點(diǎn)點(diǎn)滴滴記錄下來,現(xiàn)在分享一下關(guān)于類屬性和實(shí)例屬性的一些問題,很基礎(chǔ)的東西2012-08-08python內(nèi)置函數(shù)delattr()與dict()舉例詳解
這篇文章主要介紹了關(guān)于python內(nèi)置函數(shù)delattr()與dict()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08在Sublime Editor中配置Python環(huán)境的詳細(xì)教程
這篇文章主要介紹在sublime編輯器中安裝python軟件包,以 實(shí)現(xiàn)自動(dòng)完成等功能,并在sublime編輯器本身中運(yùn)行build,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2020-05-05