Typical problem and execution method
Find the one with the smallest sum of weights of the vertex cover $ C $ in the undirected graph $ G = (V, E) $.
usage
Signature: min_node_cover(g, weight='weight')
Docstring:
Vertex cover problem
input
g:Graph
weight:Weight attribute character
output
Vertex list
python
#CSV data
import pandas as pd, networkx as nx, matplotlib.pyplot as plt
from ortoolpy import graph_from_table, networkx_draw, min_node_cover
tbn = pd.read_csv('data/node0.csv')
tbe = pd.read_csv('data/edge0.csv')
g = graph_from_table(tbn, tbe)[0]
t = min_node_cover(g)
pos = networkx_draw(g, node_color='white')
nx.draw_networkx_nodes(g, pos, nodelist=t)
plt.show()
print(t)
result
[0, 2, 3, 5]
python
#Random number data
import networkx as nx, matplotlib.pyplot as plt
from ortoolpy import min_node_cover, networkx_draw
g = nx.random_graphs.fast_gnp_random_graph(10, 0.3, 1)
l = min_node_cover(g)
pos = networkx_draw(g, nx.spring_layout(g), node_color='white')
nx.draw_networkx_nodes(g, pos, nodelist=l)
plt.show()
Recommended Posts