Post on 14-Apr-2017
1SIGMA INSTITUTE OFENGINEERING
INFORMATION TECHNOLOGY(5th SEMESTER)
ANALYSIS AND DESIGN OF ALGORITHM
Guided by-Mrs Kalyani Adawadkar
Prepared by-Patel Paras (140500116024)Patel Pranay (140500116026)Shah Vidhi (140500116036)
Knapsack Problem
2What is Dynamic Programming Dynamic Programming is typically applied to optimization problem bull Dynamic programming is technique for solving problems with
overlapping sub problemsbull In this method each sub problem is solved only once The result
of each sub problem is recorded in a table from which we can obtain a solution to the original problem
3Knapsack Problem
The knapsack problem can be defined as follows
bull If there are n items with the weights w1w2hellipwn and values (Profit associated with each item) v1v2hellipvn and capacity of knapsack to be W then find the most valuable subset of the items that fit into the knapsack
4 To solve the knapsack problem using dynamic
programming we will write the recurrence relation as
Max table[ i-1 j vi + table[ i-1j-wi ] if j ge wi table[ i j ]= OR table [ i-1 j ] if j lt wbull That means a table is constructed using above given formula Initially Table[0 j] = 0 as well as table [i 0] = 0 When j ge 0 and i ge 0
5The initially stage of the table can be -
0 0 hellip 0 hellip 0 hellip 0 Table[i-1j-wi]0 Table[i-1j]0 Table[ij]0
Table[nw]
0
i-1
i
n
0 1 j-wi j w
Goal ieMaximum Value of items
bull The table [nW] is a goal ie it gives the total items sum of all the selected items for the knapsack
bull From this goal value the selected items can be traced out Let us solve the knapsack problem using the above mentioned formula
6Example -
Item Weight Value 1 2 32 3 43 4 54 5 6
The capacity of knapsack is W = 5
Obtain optimal solution for the knapsack problem
7 Initially table [0j] = 0 and [I0] = 0 There are 0 to n rows and 0 to W
columns in the table
0 0 0 0 0 00000
01234
0 1 2 3 4 5
bull Now we will fill up the table either row by row or column by column Let us start filling the table row by row using following formula
8Table[11] with i=1 j=1 wi=2 and vi =3As j lt wi Table [11] = table [ i-1 j ]
= table [01] =0
Table[12] with i=1 j=2 wi=2 and vi =3
As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
Table[12] with i=1 j=3 wi=2 and vi =3As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
0 0 0 0 0 00 0 3 3 3 3000
01234
0 1 2 3 4 5
Compute this values in table
9 Continuing in this fashion we can compute all the values of table The table will be -
0 0 0 0 0 00 0 3 3 3 30 0 3 4 4 70 0 3 4 5 70 0 3 4 5 7
01234
0 1 2 3 4 5
7This is the total value of selected items
10How to find actual items
bull Now as we know that table [ n W ] is the total value of selected items that can be placed in the knapsack bull Following steps are used repeatedly to select actual knapsack item
Let I = n and k = W thenWhile( I gt 0 and k gt 0) if( table[ i k ] ne table[ i-1 k ]) then
Mark ith item as in knapsacki = i - 1 and k = k ndash wi
elsei=i-1
11 Let us apply these steps to the above given problem
i = 4 and k = 5 ie table [45] = table [35] Do not select ith ie 4th item
Now set i = i ndash 1 i = 3
i = 2 and k = 5 ie table [25] ne table [15]
Select ith ie 2nd item
Now set i = i ndash 1 i = 1
and k = k - wi k = 5 ndash 3 = 2
i = 3 and k = 5 ie table [35] = table [25] Do not select ith ie 3rd item
Now set i = i ndash 1 i = 2
1 2
3 4i = 1 and k = 52ie table [12] ne table [02]
Select ith ie 1st item
Now set i = i ndash 1 i = 0
and k = k - wi k = 2 ndash 2 = 0
bull Thus we have selected item 1 and item 2 for the knapsack
bull This solution can also represented by solution vector (1100)
12Algorithm Dynamic_Knapsack(n W w[] v[] )Pro-Des Algorithm for obtaining knapsack solution using dynamic programmingInput n is total no of items W is the capacity of knapsack w[] stores weight and v[] store valuesOutput return total value of selected items for knapsackfor( i larr0 to n) do
for ( j larr 0 to W) do
table[i0]=0table[0j]=0
for ( i larr 0 to n ) do
for ( j larr 0 to W ) doif( j ltw[ i ] ) then
table[ i j ] larr table[ i-1 j ]else if ( jgt= w[ i ]) thentable[ i j ] larr max ( table [ i-1 j ]( v[i] + table [ i-1 j-w[i] ] ))
return table[ n W ]
Algorithm for knapsack problem
13Analysis of knapsack dynamic algorithm -
bull In this algorithm the basic operation is if hellip else if statement within two for loops
Hencec(n)= sum
119895=0
119882
1sum119894= 0
119899
sum119894= 0
119899
119882 minus0+1=
sum119894=0
119899
119882 +1
sum119894=0
119899
119882sum119894=0
119899
1=
=
+
sum119894=0
119899
1sum119894=0
119899
1= W +
= W(n-0+1)+(n-0+1)= Wn + W + n + 1
Thus c(n) asymp Wn
The time complexity of this algorithm is (nW)
14
THANK YOU
2What is Dynamic Programming Dynamic Programming is typically applied to optimization problem bull Dynamic programming is technique for solving problems with
overlapping sub problemsbull In this method each sub problem is solved only once The result
of each sub problem is recorded in a table from which we can obtain a solution to the original problem
3Knapsack Problem
The knapsack problem can be defined as follows
bull If there are n items with the weights w1w2hellipwn and values (Profit associated with each item) v1v2hellipvn and capacity of knapsack to be W then find the most valuable subset of the items that fit into the knapsack
4 To solve the knapsack problem using dynamic
programming we will write the recurrence relation as
Max table[ i-1 j vi + table[ i-1j-wi ] if j ge wi table[ i j ]= OR table [ i-1 j ] if j lt wbull That means a table is constructed using above given formula Initially Table[0 j] = 0 as well as table [i 0] = 0 When j ge 0 and i ge 0
5The initially stage of the table can be -
0 0 hellip 0 hellip 0 hellip 0 Table[i-1j-wi]0 Table[i-1j]0 Table[ij]0
Table[nw]
0
i-1
i
n
0 1 j-wi j w
Goal ieMaximum Value of items
bull The table [nW] is a goal ie it gives the total items sum of all the selected items for the knapsack
bull From this goal value the selected items can be traced out Let us solve the knapsack problem using the above mentioned formula
6Example -
Item Weight Value 1 2 32 3 43 4 54 5 6
The capacity of knapsack is W = 5
Obtain optimal solution for the knapsack problem
7 Initially table [0j] = 0 and [I0] = 0 There are 0 to n rows and 0 to W
columns in the table
0 0 0 0 0 00000
01234
0 1 2 3 4 5
bull Now we will fill up the table either row by row or column by column Let us start filling the table row by row using following formula
8Table[11] with i=1 j=1 wi=2 and vi =3As j lt wi Table [11] = table [ i-1 j ]
= table [01] =0
Table[12] with i=1 j=2 wi=2 and vi =3
As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
Table[12] with i=1 j=3 wi=2 and vi =3As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
0 0 0 0 0 00 0 3 3 3 3000
01234
0 1 2 3 4 5
Compute this values in table
9 Continuing in this fashion we can compute all the values of table The table will be -
0 0 0 0 0 00 0 3 3 3 30 0 3 4 4 70 0 3 4 5 70 0 3 4 5 7
01234
0 1 2 3 4 5
7This is the total value of selected items
10How to find actual items
bull Now as we know that table [ n W ] is the total value of selected items that can be placed in the knapsack bull Following steps are used repeatedly to select actual knapsack item
Let I = n and k = W thenWhile( I gt 0 and k gt 0) if( table[ i k ] ne table[ i-1 k ]) then
Mark ith item as in knapsacki = i - 1 and k = k ndash wi
elsei=i-1
11 Let us apply these steps to the above given problem
i = 4 and k = 5 ie table [45] = table [35] Do not select ith ie 4th item
Now set i = i ndash 1 i = 3
i = 2 and k = 5 ie table [25] ne table [15]
Select ith ie 2nd item
Now set i = i ndash 1 i = 1
and k = k - wi k = 5 ndash 3 = 2
i = 3 and k = 5 ie table [35] = table [25] Do not select ith ie 3rd item
Now set i = i ndash 1 i = 2
1 2
3 4i = 1 and k = 52ie table [12] ne table [02]
Select ith ie 1st item
Now set i = i ndash 1 i = 0
and k = k - wi k = 2 ndash 2 = 0
bull Thus we have selected item 1 and item 2 for the knapsack
bull This solution can also represented by solution vector (1100)
12Algorithm Dynamic_Knapsack(n W w[] v[] )Pro-Des Algorithm for obtaining knapsack solution using dynamic programmingInput n is total no of items W is the capacity of knapsack w[] stores weight and v[] store valuesOutput return total value of selected items for knapsackfor( i larr0 to n) do
for ( j larr 0 to W) do
table[i0]=0table[0j]=0
for ( i larr 0 to n ) do
for ( j larr 0 to W ) doif( j ltw[ i ] ) then
table[ i j ] larr table[ i-1 j ]else if ( jgt= w[ i ]) thentable[ i j ] larr max ( table [ i-1 j ]( v[i] + table [ i-1 j-w[i] ] ))
return table[ n W ]
Algorithm for knapsack problem
13Analysis of knapsack dynamic algorithm -
bull In this algorithm the basic operation is if hellip else if statement within two for loops
Hencec(n)= sum
119895=0
119882
1sum119894= 0
119899
sum119894= 0
119899
119882 minus0+1=
sum119894=0
119899
119882 +1
sum119894=0
119899
119882sum119894=0
119899
1=
=
+
sum119894=0
119899
1sum119894=0
119899
1= W +
= W(n-0+1)+(n-0+1)= Wn + W + n + 1
Thus c(n) asymp Wn
The time complexity of this algorithm is (nW)
14
THANK YOU
3Knapsack Problem
The knapsack problem can be defined as follows
bull If there are n items with the weights w1w2hellipwn and values (Profit associated with each item) v1v2hellipvn and capacity of knapsack to be W then find the most valuable subset of the items that fit into the knapsack
4 To solve the knapsack problem using dynamic
programming we will write the recurrence relation as
Max table[ i-1 j vi + table[ i-1j-wi ] if j ge wi table[ i j ]= OR table [ i-1 j ] if j lt wbull That means a table is constructed using above given formula Initially Table[0 j] = 0 as well as table [i 0] = 0 When j ge 0 and i ge 0
5The initially stage of the table can be -
0 0 hellip 0 hellip 0 hellip 0 Table[i-1j-wi]0 Table[i-1j]0 Table[ij]0
Table[nw]
0
i-1
i
n
0 1 j-wi j w
Goal ieMaximum Value of items
bull The table [nW] is a goal ie it gives the total items sum of all the selected items for the knapsack
bull From this goal value the selected items can be traced out Let us solve the knapsack problem using the above mentioned formula
6Example -
Item Weight Value 1 2 32 3 43 4 54 5 6
The capacity of knapsack is W = 5
Obtain optimal solution for the knapsack problem
7 Initially table [0j] = 0 and [I0] = 0 There are 0 to n rows and 0 to W
columns in the table
0 0 0 0 0 00000
01234
0 1 2 3 4 5
bull Now we will fill up the table either row by row or column by column Let us start filling the table row by row using following formula
8Table[11] with i=1 j=1 wi=2 and vi =3As j lt wi Table [11] = table [ i-1 j ]
= table [01] =0
Table[12] with i=1 j=2 wi=2 and vi =3
As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
Table[12] with i=1 j=3 wi=2 and vi =3As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
0 0 0 0 0 00 0 3 3 3 3000
01234
0 1 2 3 4 5
Compute this values in table
9 Continuing in this fashion we can compute all the values of table The table will be -
0 0 0 0 0 00 0 3 3 3 30 0 3 4 4 70 0 3 4 5 70 0 3 4 5 7
01234
0 1 2 3 4 5
7This is the total value of selected items
10How to find actual items
bull Now as we know that table [ n W ] is the total value of selected items that can be placed in the knapsack bull Following steps are used repeatedly to select actual knapsack item
Let I = n and k = W thenWhile( I gt 0 and k gt 0) if( table[ i k ] ne table[ i-1 k ]) then
Mark ith item as in knapsacki = i - 1 and k = k ndash wi
elsei=i-1
11 Let us apply these steps to the above given problem
i = 4 and k = 5 ie table [45] = table [35] Do not select ith ie 4th item
Now set i = i ndash 1 i = 3
i = 2 and k = 5 ie table [25] ne table [15]
Select ith ie 2nd item
Now set i = i ndash 1 i = 1
and k = k - wi k = 5 ndash 3 = 2
i = 3 and k = 5 ie table [35] = table [25] Do not select ith ie 3rd item
Now set i = i ndash 1 i = 2
1 2
3 4i = 1 and k = 52ie table [12] ne table [02]
Select ith ie 1st item
Now set i = i ndash 1 i = 0
and k = k - wi k = 2 ndash 2 = 0
bull Thus we have selected item 1 and item 2 for the knapsack
bull This solution can also represented by solution vector (1100)
12Algorithm Dynamic_Knapsack(n W w[] v[] )Pro-Des Algorithm for obtaining knapsack solution using dynamic programmingInput n is total no of items W is the capacity of knapsack w[] stores weight and v[] store valuesOutput return total value of selected items for knapsackfor( i larr0 to n) do
for ( j larr 0 to W) do
table[i0]=0table[0j]=0
for ( i larr 0 to n ) do
for ( j larr 0 to W ) doif( j ltw[ i ] ) then
table[ i j ] larr table[ i-1 j ]else if ( jgt= w[ i ]) thentable[ i j ] larr max ( table [ i-1 j ]( v[i] + table [ i-1 j-w[i] ] ))
return table[ n W ]
Algorithm for knapsack problem
13Analysis of knapsack dynamic algorithm -
bull In this algorithm the basic operation is if hellip else if statement within two for loops
Hencec(n)= sum
119895=0
119882
1sum119894= 0
119899
sum119894= 0
119899
119882 minus0+1=
sum119894=0
119899
119882 +1
sum119894=0
119899
119882sum119894=0
119899
1=
=
+
sum119894=0
119899
1sum119894=0
119899
1= W +
= W(n-0+1)+(n-0+1)= Wn + W + n + 1
Thus c(n) asymp Wn
The time complexity of this algorithm is (nW)
14
THANK YOU
4 To solve the knapsack problem using dynamic
programming we will write the recurrence relation as
Max table[ i-1 j vi + table[ i-1j-wi ] if j ge wi table[ i j ]= OR table [ i-1 j ] if j lt wbull That means a table is constructed using above given formula Initially Table[0 j] = 0 as well as table [i 0] = 0 When j ge 0 and i ge 0
5The initially stage of the table can be -
0 0 hellip 0 hellip 0 hellip 0 Table[i-1j-wi]0 Table[i-1j]0 Table[ij]0
Table[nw]
0
i-1
i
n
0 1 j-wi j w
Goal ieMaximum Value of items
bull The table [nW] is a goal ie it gives the total items sum of all the selected items for the knapsack
bull From this goal value the selected items can be traced out Let us solve the knapsack problem using the above mentioned formula
6Example -
Item Weight Value 1 2 32 3 43 4 54 5 6
The capacity of knapsack is W = 5
Obtain optimal solution for the knapsack problem
7 Initially table [0j] = 0 and [I0] = 0 There are 0 to n rows and 0 to W
columns in the table
0 0 0 0 0 00000
01234
0 1 2 3 4 5
bull Now we will fill up the table either row by row or column by column Let us start filling the table row by row using following formula
8Table[11] with i=1 j=1 wi=2 and vi =3As j lt wi Table [11] = table [ i-1 j ]
= table [01] =0
Table[12] with i=1 j=2 wi=2 and vi =3
As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
Table[12] with i=1 j=3 wi=2 and vi =3As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
0 0 0 0 0 00 0 3 3 3 3000
01234
0 1 2 3 4 5
Compute this values in table
9 Continuing in this fashion we can compute all the values of table The table will be -
0 0 0 0 0 00 0 3 3 3 30 0 3 4 4 70 0 3 4 5 70 0 3 4 5 7
01234
0 1 2 3 4 5
7This is the total value of selected items
10How to find actual items
bull Now as we know that table [ n W ] is the total value of selected items that can be placed in the knapsack bull Following steps are used repeatedly to select actual knapsack item
Let I = n and k = W thenWhile( I gt 0 and k gt 0) if( table[ i k ] ne table[ i-1 k ]) then
Mark ith item as in knapsacki = i - 1 and k = k ndash wi
elsei=i-1
11 Let us apply these steps to the above given problem
i = 4 and k = 5 ie table [45] = table [35] Do not select ith ie 4th item
Now set i = i ndash 1 i = 3
i = 2 and k = 5 ie table [25] ne table [15]
Select ith ie 2nd item
Now set i = i ndash 1 i = 1
and k = k - wi k = 5 ndash 3 = 2
i = 3 and k = 5 ie table [35] = table [25] Do not select ith ie 3rd item
Now set i = i ndash 1 i = 2
1 2
3 4i = 1 and k = 52ie table [12] ne table [02]
Select ith ie 1st item
Now set i = i ndash 1 i = 0
and k = k - wi k = 2 ndash 2 = 0
bull Thus we have selected item 1 and item 2 for the knapsack
bull This solution can also represented by solution vector (1100)
12Algorithm Dynamic_Knapsack(n W w[] v[] )Pro-Des Algorithm for obtaining knapsack solution using dynamic programmingInput n is total no of items W is the capacity of knapsack w[] stores weight and v[] store valuesOutput return total value of selected items for knapsackfor( i larr0 to n) do
for ( j larr 0 to W) do
table[i0]=0table[0j]=0
for ( i larr 0 to n ) do
for ( j larr 0 to W ) doif( j ltw[ i ] ) then
table[ i j ] larr table[ i-1 j ]else if ( jgt= w[ i ]) thentable[ i j ] larr max ( table [ i-1 j ]( v[i] + table [ i-1 j-w[i] ] ))
return table[ n W ]
Algorithm for knapsack problem
13Analysis of knapsack dynamic algorithm -
bull In this algorithm the basic operation is if hellip else if statement within two for loops
Hencec(n)= sum
119895=0
119882
1sum119894= 0
119899
sum119894= 0
119899
119882 minus0+1=
sum119894=0
119899
119882 +1
sum119894=0
119899
119882sum119894=0
119899
1=
=
+
sum119894=0
119899
1sum119894=0
119899
1= W +
= W(n-0+1)+(n-0+1)= Wn + W + n + 1
Thus c(n) asymp Wn
The time complexity of this algorithm is (nW)
14
THANK YOU
5The initially stage of the table can be -
0 0 hellip 0 hellip 0 hellip 0 Table[i-1j-wi]0 Table[i-1j]0 Table[ij]0
Table[nw]
0
i-1
i
n
0 1 j-wi j w
Goal ieMaximum Value of items
bull The table [nW] is a goal ie it gives the total items sum of all the selected items for the knapsack
bull From this goal value the selected items can be traced out Let us solve the knapsack problem using the above mentioned formula
6Example -
Item Weight Value 1 2 32 3 43 4 54 5 6
The capacity of knapsack is W = 5
Obtain optimal solution for the knapsack problem
7 Initially table [0j] = 0 and [I0] = 0 There are 0 to n rows and 0 to W
columns in the table
0 0 0 0 0 00000
01234
0 1 2 3 4 5
bull Now we will fill up the table either row by row or column by column Let us start filling the table row by row using following formula
8Table[11] with i=1 j=1 wi=2 and vi =3As j lt wi Table [11] = table [ i-1 j ]
= table [01] =0
Table[12] with i=1 j=2 wi=2 and vi =3
As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
Table[12] with i=1 j=3 wi=2 and vi =3As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
0 0 0 0 0 00 0 3 3 3 3000
01234
0 1 2 3 4 5
Compute this values in table
9 Continuing in this fashion we can compute all the values of table The table will be -
0 0 0 0 0 00 0 3 3 3 30 0 3 4 4 70 0 3 4 5 70 0 3 4 5 7
01234
0 1 2 3 4 5
7This is the total value of selected items
10How to find actual items
bull Now as we know that table [ n W ] is the total value of selected items that can be placed in the knapsack bull Following steps are used repeatedly to select actual knapsack item
Let I = n and k = W thenWhile( I gt 0 and k gt 0) if( table[ i k ] ne table[ i-1 k ]) then
Mark ith item as in knapsacki = i - 1 and k = k ndash wi
elsei=i-1
11 Let us apply these steps to the above given problem
i = 4 and k = 5 ie table [45] = table [35] Do not select ith ie 4th item
Now set i = i ndash 1 i = 3
i = 2 and k = 5 ie table [25] ne table [15]
Select ith ie 2nd item
Now set i = i ndash 1 i = 1
and k = k - wi k = 5 ndash 3 = 2
i = 3 and k = 5 ie table [35] = table [25] Do not select ith ie 3rd item
Now set i = i ndash 1 i = 2
1 2
3 4i = 1 and k = 52ie table [12] ne table [02]
Select ith ie 1st item
Now set i = i ndash 1 i = 0
and k = k - wi k = 2 ndash 2 = 0
bull Thus we have selected item 1 and item 2 for the knapsack
bull This solution can also represented by solution vector (1100)
12Algorithm Dynamic_Knapsack(n W w[] v[] )Pro-Des Algorithm for obtaining knapsack solution using dynamic programmingInput n is total no of items W is the capacity of knapsack w[] stores weight and v[] store valuesOutput return total value of selected items for knapsackfor( i larr0 to n) do
for ( j larr 0 to W) do
table[i0]=0table[0j]=0
for ( i larr 0 to n ) do
for ( j larr 0 to W ) doif( j ltw[ i ] ) then
table[ i j ] larr table[ i-1 j ]else if ( jgt= w[ i ]) thentable[ i j ] larr max ( table [ i-1 j ]( v[i] + table [ i-1 j-w[i] ] ))
return table[ n W ]
Algorithm for knapsack problem
13Analysis of knapsack dynamic algorithm -
bull In this algorithm the basic operation is if hellip else if statement within two for loops
Hencec(n)= sum
119895=0
119882
1sum119894= 0
119899
sum119894= 0
119899
119882 minus0+1=
sum119894=0
119899
119882 +1
sum119894=0
119899
119882sum119894=0
119899
1=
=
+
sum119894=0
119899
1sum119894=0
119899
1= W +
= W(n-0+1)+(n-0+1)= Wn + W + n + 1
Thus c(n) asymp Wn
The time complexity of this algorithm is (nW)
14
THANK YOU
6Example -
Item Weight Value 1 2 32 3 43 4 54 5 6
The capacity of knapsack is W = 5
Obtain optimal solution for the knapsack problem
7 Initially table [0j] = 0 and [I0] = 0 There are 0 to n rows and 0 to W
columns in the table
0 0 0 0 0 00000
01234
0 1 2 3 4 5
bull Now we will fill up the table either row by row or column by column Let us start filling the table row by row using following formula
8Table[11] with i=1 j=1 wi=2 and vi =3As j lt wi Table [11] = table [ i-1 j ]
= table [01] =0
Table[12] with i=1 j=2 wi=2 and vi =3
As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
Table[12] with i=1 j=3 wi=2 and vi =3As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
0 0 0 0 0 00 0 3 3 3 3000
01234
0 1 2 3 4 5
Compute this values in table
9 Continuing in this fashion we can compute all the values of table The table will be -
0 0 0 0 0 00 0 3 3 3 30 0 3 4 4 70 0 3 4 5 70 0 3 4 5 7
01234
0 1 2 3 4 5
7This is the total value of selected items
10How to find actual items
bull Now as we know that table [ n W ] is the total value of selected items that can be placed in the knapsack bull Following steps are used repeatedly to select actual knapsack item
Let I = n and k = W thenWhile( I gt 0 and k gt 0) if( table[ i k ] ne table[ i-1 k ]) then
Mark ith item as in knapsacki = i - 1 and k = k ndash wi
elsei=i-1
11 Let us apply these steps to the above given problem
i = 4 and k = 5 ie table [45] = table [35] Do not select ith ie 4th item
Now set i = i ndash 1 i = 3
i = 2 and k = 5 ie table [25] ne table [15]
Select ith ie 2nd item
Now set i = i ndash 1 i = 1
and k = k - wi k = 5 ndash 3 = 2
i = 3 and k = 5 ie table [35] = table [25] Do not select ith ie 3rd item
Now set i = i ndash 1 i = 2
1 2
3 4i = 1 and k = 52ie table [12] ne table [02]
Select ith ie 1st item
Now set i = i ndash 1 i = 0
and k = k - wi k = 2 ndash 2 = 0
bull Thus we have selected item 1 and item 2 for the knapsack
bull This solution can also represented by solution vector (1100)
12Algorithm Dynamic_Knapsack(n W w[] v[] )Pro-Des Algorithm for obtaining knapsack solution using dynamic programmingInput n is total no of items W is the capacity of knapsack w[] stores weight and v[] store valuesOutput return total value of selected items for knapsackfor( i larr0 to n) do
for ( j larr 0 to W) do
table[i0]=0table[0j]=0
for ( i larr 0 to n ) do
for ( j larr 0 to W ) doif( j ltw[ i ] ) then
table[ i j ] larr table[ i-1 j ]else if ( jgt= w[ i ]) thentable[ i j ] larr max ( table [ i-1 j ]( v[i] + table [ i-1 j-w[i] ] ))
return table[ n W ]
Algorithm for knapsack problem
13Analysis of knapsack dynamic algorithm -
bull In this algorithm the basic operation is if hellip else if statement within two for loops
Hencec(n)= sum
119895=0
119882
1sum119894= 0
119899
sum119894= 0
119899
119882 minus0+1=
sum119894=0
119899
119882 +1
sum119894=0
119899
119882sum119894=0
119899
1=
=
+
sum119894=0
119899
1sum119894=0
119899
1= W +
= W(n-0+1)+(n-0+1)= Wn + W + n + 1
Thus c(n) asymp Wn
The time complexity of this algorithm is (nW)
14
THANK YOU
7 Initially table [0j] = 0 and [I0] = 0 There are 0 to n rows and 0 to W
columns in the table
0 0 0 0 0 00000
01234
0 1 2 3 4 5
bull Now we will fill up the table either row by row or column by column Let us start filling the table row by row using following formula
8Table[11] with i=1 j=1 wi=2 and vi =3As j lt wi Table [11] = table [ i-1 j ]
= table [01] =0
Table[12] with i=1 j=2 wi=2 and vi =3
As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
Table[12] with i=1 j=3 wi=2 and vi =3As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
0 0 0 0 0 00 0 3 3 3 3000
01234
0 1 2 3 4 5
Compute this values in table
9 Continuing in this fashion we can compute all the values of table The table will be -
0 0 0 0 0 00 0 3 3 3 30 0 3 4 4 70 0 3 4 5 70 0 3 4 5 7
01234
0 1 2 3 4 5
7This is the total value of selected items
10How to find actual items
bull Now as we know that table [ n W ] is the total value of selected items that can be placed in the knapsack bull Following steps are used repeatedly to select actual knapsack item
Let I = n and k = W thenWhile( I gt 0 and k gt 0) if( table[ i k ] ne table[ i-1 k ]) then
Mark ith item as in knapsacki = i - 1 and k = k ndash wi
elsei=i-1
11 Let us apply these steps to the above given problem
i = 4 and k = 5 ie table [45] = table [35] Do not select ith ie 4th item
Now set i = i ndash 1 i = 3
i = 2 and k = 5 ie table [25] ne table [15]
Select ith ie 2nd item
Now set i = i ndash 1 i = 1
and k = k - wi k = 5 ndash 3 = 2
i = 3 and k = 5 ie table [35] = table [25] Do not select ith ie 3rd item
Now set i = i ndash 1 i = 2
1 2
3 4i = 1 and k = 52ie table [12] ne table [02]
Select ith ie 1st item
Now set i = i ndash 1 i = 0
and k = k - wi k = 2 ndash 2 = 0
bull Thus we have selected item 1 and item 2 for the knapsack
bull This solution can also represented by solution vector (1100)
12Algorithm Dynamic_Knapsack(n W w[] v[] )Pro-Des Algorithm for obtaining knapsack solution using dynamic programmingInput n is total no of items W is the capacity of knapsack w[] stores weight and v[] store valuesOutput return total value of selected items for knapsackfor( i larr0 to n) do
for ( j larr 0 to W) do
table[i0]=0table[0j]=0
for ( i larr 0 to n ) do
for ( j larr 0 to W ) doif( j ltw[ i ] ) then
table[ i j ] larr table[ i-1 j ]else if ( jgt= w[ i ]) thentable[ i j ] larr max ( table [ i-1 j ]( v[i] + table [ i-1 j-w[i] ] ))
return table[ n W ]
Algorithm for knapsack problem
13Analysis of knapsack dynamic algorithm -
bull In this algorithm the basic operation is if hellip else if statement within two for loops
Hencec(n)= sum
119895=0
119882
1sum119894= 0
119899
sum119894= 0
119899
119882 minus0+1=
sum119894=0
119899
119882 +1
sum119894=0
119899
119882sum119894=0
119899
1=
=
+
sum119894=0
119899
1sum119894=0
119899
1= W +
= W(n-0+1)+(n-0+1)= Wn + W + n + 1
Thus c(n) asymp Wn
The time complexity of this algorithm is (nW)
14
THANK YOU
8Table[11] with i=1 j=1 wi=2 and vi =3As j lt wi Table [11] = table [ i-1 j ]
= table [01] =0
Table[12] with i=1 j=2 wi=2 and vi =3
As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
Table[12] with i=1 j=3 wi=2 and vi =3As j ge wi Table [12] = max table [ i-1 j ] vi + table[i-1j-wi]
= max table [02] 3 + table [00] = max 0 3+0 = 3
0 0 0 0 0 00 0 3 3 3 3000
01234
0 1 2 3 4 5
Compute this values in table
9 Continuing in this fashion we can compute all the values of table The table will be -
0 0 0 0 0 00 0 3 3 3 30 0 3 4 4 70 0 3 4 5 70 0 3 4 5 7
01234
0 1 2 3 4 5
7This is the total value of selected items
10How to find actual items
bull Now as we know that table [ n W ] is the total value of selected items that can be placed in the knapsack bull Following steps are used repeatedly to select actual knapsack item
Let I = n and k = W thenWhile( I gt 0 and k gt 0) if( table[ i k ] ne table[ i-1 k ]) then
Mark ith item as in knapsacki = i - 1 and k = k ndash wi
elsei=i-1
11 Let us apply these steps to the above given problem
i = 4 and k = 5 ie table [45] = table [35] Do not select ith ie 4th item
Now set i = i ndash 1 i = 3
i = 2 and k = 5 ie table [25] ne table [15]
Select ith ie 2nd item
Now set i = i ndash 1 i = 1
and k = k - wi k = 5 ndash 3 = 2
i = 3 and k = 5 ie table [35] = table [25] Do not select ith ie 3rd item
Now set i = i ndash 1 i = 2
1 2
3 4i = 1 and k = 52ie table [12] ne table [02]
Select ith ie 1st item
Now set i = i ndash 1 i = 0
and k = k - wi k = 2 ndash 2 = 0
bull Thus we have selected item 1 and item 2 for the knapsack
bull This solution can also represented by solution vector (1100)
12Algorithm Dynamic_Knapsack(n W w[] v[] )Pro-Des Algorithm for obtaining knapsack solution using dynamic programmingInput n is total no of items W is the capacity of knapsack w[] stores weight and v[] store valuesOutput return total value of selected items for knapsackfor( i larr0 to n) do
for ( j larr 0 to W) do
table[i0]=0table[0j]=0
for ( i larr 0 to n ) do
for ( j larr 0 to W ) doif( j ltw[ i ] ) then
table[ i j ] larr table[ i-1 j ]else if ( jgt= w[ i ]) thentable[ i j ] larr max ( table [ i-1 j ]( v[i] + table [ i-1 j-w[i] ] ))
return table[ n W ]
Algorithm for knapsack problem
13Analysis of knapsack dynamic algorithm -
bull In this algorithm the basic operation is if hellip else if statement within two for loops
Hencec(n)= sum
119895=0
119882
1sum119894= 0
119899
sum119894= 0
119899
119882 minus0+1=
sum119894=0
119899
119882 +1
sum119894=0
119899
119882sum119894=0
119899
1=
=
+
sum119894=0
119899
1sum119894=0
119899
1= W +
= W(n-0+1)+(n-0+1)= Wn + W + n + 1
Thus c(n) asymp Wn
The time complexity of this algorithm is (nW)
14
THANK YOU
9 Continuing in this fashion we can compute all the values of table The table will be -
0 0 0 0 0 00 0 3 3 3 30 0 3 4 4 70 0 3 4 5 70 0 3 4 5 7
01234
0 1 2 3 4 5
7This is the total value of selected items
10How to find actual items
bull Now as we know that table [ n W ] is the total value of selected items that can be placed in the knapsack bull Following steps are used repeatedly to select actual knapsack item
Let I = n and k = W thenWhile( I gt 0 and k gt 0) if( table[ i k ] ne table[ i-1 k ]) then
Mark ith item as in knapsacki = i - 1 and k = k ndash wi
elsei=i-1
11 Let us apply these steps to the above given problem
i = 4 and k = 5 ie table [45] = table [35] Do not select ith ie 4th item
Now set i = i ndash 1 i = 3
i = 2 and k = 5 ie table [25] ne table [15]
Select ith ie 2nd item
Now set i = i ndash 1 i = 1
and k = k - wi k = 5 ndash 3 = 2
i = 3 and k = 5 ie table [35] = table [25] Do not select ith ie 3rd item
Now set i = i ndash 1 i = 2
1 2
3 4i = 1 and k = 52ie table [12] ne table [02]
Select ith ie 1st item
Now set i = i ndash 1 i = 0
and k = k - wi k = 2 ndash 2 = 0
bull Thus we have selected item 1 and item 2 for the knapsack
bull This solution can also represented by solution vector (1100)
12Algorithm Dynamic_Knapsack(n W w[] v[] )Pro-Des Algorithm for obtaining knapsack solution using dynamic programmingInput n is total no of items W is the capacity of knapsack w[] stores weight and v[] store valuesOutput return total value of selected items for knapsackfor( i larr0 to n) do
for ( j larr 0 to W) do
table[i0]=0table[0j]=0
for ( i larr 0 to n ) do
for ( j larr 0 to W ) doif( j ltw[ i ] ) then
table[ i j ] larr table[ i-1 j ]else if ( jgt= w[ i ]) thentable[ i j ] larr max ( table [ i-1 j ]( v[i] + table [ i-1 j-w[i] ] ))
return table[ n W ]
Algorithm for knapsack problem
13Analysis of knapsack dynamic algorithm -
bull In this algorithm the basic operation is if hellip else if statement within two for loops
Hencec(n)= sum
119895=0
119882
1sum119894= 0
119899
sum119894= 0
119899
119882 minus0+1=
sum119894=0
119899
119882 +1
sum119894=0
119899
119882sum119894=0
119899
1=
=
+
sum119894=0
119899
1sum119894=0
119899
1= W +
= W(n-0+1)+(n-0+1)= Wn + W + n + 1
Thus c(n) asymp Wn
The time complexity of this algorithm is (nW)
14
THANK YOU
10How to find actual items
bull Now as we know that table [ n W ] is the total value of selected items that can be placed in the knapsack bull Following steps are used repeatedly to select actual knapsack item
Let I = n and k = W thenWhile( I gt 0 and k gt 0) if( table[ i k ] ne table[ i-1 k ]) then
Mark ith item as in knapsacki = i - 1 and k = k ndash wi
elsei=i-1
11 Let us apply these steps to the above given problem
i = 4 and k = 5 ie table [45] = table [35] Do not select ith ie 4th item
Now set i = i ndash 1 i = 3
i = 2 and k = 5 ie table [25] ne table [15]
Select ith ie 2nd item
Now set i = i ndash 1 i = 1
and k = k - wi k = 5 ndash 3 = 2
i = 3 and k = 5 ie table [35] = table [25] Do not select ith ie 3rd item
Now set i = i ndash 1 i = 2
1 2
3 4i = 1 and k = 52ie table [12] ne table [02]
Select ith ie 1st item
Now set i = i ndash 1 i = 0
and k = k - wi k = 2 ndash 2 = 0
bull Thus we have selected item 1 and item 2 for the knapsack
bull This solution can also represented by solution vector (1100)
12Algorithm Dynamic_Knapsack(n W w[] v[] )Pro-Des Algorithm for obtaining knapsack solution using dynamic programmingInput n is total no of items W is the capacity of knapsack w[] stores weight and v[] store valuesOutput return total value of selected items for knapsackfor( i larr0 to n) do
for ( j larr 0 to W) do
table[i0]=0table[0j]=0
for ( i larr 0 to n ) do
for ( j larr 0 to W ) doif( j ltw[ i ] ) then
table[ i j ] larr table[ i-1 j ]else if ( jgt= w[ i ]) thentable[ i j ] larr max ( table [ i-1 j ]( v[i] + table [ i-1 j-w[i] ] ))
return table[ n W ]
Algorithm for knapsack problem
13Analysis of knapsack dynamic algorithm -
bull In this algorithm the basic operation is if hellip else if statement within two for loops
Hencec(n)= sum
119895=0
119882
1sum119894= 0
119899
sum119894= 0
119899
119882 minus0+1=
sum119894=0
119899
119882 +1
sum119894=0
119899
119882sum119894=0
119899
1=
=
+
sum119894=0
119899
1sum119894=0
119899
1= W +
= W(n-0+1)+(n-0+1)= Wn + W + n + 1
Thus c(n) asymp Wn
The time complexity of this algorithm is (nW)
14
THANK YOU
11 Let us apply these steps to the above given problem
i = 4 and k = 5 ie table [45] = table [35] Do not select ith ie 4th item
Now set i = i ndash 1 i = 3
i = 2 and k = 5 ie table [25] ne table [15]
Select ith ie 2nd item
Now set i = i ndash 1 i = 1
and k = k - wi k = 5 ndash 3 = 2
i = 3 and k = 5 ie table [35] = table [25] Do not select ith ie 3rd item
Now set i = i ndash 1 i = 2
1 2
3 4i = 1 and k = 52ie table [12] ne table [02]
Select ith ie 1st item
Now set i = i ndash 1 i = 0
and k = k - wi k = 2 ndash 2 = 0
bull Thus we have selected item 1 and item 2 for the knapsack
bull This solution can also represented by solution vector (1100)
12Algorithm Dynamic_Knapsack(n W w[] v[] )Pro-Des Algorithm for obtaining knapsack solution using dynamic programmingInput n is total no of items W is the capacity of knapsack w[] stores weight and v[] store valuesOutput return total value of selected items for knapsackfor( i larr0 to n) do
for ( j larr 0 to W) do
table[i0]=0table[0j]=0
for ( i larr 0 to n ) do
for ( j larr 0 to W ) doif( j ltw[ i ] ) then
table[ i j ] larr table[ i-1 j ]else if ( jgt= w[ i ]) thentable[ i j ] larr max ( table [ i-1 j ]( v[i] + table [ i-1 j-w[i] ] ))
return table[ n W ]
Algorithm for knapsack problem
13Analysis of knapsack dynamic algorithm -
bull In this algorithm the basic operation is if hellip else if statement within two for loops
Hencec(n)= sum
119895=0
119882
1sum119894= 0
119899
sum119894= 0
119899
119882 minus0+1=
sum119894=0
119899
119882 +1
sum119894=0
119899
119882sum119894=0
119899
1=
=
+
sum119894=0
119899
1sum119894=0
119899
1= W +
= W(n-0+1)+(n-0+1)= Wn + W + n + 1
Thus c(n) asymp Wn
The time complexity of this algorithm is (nW)
14
THANK YOU
12Algorithm Dynamic_Knapsack(n W w[] v[] )Pro-Des Algorithm for obtaining knapsack solution using dynamic programmingInput n is total no of items W is the capacity of knapsack w[] stores weight and v[] store valuesOutput return total value of selected items for knapsackfor( i larr0 to n) do
for ( j larr 0 to W) do
table[i0]=0table[0j]=0
for ( i larr 0 to n ) do
for ( j larr 0 to W ) doif( j ltw[ i ] ) then
table[ i j ] larr table[ i-1 j ]else if ( jgt= w[ i ]) thentable[ i j ] larr max ( table [ i-1 j ]( v[i] + table [ i-1 j-w[i] ] ))
return table[ n W ]
Algorithm for knapsack problem
13Analysis of knapsack dynamic algorithm -
bull In this algorithm the basic operation is if hellip else if statement within two for loops
Hencec(n)= sum
119895=0
119882
1sum119894= 0
119899
sum119894= 0
119899
119882 minus0+1=
sum119894=0
119899
119882 +1
sum119894=0
119899
119882sum119894=0
119899
1=
=
+
sum119894=0
119899
1sum119894=0
119899
1= W +
= W(n-0+1)+(n-0+1)= Wn + W + n + 1
Thus c(n) asymp Wn
The time complexity of this algorithm is (nW)
14
THANK YOU
13Analysis of knapsack dynamic algorithm -
bull In this algorithm the basic operation is if hellip else if statement within two for loops
Hencec(n)= sum
119895=0
119882
1sum119894= 0
119899
sum119894= 0
119899
119882 minus0+1=
sum119894=0
119899
119882 +1
sum119894=0
119899
119882sum119894=0
119899
1=
=
+
sum119894=0
119899
1sum119894=0
119899
1= W +
= W(n-0+1)+(n-0+1)= Wn + W + n + 1
Thus c(n) asymp Wn
The time complexity of this algorithm is (nW)
14
THANK YOU
14
THANK YOU