The document discusses the 0-1 knapsack problem and presents a dynamic programming algorithm to solve it. The 0-1 knapsack problem aims to maximize the total value of items selected without exceeding the knapsack's weight capacity, where each item must either be fully included or excluded. The algorithm uses a table B to store the maximum value for each sub-problem of filling weight w with the first i items, calculating entries recursively to find the overall optimal value B[n,W]. An example demonstrates filling the table to solve an instance of the problem.
Fourth order improved finite difference approach to pure bending analysis o...eSAT Journals
Abstract Analytical Methods provides exact solutions which have restrictions due to their inherent difficulties. On the other hand, Numerical methods provide approximate solution to governing differential equations. In this research, Improved Finite Difference Method (IFDM) using Polynomial series was used to develop a finite difference pattern. The patterns were applied in analyzing pure bending of line continuum with various boundary conditions. The results obtained from this numerical method were compared with those from exact method to check the accuracy of the solution. The results were found to be identical and very close to the exact results, with percentage difference ranging from 0 – 0.02%. Hence, Improved Finite Difference Method provides simple and approximate solution that are close to the exact value for pure bending analysis of line continuum. Key Words: Improved Finite Difference, Polynomial Series, Linear continuum, Numerical Methods…
Fourth order improved finite difference approach to pure bending analysis o...eSAT Journals
Abstract Analytical Methods provides exact solutions which have restrictions due to their inherent difficulties. On the other hand, Numerical methods provide approximate solution to governing differential equations. In this research, Improved Finite Difference Method (IFDM) using Polynomial series was used to develop a finite difference pattern. The patterns were applied in analyzing pure bending of line continuum with various boundary conditions. The results obtained from this numerical method were compared with those from exact method to check the accuracy of the solution. The results were found to be identical and very close to the exact results, with percentage difference ranging from 0 – 0.02%. Hence, Improved Finite Difference Method provides simple and approximate solution that are close to the exact value for pure bending analysis of line continuum. Key Words: Improved Finite Difference, Polynomial Series, Linear continuum, Numerical Methods…
Knapsack problem ==>>
Given some items, pack the knapsack to get
the maximum total value. Each item has some
weight and some value. Total weight that we can
carry is no more than some fixed number W.
So we must consider weights of items as well as
their values.
This Presentation will Use to develop your knowledge and doubts in Knapsack problem. This Slide also include Memory function part. Use this Slides to Develop your knowledge on Knapsack and Memory function
Knapsack problem ==>>
Given some items, pack the knapsack to get
the maximum total value. Each item has some
weight and some value. Total weight that we can
carry is no more than some fixed number W.
So we must consider weights of items as well as
their values.
This Presentation will Use to develop your knowledge and doubts in Knapsack problem. This Slide also include Memory function part. Use this Slides to Develop your knowledge on Knapsack and Memory function
Saudi Arabia stands as a titan in the global energy landscape, renowned for its abundant oil and gas resources. It's the largest exporter of petroleum and holds some of the world's most significant reserves. Let's delve into the top 10 oil and gas projects shaping Saudi Arabia's energy future in 2024.
Forklift Classes Overview by Intella PartsIntella Parts
Discover the different forklift classes and their specific applications. Learn how to choose the right forklift for your needs to ensure safety, efficiency, and compliance in your operations.
For more technical information, visit our website https://intellaparts.com
Immunizing Image Classifiers Against Localized Adversary Attacksgerogepatton
This paper addresses the vulnerability of deep learning models, particularly convolutional neural networks
(CNN)s, to adversarial attacks and presents a proactive training technique designed to counter them. We
introduce a novel volumization algorithm, which transforms 2D images into 3D volumetric representations.
When combined with 3D convolution and deep curriculum learning optimization (CLO), itsignificantly improves
the immunity of models against localized universal attacks by up to 40%. We evaluate our proposed approach
using contemporary CNN architectures and the modified Canadian Institute for Advanced Research (CIFAR-10
and CIFAR-100) and ImageNet Large Scale Visual Recognition Challenge (ILSVRC12) datasets, showcasing
accuracy improvements over previous techniques. The results indicate that the combination of the volumetric
input and curriculum learning holds significant promise for mitigating adversarial attacks without necessitating
adversary training.
Overview of the fundamental roles in Hydropower generation and the components involved in wider Electrical Engineering.
This paper presents the design and construction of hydroelectric dams from the hydrologist’s survey of the valley before construction, all aspects and involved disciplines, fluid dynamics, structural engineering, generation and mains frequency regulation to the very transmission of power through the network in the United Kingdom.
Author: Robbie Edward Sayers
Collaborators and co editors: Charlie Sims and Connor Healey.
(C) 2024 Robbie E. Sayers
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdffxintegritypublishin
Advancements in technology unveil a myriad of electrical and electronic breakthroughs geared towards efficiently harnessing limited resources to meet human energy demands. The optimization of hybrid solar PV panels and pumped hydro energy supply systems plays a pivotal role in utilizing natural resources effectively. This initiative not only benefits humanity but also fosters environmental sustainability. The study investigated the design optimization of these hybrid systems, focusing on understanding solar radiation patterns, identifying geographical influences on solar radiation, formulating a mathematical model for system optimization, and determining the optimal configuration of PV panels and pumped hydro storage. Through a comparative analysis approach and eight weeks of data collection, the study addressed key research questions related to solar radiation patterns and optimal system design. The findings highlighted regions with heightened solar radiation levels, showcasing substantial potential for power generation and emphasizing the system's efficiency. Optimizing system design significantly boosted power generation, promoted renewable energy utilization, and enhanced energy storage capacity. The study underscored the benefits of optimizing hybrid solar PV panels and pumped hydro energy supply systems for sustainable energy usage. Optimizing the design of solar PV panels and pumped hydro energy supply systems as examined across diverse climatic conditions in a developing country, not only enhances power generation but also improves the integration of renewable energy sources and boosts energy storage capacities, particularly beneficial for less economically prosperous regions. Additionally, the study provides valuable insights for advancing energy research in economically viable areas. Recommendations included conducting site-specific assessments, utilizing advanced modeling tools, implementing regular maintenance protocols, and enhancing communication among system components.
Final project report on grocery store management system..pdfKamal Acharya
In today’s fast-changing business environment, it’s extremely important to be able to respond to client needs in the most effective and timely manner. If your customers wish to see your business online and have instant access to your products or services.
Online Grocery Store is an e-commerce website, which retails various grocery products. This project allows viewing various products available enables registered users to purchase desired products instantly using Paytm, UPI payment processor (Instant Pay) and also can place order by using Cash on Delivery (Pay Later) option. This project provides an easy access to Administrators and Managers to view orders placed using Pay Later and Instant Pay options.
In order to develop an e-commerce website, a number of Technologies must be studied and understood. These include multi-tiered architecture, server and client-side scripting techniques, implementation technologies, programming language (such as PHP, HTML, CSS, JavaScript) and MySQL relational databases. This is a project with the objective to develop a basic website where a consumer is provided with a shopping cart website and also to know about the technologies used to develop such a website.
This document will discuss each of the underlying technologies to create and implement an e- commerce website.
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...ssuser7dcef0
Power plants release a large amount of water vapor into the
atmosphere through the stack. The flue gas can be a potential
source for obtaining much needed cooling water for a power
plant. If a power plant could recover and reuse a portion of this
moisture, it could reduce its total cooling water intake
requirement. One of the most practical way to recover water
from flue gas is to use a condensing heat exchanger. The power
plant could also recover latent heat due to condensation as well
as sensible heat due to lowering the flue gas exit temperature.
Additionally, harmful acids released from the stack can be
reduced in a condensing heat exchanger by acid condensation. reduced in a condensing heat exchanger by acid condensation.
Condensation of vapors in flue gas is a complicated
phenomenon since heat and mass transfer of water vapor and
various acids simultaneously occur in the presence of noncondensable
gases such as nitrogen and oxygen. Design of a
condenser depends on the knowledge and understanding of the
heat and mass transfer processes. A computer program for
numerical simulations of water (H2O) and sulfuric acid (H2SO4)
condensation in a flue gas condensing heat exchanger was
developed using MATLAB. Governing equations based on
mass and energy balances for the system were derived to
predict variables such as flue gas exit temperature, cooling
water outlet temperature, mole fraction and condensation rates
of water and sulfuric acid vapors. The equations were solved
using an iterative solution technique with calculations of heat
and mass transfer coefficients and physical properties.
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)MdTanvirMahtab2
This presentation is about the working procedure of Shahjalal Fertilizer Company Limited (SFCL). A Govt. owned Company of Bangladesh Chemical Industries Corporation under Ministry of Industries.
2. Knapsack 0-1 Problem
• The goal is to maximize
the value of a knapsack
that can hold at most W
units (i.e. lbs or kg) worth
of goods from a list of
items I0, I1, … In-1.
– Each item has 2
attributes:
1) Value – let this be vi for
item Ii
2) Weight – let this be wi for
item Ii
Dr. AMIT KUMAR @JUET
3. Knapsack 0-1 Problem
• The difference
between this problem
and the fractional
knapsack one is that
you CANNOT take a
fraction of an item.
– You can either take it or
not.
– Hence the name
Knapsack 0-1 problem.
Dr. AMIT KUMAR @JUET
4. Knapsack 0-1 Problem
• Brute Force
– The naïve way to solve this problem is to cycle
through all 2n subsets of the n items and pick the
subset with a legal weight that maximizes the
value of the knapsack.
– We can come up with a dynamic programming
algorithm that will USUALLY do better than this
brute force technique.
Dr. AMIT KUMAR @JUET
5. Knapsack 0-1 Problem
• As we did before we are going to solve the
problem in terms of sub-problems.
– So let’s try to do that…
• Our first attempt might be to characterize a sub-
problem as follows:
– Let Sk be the optimal subset of elements from {I0, I1, …, Ik}.
• What we find is that the optimal subset from the elements
{I0, I1, …, Ik+1} may not correspond to the optimal subset of
elements from {I0, I1, …, Ik} in any regular pattern.
– Basically, the solution to the optimization problem for
Sk+1 might NOT contain the optimal solution from
problem Sk. Dr. AMIT KUMAR @JUET
6. Knapsack 0-1 Problem
• Let’s illustrate that point with an example:
Item Weight Value
I0 3 10
I1 8 4
I2 9 9
I3 8 11
• The maximum weight the knapsack can hold is 20.
• The best set of items from {I0, I1, I2} is {I0, I1, I2}
• BUT the best set of items from {I0, I1, I2, I3} is {I0, I2, I3}.
– In this example, note that this optimal solution, {I0, I2, I3}, does
NOT build upon the previous optimal solution, {I0, I1, I2}.
• (Instead it build's upon the solution, {I0, I2}, which is really the
optimal subset of {I0, I1, I2} with weight 12 or less.)
Dr. AMIT KUMAR @JUET
7. Knapsack 0-1 problem
• So now we must re-work the way we build upon previous sub-
problems…
– Let B[k, w] represent the maximum total value of a subset Sk with
weight w.
– Our goal is to find B[n, W], where n is the total number of items and
W is the maximal weight the knapsack can carry.
• So our recursive formula for subproblems:
B[k, w] = B[k - 1,w], if wk > w
= max { B[k - 1,w], B[k - 1,w - wk] + vk}, otherwise
• In English, this means that the best subset of Sk that has total
weight w is:
1) The best subset of Sk-1 that has total weight w, or
2) The best subset of Sk-1 that has total weight w-wk plus the item kDr. AMIT KUMAR @JUET
8. Knapsack 0-1 Problem –
Recursive Formula
• The best subset of Sk that has the total weight
w, either contains item k or not.
• First case: wk > w
– Item k can’t be part of the solution! If it was the
total weight would be > w, which is unacceptable.
• Second case: wk ≤ w
– Then the item k can be in the solution, and we
choose the case with greater value.Dr. AMIT KUMAR @JUET
9. Knapsack 0-1 Algorithm
for w = 0 to W { // Initialize 1st row to 0’s
B[0,w] = 0
}
for i = 1 to n { // Initialize 1st column to 0’s
B[i,0] = 0
}
for i = 1 to n {
for w = 0 to W {
if wi <= w { //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
}
else B[i,w] = B[i-1,w] // wi > w
}
}
Dr. AMIT KUMAR @JUET
10. Knapsack 0-1 Problem
• Let’s run our algorithm on the following data:
– n = 4 (# of elements)
– W = 5 (max weight)
– Elements (weight, value):
(2,3), (3,4), (4,5), (5,6)
Dr. AMIT KUMAR @JUET
11. Knapsack 0-1 Example
// Initialize the base cases
for w = 0 to W
B[0,w] = 0
for i = 1 to n
B[i,0] = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
Dr. AMIT KUMAR @JUET
12. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
i = 1
vi = 3
wi = 2
w = 1
w-wi = -1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0
2 0
3 0
4 0
Dr. AMIT KUMAR @JUET
13. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0
2 0
3 0
4 0
i = 1
vi = 3
wi = 2
w = 2
w-wi = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3
2 0
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Dr. AMIT KUMAR @JUET
14. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3
2 0
3 0
4 0
i = 1
vi = 3
wi = 2
w = 3
w-wi = 1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3
2 0
3 0
4 0
Dr. AMIT KUMAR @JUET
15. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3
2 0
3 0
4 0
i = 1
vi = 3
wi = 2
w = 4
w-wi = 2
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3
2 0
3 0
4 0
Dr. AMIT KUMAR @JUET
16. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3
2 0
3 0
4 0
i = 1
vi = 3
wi = 2
w = 5
w-wi = 3
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0
3 0
4 0
Dr. AMIT KUMAR @JUET
17. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0
3 0
4 0
i = 2
vi = 4
wi = 3
w = 1
w-wi = -2
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Dr. AMIT KUMAR @JUET
18. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0
3 0
4 0
i = 2
vi = 4
wi = 3
w = 2
w-wi = -1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3
3 0
4 0
Dr. AMIT KUMAR @JUET
19. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3
3 0
4 0
i = 2
vi = 4
wi = 3
w = 3
w-wi = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Dr. AMIT KUMAR @JUET
20. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4
3 0
4 0
i = 2
vi = 4
wi = 3
w = 4
w-wi = 1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4
3 0
4 0
Dr. AMIT KUMAR @JUET
21. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4
3 0
4 0
i = 2
vi = 4
wi = 3
w = 5
w-wi = 2
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0
4 0
Dr. AMIT KUMAR @JUET
22. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0
4 0
i = 3
vi = 5
wi = 4
w = 1..3
w-wi = -3..-1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Dr. AMIT KUMAR @JUET
23. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4
4 0
i = 3
vi = 5
wi = 4
w = 4
w-wi = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Dr. AMIT KUMAR @JUET
24. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5
4 0
i = 3
vi = 5
wi = 4
w = 5
w-wi = 1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Dr. AMIT KUMAR @JUET
25. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0
i = 4
vi = 6
wi = 5
w = 1..4
w-wi = -4..-1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Dr. AMIT KUMAR @JUET
26. Knapsack 0-1 Example
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5
i = 4
vi = 6
wi = 5
w = 5
w-wi = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Dr. AMIT KUMAR @JUET
27. Knapsack 0-1 Example
We’re DONE!!
The max possible value that can be carried in this knapsack is $7
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Dr. AMIT KUMAR @JUET
28. Knapsack 0-1 Algorithm
• This algorithm only finds the max possible
value that can be carried in the knapsack
– The value in B[n,W]
• To know the items that make this maximum
value, we need to trace back through the
table.
Dr. AMIT KUMAR @JUET
29. Knapsack 0-1 Algorithm
Finding the Items
• Let i = n and k = W
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1 // Assume the ith item is not in the knapsack
// Could it be in the optimally packed knapsack?
Dr. AMIT KUMAR @JUET
30. Knapsack 0-1 Algorithm
Finding the Items
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i = 4
k = 5
vi = 6
wi = 5
B[i,k] = 7
B[i-1,k] = 7
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
i = n , k = W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Knapsack:
Dr. AMIT KUMAR @JUET
31. Knapsack 0-1 Algorithm
Finding the Items
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i = 3
k = 5
vi = 5
wi = 4
B[i,k] = 7
B[i-1,k] = 7
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
i = n , k = W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Knapsack:
Dr. AMIT KUMAR @JUET
32. Knapsack 0-1 Algorithm
Finding the Items
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i = 2
k = 5
vi = 4
wi = 3
B[i,k] = 7
B[i-1,k] = 3
k – wi = 2
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
i = n , k = W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Knapsack:
Item 2
Dr. AMIT KUMAR @JUET
33. Knapsack 0-1 Algorithm
Finding the Items
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i = 1
k = 2
vi = 3
wi = 2
B[i,k] = 3
B[i-1,k] = 0
k – wi = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
i = n , k = W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Knapsack:
Item 1
Item 2
Dr. AMIT KUMAR @JUET
34. Knapsack 0-1 Algorithm
Finding the Items
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i = 1
k = 2
vi = 3
wi = 2
B[i,k] = 3
B[i-1,k] = 0
k – wi = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
k = 0, so we’re DONE!
The optimal knapsack should contain:
Item 1 and Item 2
Knapsack:
Item 1
Item 2
Dr. AMIT KUMAR @JUET
35. Knapsack 0-1 Problem – Run Time
for w = 0 to W
B[0,w] = 0
for i = 1 to n
B[i,0] = 0
for i = 1 to n
for w = 0 to W
< the rest of the code >
What is the running time of this algorithm?
O(n*W)
Remember that the brute-force algorithm takes: O(2n)
O(W)
O(W)
Repeat n times
O(n)
Dr. AMIT KUMAR @JUET
36. Knapsack Problem
1) Fill out the
dynamic
programming
table for the
knapsack
problem to the
right.
2) Trace back
through the
table to find the
items in the Dr. AMIT KUMAR @JUET
37. References
• Slides adapted from Arup Guha’s Computer
Science II Lecture notes:
http://www.cs.ucf.edu/~dmarino/ucf/cop3503/
lectures/
• Additional material from the textbook:
Data Structures and Algorithm Analysis in Java (Second
Edition) by Mark Allen Weiss
• Additional images:
www.wikipedia.com
xkcd.com
Dr. AMIT KUMAR @JUET