Typical problem and execution method
The weight matching problem is a general term such as "maximum weight matching problem, maximum weight maximum matching problem, maximum weight perfect matching problem, minimum weight maximum matching problem, minimum weight perfect matching problem".
Weight matching problem | Problem type | Number of matched sides |
---|---|---|
Maximum weight matching problem | Maximize | Any |
Maximum weight maximum matching problem | Maximize | Must be equal to the maximum matching problem |
Maximum weight perfect matching problem | Maximize | Must be half the score |
Minimum weight maximum matching problem | Minimize | Must be equal to the maximum matching problem |
Minimum weight perfect matching problem | Minimize | Must be half the score |
--All weights are 0 or more. --The minimum weight matching problem is usually not considered because ** empty ** is a trivial optimal solution. --When all the weights are 1 in "Maximum weight matching problem and maximum weight maximum matching problem and minimum weight maximum matching problem", simply "Maximum weight matching problem" Called. --When the weights are all 1 in the "maximum weight perfect matching problem and the minimum weight perfect matching problem", it is simply called the "perfect matching problem". --The minimum weight maximum matching problem and the minimum weight perfect matching problem can be reduced to the maximum weight maximum matching problem and the maximum weight perfect matching problem by changing the weight to "maximum weight-the weight". --The maximum weight perfect matching problem is a solution only when the solution of the maximum weight maximum matching problem is perfect matching. ――From these things, it is only necessary to have a solution for the maximum weight matching problem and the maximum weight maximum matching problem. --The maximum weight matching problem can be solved by the following max_weight_matching (Edmonds method). --Maximum weight To solve the maximum matching problem, specify maxcardinality = True in max_weight_matching below. --If the graph is a bipartite graph, there is an algorithm (such as Hungarian law) that has higher performance than general graphs.
For the undirected graph $ G = (V, E) $, when the weight $ w (e) $ of each side $ e \ in E $ is given, $ \ sum_ {e \ in M} {w ( e) Find the matching $ M $ with the largest} $.
usage
Signature: nx.max_weight_matching(G, maxcardinality=False)
Docstring:
Compute a maximum-weighted matching of G.
A matching is a subset of edges in which no node occurs more than once.
The cardinality of a matching is the number of matched edges.
The weight of a matching is the sum of the weights of its edges.
python
#CSV data
import pandas as pd, networkx as nx, matplotlib.pyplot as plt
from ortoolpy import graph_from_table, networkx_draw
tbn = pd.read_csv('data/node0.csv')
tbe = pd.read_csv('data/edge0.csv')
g = graph_from_table(tbn, tbe)[0]
d = nx.max_weight_matching(g)
pos = networkx_draw(g)
nx.draw_networkx_edges(g, pos, width=3, edgelist=[(i, j) for i, j in d])
plt.show()
print(d)
result
{5: 1, 1: 5, 0: 2, 2: 0, 4: 3, 3: 4}
python
# pandas.DataFrame
from ortoolpy.optimization import MaxWeightMatching
MaxWeightMatching('data/edge0.csv')
node1 | node2 | capacity | weight | |
---|---|---|---|---|
0 | 0 | 2 | 2 | 4 |
1 | 1 | 5 | 2 | 5 |
2 | 3 | 4 | 2 | 4 |
python
#Random number data
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():
g.adj[i][j]['weight'] = 1
d = nx.max_weight_matching(g)
pos = networkx_draw(g, nx.spring_layout(g))
nx.draw_networkx_edges(g, pos, width=3, edgelist=[(i, j) for i, j in d])
plt.show()
Recommended Posts