简体   繁体   中英

Is there an algorithm for finding the path of length k with the highest cost in a undirected graph

I have been thinking about this problem for a few weeks but can't wrap my head around an efficient solution.

So basically imagine that you have a undirected graph where every node has a value assigned to it. I want to find a path of length k (start and end node don't matter) that has, if you sum up the values of the visited nodes, the highest "cost".

Let's take this graph for example:

    d
    |
a - b - c
|   |
e - f

With the following values for the nodes:

a: 5
b: 10
c: 2
d: 3
e: 6
f: 7

The path with the highest cost with the length k=3 would be efb , because the sum is 23.

I found a solution that solves this in O(n^k) time by basically finding every possible path for every node and then finding the one with the highest cost, but i think that there must be a more optimal solution.

Step1 Having the costs assigned to the nodes is awkward. The Dijsktra algorithm expects the costs to be on the links. So it would be good to transform the nodes to links and the links to nodes

(a,cost ac) -> (b,cost bc)

becomes

() - cost ac > () - cost bc > ()

Step2 Dijsktra calculates the lowest cost path. So you have recalculate every cost as

c = max cost - c

Step3 Now you can use Dijkstra to find the most expensive path

LOOP n1 over nodes
    Dijsktra
    LOOP n2 over nodes starting at n1 + 1
       IF n1 to n2 most expensive so far
           Save n1 to n2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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