04 --- greedy algorithm algorithm notes

~ 5min learning algorithm public reference number

Outline

       When problem solving, always made in the current appears to be the best choice. In other words, not be considered as a whole the best, he made only a partial sense of the optimal solution.

  Greedy algorithm is not able to get the best overall solution to all problems, select the greedy strategy must have no after-effect, that is, after a certain state of the process will not affect the previous state, only with the current status.

       Applicable premise: local optimization strategy can lead to global optimal solution

  As one of the five greedy algorithm algorithm used in a wide range of data structures.

  For example: In the Prim algorithm minimum spanning tree, the selected vertex is the minimum weight of the candidate endpoint side edges.

  In Kruskal's algorithm, each selection is added with minimum weight set. During the construction of the Huffman tree is a binary tree structure each node selected minimum weight value. Such a time when performing sub-problem solving, always choose the best of the current situation, exactly in line with the meaning of greed.

 

Problem-solving ideas

  (1) a mathematical model to describe the problem.
  (2) solving the problem into several sub-problems.
  (3) For each sub-problem solving, get local optimal solutions to subproblems.
  (4) a partial solution of the sub-optimal synthesis problem the original solution of the problem.

1  // / pseudocode 
2  
. 3  initial solutions from an issue
 . 4      the while (Jōchō to the former can further overall objective) 
 . 5          do 
. 6              selects the current optimum solution as a feasible solution element;
 7      combination of all elements solution a feasible solution to the problem.

 

Scenarios

 problem:

  Xiao Ming hands denomination banknotes 1,5,10,50,100 five kinds, each corresponding to the number of bills were 5,2,2,3,5 sheets. If the need to pay 456 yuan Xiao Ming, how many bills you need?

Problem decomposition:

(1) establishing a mathematical model
  based Xiaoming each selection banknotes in denominations of Xi, the number of banknotes needed for the n sheets, the remaining amount to be paid is V, there are: X1 + X2 + ... + Xn = 456. The
(2) issue demolition is divided into sub-problems
  Xiaoming selection banknote payment process can be divided into n sub-problems: i.e., corresponding to each sub-question is: not more than 456 in the premise, select one of the remaining banknotes in the banknote.

(3) the development of greedy strategy, solving the problem of child

       Developed greedy strategy: Choose the largest denomination banknotes under conditions permit. Solving the entire process is as follows:

  • Select the nominal value of the bill 100, then X1 = 100, V = 456 - 100 = 356;

  • Continue selecting denominations of the banknotes 100, the X2 = 100, V = 356 - 100 = 256;

  • Continue selecting denominations of the banknotes 100, the X3 = 100, V = 256 - 100 = 156;

  • Continue selecting denominations of the banknotes 100, the X4 = 100, V = 156 - 100 = 56;

  • Select the face value of the banknote 50, then X5 = 50, V = 56 - 50 = 6;

  • 5 Select the nominal value of the banknote, then X6 = 5, V = 6 - 5 = 1;

  • Select a face value of paper money, the X7 = 1, V = 1 - 1 = 0; end Solution

(4) All elements of the combined solution to the original problem solution

       Xiaoming number of bills to pay for 7, wherein the face value of $ 100 4, 50 1, 5 per 1, 1 yuan 1.

 

 The sample code

. 1  const  int N =. 5 ; 
 2  int the Count [N] = {5,2,2,3,5};           // number of banknotes in each 
. 3  int the Value [N] = {} 1,5,10,50,100 ;    // corresponding denomination are arranged 
. 4  
. 5  int Solve ( int Money) {
 . 6      int NUM = 0 ;
 . 7      for ( int I = N -. 1; I> = 0; i-- ) {// start with the largest denomination matches
 8          int C = Math.min (Money / the Value [I], the Count [I]);   // for each denomination corresponding to the maximum sheet number may be selected 
. 9          Money Money = - C * the Value [I];
 10         = C + NUM;                                         // selected the number of updates 
. 11      }
 12 is      IF (Money> 0) NUM = -1 ; // if there is not the money exchange is completed, the error
 13 is      return NUM;
 14 }

 

Guess you like

Origin www.cnblogs.com/clarino/p/11965090.html
Recommended