...

/

Rod Cutting Problem - Solution Using DP

Rod Cutting Problem - Solution Using DP

Implement the solution of the rod-cutting problem using dynamic programming.

The problem with the previous approach

Because we implemented the solution using Recursion in the previous lesson, we got exponential time complexity. By visualizing the recursion tree, we can easily confirm that the subproblems are overlapping, and thus we can use dynamic programming to solve this problem. Let’s first look at the recursion tree for RodCut(6), RodCut(5), RodCut(4), and RodCut(3) only to confirm that the subproblems are overlapping.

%0 node_1 RodCut(6) node_2 RodCut(5) + p[1] node_1->node_2 node_3 RodCut(4) + p[2] node_1->node_3 node_4 RodCut(3) + p[3] node_1->node_4 node_1615308160934 RodCut(2) + p[4] node_1->node_1615308160934 node_1615308160684 RodCut(1) + p[5] node_1->node_1615308160684
Recursion Tree for RodCut(6)
%0 node_1 RodCut(5) node_2 RodCut(4) + p[1] node_1->node_2 node_3 RodCut(3) + p[2] node_1->node_3 node_4 RodCut(2) + p[3] node_1->node_4 node_1615308160934 RodCut(1) + p[4] node_1->node_1615308160934 node_1615308160684 RodCut(0) + p[5] node_1->node_1615308160684
Recursion Tree for RodCut(5)
%0 node_1 RodCut(4) node_2 RodCut(3) + p[1] node_1->node_2 node_3 RodCut(2) + p[2] node_1->node_3 node_4 RodCut(1) + p[3] node_1->node_4 node_1615308160934 RodCut(0) + p[4] node_1->node_1615308160934
Recursion Tree for RodCut(4)
%0 node_1 RodCut(3) node_2 RodCut(2) + p[1] node_1->node_2 node_3 RodCut(1) + p[2] node_1->node_3 node_4 RodCut(0) + p[3] node_1->node_4
Recursion Tree for RodCut(3)

Solution:

...