C # Algorithm Series - Greed Algorithm (Backpack Problem)

tags: Algorithm series  Asp.net & C#  C # algorithm  algorithm  Greedy algorithm  how are you  Backpack algorithm

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            / * =========== greedy algorithm - backpack problem ==============
                                 The knapsack problem is a combined optimized NP complete problem.
                                 The problem can be described as: given a set of items, each item has its own weight and price, within the limited total weight, how we choose to make the total price of the items in the backpack.
                                 The name of the problem comes from how to select the most suitable item placed in a given backpack.
                                 Similar problems often appear in the fields of commercial, combined mathematics, computational complexity theory, cryptography and application mathematics.
                                 You can also describe the backpack problem as a decisive problem, that is, the total value can reach V, that is, the total weight does not exceed W.             
             */
                         // Cargo weight and value
            int[,] a;
                         // backpack load
            int w;
                         // Test data, the answer is 334
            a = new int[,] { { 79, 58, 86, 11, 28, 62, 15, 68 }, { 83, 14, 54, 79, 72, 52, 48, 62 } };
            w = 200;
                         INT V; / / maximum value
            v = GetMaxValue(w, a, 0);
                         Console.writeline ($ "The maximum value of the backpack can be: {v}");
            Console.ReadKey();
        }

        /// <summary>
                 /// After the backpack load W, the goods O, the maximum value obtained from the kth piece
        /// </summary>
                 /// <param name = "w"> Backpack load </ param>
                 /// <param name = "o"> Cargo O </ param>
                 /// <param name = "k"> The second piece starts the default index 0 </ param>
        /// <returns></returns>
        public static int GetMaxValue(int w, int[,] o, int k)
        {
            int v, t1, t2;
                         // If it is the last piece of goods
            if (k == o.GetLength(1) - 1)
            {
                if (w >= o[0, k])
                {
                    v = o[1, k];
                }
                else
                {
                    v = 0;
                }
            }
                         / / If you load the current goods
            else if (w >= o[0, k])
            {
                t1 = GetMaxValue(w - o[0, k], o, k + 1) + o[1, k];
                t2 = GetMaxValue(w, o, k + 1);
                v = GetMax(t1, t2);
            }
                         // Install the situation of the current goods
            else
            {
                v = GetMaxValue(w, o, k + 1);
            }
            return v;
        }
        public static int GetMax(int a, int b)
        {
            return a > b ? a : b;
        }

    }
}

MSCL Super Tool Category Library
Based on C # development, the super tool class, contains database operations, string processing, file or folder processing
Hundreds of common tool classes, including network requests, cache processing, data containers, etc., came with instructions,
Provide CHM detailed documentation, hundreds of production environment, stable and efficient, easy to use.
True to do "Tools in Hands, Everything", let you greatly improve programming efficiency and improve programming levels.
Contact QQ: 7400799 (please note "MSCL")

===============================================

Important compressed file Forgot to decompress your password? Download RAR / ZIP / 7Z compressed files on the Internet, need your password?
==== Speed ​​Decryption Assistant, support support for RAR / ZIP / 7Z and other compressed documents decryption ======
★ Decryption does not exceed 24 hours, with the complexity of password
★ After the decryption is successful, then charge, no routine
★ After the decryption is successful, the original is automatically deleted, and there is no worries.
Contact QQ: 7400799 (please note "file decryption")

==============================================

Magic.orm has been applied in hundreds of mature projects, which is a relatively complete ORM framework (based on C # development). In the development process, refer to NBEAR and MySoft, some of them, add new ideas,
LaMBDA grammar for later reference EF conducts a lot of expansion.

Why choose Magic.orm?

  • Simple, 0 to learn costs. Easy to use, write a C # .NET code according to SQL writing habits. Powerful.
  • High performance, close to handwriting SQL.
  • Small size (less than 200KB, only one DLL).
  • Perfect support for SQL Server (2000 to Latest), MySQL, Oracle, Access, SQLITE and other databases.
  • Support a large number of lambda expressions.
  • There is no need to configure the XML configuration like NHibernate, and it is not necessary to connect to various database connections like EF, and integrate simple.

Please contact QQ: 7400799 (please note "ORM")

Intelligent Recommendation

Algorithm course design (3): Document connection problem - greed algorithm, C ++

Document connection problem: given an array f, array element F [i] represents the length of the i-th file. Now you need to merge all files into a file. The longer the file is connected to the new file...

Algorithm review - some backpack issues of greed strategies

Algorithm review - some backpack issues of greed strategies The following content is mainly referred to China University MOOC"Design and Analysis of Algorithms", The wall is recommended to g...

Greed algorithm understanding and classic case --- backpack case

As the title: The greedy algorithm (also known as the greedy algorithm) means that when solving the problem, it is always the best choice to make it at present. In other words, not considering the ove...

Leetcode algorithm series three (greed)

1.7 related questions related to classic greedy algorithms Example 1: Split Candy (Easy) (Sort, Piggy) Example 2: Shake sequence (Medium) (greed) Example 3: Remove K Numen (MEDIUM) (Stack, Piggy) Exam...

[Algorithm experiment project] Backpack problem (c ++)

Given n items and a backpack with capacity C, the weight of item i is wi and its value is vi. The problem with backpacks is how to choose the items that are loaded into the backpack so that the total ...

More Recommendation

[C ++] Algorithm Collection (9): Backpack Problem

Article catalog 0-1 backpack problem Dynamic planning standard routine Fake code Repair code Subset backpack problem Ideological analysis Code Complete backpack problem Originally, I would like to tak...

Piggy Algorithm - Simple Backpack Problem (C ++)

Simple backpack problem - greed When the item can be divided, the value of the item loaded in a backpack that is fixed to the M is required to be maximized. Problem Description: There arenItems, each ...

Algorithm notes (C ++) - complete backpack problem

Algorithm notes (C ++) - complete backpack and multiple backpack issues Complete backpack Complete backpack is different from the 01 backpack - the amount of things inside the full backpack is infinit...

C ++ dynamic planning algorithm -01 backpack problem

The so-called dynamic planning is only the optimization of violent search. When you write violent search, dynamic planning is not far away....

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top