我试图在某些图表上使用max_flow_min_cost,但似乎有时候算法会永远运行(或者至少持续很长时间),而且我不明白为什么会出现这种情况。这是一个导致它运行那么久的图表。它们都有相同的节点
nodes = [
('1in', {'y': -1, 'x': -1, 'type': 'passenger in', 'number': 1}),
('2out', {'y': 1, 'x': -1, 'type': 'passenger out', 'number': 2}),
('destination', {'y': 0, 'x': 0, 'type': 'destination'}),
('2in', {'y': 1, 'x': -1, 'type': 'passenger in', 'number': 2}),
('source', {'type': 'meta'}),
('4in', {'y': -1, 'x': 1, 'type': 'passenger in', 'number': 4}),
('1out', {'y': -1, 'x': -1, 'type': 'passenger out', 'number': 1}),
('4out', {'y': -1, 'x': 1, 'type': 'passenger out', 'number': 4}),
('sink', {'type': 'meta'}),
('3in', {'y': 1, 'x': 1, 'type': 'passenger in', 'number': 3}),
('3out', {'y': 1, 'x': 1, 'type': 'passenger out', 'number': 3})
]
edges = [
('1in', '1out', {'cost': 0, 'capacity': 1}),
('2out', '4in', {'cost': -9.48, 'capacity': 1}),
('2out', 'destination', {'cost': -10.9, 'capacity': 1}),
('2out', '3in', {'cost': -10.31, 'capacity': 1}),
('destination', 'sink', {'cost': 0, 'capacity': 1}),
('2in', '2out', {'cost': 0, 'capacity': 1}),
('source', '2in', {'cost': 0, 'capacity': 1}),
('source', '4in', {'cost': 0, 'capacity': 1}),
('source', '1in', {'cost': 0, 'capacity': 1}),
('source', '3in', {'cost': 0, 'capacity': 1}),
('4in', '4out', {'cost': 0, 'capacity': 1}),
('1out', '2in', {'cost': -10.31, 'capacity': 1}),
('1out', '4in', {'cost': -10.31, 'capacity': 1}),
('1out', 'destination', {'cost':-10.9, 'capacity': 1}),
('1out', '3in', {'cost': -9.48, 'capacity': 1}),
('4out', 'destination', {'cost': -10.9, 'capacity': 1}),
('3in', '3out', {'cost': 0, 'capacity': 1}),
('3out', '4in', {'cost': -10.31, 'capacity': 1}),
('3out', 'destination', {'cost': -10.9, 'capacity': 1})
]
如果我修改上面的图表但是将容量从目标设置为接收器2或3,它也会永远运行。如果我使容量等于4,算法运行正常。这是确切的电话:
nx.max_flow_min_cost(G,'source','sink',weight='cost')
谢谢!任何帮助将不胜感激。值得一提的是G是一个有向图(DiGraph)
编辑:我在NetworkX项目上opened an issue,以防它们的代码出现问题。
答案 0 :(得分:1)
它适用于我:
>>> G = nx.Graph()
>>> G.add_edges_from([
('1in', '1out', {'cost': 0, 'capacity': 1}),
('2out', '4in', {'cost': -9.48, 'capacity': 1}),
('2out', 'destination', {'cost': -10.9, 'capacity': 1}),
('2out', '3in', {'cost': -10.31, 'capacity': 1}),
('destination', 'sink', {'cost': 0, 'capacity': 1}),
('2in', '2out', {'cost': 0, 'capacity': 1}),
('source', '2in', {'cost': 0, 'capacity': 1}),
('source', '4in', {'cost': 0, 'capacity': 1}),
('source', '1in', {'cost': 0, 'capacity': 1}),
('source', '3in', {'cost': 0, 'capacity': 1}),
('4in', '4out', {'cost': 0, 'capacity': 1}),
('1out', '2in', {'cost': -10.31, 'capacity': 1}),
('1out', '4in', {'cost': -10.31, 'capacity': 1}),
('1out', 'destination', {'cost':-10.9, 'capacity': 1}),
('1out', '3in', {'cost': -9.48, 'capacity': 1}),
('4out', 'destination', {'cost': -10.9, 'capacity': 1}),
('3in', '3out', {'cost': 0, 'capacity': 1}),
('3out', '4in', {'cost': -10.31, 'capacity': 1}),
('3out', 'destination', {'cost': -10.9, 'capacity': 1})
])
>>> nx.max_flow_min_cost(G, 'source', 'sink', weight='cost')
{'1in': {'1out': 0, 'source': 0},
'1out': {'1in': 0, '2in': 1, '3in': 1, '4in': 1, 'destination': 1},
'2in': {'1out': 1, '2out': 1, 'source': 0},
'2out': {'2in': 0, '3in': 1, '4in': 1, 'destination': 1},
'3in': {'1out': 1, '2out': 1, '3out': 0, 'source': 0},
'3out': {'3in': 0, '4in': 1, 'destination': 1},
'4in': {'1out': 1, '2out': 1, '3out': 1, '4out': 0, 'source': 0},
'4out': {'4in': 0, 'destination': 1},
'destination': {'1out': 1, '2out': 0, '3out': 1, '4out': 1, 'sink': 1},
'sink': {'destination': 0},
'source': {'1in': 0, '2in': 1, '3in': 0, '4in': 0}}
答案 1 :(得分:1)
如果权重是浮点数,则networkx.max_flow_min_cost
中的算法不能保证工作。请参阅networkx网站上的原始作者问题
https://github.com/networkx/networkx/issues/2076#issuecomment-210200259