最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中的Networkx的基本使用

 更新時(shí)間:2023年02月14日 16:22:41   作者:酒釀小圓子~  
Networkx是一個(gè)Python的包,可以用來創(chuàng)建和處理復(fù)雜的圖網(wǎng)絡(luò)結(jié)構(gòu),這篇文章主要介紹了Python中的Networkx詳解,需要的朋友可以參考下

中文教程: https://www.osgeo.cn/networkx/install.html
英文教程: https://networkx.org/documentation/stable/install.html

1. 安裝Networkx

# 使用pip安裝
pip install networkx

# 使用conda安裝
conda install networkx

2. Networkx的基本使用

2.1 導(dǎo)入networkx

import networkx as nx

2.2 創(chuàng)建Graph

G = nx.Graph()          # 無向圖
G = nx.DiGraph()        # 有向圖
G = nx.MultiGraph()     # 多重?zé)o向圖
G = nx.MultiDigraph()   # 多重有向圖
G.clear()               # 清空圖

根據(jù)定義,Graph 是一組節(jié)點(diǎn)(頂點(diǎn))和已識(shí)別的節(jié)點(diǎn)對(duì)(稱為邊、鏈接等)的集合。在NetworkX中,節(jié)點(diǎn)可以是任何 hashable 對(duì)象,例如文本字符串、圖像、XML對(duì)象、另一個(gè)圖形、自定義節(jié)點(diǎn)對(duì)象等。

2.3 給Graph添加邊

G.add_edge(1, 2)             # default edge data=1
G.add_edge(2, 3, weight=0.9) # specify edge data
# 如果是邊有許多的權(quán),比如有長度和寬度的屬性,那么:
G.add_edge(n1, n2, length=2, width=3)
 
elist = [(1, 2), (2, 3), (1, 4), (4, 2)]
G.add_edges_from(elist)
elist = [('a', 'b', 5.0), ('b', 'c', 3.0), ('a', 'c', 1.0), ('c', 'd', 7.3)]
G.add_weighted_edges_from(elist)
 
# 如果給結(jié)點(diǎn)的名稱是其它符號(hào),想離散化成從x開始的數(shù)字標(biāo)記,那么:
G = nx.convert_node_labels_to_integers(G, first_label=x)

2.4 Graph基本信息獲取

nx.info(G) # 圖信息的概覽
G.number_of_nodes()
G.number_of_edges()
# 獲取和節(jié)點(diǎn)idx連接的邊的attr屬性之和
G.in_degree(idx, weight='attr')
 
# 如果想知道某個(gè)結(jié)點(diǎn)相連的某個(gè)邊權(quán)之和:
DG.degree(nodeIdx, weight='weightName')
 
# 獲取結(jié)點(diǎn)或者邊的屬性集合,返回的是元組的列表
G.nodes.data('attrName')
G.edges.data('attrName')
 
# 獲取n1 n2的邊的length權(quán)重,那么:
G[n1][n2]['length']
# 如果是有重邊的圖,選擇n1,n2第一條邊的length權(quán)重,則:
G[n1][n2][0]['length']
 
# 獲取n1結(jié)點(diǎn)的所有鄰居
nx.all_neighbors(G, n1)
 
# 判斷圖中n1到n2是否存在路徑
nx.has_path(G, n1, n2)
# 根據(jù)一個(gè)結(jié)點(diǎn)的list,獲取子圖
subG = nx.subgraph(G, nodeList)

2.5 Graph的繪制

# 最簡單的繪制
import matplotlib.pyplot as plt
nx.draw(G)
plt.show()
 
# 設(shè)置其他相關(guān)參數(shù)
nx.draw(G,
    with_labels=True,
    pos = nx.sprint_layout(G),
    node_color=color_list,
    edge_color='k',
    node_size=100,
    node_shape='o',
    linewidths=2,
    width=1.0,
    alpha=0.55,
    style='solid',
    font_size=9,
    font_color='k'
)

2.6 Graph的其他內(nèi)置算法

# 最短路算法 返回最短路的路徑列表
nx.shortest_path(G, n1, n2, method='dijkstra')

# 以及各種圖的算法,比如流,割等等等等,大家可以看文檔探索下

3 其他

3.1 read_edgelist( )

Read a graph from a list of edges.
函數(shù)定義如下:

 read_edgelist(path, comments='#', delimiter=None, create_using=None, nodetype=None, data=True, edgetype=None, encoding='utf-8'):
    '''
    Read a graph from a list of edges.
    
    Parameters :	
    path : file or string
        File or filename to write. If a file is provided, it must be opened in ‘rb' mode. Filenames ending in .gz or .bz2 will be uncompressed.

    comments : string, optional
        The character used to indicate the start of a comment.
        
    delimiter : string, optional
        The string used to separate values. The default is whitespace.
        
    create_using : Graph container, optional,
        Use specified container to build graph. The default is networkx.Graph, an undirected graph.

    nodetype : int, float, str, Python type, optional
        Convert node data from strings to specified type

    data : bool or list of (label,type) tuples
        Tuples specifying dictionary key names and types for edge data

    edgetype : int, float, str, Python type, optional OBSOLETE
        Convert edge data from strings to specified type and use as ‘weight'

    encoding: string, optional
        Specify which encoding to use when reading file.

    Returns :	
    G : graph
        A networkx Graph or other type specified with create_using
    '''

樣例:

nx.write_edgelist(nx.path_graph(4), "test.edgelist")
G=nx.read_edgelist("test.edgelist")

fh=open("test.edgelist", 'rb')
G=nx.read_edgelist(fh)
fh.close()

G=nx.read_edgelist("test.edgelist", nodetype=int) G=nx.read_edgelist("test.edgelist",create_using=nx.DiGraph())

到此這篇關(guān)于Python中的Networkx詳解的文章就介紹到這了,更多相關(guān)Python中Networkx內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

酉阳| 陆河县| 广河县| 名山县| 岳阳县| 开阳县| 喀什市| 仙桃市| 江阴市| 平陆县| 长沙市| 岳普湖县| 平昌县| 建瓯市| 驻马店市| 曲阜市| 元朗区| 定南县| 治多县| 通榆县| 虞城县| 靖远县| 桐庐县| 应用必备| 泰兴市| 双鸭山市| 邮箱| 康保县| 抚宁县| 错那县| 且末县| 云南省| 元谋县| 大同县| 闽侯县| 禹州市| 清丰县| 荔浦县| 宜宾县| 疏附县| 安义县|