Greedy algorithm one: the optimal loading problem

Source: Internet
Author: User

1. Basic ideas:

Greedy algorithm is a series of choices to get the solution of the problem, it makes the choice is the best choice in the current situation, that is, the greedy algorithm does not consider the overall optimal, but consider the current situation of the local optimal, that is greedy choice.

2. Two properties of the greedy algorithm:

1) Greedy Choice nature: the overall optimal solution of the problem can be solved by a series of local optimal selection, that is, greedy choice to achieve. Greedy choices depend on the choices that have been made before, and have no relation to choices made in the future.

2) Optimal substructure properties: The optimal solution of a problem contains the optimal solution of its sub-problem.

3. The difference between greedy algorithm and dynamic programming:

The dynamic programming is to solve the sub-problem by the bottom-up method, and the greedy algorithm makes the greedy choice by the top-down iterative way, and solves the problem's optimal solution. The two things in common are the optimal substructure properties.

4. Optimal loading problem: Adopt the greedy selection strategy which is loaded first by the lightest weight.

  

1#include"stdafx.h"  2#include <iostream>3 using namespacestd;4 Const intN =4;5Template <classType>6 voidSwap (Type &x, type &y) {7Type TEMP =x;8x =y;9y =temp;Ten } One voidBublesort (intW[],intT[],intN) { A      for(inti =1; I <= N; i++) -     { -T[i] =i; the     } -  -      for(inti =1; I < n; i++) -     { +         inttemp =i; -          for(intJ =i+1; J <= N; J + +){ +             if(w[temp]>W[j]) A             { attemp =J; - Swap (W[i], w[j]); -             }     -         } - Swap (T[i], t[temp]); -     } in } - voidBestload (intW[],intX[],intCintN) { to      +     intT[n +1] = {0};//Record Original Index - Bublesort (W, T, N); the      for(inti =1; I <= N; i++) *     { $X[i] =0;Panax Notoginseng     } -      for(inti =1; I <= n&& W[t[i]] <= C; i++) the     { +X[t[i]] =1; AC-=W[t[i]]; the     } + } - intMain () { $     intc = -; $     intW[] = {0, -,Ten, -, the }; -     intX[n +1]; -cout <<"load capacity: \ n"<< C <<Endl; thecout <<"the weights of the items are:"<<Endl; -      for(inti =1; I <= N; i++)Wuyi     { thecout << W[i] <<" "; -     } Wucout <<Endl; - Bestload (W,x, C, N); Aboutcout <<"The greedy choice results are:"<<Endl; $      for(inti =1; I <= N; i++) -     { -cout << X[i] <<" "; -     } Acout <<Endl; +}

  

Greedy algorithm one: the optimal loading problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.