1. 求无向图中任意两点间最短路径
# -*- coding: cp936 -*-
import networkx as nx
import 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)])
paths = dict(nx.all_pairs_shortest_path(G))
print(paths)
nx.draw(G, with_labels=True)
plt.show()
{1: {1: [1], 2: [1, 2], 3: [1, 3], 4: [1, 4], 5: [1, 5], 6: [1, 4, 6]}, 2: {2: [2], 1: [2, 1], 3: [2, 1, 3], 4: [2, 1, 4], 5: [2, 1, 5], 6: [2, 1, 4, 6]}, 3: {3: [3], 1: [3, 1], 2: [3, 1, 2], 4: [3, 1, 4], 5: [3, 1, 5], 6: [3, 1, 4, 6]}, 4: {4: [4], 1: [4, 1], 5: [4, 5], 6: [4, 6], 2: [4, 1, 2], 3: [4, 1, 3]}, 5: {5: [5], 1: [5, 1], 4: [5, 4], 6: [5, 6], 2: [5, 1, 2], 3: [5, 1, 3]}, 6: {6: [6], 4: [6, 4], 5: [6, 5], 1: [6, 4, 1], 2: [6, 4, 1, 2], 3: [6, 4, 1, 3]}}
2. 图中两点的最短路径
nx.shortest_path_length(G,1,4)
import networkx as nx
G=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(n)
except nx.NetworkXNoPath:
print('No path')
3. 强连通,弱连通
- 强连通:有向图中任意两点v1、v2间存在v1到v2的路径(path)及v2到v1的路径。
- 弱联通:将有向图的所有的有向边替换为无向边,所得到的图称为原图的基图。如果一个有向图的基图是连通图,则有向图是弱连通图。
例1:弱连通
# -*- coding:utf8-*-
import networkx as nx
import matplotlib.pyplot as plt
# G = nx.path_graph(4, create_using=nx.Graph())
# 0 1 2 3
G = nx.path_graph(4, create_using=nx.DiGraph()) # 默认生成节点0 1 2 3,生成有向变0->1,1->2,2->3
nx.add_path(G,[7, 8, 3]) # 生成有向边:7->8->3
for c in nx.weakly_connected_components(G):
print(c)
print([len(c) for c in sorted(nx.weakly_connected_components(G), key=len, reverse=True)])
nx.draw(G,with_labels=True)
plt.savefig("youxiangtu.png")
plt.show()
{0, 1, 2, 3, 7, 8}
[6]
例2:强连通
# -*- coding:utf8-*-
import networkx as nx
import matplotlib.pyplot as plt
G = nx.path_graph(4, create_using=nx.DiGraph())
nx.add_path(G, [3, 8, 1])
con = nx.strongly_connected_components(G)
print(con)
print(type(con))
print(list(con))
nx.draw(G, with_labels=True)
plt.savefig("youxiangtu.png")
plt.show()
<generator object strongly_connected_components at 0x000001DC7B628580>
<class 'generator'>
[{8, 1, 2, 3}, {0}]
4. 子图
# -*- coding:utf8-*-
import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph()
nx.add_path(G,[5, 6, 7, 8])
sub_graph = G.subgraph([5, 6, 8])
# sub_graph = G.subgraph((5, 6, 8)) #ok 一样
nx.draw(sub_graph, with_labels=True)
plt.savefig("youxiangtu.png")
plt.show()
5. 条件过滤
原图
#-*- coding:utf8-*-
import networkx as nx
import matplotlib.pyplot as plt
G = 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, with_labels=True)
plt.savefig("youxiangtu.png")
plt.show()
过滤函数:
#-*- coding:utf8-*-
import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph()
def flt_func_draw():
flt_func = lambda d: d['id'] != 1
return flt_func
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.items())
G.add_edges_from(road_edges)
flt_func = flt_func_draw()
part_G = G.subgraph(n for n, d in G.nodes(data=True) if flt_func(d))
nx.draw(part_G, with_labels=True)
plt.savefig("youxiangtu.png")
plt.show()
6. 前驱和后继节点
pred,succ
#-*- coding:utf8-*-
import networkx as nx
import matplotlib.pyplot as plt
G = 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.items())
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()
['a', 'b', 'c', 'd']
[('a', 'b'), ('a', 'c'), ('c', 'd')]
a's pred {}
b's pred {'a': {}}
c's pred {'a': {}}
d's pred {'c': {}}
a's succ {'b': {}, 'c': {}}
b's succ {}
c's succ {'d': {}}
d's succ {}