开发者

how to determine maximum route cost in a n high numeric pyramid

I've got a numeric pyramid like this

       7
      4 8
     1 8 9
    2 4 6 7
   4 6 7 4 9
  4 9 7 3 8 8

routes: 32

every number indexed by how powerful in its line.

 0 ( 9 => 1 ) 1 ( 8 => 5 ) 2 ( 8 => 4 ) 3 ( 7 => 2 ) 4 ( 4 => 0 ) 5 ( 3 => 3 )
 0 ( 9 => 4 ) 1 ( 7 => 2 ) 2 ( 6 => 1 ) 3 ( 4 => 3 ) 4 ( 4 => 0 )
 0 ( 7 => 3 ) 1 ( 6 => 2 ) 2 ( 4 => 1 ) 3 ( 2 => 0 )
 0 ( 9 => 2 ) 1 ( 8 => 1 ) 2 ( 1 => 0 )
 0 ( 8 => 1 ) 1 ( 4 => 0 )
 0 ( 7 => 0 )

in this pyramid there are 2^(n-1) routes (you can go 2 way form each number) If the pyramid high this low, it's easy you can calculat开发者_Go百科e all the routes, and compare to eachother. But if you have an 50 high pyramid with 562949953421312 routes, the problem little bit more difficult.

I thoght I start from the bottom beginning from the most powerful numbers, but soon i realized, the max route cost ain't necesserly start or end up in big numbers.

Then I thought maybe secoundary indexes (where can you go futher from a number) will help, but I didin't even implement that becouse I assumed its still uses much resources and not optimal.

And now i'm confused how to restart thinking about this problem... any advice appreciated


Think of your pyramid as a tree with the root at the top of the pyramid: I think you want the route with the maximum cost from the root to any of the leaf nodes (bottom of the pyramid). OK, it's not actually a tree, because a node may have two parents, in fact you can get to a node at level i from at most two nodes at level i-1.

Anyway, I think you can compute the route with the maximum cost by using dynamic programming. Let me rewrite your data in a matrix like way:

7 
4 8 
1 8 9 
2 4 6 7 
4 6 7 4 9 
4 9 7 3 8 8

and let the missing elements of the matrix be 0. Let's call this matrix v (for values). Now you can build a matrix c (for costs) where c(i,j) is the maximum cost for reaching the tree node at position (i,j). You can compute it with this recurrence:

c(i,j) = v(i,j) + max{ c(i-1,j-1), c(i-1,j) }

where c(h,k) is 0 when you get to a position out of the matrix. Essentially we say that the maximum cost for getting to node at position (i,j) is the cost of the node itself plus the maximum between the costs of getting to its two possible parents at level i-1.

Here is c for your example:

7     
11 15    
12 23 24   
14 27 30 31  
18 33 37 35 40 
22 42 44 40 48 48

For instance, let's take i=3, j=2:

c(3,2) = v(3,2) + max{ c(2,1), c(2,2) }
       = 6      + max{ 23    , 24     }
       = 30

From c you see that the most expensive rout costs 48 (and you have two of them).


Simplest way is to go bottom up and you have O(N) compexity. This case you do not need dynamic programming or recursion. Just compose another tree where number at higher level is max of numbers of lower layer.


I suggest you look at Dijkstra's Algorithm and A*.

I believe Dijkstra's is more accurate than A*, but slower.


If the numbers represent the cost to travel between 2 nodes of a graph, then Dijkstra's algorithm will find the shortest path.


I think that even if you use the Suggested Dijkstra's Algorithm, you still have to test every route. First of all because there is no single starting and end point, but there are 50 starting points for the end point. So the algorithm has to be tested 50 times.

And because every option has 2 paths, there is no way of skipping one. You can never rule out a path untill you are at the very end.

So I dont think there is a quicker way to find the longest path (so not the shortest as in Dijkstra's Algorithm) then to test all routes.


as you can Consider the tree a DAG Do a Topological sort then relax(relax to the max not min) each edge as they are in the topological sort O(E+V) .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