官方文檔:
https://networkx.github.io/documentation/networkx-1.10/reference/algorithms.html
最短路和最小生成樹:
import networkx as nx import matplotlib.pyplot as plt G = nx.Graph() #G.add_node(1) #添加一個節點1 #G.add_edge(2,3,10) #添加一條邊2-3(隱含着添加了兩個節點2、3) #G.add_edge(3,2) #對於無向圖,邊3-2與邊2-3被認為是一條邊 #G.add_weighted_edges_from([(1,2,8)]) #G.add_weighted_edges_from([(1,3,10)]) #G.add_weighted_edges_from([(2,3,6)]) G.add_edge('A', 'B', weight=4) G.add_edge('B', 'D', weight=2) G.add_edge('A', 'C', weight=3) G.add_edge('C', 'D', weight=5) G.add_edge('A', 'D', weight=6) G.add_edge('C', 'F', weight=7) G.add_edge('A', 'G', weight=1) G.add_edge('H', 'B', weight=2) for u,v,d in G.edges(data=True): print(u,v,d['weight']) edge_labels=dict([((u,v,),d['weight']) for u,v,d in G.edges(data=True)]) #fixed_position = {'A':[ 1., 2.], # 'B': [ 1., 0.], # 'D': [ 5., 5.], # 'C': [ 0.,6.]}#每個點在坐標軸中的位置 #pos=nx.spring_layout(G,pos = fixed_position)#獲取結點的位置,每次點的位置都是隨機的 pos = nx.spring_layout(G) #也可以不固定點 nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels,font_size=14)#繪制圖中邊的權重 print(edge_labels) print("nodes:", G.nodes()) #輸出全部的節點: [1, 2, 3] print("edges:", G.edges()) #輸出全部的邊:[(2, 3)] print("number of edges:", G.number_of_edges()) #輸出邊的數量 nx.draw_networkx(G,pos,node_size=400) plt.savefig("wuxiangtu.png") plt.show() # 生成鄰接矩陣 mat = nx.to_numpy_matrix(G) print(mat) # 計算兩點間的最短路 # dijkstra_path print('dijkstra方法尋找最短路徑:') path=nx.dijkstra_path(G, source='H', target='F') print('節點H到F的路徑:', path) print('dijkstra方法尋找最短距離:') distance=nx.dijkstra_path_length(G, source='H', target='F') print('節點H到F的距離為:', distance) # 一點到所有點的最短路 p=nx.shortest_path(G,source='H') # target not specified d=nx.shortest_path_length(G,source='H') for node in G.nodes(): print("H 到",node,"的最短路徑為:",p[node]) print("H 到",node,"的最短距離為:",d[node]) # 所有點到一點的最短距離 p=nx.shortest_path(G,target='H') # target not specified d=nx.shortest_path_length(G,target='H') for node in G.nodes(): print(node,"到 H 的最短路徑為:",p[node]) print(node,"到 H 的最短距離為:",d[node]) # 任意兩點間的最短距離 p=nx.shortest_path_length(G) p=dict(p) d=nx.shortest_path_length(G) d=dict(d) for node1 in G.nodes(): for node2 in G.nodes(): print(node1,"到",node2,"的最短距離為:",d[node1][node2]) # 最小生成樹 T=nx.minimum_spanning_tree(G) # 邊有權重 print(sorted(T.edges(data=True))) mst=nx.minimum_spanning_edges(G,data=False) # a generator of MST edges edgelist=list(mst) # make a list of the edges print(sorted(edgelist)) # 使用A *算法的最短路徑和路徑長度 p=nx.astar_path(G, source='H', target='F') print('節點H到F的路徑:', path) d=nx.astar_path_length(G, source='H', target='F') print('節點H到F的距離為:', distance) # 找回路 hl = nx.algorithms.find_cycle(G) print(hl) # 二分圖匹配 G = nx.complete_bipartite_graph(2, 3) nx.draw_networkx(G) left, right = nx.bipartite.sets(G) list(left) #[0, 1] list(right) #[2, 3, 4] p = nx.bipartite.maximum_matching(G) print("輸出匹配:",p) # 最大流 # graph's maximum flow # flow is computed between vertex 0 and vertex n-1 # expected input format: # n # m #g = nx.DiGraph() #n, m = int(input()), int(input()) #for i in range(n): # g.add_node(i) #for _ in range(m): # a, b, c = [ int(i) for i in input().split(' ') ] # g.add_edge(a, b, capacity=c) #max_flow = nx.algorithms.flow.maxflow.maximum_flow(g, 0, n-1)[0] #print(max_flow) g = nx.DiGraph() n, m = 4, 5 for i in range(n): g.add_node(i) edge=["0 1 3","1 3 2","0 2 2","2 3 3","0 3 1"] for x in edge: a, b, c = [ int(i) for i in x.split(' ') ] g.add_edge(a, b, capacity=c) nx.draw(g) max_flow = nx.algorithms.flow.maxflow.maximum_flow(g, 0, n-1)[0] print(max_flow)