14

More than 3 years have passed since last update.

# 組合せ最適化 - 典型問題 - 最大流問題

Last updated at Posted at 2015-07-10

## 最大流問題

グラフ$G=(V,E)$の各辺$e_{ij}=(v_i,v_j)\in E$が容量$c_{ij}$をもつとき、始点$v_s \in V$(ソース)から終点$v_t \in V$(シンク)への総流量が最大となるフローを求めよ。

## 実行方法

usage
Signature: nx.maximum_flow(G, s, t, capacity='capacity', flow_func=None, **kwargs)
Docstring:
Find a maximum single-commodity flow.

python
# CSVデータ
import pandas as pd, networkx as nx
from ortoolpy import graph_from_table, networkx_draw
g = graph_from_table(tbn, tbe)[0]
t = nx.maximum_flow(g, 5, 2)
pos = networkx_draw(g)
nx.draw_networkx_edges(g, pos, width=3, edgelist
=[(k1, k2) for k1, d in t[1].items() for k2, v in d.items() if v])
plt.show()
for i, d in t[1].items():
for j, f in d.items():
if f: print((i, j), f)


(0, 2) 2
(0, 3) 2
(1, 2) 2
(3, 2) 2
(4, 0) 2
(5, 0) 2
(5, 1) 2
(5, 4) 2


python
# pandas.DataFrame
from ortoolpy.optimization import MaximumFlow
MaximumFlow('data/edge0.csv', 5, 2)[1]

node1 node2 capacity weight flow
0 0 2 2 4 2
1 0 3 2 2 2
2 0 4 2 2 2
3 0 5 2 4 2
4 1 2 2 5 2
5 1 5 2 5 2
6 2 3 2 3 2
7 4 5 2 1 2
python
# 乱数データ
import networkx as nx, matplotlib.pyplot as plt
from ortoolpy import networkx_draw
g = nx.random_graphs.fast_gnp_random_graph(10, 0.3, 1)
for i, j in g.edges():
t = nx.maximum_flow(g, 5, 6)
pos = networkx_draw(g, nx.spring_layout(g))
nx.draw_networkx_edges(g, pos, width=3, edgelist
=[(k1, k2) for k1, d in t[1].items() for k2, v in d.items() if v])
plt.show()


## データ

Register as a new user and use Qiita more conveniently

1. You get articles that match your needs
2. You can efficiently read back useful information
What you can do with signing up
14