Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 动态规划问题中的最优路径_Algorithm_Dynamic Programming_Mathematical Optimization - Fatal编程技术网

Algorithm 动态规划问题中的最优路径

Algorithm 动态规划问题中的最优路径,algorithm,dynamic-programming,mathematical-optimization,Algorithm,Dynamic Programming,Mathematical Optimization,我试图找出一个可以用动态规划解决的问题的最佳路径。我对我们试图优化空间的情况很感兴趣 为了更好地解释我的问题,让我们考虑背包问题。 设以下三项: I1 I2 I3 --------------------------- Val 5 4 3 Weight 4 5 2 Let n be the nth item let c be the remaining capacity in the

我试图找出一个可以用动态规划解决的问题的最佳路径。我对我们试图优化空间的情况很感兴趣

为了更好地解释我的问题,让我们考虑背包问题。


设以下三项:

        I1      I2      I3
---------------------------
Val     5       4       3     

Weight  4       5       2
Let n be the nth item 
let c be the remaining capacity in the knapsack

f(n, c) = 0             // if n=0
f(n, c) = f(n-1, c)     // if weight[n] > c
f(n, c) = max(f(n-1, c), value[n] + f(n-1, c-weight[n]))  // if weight[n] <= c
public static void main(String[] args) {
    int[] value = {5, 4, 3};
    int[] weight = {4, 5, 2};

    int capacity = 9;


    int[][] dp = new int[value.length+1][capacity+1];

    for(int i=0; i<=value.length; i++) {
        for(int j=0; j<=capacity; j++) {
            if(i==0) {
                dp[i][j] = 0;
            } else {
                if(weight[i-1] <= j){
                    dp[i][j] = Math.max(dp[i-1][j], value[i-1] + dp[i-1][j - weight[i-1] ]);
                } else {
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
    }

    System.out.println("optimal value is: " + dp[value.length][capacity]);
}
public static void main(String[] args) {
    int[] value = {5, 4, 3};
    int[] weight = {4, 5, 2};

    int capacity = 9;

    int[] row0 = new int[capacity+1];
    int[] row1 = new int[capacity+1];
    for(int i=0; i<=3; i++) {
        for(int j=0; j<=capacity; j++) {
            if(i==0) {
                row1[j] = 0;
            } else {
                if(weight[i-1] <= j) {
                    row1[j] = Math.max(row0[j], value[i-1]+ row0[j-weight[i-1]]);
                } else {
                    row1[j] = row0[j];
                }
            }
        }
        for(int j = 0; j< row0.length; j++)
            row0[j] = row1[j];
    }

    System.out.println("optimal value is: " + row1[capacity]);

}
在这里,最佳路径是为最佳解决方案选择的项目

递归关系如下所示:

        I1      I2      I3
---------------------------
Val     5       4       3     

Weight  4       5       2
Let n be the nth item 
let c be the remaining capacity in the knapsack

f(n, c) = 0             // if n=0
f(n, c) = f(n-1, c)     // if weight[n] > c
f(n, c) = max(f(n-1, c), value[n] + f(n-1, c-weight[n]))  // if weight[n] <= c
public static void main(String[] args) {
    int[] value = {5, 4, 3};
    int[] weight = {4, 5, 2};

    int capacity = 9;


    int[][] dp = new int[value.length+1][capacity+1];

    for(int i=0; i<=value.length; i++) {
        for(int j=0; j<=capacity; j++) {
            if(i==0) {
                dp[i][j] = 0;
            } else {
                if(weight[i-1] <= j){
                    dp[i][j] = Math.max(dp[i-1][j], value[i-1] + dp[i-1][j - weight[i-1] ]);
                } else {
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
    }

    System.out.println("optimal value is: " + dp[value.length][capacity]);
}
public static void main(String[] args) {
    int[] value = {5, 4, 3};
    int[] weight = {4, 5, 2};

    int capacity = 9;

    int[] row0 = new int[capacity+1];
    int[] row1 = new int[capacity+1];
    for(int i=0; i<=3; i++) {
        for(int j=0; j<=capacity; j++) {
            if(i==0) {
                row1[j] = 0;
            } else {
                if(weight[i-1] <= j) {
                    row1[j] = Math.max(row0[j], value[i-1]+ row0[j-weight[i-1]]);
                } else {
                    row1[j] = row0[j];
                }
            }
        }
        for(int j = 0; j< row0.length; j++)
            row0[j] = row1[j];
    }

    System.out.println("optimal value is: " + row1[capacity]);

}

如何仅使用此信息追溯路径?

对于所有DP问题都没有好的解决方案

例如,对于这个问题,我将为每个可访问的和保留一个位掩码,以指示您选择哪些元素来生成该和。这适用于背包,因为元素的数量很小,选择顺序并不重要

对于许多其他DP问题(例如LCS或最短路径),将路径记为反向顺序链表效果很好。这些列表共享尾部,通常你必须记住的列表具有相似的历史记录。每隔一段时间,您可能需要扫描结构,以确保它仍然紧凑。如果确实需要,您可以删除每个第n个元素,这将要求您在重建路径时进行小搜索以连接每对元素