I have a directed cyclic weighted graph. I want to find a path with the highest some of weights, in length of X vertices, and I don't care what is the destination. I only want to find the highest cost.
-
1How large is the graph? How large are the weights? What have you tried? – MPeti Nov 01 '15 at 15:39
-
Didn't tried any "known" alogrithm, since I could't find anything similar to what I need. I have few hundred nodes, and each node has ~2-3 vertices. – Ido Nov 01 '15 at 15:40
-
And you're only looking for paths, not walks, right? So repetition of vertices is not allowed. (If it was allowed, I'd have an O(E*X) solution, E being the number of edges..) Also, are there any limits on X, or is it anything up to the total vertex count? – MPeti Nov 01 '15 at 16:07
-
Actually it is aloud, but the weight will be lower on the second visit. X is probably around 10 or so – Ido Nov 01 '15 at 20:52
2 Answers
This can be solved via dynamical-programming-like algorithm.
As you have only just a few hundreds of nodes and X is round 10. You can assign each node v an array Fv with size X, and Fv[i] represents the maximum cost from the source to the node v with length i.
Let s be the source. Set Fs[0] = 0, and all other Fs[i] = -infinity.
All other arrays are initialized as -infinity array.
Now,
for each node v, do the following update:
Fv[i] = max{Fv[i], Fw[i-1] + cost(w, v) | where w is neighbor of v}
repeat above updates for at least X times, and then check Fv[X] for all v to get the maximum possible value you want.
You can use some extra information to retrieve the path, which should be very easy to do.
Above algorithm is a special case of Bellman-Ford Algorithm

- 126
- 5