简体   繁体   中英

Get path in graph with weighted vertices and edges

If i have the following undirected graph with weighted vertices and edges: 在此输入图像描述

I am trying to come up with a ruby algorithm to find a best shortest path within a defined limit (sum of edges) with the highest value (sum of vertices).

The start point will also be the ending point.

for eg finding a path with a maximum of 20 with the highest total value.

This problem seems like a np hard problem and it is hard to find the best solution.

Is there a modified algorithm of dijkstra? I tried using a greedy algorithm but it did not give me a optimal solution. and by using bruteforce on all poosible path will work, but it will take very long if the number of nodes increases.

Was wondering if there is any combination of algorithms that i can use to improve my solution?

Thanks.

You can find an example of Djikstra's algorithm here . What I would do is add a variable to count the number of vertices in the shortest path, and evaluate if the shortest path has too many vertices or is too long once determining what the shortest path even is .

The problem is actually NP hard, you can prove this doing a reduction of Hamiltonian path problem to this problem. Basically given an input for the Hamiltonian path problem (we can stay with the undirected graph) you can create an input for the problem you describe if follow this steps:

  • build a new graph

    create a new graph that is the same that receive the Hamiltonian path problem but give to each vertex and edge weight 1.

  • create an input for your problem

    Pass to your problem the graph just created in the previous step and limit equals to infinity.

Notice now that the result given by your problem is a path as longest as posible with respect at the number of vertex, since the limit restriction is an upper bound in the sum of weight of the edges. This mean that you can add as vertex as posible and still complaint the limit restriction.

How many vertex are in the path determine the total value of this (count of vertex in the path) since the weight is 1 for all of them. So the resulting path is the longest posible in the graph. With this insight we can verify if this path is a hamiltonian path, just check the length of the path. If the path is of length N where N is the number of nodes in the graph there is a hamiltonian path otherwise there isn't.

To solve the problem you can use an approach similar to Bellman-Ford with some modifications. First create a matrix A[i, j] where you storage all the highest total value paths that ends with j and has length (number of edges) i. This is the key, you can't just storage one of this path, because in the next step you need to check all of them to make a relax, here is where the implementation become non-polynomial. The relax technique in the step i iterate through all the storage path in A[i-1, u] and try to improve the value in A[i, v] saving the new paths that actually do it (you must check for >= when try to improve A[i, v] in order to get all the paths), of course this relax take into account the limit restriction. If you uses a global max and a global path and update it in each relax process, you end with the highest value path for the entire graph.

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