In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the Java data structure". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "what is the Java data structure"?
Application scenario-knapsack problem
Backpack problem: there is a backpack with a capacity of 4 pounds and the following items are available:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
The goal required to be achieved is that the total value of the loaded backpack is the largest and the weight does not exceed
The items required to be loaded cannot be repeated.
Introduction of dynamic programming algorithm
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
The core idea of dynamic programming (Dynamic Programming) algorithm is to divide large problems into small problems to solve, so as to obtain the optimal solution step by step.
The dynamic programming algorithm is similar to the divide-and-conquer algorithm, its basic idea is to decompose the problem into several sub-problems, first solve the sub-problems, and then get the solution of the original problem from the solutions of these sub-problems.
Different from the divide-and-conquer algorithm, when it is suitable for dynamic programming, the sub-problems 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.)
Dynamic programming can be promoted step by step by filling in the form to get the optimal solution.
Analysis of knapsack problem
Knapsack problem mainly refers to a given capacity of the knapsack, a number of items with a certain value and weight, how to select items into the backpack is the greatest value of items. It is divided into 01 backpack and complete backpack (full backpack refers to: there are unlimited items available for each item).
The problem here belongs to the 01 knapsack, that is, each item can put at most one, and the infinite backpack can be converted into the 01 backpack.
Train of thought analysis
The main idea of the algorithm is solved by dynamic programming. For the first item traversed each time, determine whether the item needs to be put into the backpack according to w [I] and v [I]. That is, for a given n items, let v [I] and w [I] be the value and weight of the first item respectively, and C is the capacity of the backpack. Then let v [I] [j] represent the maximum value that can be loaded into a backpack with a capacity of j in the first I items. The results are as follows:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
V [I] [0] = v [0] [j] = 0 position / indicates that the first row and first column of the table are 0
When w [I] > j: v [I] [j] = v [I-1] [j]; / / when the capacity of the new item to be added is greater than the capacity of the current backpack, directly use the loading strategy of the previous cell.
When j > = w [I]: v [I] [j] = max {v [I-1] [j], v [I] + v [I-1] [jripw [I]]} / / when the capacity of the newly added item to be added is less than or equal to the capacity of the current backpack, the loading method: v [I-1] [j]: the maximum value of the previous cell v [I]: the value of the current commodity v [I-1] [Jmurw [I]]: load the imur1 commodity to the maximum value of the remaining space [jmurw [I]
The process of filling out a form
Code case package com.xie.algorithm; import java.util.Arrays; public class KnapsackProblem {public static void main (String [] args) {/ / the weight of the item int [] w = {1,4,3}; / / the value of the item int [] val = {1500, 3000, 2000}; / / the capacity of the backpack int m = 4 / / number of items int n = val.length; / / in order to record the placement of goods, define a two-dimensional array int [] [] path = new int [n + 1] [m + 1] / / create a two-dimensional array / / v [I] [j] represents the maximum value int [] [] v = new int [n + 1] [m + 1] that can be loaded into a backpack of capacity j in the first I items; / / initialize the first row and first column / / set the first column to 0 for (int I = 0; I)
< v.length; i++) { v[i][0] = 0; } //将第一行设置为0 for (int i = 0; i < v[0].length; i++) { v[0][i] = 0; } //根据前面的公式来动态规划处理 //不处理第一行 for (int i = 1; i < v.length; i++) { //不处理第一列 for (int j = 1; j < v[0].length; j++) { //公式 //因为我们的程序 i 是从1开始的,因此原来的公式中的w[i]修改成w[i-1] if (w[i - 1] >J) {v [I] [j] = v [I-1] [j];} else {/ / because our program I starts with 1 / v [I] [j] = Math.max (v [I-1] [j], val [I-1] + v [I-1] [j-w [I-1]]) If (v [I-1] [j] > val [I-1] + v [I-1] [j-w [I-1]]) {v [I] [j] = v [I-1] [j] } else {v [I] [j] = val [I-1] + v [I-1] [j-w [I-1]]; / / record the current situation to path path [I] [j] = 1 } for (int I = 0; I
< v.length; i++) { System.out.println(Arrays.toString(v[i])); } int i = path.length - 1; int j = path[0].length - 1; while (i >0 & & j > 0) {if (path [I] [j] = = 1) {System.out.printf ("% d item in backpack\ n", I); j-= w [i1];} iMuir- }} / * * [0,0,0,0] * [0, 1500, 1500, 1500, 1500] * [0, 1500, 1500, 1500, 3000] * [0, 1500, 1500, 1500, 3500] * the third commodity in the backpack * the first commodity in the backpack * /} Thank you for your reading The above is the content of "what is the Java data structure". After the study of this article, I believe you have a deeper understanding of what is the Java data structure, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.