I. iteration and recurrence
1) iterative method, also known as the "trigger method", is a method to continuously use the old value of the variable to recursive the new value to solve the problem. Iterative algorithms are generally used for numerical calculation. Iterations should be an algorithm strategy that we are already familiar with. The accumulation and multiplication learned in the programming language course are the basic applications of iterative algorithm policies. Example: Fibonacci Series
Example: Rabbit Breeding
From the third month after birth, a rabbit gave birth to a rabbit every month. The Bunny began to give birth to the next generation of bunny in the third month. If the rabbit is not dead, I will hold a new bunny in February and ask how many rabbits each month have in a year.
• Problem Analysis
Because a rabbit has produced a rabbit every month since the third month after birth, the number of new rabbits in each month is obviously determined by the number of rabbits in the previous two months. The reproduction process is as follows:
From January 1, to January 1 ......
1 1 1 + 1 = 2 2 + 1 = 3 3 + 2 = 5 5 + 3 = 8 ......
• Mathematical modeling (Fibonacci series)
Y1 = Y2 = 1, yn = yn-1 + yn-2, n = 3, 4, 5 ,......
2) concept of Reverse Lookup
• Reverse lookup: it is a method used to solve certain special problems in violation of common habits. For example, if you do not know the prerequisite, the result is used to reverse the process to solve the problem. Another example is that due to storage requirements, it must be calculated from the forward. In addition, when analyzing some problems or establishing a mathematical model, it is difficult to analyze the problem from the past to the back. The problem is easy to understand and solve by using the reverse method.
Example: desert crossing
Use a jeep to cross the 1000 km desert. The total assembly volume of the jeep is 500 gallons and the fuel consumption is 1 gallon/km. Because there is no oil depot in the desert, you must first build a temporary oil depot in the desert. The jeep uses the least fuel consumption to penetrate the desert, where an oil depot should be built, and the amount of oil stored everywhere.
• Problem Analysis
The oil storage point requires that the desert be crossed with the least fuel consumption, that is, when the destination is reached, the amount of fuel for each temporary Oil Depot and vehicle in the desert is 0. In this way, the oil storage point and volume can only be pushed forward from the end point.
• Mathematical Model
Based on the analysis of the minimum fuel consumption target, we will discuss it from the forward section below.
The first section is 500 kilometers in length and the first fuel storage is 500 gallons.
In the second paragraph, in order to reserve oil, the Jeep must have a round trip during this trip.
The following describes how to achieve high efficiency:
1) First, this section should take an odd number (ensure that the last step forward ).
2) The jeep is fully loaded every time you move forward.
3) You must be able to store enough oil at the next fuel point, with the least fuel consumption on the road.
• Comprehensive Analysis
Separate intervals from the end point
500,500/3,500/5,500/7 ,...... (Km) oil storage points are established until the total distance exceeds 1000 km. The fuel volume of each storage point is 1500 ,.......
• Ultimate explanation:
1) from the end to the end, there must be no oil, and the gas station is also consumed in the middle. So there is a 500-gallon oil storage point 500 km from the end.
2) second, it takes at least three times for the first oil storage point to store 500 gallons, and the car needs to be fully loaded with oil, the distance is 500*2-3x = 500. The distance x = 500/3 fuel storage: 500*2 = 1000.
3) Third, distance calculation formula: 500*3-5X = 1000 --> X = 500/7 fuel storage: 3*500 = 1500
Ii. Divide and Recursion
1) There are three steps in recursion of each layer:
(1) Division: divide the input problem instances into several subproblems, and try to make the subproblems roughly the same scale.
(2) solution: If the subproblem is small and easy to be solved, it is directly solved. Otherwise, when the scale of the subproblem is larger than a predefined threshold value, the solution consists of recursive calls.
(3) Merge: Merge the solutions of each subproblem into the solutions of the original problem.
The relationship between sub-governance and Recursion
Grouping and Recursion are like twins, which are often applied in algorithm design at the same time. It can be seen from the algorithm framework of the divide and conquer method that the algorithm designed by the divide and conquer method is generally a recursive process.
2) Basic Idea of binary governance
In algorithm design, the number of subproblems that each problem is divided into is generally fixed, and the scale of each subproblem is also evenly allocated. Every time the problem is divided into half of the original problem scale, it is called the binary method. The bipartite method is a commonly used decomposition strategy. algorithms such as semi-query and Merge Sorting in the Data Structure course are implemented using this strategy.
Example: obtain the maximum and minimum values by using the bipartite Method
# Include <iostream> <br/> using namespace STD; <br/> void findmaxandmin (int A [], int begin, int end, int * Pmax, int * pmin) <br/>{< br/> If (end-begin <= 1) // recursive exit <br/>{< br/> if (a [begin] <= A [end]) <br/>{< br/> * Pmax = A [end]; <br/> * pmin = A [begin]; <br/> return; <br/>}< br/> else <br/> {<br/> * pmin = A [end]; <br/> * Pmax = A [begin]; <br/> return; <br/>}< br/> int min1, min2, max1, max2; <br/> int mid = (Begin + end)/2; <br/> findmaxandmin (A, begin, mid, & max1, & min1); <br/> findmaxandmin (A, Mid + 1, end, & max2, & min2); <br/> * pmin = min1 <min2? Min1: min2; <br/> * Pmax = max1 <max2? Max2: max1; <br/>}< br/> int main () <br/>{< br/> int A [] =, 12, 20, 98 }; <br/> int Max, min; <br/> findmaxandmin (A, & MAX, & min ); <br/> cout <"The Max num is:" <max <", the min num is:" <min <Endl; <br/> return 0; <br/>}</P> <p>Example: Linear Time Array, K small element
•
Problem Analysis
This problem can be solved through sorting. The complexity of the best sorting algorithm is O (n * log (N). Next we will use the division and control method to find the complexity as O (n). However, this problem cannot be simply divided into two subproblems: completely independent and similar. Because the first group's k-small data and the second group's k-small data after the decomposition are selected, it cannot be guaranteed that one of the two data is the solution of the original problem.
•
For example:
Calculate 2nd Small data in a group of data: (-,-,-5,-3)
If the data in the instance is divided into two groups (-,-4), (13,-5,-3) by using the bipartite method, the first subproblem is-2, the solution to the second subproblem is-3. The solution of the two subproblems cannot be a simple solution to the original problem. The solution to the original problem is-4.
•
Solution
Solution 1: Create a New K array, sort the K array, and compare the maximum number with each number in the remaining array each time. If the remaining number is small, the maximum number in the K array is deleted, then insert the remaining number into the K array and traverse it to the end. The maximum number in the K array is required.
Solution 2: Divide and conquer algorithm (******)
# Include <iostream> <br/> using namespace STD; </P> <p> class quicksort <br/>{< br/> PRIVATE: <br/> int * arr; // array to be sorted <br/> int length; // array length <br/> public: <br/> // constructor <br/> quicksort (INT length) <br/>{< br/> arr = new int [Length + 1]; <br/> This-> length = length; <br/>}< br/> // exchange function <br/> void swap (int I, Int J) <br/>{< br/> int temp = arr [I]; <br/> arr [I] = arr [J]; <br/> arr [J] = temp; <br/>}< br/> // input data <br/> void input () <br />{< Br/> int I = 1; <br/> cout <"Please input 10 number:" <Endl; <br/> while (I <= length) <br/>{< br/> CIN> arr [I]; <br/> ++ I; <br/>}< br/> // output function <br/> void display () <br/>{< br/> int I = 1; <br/> while (I <= length) <br/> {<br/> cout <arr [I] <""; <br/> + I; <br/>}< br/> cout <Endl; <br/>}< br/> // randomization partitioning function <br/> int randomizedpartition (INT start, int end) <br/>{< br/> int I = start + rand () % (end-start + 1); // generate a random start ~ End <br/> swap (I, start); // exchange element <br/> return partition (START, end ); // return the Division position <br/>}</P> <p> // division function <br/> int partition (INT start, int end) <br/> {<br/> int key = arr [start]; // arr [start] As the partitioning keyword <br/> int I = start; <br/> Int J = end; <br/> // exchange element <br/> while (true) <br/>{< br/> while (ARR [I] <key) <br/>{< br/> ++ I; <br/>}< br/> while (ARR [J]> key) <br/>{< br/> -- J; <br/>}< br/> // find two elements that can be exchanged <br/> // If j <= I, it indicates that arr [J] is in the left half domain, arr [I] is in the right half of a domain. Division is completed between J and J + 1. <br/> if (I <j) <br/>{< br/> swap (I, j); <br/> ++ I; <br/> -- J; <br/>}< br/> else <br/>{< br/> return J; <br/>}< br/> // quick sorting <br/> void quicksort (INT start, int end) <br/>{< br/> If (start <End) // at least two elements <br/>{< br/> int middle; <br/> middle = randomizedpartition (START, end); <br/> quicksort (START, middle); <br/> quicksort (middle + 1, end ); <br/>}< br/> // select the k-th small element in linear time. <br/> int randomizedselect (INT start, int end, int K) <br/> {<br/> If (START = END) // If the segments of two elements are randomly divided, the formula is true, the k-th small element is found <br/>{< br/> return arr [start]; <br/>}< br/> else <br/>{< br/> int middle = randomizedpartition (START, end ); <br/> int left = middle-start + 1; <br/> If (k <= left) // <br/>{< br/> return randomizedselect (start, middle, k ); // search for the smallest K element on the left side of the partition <br/>}< br/> else <br/>{< br/> return randomizedselect (middle + 1, end, k-left ); // search for elements with a small length in the K-left segment on the right after Division <br/>}< br/> }; </P> <p> int main () <br/>{< br/> quicksort test (10); <br/> test. input (); <br/> test. display (); <br/> cout <test. randomizedselect (3rd, 3) <Endl; // search for small elements <br/> test. quicksort (); // randomize quick sorting <br/> test. display (); // print the result </P> <p> return 0; <br/>}