中文字幕理论片,69视频免费在线观看,亚洲成人app,国产1级毛片,刘涛最大尺度戏视频,欧美亚洲美女视频,2021韩国美女仙女屋vip视频

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
python復(fù)雜網(wǎng)絡(luò)分析庫NetworkX

NetworkX是一個用Python語言開發(fā)的圖論與復(fù)雜網(wǎng)絡(luò)建模工具,內(nèi)置了常用的圖與復(fù)雜網(wǎng)絡(luò)分析算法,可以方便的進(jìn)行復(fù)雜網(wǎng)絡(luò)數(shù)據(jù)分析、仿真建模等工作。networkx支持創(chuàng)建簡單無向圖、有向圖和多重圖(multigraph);內(nèi)置許多標(biāo)準(zhǔn)的圖論算法,節(jié)點可為任意數(shù)據(jù);支持任意的邊值維度,功能豐富,簡單易用。

引入模塊

import networkx as nxprint nx

無向圖

例1:

#!-*- coding:utf8-*- import networkx as nximport matplotlib.pyplot as pltG = nx.Graph()                 #建立一個空的無向圖GG.add_node(1)                  #添加一個節(jié)點1G.add_edge(2,3)                #添加一條邊2-3(隱含著添加了兩個節(jié)點2、3)G.add_edge(3,2)                #對于無向圖,邊3-2與邊2-3被認(rèn)為是一條邊print "nodes:", G.nodes()      #輸出全部的節(jié)點: [1, 2, 3]print "edges:", G.edges()      #輸出全部的邊:[(2, 3)]print "number of edges:", G.number_of_edges()   #輸出邊的數(shù)量:1nx.draw(G)plt.savefig("wuxiangtu.png")plt.show()

輸出

1
2
3
nodes: [1, 2, 3]
edges: [(2, 3)]
number of edges: 1

例2:

#-*- coding:utf8-*- import networkx as nximport matplotlib.pyplot as pltG = nx.DiGraph()G.add_node(1)G.add_node(2)                  #加點G.add_nodes_from([3,4,5,6])    #加點集合G.add_cycle([1,2,3,4])         #加環(huán)G.add_edge(1,3)     G.add_edges_from([(3,5),(3,6),(6,7)])  #加邊集合nx.draw(G)plt.savefig("youxiangtu.png")plt.show()

有向圖

例1:

#!-*- coding:utf8-*- import networkx as nximport matplotlib.pyplot as pltG = nx.DiGraph()G.add_node(1)G.add_node(2)G.add_nodes_from([3,4,5,6])G.add_cycle([1,2,3,4])G.add_edge(1,3)G.add_edges_from([(3,5),(3,6),(6,7)])nx.draw(G)plt.savefig("youxiangtu.png")plt.show()

:有向圖和無向圖可以互相轉(zhuǎn)換,使用函數(shù):

  • Graph.to_undirected()
  • Graph.to_directed()

例2,例子中把有向圖轉(zhuǎn)化為無向圖:

#!-*- coding:utf8-*- import networkx as nximport matplotlib.pyplot as pltG = nx.DiGraph()G.add_node(1)G.add_node(2)G.add_nodes_from([3,4,5,6])G.add_cycle([1,2,3,4])G.add_edge(1,3)G.add_edges_from([(3,5),(3,6),(6,7)])G = G.to_undirected()nx.draw(G)plt.savefig("wuxiangtu.png")plt.show()

注意區(qū)分以下2例

例3-1

#-*- coding:utf8-*-import networkx as nximport matplotlib.pyplot as pltG = nx.DiGraph()road_nodes = {'a': 1, 'b': 2, 'c': 3}#road_nodes = {'a':{1:1}, 'b':{2:2}, 'c':{3:3}}road_edges = [('a', 'b'), ('b', 'c')]G.add_nodes_from(road_nodes.iteritems())G.add_edges_from(road_edges)nx.draw(G)plt.savefig("youxiangtu.png")plt.show()

例3-2

#-*- coding:utf8-*-import networkx as nximport matplotlib.pyplot as pltG = nx.DiGraph()#road_nodes = {'a': 1, 'b': 2, 'c': 3}road_nodes = {'a':{1:1}, 'b':{2:2}, 'c':{3:3}}road_edges = [('a', 'b'), ('b', 'c')]G.add_nodes_from(road_nodes.iteritems())G.add_edges_from(road_edges)nx.draw(G)plt.savefig("youxiangtu.png")plt.show()

加權(quán)圖

有向圖和無向圖都可以給邊賦予權(quán)重,用到的方法是add_weighted_edges_from,它接受1個或多個三元組[u,v,w]作為參數(shù),其中u是起點,v是終點,w是權(quán)重。

例1:

#!-*- coding:utf8-*- import networkx as nximport matplotlib.pyplot as pltG = nx.Graph()                                        #建立一個空的無向圖GG.add_edge(2,3)                                     #添加一條邊2-3(隱含著添加了兩個節(jié)點2、3)G.add_weighted_edges_from([(3, 4, 3.5),(3, 5, 7.0)])                                     #對于無向圖,邊3-2與邊2-3被認(rèn)為是一條邊print G.get_edge_data(2, 3)print G.get_edge_data(3, 4)print G.get_edge_data(3, 5)nx.draw(G)plt.savefig("wuxiangtu.png")plt.show()

輸出

{}{'weight': 3.5}{'weight': 7.0}

 

經(jīng)典圖論算法計算

計算1:求無向圖的任意兩點間的最短路徑

# -*- coding: cp936 -*-import networkx as nximport matplotlib.pyplot as plt #計算1:求無向圖的任意兩點間的最短路徑G = nx.Graph()G.add_edges_from([(1,2),(1,3),(1,4),(1,5),(4,5),(4,6),(5,6)])path = nx.all_pairs_shortest_path(G)print path[1]

