Networkx常⽤算法和求最短路径介绍求最短路径难道很多⼈没有发现⽹上很多⼈给出的函数是对⽆权图求的么?默认边的权值都为1,其实
可以按照给定的权重求,⽂章后⾯有介绍
Algorithms
⼤概介绍下,选择⽐较有⽤的介绍下
In [98]:
from networkx.algorithms import approximation as apxa
In [1]: import networkx as nx
def draw(g): #显⽰图
if'plt'not in dir(): #可以查看很多东西,del 模块名可以删除模块
import matplotlib.pyplot as plt #可以发现plt只存在函数中,全局中并没有plt
pos=nx.spring_layout(G)
2,\
plt.show()
In [5]:
G=nx.Graph()
G.add_path([1,2,3,4,5,6])
draw(G)
连通性
In [8]:
nx.all_pairs_node_connectivity(G)
Out[8]:
{1: {2:1, 3: 1, 4: 1, 5: 1, 6: 1},
2: {1: 1, 3: 1, 4: 1, 5: 1, 6:1},
3: {1: 1, 2: 1, 4: 1, 5: 1, 6:1},
4: {1: 1, 2: 1, 3: 1, 5: 1, 6:1},
5: {1: 1, 2: 1, 3: 1, 4: 1, 6:1},
6: {1: 1, 2: 1, 3: 1, 4: 1, 5: 1}}
In [110]:
# local_node_connectivity(G, source, target,cutoff=None)
from networkx.algorithms import approximation as approx
G4 =nx.icosahedral_graph()
approx.local_node_connectivity(G4, 0, 6)
Out[110]:
5
In [111]:
draw(G4)
K-components
In [12]:
# k_components(G[, min_density])#Returns theapproximate k-component structure of a graph G.
In [15]:
k_components= apxa.k_components(G)
k_components
Out[15]: defaultdict(<type'list'>, {1: [t([1, 2, 3, 4, 5, 6])]})
In [16]:
draw(G)
Clique 团
Clustering
Estimatesthe average clustering coefficient of G.
dominating t
控制集
In [8]: G=nx.Graph()
G.add_star([1,2,3,4,5])
In [9]: # min_weighted_dominating_t(G[, weight])
apxa.min_weighted_dominating_t(G)
Out[9]: {1}
In [10]: apxa.min_edge_dominating_t(G)
Out[10]: {(1, 2)}
Independent Set
In [12]: apxa.maximum_independent_t(G)
#独⽴集或稳定集是⼀个图中的⼀组顶点,没有两个是相邻的。
Out[12]: {2, 3, 4,5}
Matching
In [13]: # Given a graph G = (V,E), a matching M in G is a tof pairwi non-adjacent edges;
# that is, no two edges share a common vertex.
In [14]: apxa.min_maximal_matching(G)
Out[14]: {(1, 2)}
vertex cover
点覆盖
你怎么那么美
In [ ]: # min_weighted_vertex_cover(G[, weight])
In [15]: apxa.min_weighted_vertex_cover(G)
Out[15]: {1, 2}
爸爸在学校上我
Graphical degree quence
Testquences for graphiness.
In [16]: #判断序列能否构成图
Minimum Spanning Tree
最⼩⽣成树
In [19]: # minimum_spanning_tree(G[, weight])
draw(nx.minimum_spanning_tree(G))
In [20]: # minimum_spanning_edges(G[, weight, data]) 最⼩⽣成树的边
cycle_basis
寻找图中的环
In [3]: G=nx.Graph()
G.add_cycle([0,1,2,3]) # 0-1-2-3-0
draw(G)
In [5]: G.add_cycle([4,5,6,7])
draw(G)
In [6]: nx.cycle_basis(G,0)
Out[6]: [[1, 2,3, 0], [5, 6, 7, 4]]
simple_cycles
Findsimple cycles (elementary circuits) of a directed graph.
In [8]: G = nx.DiGraph([(0, 0), (0, 1), (0, 2), (1, 2), (2, 0), (2, 1), (2, 2)])
draw(G)
In [12]: list(nx.simple_cycles(G))
Out[12]: [[0, 2],[0, 1, 2], [0], [1, 2], [2]]
find_cycle
没有环回报错
梦见收拾行李In [14]: nx.find_cycle(G, orientation='original')#orientation ('original' | 'rever' |'ignore')
Out[14]: [(0, 0)]
Distance Measures
1center(G[, e]) Return the center of the graph G. 2 diameter(G[, e]) Return thediameter of the graph G. 3 eccentricity(G[, v, sp]) Return the eccentricity ofnodes in G. 4 periphery(G[, e]) Return the periphery of the graph G. 5radius(G[, e]) Return the radius of the graph G.
Eulerian
欧拉图
In [16]: # is_eulerian(G) Return True if G is an Eulerian graph, Fal otherwi.
# eulerian_circuit(G[, source]) Return the edges ofan Eulerian circuit in G.
Matching
In [17]: # maximal_matching(G) Find a maximal cardinality matching in thegraph.
# max_weight_matching(G[, maxcardinality]) Compute a maximum-weighted matching of G. Shortest Paths
无我之境
适⽤于有向图和⽆向图
In [45]: #创建⼀个⽤例图:
G=nx.Graph()
dic1 = [(1,2,{'weight':1}),(2,4,{'weight':2}),
(1,3,{'weight':3}),(3,4,{'weight':4}),
(1,4,{'weight':5}),(5,6,{'weight':6})]
G.add_edges_from(dic1)
draw(G)
shortest_path(G, source=None,target=None, weight=None)
forunweighted graphs
In [18]: # weight (None or string, optional (default = None))– If None, every edge has weight/distance/
cost 1. # If a string, u this edge attribute as the edgeweight. Any edge attribute not prent defaults to 1.
In [21]: #当有多条最短路径时,只返回其中的⼀条
G=nx.path_graph(5)
draw(G)
In [9]:
nx.shortest_path(G,source=1,target=4)
Out[9]:
In [18]: G.clear()
In [28]: G.add_weighted_edges_from([(0,1,1),(1,2,2),(0,2,0.5)])
nx.shortest_path(G,source=0,target=2)
Out[28]: [0, 2]
In [25]: draw(G)
In [19]: G.add_weighted_edges_from([(0,1,1),(1,2,2),(0,2,15)])
nx.shortest_path(G,source=0,target=2,weight='123')
Out[19]: [0, 2]
all_shortest_paths(G, source,target, weight=None)
forunweighted graphs
In [1]: al import mathjax; mathjax.install_mathjax()
包村Out[1]: 0
shortest_path_length(G, source=None,target=None, weight=None)
forunweighted graphs
In [3]: # Rais:
# NetworkXNoPath – If no path exists between sourceand target.
average_shortest_path_length(G,weight=None)
In [13]:
# nx.average_shortest_path_length(G) #要求连接图
g1=nx.Graph(G.subgraph([1,2,3,4]))
nx.average_shortest_path_length(g1)
#计算公式是:sum(s,t属于v)d(s,t)/(n*(n-1))
Out[13]: 1.1666666666666667
Simple Paths
all_simple_paths(G, source, target[,cutoff])
shortest_simple_paths(G, source,target[, ...])
Generateall simple paths in the graph G from source to target, starting from shortestones.
In [6]: G = nx.cycle_graph(7)
paths = list(nx.shortest_simple_paths(G, 0, 3))
撒组词print(paths)
[[0, 1,2, 3], [0, 6, 5, 4, 3]]
has_path
In [8]: nx.has_path(G, source=0, target=3)
Out[8]: True
single_source_shortest_path(G,source, cutoff=None)
forunweighted graphs
In [16]: nx.single_source_shortest_path(G,2)
Out[16]:
1: [2, 1],
2: [2],
3: [2, 3],
4: [2, 4],
5: [2, 4, 5],
6: [2, 0, 6]}
In [38]: G.edges(data=True)
Out[38]: [(0, 1,{'weight': 1}), (0, 2, {'weight': 15}), (1, 2, {'weight': 3})]
single_source_shortest_path_length(G,source)
predecessor(G, source[, target,cutoff, ...])
forunweighted graphs祖国我爱你演讲稿
In [14]: nx.predecessor(G,1)
Out[14]: {1: [],2: [1], 3: [1], 4: [1]}
Shortest path algorithms for weighed graphs.
dijkstra_path(G, source, target[,weight])
In [16]: nx.dijkstra_path(G,1,4) #五权重的是1-4
Out[16]: [1, 2, 4]
dijkstra_path_length(G, source,target[, weight])
In [17]: nx.dijkstra_path_length(G,1,4)
Out[17]: 3
single_source_dijkstra_path(G,source, cutoff=None, weight='weight')
In [20]: p=nx.single_source_dijkstra_path(G,1)
In [22]: p[4] #1-4的最短路径
Out[22]: [1, 2, 4]
single_source_dijkstra_path_length(G,source)
all_pairs_dijkstra_path(G,cutoff=None, weight='weight')
In [26]: p1=nx.all_pairs_dijkstra_path(G)
p1[1][4]
Out[26]: [1, 2, 4]
all_pairs_dijkstra_path_length(G,cutoff=None, weight='weight')
single_source_dijkstra(G, source,target=None, cutoff=None, weight='weight')
In [28]: length,path=nx.single_source_dijkstra(G,1,4) # shortest paths and lengths in a weighted graph G. bidirectional_dijkstra(G, source, target,weight='weight')
Dijkstra’salgorithm for shortest paths using bidirectional(双向) arch.
In [30]: length,path=nx.bidirectional_dijkstra(G,1,4)宝宝发烧吃什么好
path
Out[30]: [1, 2, 4]
In [35]: