Cheapest way algorithm

I studied the dynamic programming algorithm to find the “cheapest” way from A to B. Each subpath has an associated cost.

Each angle is calculated using
D(i,j).value = min( (D(i-1,j).value + D(i,j).x), (D(i,j-1).value + D(i,j).y))
Where x and y are the cost of the path to the left of the node and below the node.

Now I have problems determining the number of possible cheapest routes at the best possible time.

Any suggestions?

+2
source share
7 answers

DAG. ( ). O (V + E) ( V E - ), .

, , , ?

, . node j, (i, j), j . - : D (x, y).optimalUp D (x, y).optimalRight( boolean), , , (x, y), , . , D (x, y).optimalUp true, (x, y-1) .

, , . , , D (x, y).count(integer), A (x, y) . (x, y): Via (x-1, y) (x, y-1). (x, y-1) (x, y), (x, y), (x, y-1). (x-1, y).

:

D(x,y).count =
    D(x,y).optimalUp   ? D(x,y-1).count : 0
  + D(x,y).optimalDown ? D(x-1,y).count : 0

(?: - C/++/Java.)

, , . , . A B 8, 12, .

x -1-- x -1-- B
|      |      |
1      9      9
|      |      |
x -1-- x -1-- x
|      |      |
9      9      1
|      |      |
A -1-- x -1-- x

, , . ( ). .

+2

. , , .

+8

, , 2D-, node node , node .

, . , , .

(.. ), , , , . ,

number_paths(i,j)=number_of_paths(i-1,j)+number_of_paths(i,j-1)

. node , . ( , node node node.)

, , , , . , X. , . node , X. node (.. ) Xe, e - . node, node. , , , node, node 0 (.. , ).

, , 3 2, ( , 3D-, 3- ). node (x_start,y_start,cost), node (x_end,y_end,0). :

paths(x,y,cost_left)=
                 0         if x_end,y_end is unreachable from x,y or cost_left<0
                 1         if x==X-end and y==y_end and cost_left==0
                 paths(x-1,y,cost_left-edge(x,y,x-1,y))+paths(x,y-1,cost_left-edge(x,y,x,u-1))   otherwise

O (nmC), n m - , C - .

+1

Hillclimbing A-star .

0

A * , , .

0

" ", .

, . A *, , , . , , . , , , , .

, , .

0

Source: https://habr.com/ru/post/1732365/


All Articles