Home Backend Development PHP Tutorial 01Dynamic programming of knapsack problem

01Dynamic programming of knapsack problem

Oct 29, 2019 am 10:52 AM
dynamic programming

01Dynamic programming of knapsack problem

The basic idea of ​​dynamic programming:

Dynamic programming algorithms are usually used to solve problems with certain optimal properties, that is, what we usually call Said optimal substructure properties.

The dynamic programming algorithm is similar to the divide-and-conquer method. Its basic idea is to decompose the problem to be solved into several sub-problems, solve the sub-problems first, and then obtain the solution to the original problem from the solutions of these sub-problems. The biggest difference from the divide-and-conquer method is that for problems that are suitable for solving by dynamic programming, the sub-problems obtained after decomposition are often not independent of each other, that is, the solution of the next sub-stage is based on the solution of the previous sub-stage. Further solution.

If the divide-and-conquer method is used to solve this type of problem, the number of sub-problems obtained by decomposition will be too many, and some sub-problems will be recalculated many times. If we can save the answers to the solved sub-problems and find the obtained answers when needed, we can avoid a lot of repeated calculations and save time. We can use a table to record the answers to all solved subproblems. Regardless of whether the subproblem is used later, as long as it is calculated, its results are filled in the table.

Problem description:

Given N items and a backpack. The weight of item i is Wi, its value is Vi, and the capacity of the backpack is C. How should I choose the items to put in the backpack so that the total value of the items transferred to the backpack is maximized? ?

When selecting items, there are only two options for each item i, namely loading it into the backpack or not loading it into the backpack. You cannot load item i multiple times, nor can you load only part of the item. Therefore, this problem is called the 0-1 knapsack problem.

Problem analysis: Let V(i,j) represent that the capacity that can be loaded into the first i(1<=i<=n) items is j(1<=j< ; = the maximum value of the items in the backpack of C), the following dynamic programming function can be obtained:

(1)   V(i,0)=V(0,j)=0 

(2)   (a)  V(i,j)=V(i-1,j)    j<wi  

      (b)  V(i,j)=max{V(i-1,j) ,V(i-1,j-wi)+vi) }   j>wi
Copy after login

(1) Formula shows: If the weight of the i-th item is greater than the capacity of the backpack, then the The maximum value obtained by item i is the same as the maximum value obtained by i-1 items before loading, that is, item i cannot be loaded into the backpack.

(2) Expression shows: If the weight of the i-th item is less than the capacity of the backpack, there will be two situations: (a) If the i-th item is not loaded into the backpack, then the value of the items in the backpack It is equivalent to the value obtained by putting the first i-1 items into a backpack with capacity j. (b) If the i-th item is put into the backpack, the value of the backpack item is equal to the value of the i-1th item in the backpack with capacity j-wi plus the value of the i-th item vi; Obviously, take The one with the greatest value is the optimal solution for loading the first i items into a backpack with a capacity of j.

Recommended tutorial: PHP tutorial

The above is the detailed content of 01Dynamic programming of knapsack problem. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
How to write a dynamic programming algorithm using C# How to write a dynamic programming algorithm using C# Sep 20, 2023 pm 04:03 PM

How to use C# to write dynamic programming algorithm Summary: Dynamic programming is a common algorithm for solving optimization problems and is suitable for a variety of scenarios. This article will introduce how to use C# to write dynamic programming algorithms and provide specific code examples. 1. What is a dynamic programming algorithm? Dynamic Programming (DP) is an algorithmic idea used to solve problems with overlapping subproblems and optimal substructure properties. Dynamic programming decomposes the problem into several sub-problems to solve, and records the solution to each sub-problem.

PHP algorithm analysis: How to use dynamic programming algorithm to solve the longest palindrome substring problem? PHP algorithm analysis: How to use dynamic programming algorithm to solve the longest palindrome substring problem? Sep 19, 2023 pm 12:19 PM

PHP algorithm analysis: How to use dynamic programming algorithm to solve the longest palindrome substring problem? Dynamic Programming (Dynamic Programming) is a commonly used algorithm idea that can solve many complex problems. One of them is the longest palindrome substring problem, which is to find the length of the longest palindrome substring in a string. This article will introduce how to use PHP to write a dynamic programming algorithm to solve this problem, and provide specific code examples. Let’s first define the longest palindrome substring. A palindrome string refers to a string that reads the same forward and backward, and the palindrome string

