I'm currently in the middle of an optimization problem. I'm using dynamic programming and after I'm done with computations, I'm left with N x N matrix that says: if you take mat[i][j]
then the value is cost of traveling from i
to j
== an adjacency matrix.
I also have access to two variables, K and C, which could be interpreted as length of path and cost of path, respectively.
So is there an algorithm, that can find all paths that have length K and cost C?
EDIT:
Here is an sample of adjacency matrix. The path is from 0 -> 8, cost is 48 and length is 3.
So for example, one valid path would be: 0->3 (15), 3->6 (15 + 13), 6->8 (15 + 13 + 20 = 48). Another might be : 0->4 (20), 4->6 (20 + 8), 6 -> 8 (20 + 8 + 20 = 48).
Non-valid path might include: 0->1(8), 1->2(8 + 4), 2->3(8 + 4 + 3), 3->4 (8 + 4 + 3 +5), ..., but length greater than 3, so that is invalid, same thing 0->8 has cost 48, but length 1.
1 2 3 4 5 6 7 8
---------------------------------
0: 8 12 15 20 26 28 37 48
1:---- 4 7 12 18 20 29 40
2:-------- 3 8 14 16 25 36
3:------------ 5 11 13 22 33
4:---------------- 6 8 17 28
5:-------------------- 2 11 22
6:------------------------ 9 20
7:---------------------------- 11
Actually, looking at it now, I see I have to rephrase my question. The number of vertices is determined during input from user, so is the length and cost. You always travel from first to last vertex. No matter what path you take (ignoring length now), getting from first to last vertex always has cost C, only thing that varies is length.
So the new (and better) question is, how to find all paths from first vertex to last, with length K?