計算2:找圖中兩個點的最短路徑

import networkx as nxG=nx.Graph()G.add_nodes_from([1,2,3,4])G.add_edge(1,2)G.add_edge(3,4)try:    n=nx.shortest_path_length(G,1,4)    print nexcept nx.NetworkXNoPath:    print 'No path'

強(qiáng)連通、弱連通

  • 強(qiáng)連通:有向圖中任意兩點v1、v2間存在v1到v2的路徑(path)及v2到v1的路徑。
  • 弱聯(lián)通:將有向圖的所有的有向邊替換為無向邊,所得到的圖稱為原圖的基圖。如果一個有向圖的基圖是連通圖,則有向圖是弱連通圖。

距離

例1:弱連通

#-*- coding:utf8-*- import networkx as nximport matplotlib.pyplot as plt#G = nx.path_graph(4, create_using=nx.Graph())#0 1 2 3G = nx.path_graph(4, create_using=nx.DiGraph())    #默認(rèn)生成節(jié)點0 1 2 3,生成有向變0->1,1->2,2->3G.add_path([7, 8, 3])  #生成有向邊:7->8->3for c in nx.weakly_connected_components(G):    print cprint [len(c) for c in sorted(nx.weakly_connected_components(G), key=len, reverse=True)]nx.draw(G)plt.savefig("youxiangtu.png")plt.show()

執(zhí)行結(jié)果

set([0, 1, 2, 3, 7, 8])[6]

例2:強(qiáng)連通

#-*- coding:utf8-*- import networkx as nximport matplotlib.pyplot as plt#G = nx.path_graph(4, create_using=nx.Graph())#0 1 2 3G = nx.path_graph(4, create_using=nx.DiGraph())G.add_path([3, 8, 1])#for c in nx.strongly_connected_components(G):#    print c##print [len(c) for c in sorted(nx.strongly_connected_components(G), key=len, reverse=True)]con = nx.strongly_connected_components(G)print conprint type(con)print list(con)nx.draw(G)plt.savefig("youxiangtu.png")plt.show()

執(zhí)行結(jié)果

<generator object strongly_connected_components at 0x0000000008AA1D80><type 'generator'>[set([8, 1, 2, 3]), set([0])]

子圖

#-*- coding:utf8-*- import networkx as nximport matplotlib.pyplot as pltG = nx.DiGraph()G.add_path([5, 6, 7, 8])sub_graph = G.subgraph([5, 6, 8])#sub_graph = G.subgraph((5, 6, 8))  #ok  一樣nx.draw(sub_graph)plt.savefig("youxiangtu.png")plt.show()

條件過濾

#原圖

#-*- coding:utf8-*-import networkx as nximport matplotlib.pyplot as pltG = nx.DiGraph()road_nodes = {'a':{'id':1}, 'b':{'id':1}, 'c':{'id':3}, 'd':{'id':4}}road_edges = [('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'd')]G.add_nodes_from(road_nodes)G.add_edges_from(road_edges)nx.draw(G)plt.savefig("youxiangtu.png")plt.show()

#過濾函數(shù)

#-*- coding:utf8-*-import networkx as nximport matplotlib.pyplot as pltG = nx.DiGraph()def flt_func_draw():    flt_func = lambda d: d['id'] != 1    return flt_funcroad_nodes = {'a':{'id':1}, 'b':{'id':1}, 'c':{'id':3}, 'd':{'id':4}}road_edges = [('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'd')]G.add_nodes_from(road_nodes.iteritems())G.add_edges_from(road_edges)flt_func = flt_func_draw()part_G = G.subgraph(n for n, d in G.nodes_iter(data=True) if flt_func(d))nx.draw(part_G)plt.savefig("youxiangtu.png")plt.show()

pred,succ

#-*- coding:utf8-*-import networkx as nximport matplotlib.pyplot as pltG = nx.DiGraph()road_nodes = {'a':{'id':1}, 'b':{'id':1}, 'c':{'id':3}}road_edges = [('a', 'b'), ('a', 'c'), ('c', 'd')]G.add_nodes_from(road_nodes.iteritems())G.add_edges_from(road_edges)print G.nodes()print G.edges()print "a's pred ", G.pred['a']print "b's pred ", G.pred['b']print "c's pred ", G.pred['c']print "d's pred ", G.pred['d']print "a's succ ", G.succ['a']print "b's succ ", G.succ['b']print "c's succ ", G.succ['c']print "d's succ ", G.succ['d']nx.draw(G)plt.savefig("wuxiangtu.png")plt.draw()

結(jié)果

1
2
3
4
5
6
7
8
9
10
11
12
['a', 'c', 'b', 'd']
[('a', 'c'), ('a', 'b'), ('c', 'd')]
a's pred  {}
b's pred  {'a': {}}
c's pred  {'a': {}}
d's pred  {'c': {}}
a's succ  {'c': {}, 'b': {}}
b's succ  {}
c's succ  {'d': {}}
d's succ  {}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
利用Python繪制關(guān)系網(wǎng)絡(luò)圖
Networkx study notes(0)
使用networkx及matplotlib庫實現(xiàn)社會網(wǎng)絡(luò)分析及可視化
科學(xué)網(wǎng)—復(fù)雜網(wǎng)絡(luò)分析庫NetworkX學(xué)習(xí)筆記(2):統(tǒng)計指標(biāo)計算
用 GraphScope 像 NetworkX 一樣做圖分析
直播案例 | 決策樹、隨機(jī)森林和 AdaBoost 的 Python 實現(xiàn)
更多類似文章 >>
生活服務(wù)
熱點新聞
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服