How to use the knapsack problem algorithm in C++ How to use the knapsack problem algorithm in C++ Sep 21, 2023 pm 02:18 PM

How to use the knapsack problem algorithm in C++ The knapsack problem is one of the classic problems in computer algorithms. It involves how to select some items to put into the knapsack under a given knapsack capacity to maximize the total value of the items. This article will introduce in detail how to use the dynamic programming algorithm in C++ to solve the knapsack problem, and give specific code examples. First, we need to define the input and output of the knapsack problem. The input includes the weight array wt[] of the item, the value array val[] of the item, and the capacity W of the backpack. The output is which objects are selected

Memoization (1D, 2D and 3D) Dynamic Programming in Java Memoization (1D, 2D and 3D) Dynamic Programming in Java Aug 23, 2023 pm 02:13 PM

Memoization is a technique based on dynamic programming used to improve the performance of recursive algorithms by ensuring that a method does not run multiple times on the same set of inputs, by recording the results (stored in an array) for the inputs provided. Memoization can be achieved through a top-down approach that implements recursive methods. Let’s understand this situation through a basic Fibonacci sequence example. 1-D memoization We will consider a recursive algorithm with only one non-constant parameter (only one parameter changes in value), so this method is called 1-D memoization. The following code is for finding the Nth (all terms up to N) in the Fibonacci sequence. Example publicintfibonacci(intn){ &nb

How to solve knapsack problem in PHP using dynamic programming algorithm and get optimal solution? How to solve knapsack problem in PHP using dynamic programming algorithm and get optimal solution? Sep 21, 2023 am 10:33 AM

How to solve knapsack problem in PHP using dynamic programming algorithm and get optimal solution? The knapsack problem is one of the classic combinatorial optimization problems in computer science. Given a set of items and the capacity of a knapsack, how to select items to put into the knapsack so as to maximize the total value of the items in the knapsack is the core of the knapsack problem that needs to be solved. Dynamic programming is one of the common methods to solve the knapsack problem. It finally obtains the optimal solution by splitting the problem into sub-problems and saving the solutions to the sub-problems. Below we will explain in detail how to use dynamic programming algorithm in PHP

Detailed explanation of dynamic programming algorithm in PHP Detailed explanation of dynamic programming algorithm in PHP Jul 07, 2023 am 10:48 AM

Detailed explanation of dynamic programming algorithm in PHP Dynamic programming (Dynamic Programming) is an algorithmic idea for solving problems. It solves the overall problem by decomposing the problem into smaller sub-problems and using the results of the solved sub-problems. In PHP, dynamic programming algorithms can be widely used in many fields of computer science and mathematics, such as shortest paths, string matching, and knapsack problems. This article will introduce the principles of dynamic programming algorithm in PHP in detail and provide code examples to illustrate. 1. Dynamic programming calculation

Recursive implementation of C++ functions: What are the similarities and differences between recursion and dynamic programming algorithms? Recursive implementation of C++ functions: What are the similarities and differences between recursion and dynamic programming algorithms? Apr 22, 2024 pm 10:00 PM

Recursion is a technique in which functions call themselves. The recursion keyword is used in C++ to define recursive functions. The syntax of the recursive function is: returnTypefunctionName(parameters){if(condition){returnresult;}else{returnfunctionName(newParameters);}}. Compared with the dynamic programming algorithm, the recursive algorithm is less efficient and requires larger memory, while the dynamic Planning algorithms improve efficiency and reduce memory usage by storing intermediate results.

PHP algorithm analysis: How to use dynamic programming algorithm to solve the longest common subsequence problem? PHP algorithm analysis: How to use dynamic programming algorithm to solve the longest common subsequence problem? Sep 20, 2023 pm 04:25 PM

PHP algorithm analysis: How to use dynamic programming algorithm to solve the longest common subsequence problem? Dynamic Programming algorithm (Dynamic Programming) is a mathematical optimization method usually used to solve problems with overlapping sub-problems and optimal sub-structure properties. Among them, the longest common subsequence problem is a classic dynamic programming problem, which has wide applications in fields such as string processing, graph theory, and bioinformatics. The longest common subsequence problem can be briefly described as: given two strings s1 and s2, find their

See all articles