简体   繁体   English

沿有向图中的最大成本路径查找顶点

[英]Find vertices along maximum cost path in directed graph

I have a weighted directed graph like this:我有一个像这样的加权有向图:

在此处输入图片说明

I want to find the list of vertices along the maximum cost path from start to finish.我想从头到尾沿着最大成本路径找到顶点列表。

In this example I should get:在这个例子中,我应该得到:

'enableBrowserExtension' -> 'newWindow' -> 'newTab' -> 'startPage' -> 'typed' -> 'selectTab' -> 'clickTextField' -> 'changeField-frullatore' -> 'clickButton-VAI' -> 'submit' -> 'formSubmit' -> 'mouseClick' -> 'link'

I'm trying with this code found here :我想这个代码中发现在这里

import networkx as nx

def inverse_weight(graph, weight='weight'):
    copy_graph = graph.copy()
    for n, eds in copy_graph.adjacency():
        for ed, eattr in eds.items():
            copy_graph[n][ed][weight] = eattr[weight] * -1
    return copy_graph

def longest_path_and_length(graph, s, t, weight='weight'):
    i_w_graph = inverse_weight(graph, weight)
    path = nx.dijkstra_path(i_w_graph, s, t)
    length = nx.dijkstra_path_length(i_w_graph, s, t) * -1
    return path, length

if __name__ == '__main__':
    DG = nx.DiGraph()
    DG.add_edge('enableBrowserExtension', 'enableBrowserExtension', weight=3)
    DG.add_edge('enableBrowserExtension', 'newWindow', weight=1)
    DG.add_edge('newWindow', 'newTab', weight=1)
    DG.add_edge('newTab', 'startPage', weight=1)
    DG.add_edge('startPage', 'typed', weight=1)
    DG.add_edge('typed', 'selectTab', weight=2)
    DG.add_edge('selectTab', 'newTab', weight=1)
    DG.add_edge('newTab', 'typed', weight=1)
    DG.add_edge('typed', 'typed', weight=1)
    DG.add_edge('selectTab', 'clickTextField', weight=2)
    DG.add_edge('clickTextField', 'changeField', weight=1)
    DG.add_edge('changeField', 'clickButton', weight=1)
    DG.add_edge('clickButton', 'submit', weight=1)
    DG.add_edge('submit', 'formSubmit', weight=1)
    DG.add_edge('formSubmit', 'mouseClick', weight=1)
    DG.add_edge('mouseClick', 'link', weight=2)
    DG.add_edge('link', 'formSubmit', weight=1)
    DG.add_edge('formSubmit', 'selectTab', weight=1)
    DG.add_edge('selectTab', 'mouseClick', weight=1)
    DG.add_edge('mouseClick', 'clickLink', weight=1)
    DG.add_edge('clickLink', 'link', weight=1)
    DG.add_edge('link', 'mouseClick', weight=2)
    DG.add_edge('mouseClick', 'changeField', weight=1)
    DG.add_edge('changeField', 'mouseClick', weight=1)
    DG.add_edge('mouseClick', 'selectTab', weight=1)

    path, length = longest_path_and_length(DG, 'enableBrowserExtension', 'link')

But I get the error:但我收到错误:

ValueError: ('Contradictory paths found:', 'negative weights?')

I also tried this java code but it only returns the total maximum cost as integer, I want the name of the vertices along the path.我也试过这个java代码,但它只以整数形式返回总最大成本,我想要沿着路径的顶点的名称。

Is there a way to fix this?有没有办法来解决这个问题?

Since you have positive-value loops in the graph, the maximum-cost path is infinite.由于图中存在正值循环,因此最大成本路径是无限的。 You need to remove the trivial loops and finesse the others.您需要删除琐碎的循环并完善其他循环。 Perhaps you have an unimplemented requirement that no node is visited more than once?也许您有一个未实现的要求,即没有节点被多次访问?

The various ways of infinite looping are why you get the "contradictory paths".无限循环的各种方式是你得到“矛盾路径”的原因。

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

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