So, to find the minimum time to leave station S1, j the minimum time to leave the previous two stations must be calculated (as explained in above recursion ).
Similarly, there are two ways to reach station S2, j:
From station S2, j-1From station S1, J-1
Please note that the minimum times to leave stations S1, J-1 and S2, J-1 have already been calculated.
So, we need two tables to store the partial results calculated for each station in an assembly line. The table will be filled in bottom-up fashion.
Note:
In this post, the word "leave" has been used in place of "reach" to avoid the confusion. since the car chassis must spend a fixed time in each station, the word leave suits better.
The typical example of DP is pipeline scheduling. The goods arriving at an assembly point can be either from this assembly line or from another assembly line, depending on which one is more cost-effective.
The following is an example:
Output:
35
The bold line shows the path covered by the car chassis for given input values.
Exercise:
Extend the above algorithm to print the path covered by the car chassis in the factory.
Package DP; public class AssemblyLineScheduling {public static void main (String [] args) {int [] [] a = {4, 5, 3, 2}, {2, 10, 1, 4 }}; int [] [] t = {0, 7, 4, 5}, {0, 9, 2, 8 }}; int [] e = {10, 12}; int [] x = {18, 7}; System. out. println (carAssemblyDP (a, t, e, x); System. out. println (carAssembly (a, t, e, x);} public static int carAssembly (int [] [] a, int [] [] t, int [] e, int [] x) {int n = a [0]. length-1; return Math. min (carAssemblyRec (a, t, e, x, n, 0) + x [0], carAssemblyRec (a, t, e, x, n, 1) + x [1]);} public static int carAssemblyRec (int [] [] a, int [] [] t, int [] e, int [] x, int n, int line) {if (n = 0) {return e [line] + a [line] [0];} int T0 = Integer. MAX_VALUE; int T1 = Integer. MAX_VALUE; if (line = 0) {// The following only applies to line0T0 = Math. min (carAssemblyRec (a, t, e, x, n-1, 0) + a [0] [n], // carAssemblyRec (a, t, e, x, n-1, 1) + t [1] [n] + a [0] [n]); //} else if (line = 1) from another line {// The following only applies to line1T1 = Math. min (carAssemblyRec (a, t, e, x, n-1, 1) + a [1] [n], // carAssemblyRec (a, t, e, x, n-1, 0) + t [0] [n] + a [1] [n]); // return Math from another line. min (T0, T1);} public static int carAssemblyDP (int [] [] a, int [] [] t, int [] e, int [] x) {int n = a [0]. length; int [] T1 = new int [n]; int [] T2 = new int [n]; t1 [0] = e [0] + a [0] [0]; T2 [0] = e [1] + a [1] [0]; for (int I = 1; I
Let's talk about the recursion of this question. At first I wrote it like this:
public static int carAssemblyRec(int[][] a, int[][] t, int[] e, int[] x, int n, int line){if(n == 0){return e[line] + a[line][0];}int T0 = Math.min(carAssemblyRec(a, t, e, x, n-1, 0) + a[0][n], carAssemblyRec(a, t, e, x, n-1, 1) + t[1][n] + a[0][n]);int T1 = Math.min(carAssemblyRec(a, t, e, x, n-1, 1) + a[1][n], carAssemblyRec(a, t, e, x, n-1, 0) + t[0][n] + a[1][n]);return Math.min(T0, T1);}
The results are different from those of DP, so it took a lot of effort to find out where the problem occurred for a long time. After a single-step trace by debugger, we found that we forgot to add restrictions !!!
The correct statement should be:
Public static int carAssemblyRec (int [] [] a, int [] [] t, int [] e, int [] x, int n, int line) {if (n = 0) {return e [line] + a [line] [0];} int T0 = Integer. MAX_VALUE; int T1 = Integer. MAX_VALUE; if (line = 0) {// The following only applies to line0T0 = Math. min (carAssemblyRec (a, t, e, x, n-1, 0) + a [0] [n], // carAssemblyRec (a, t, e, x, n-1, 1) + t [1] [n] + a [0] [n]); //} else if (line = 1) from another line {// The following only applies to line1T1 = Math. min (carAssemblyRec (a, t, e, x, n-1, 1) + a [1] [n], // carAssemblyRec (a, t, e, x, n-1, 0) + t [0] [n] + a [1] [n]); // return Math from another line. min (T0, T1 );}
Http://www.geeksforgeeks.org/dynamic-programming-set-34-assembly-line-scheduling/