LoginSignup
6

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$が重み$a_{ij}$をもつとき、始点$v_s \in V$から終点$v_t \in V$への路の中で最も重みの和の小さいものを求めよ。

実行方法

usage
Signature: nx.dijkstra_path(G, source, target, weight='weight')
Docstring:
Returns the shortest path from source to target in a weighted graph G.
python
# CSVデータ
import pandas as pd, networkx as nx
from ortoolpy import graph_from_table
tbn = pd.read_csv('data/node0.csv')
tbe = pd.read_csv('data/edge0.csv')
g = graph_from_table(tbn, tbe)[0]
print(nx.dijkstra_path(g, 5, 2))
結果
[5, 4, 0, 2]
python
# pandas.DataFrame
from ortoolpy.optimization import DijkstraPath
DijkstraPath('data/edge0.csv', 5, 2)
node1 node2 capacity weight
9 4 5 2 1
3 0 4 2 2
1 0 2 2 4
python
# 乱数データ
import networkx as nx
g = nx.fast_gnp_random_graph(8, 0.26, 1)
print(nx.dijkstra_path(g, 0, 2))
結果
[0, 1, 6, 3, 5, 2]

dij.png

データ

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
  3. You can use dark theme
What you can do with signing up
6