简体   繁体   English

python 中的匈牙利算法图

[英]hungarian algorithm's graph in python

I am trying to implement hungarian algorithm in my project, but I don't understand why it gives a endless loop...I have tried with an other bibartite graph and it works.我正在尝试在我的项目中实现匈牙利算法,但我不明白为什么它会产生无限循环......我尝试过使用其他 bibartite 图并且它有效。 So I want to know what's wrong with my graph G所以我想知道我的图G有什么问题

 from hungarian_algorithm import algorithm
 G={
'agt2': {'Commentaire':200,'PhotoProfil': 8, 'PhotoSupp': 10, 'Prenom': 0}, 
'coco': {'Commentaire': 300, 'PhotoProfil': 200, 'PhotoSupp': 300, 'Prenom': 300}
 }
res=algorithm.find_matching(G,matching_type='max',return_type='list')
print(res)

The graph is fine, it's probably a bug in the implementation of that package.该图很好,这可能是 package 的实现中的一个错误。 As pointed out in my comment you can use scipy.optimize.linear_sum_assignment from SciPy instead.正如我在评论中指出的那样,您可以改用scipy.optimize.linear_sum_assignment中的scipy.optimize.linear_sum_assignment

For example例如

import numpy as np
from scipy.optimize import linear_sum_assignment

# solve the assignment problem
G = np.array([[200, 8,   10,  0],
              [300, 200, 300, 300]])
row_indices, col_indices = linear_sum_assignment(G, maximize=True)

# print results
row_names = ['agt2', 'coco']
col_names = ['Commentaire', 'PhotoProfil', 'PhotoSupp', 'Prenom']
edges = [((row_names[r], col_names[c]), G[r, c]) for r, c in zip(row_indices, col_indices)]
print(edges)

prints印刷

[(('agt2', 'Commentaire'), 200), (('coco', 'PhotoSupp'), 300)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM