5

More than 3 years have passed since last update.

# 組合せ最適化 - 典型問題 - 最小全域木問題

Last updated at Posted at 2015-07-10

## 実行方法

usage
Signature: nx.minimum_spanning_tree(G, weight='weight')
Docstring:
Return a minimum spanning tree or forest of an undirected
weighted graph.

A minimum spanning tree is a subgraph of the graph (a tree) with
the minimum sum of edge weights.

python
# CSVデータ
import pandas as pd, networkx as nx, matplotlib.pyplot as plt
from ortoolpy import graph_from_table, networkx_draw
g = graph_from_table(tbn, tbe)[0]
t = nx.minimum_spanning_tree(g)
pos = networkx_draw(g)
nx.draw_networkx_edges(t, pos, width=3)
plt.show()
print(t.edges())


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


python
# pandas.DataFrame
from ortoolpy.optimization import MinimumSpanningTree
MinimumSpanningTree('data/edge0.csv')

node1 node2 capacity weight
0 0 1 2 1
1 0 3 2 2
2 0 4 2 2
3 2 3 2 3
4 4 5 2 1
python
# 乱数データ
import math, networkx as nx, matplotlib.pyplot as plt
from ortoolpy import networkx_draw
g = nx.random_graphs.fast_gnp_random_graph(10, 0.3, 1)
pos = nx.spring_layout(g)
for i, j in g.edges():
t = nx.minimum_spanning_tree(g)
pos = networkx_draw(g, nx.spring_layout(g))
nx.draw_networkx_edges(t, pos, width=3)
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
3. You can use dark theme
What you can do with signing up
5