The maximum value of the subsequence of a sequence, which is a classical algorithm, is collated here.
Questions: Give a sequence of integers, such as {-2, 5, 7, 11,-4, 13,-5,-2,-6, 3,-1, 3}, to find the maximum number of consecutive sub-sequences in this sequence, the maximum of which is 32, and the subsequence is {5, 7, 11, 4, 13}.
method One : The simplest act of violence. Establish a starting point, an end point, and calculate the starting point to the end. In the code below, int brute_force (int* a,int N) implements the algorithm, and the time complexity is determined by the
The calculation can be O (n^3).
method Two : To optimize the brute force method, use the additive method to determine a starting point, and then add the sum of the preceding sequence to the end point to select a maximum value. The following code, int Improved_brute_force (int* A, int n), implements the algorithm. The complexity of time is determined by
Available as O (n^2).
method Three : Using the idea of dividing the method, from the middle point sequence is divided into two sub-sequences, the entire sequence of the maximum sub-sequence position in three cases (see figure, this figure from the teacher ppt)
1. In the left sub-sequence
2. In the right sub-sequence
3. Contains the middle point, each of the two sequences has a part.
The following int divide_conquer (int* a,int start,int End) method implements the algorithm, the recursive implementation of the recursive type T (n) =2t (N/2) +cn. The time complexity of O (NLOGN) is available from Master theorem.
method four : Using the idea of dynamic planning, also known as online algorithm, the definition of online learning algorithm: The line algorithm is a unique form of processing input data, the algorithm does not need all the data at the beginning of the preparation, but the gradual input of the data processing, and after the input is completed output operation results. The opposite is the offline algorithm, assuming that all the data at the beginning of the algorithm is ready, choose the sort is offline algorithm, insert sort is an online algorithm. The idea of processing maximal subsequence length with dynamic programming is to save the sum of the maximal subsequence with the variable max_value, save the sum from i=0 with Current_value, if the current_value is greater than max_value, save, otherwise give up, If current is reduced to zero, the Current_value is restarted from 0 because the sum of the subsequence is reduced only by the addition of Current_value. The following int dynamic_prom (int* a,int len) implements the algorithm. The algorithm has a time complexity of O (n)
#include <stdio.h> #include <stdlib.h> #include <limits.h> #define Len_array 12/* for maximum subsequence and *///brute Force: Time Complexity N^3int Brute_force (int* a,int n) {int I,j,k;int max_value = int_min;int current_value;for (i=0;i<n;i++) {// Take I as the starting point for (j=i;j<n;j++) {//with J for end current_value = 0;for (k=i;k<=j;k++) {//for subsequence from I to J and current_value + = A[k];} if (Current_value > Max_value) {max_value = Current_value;}}} return max_value;} Increased brute force, time complexity N^2int Improved_brute_force (int* A, int n) {int I,j;int max_value = int_min;int Current_value = 0;fo R (i=0;i<n;i++) {//with I as the starting point current_value = 0;//Set the initial value of the subsequence with I as the starting point is 0for (j=i;j<n;j++) {//with J as the end point Current_value + = A[j];if ( Current_value>max_value) {max_value = Current_value;}}} return max_value;} Divide-and-conquer method, Time complexity Nlog (n)//from the middle point sequence is divided into two sub-sequences, the entire sequence of the maximum sub-sequence position in three cases//1.//2 in the left subsequence.//3 in the right sub-sequence. Contains intermediate points, and two sequences have part int Divide_conquer (int* a,int Start,int end) {int Mid;int Mid_to_left_max = 0;int Mid_to_right_max = 0;int i,current_left=0,current_right=0;int Side_max,mid_max,max , Left_max,rigHt_max;if (start==end) return a[start];mid = (start+end)/2;left_max = Divide_conquer (A,start,mid);//Find the maximum value of the left sub-sequence Right_ max = Divide_conquer (a,mid+1,end);//Find the maximum value for the right sub-sequence for (i=mid;i>=0;i--) {//Look for the middle point as the starting point, left to extend the largest sub-sequence Current_left + = A[i]; if (Current_left>mid_to_left_max) {Mid_to_left_max = Current_left;}} for (i=mid;i<=end;i++) {//Looking for the middle point as the starting point, the largest sub-sequence extending to the right current_right + = A[i];if (Current_right>mid_to_right_max) {mid_ To_right_max = Current_right;}} Mid_max = Mid_to_left_max+mid_to_right_max-a[mid];side_max = Left_max>right_max?left_max:right_max;max = Side_max >mid_max?side_max:mid_max;return Max;} Also known as online algorithm, the definition of an on-line learning algorithm: The algorithm is a unique form of processing input data, the beginning of the algorithm//do not need all the data are ready, but the gradual input of the data processing, and after the input is completed output operation results. In contrast to the//offline algorithm, assuming that all data at the beginning of the algorithm is ready, select sort is off-line algorithm, insert sort is online algorithm//with dynamic programming to deal with the maximum subsequence length of the idea for the use of variable Max_value to save the maximum number of sub-sequences, with Current_ Value is saved from i=0 cumulative//And if Current_value is greater than max_value is saved, otherwise discarded, if current is reduced to zero, then Current_value is restarted from 0//because again Current_ Value will only reduce the sum of the subsequence and int dynamic_prom (int* a,int len) {int current_value = 0;int Max_value = Int_miN;int i;for (i=0;i<len;i++) {current_value + = A[i];if (current_value>max_value) {//current value is greater than maximum value, update max_value = present _value;} if (current_value<0) {//current value is less than 0, starting from 0 current_value = 0;}} return max_value;} int main () {int a[12] = {-2, 5, 7, one, -4,, -5,-2,-6, 3,-1, 3};p rintf ("Brute force:%d\n", Brute_force (A,len_array));p RINTF ("Improved Brute force:%d\n", Improved_brute_force (A,len_array));p rintf ("Divide Conquer:%d\n", Divide_conquer ( a,0,len_array-1));p rintf ("Dynamic Programming:%d\n", Dynamic_prom (A,len_array));p rintf ("\ n"); return exit_success;}
Maximum subsequence and collation, complexity from O (n^3) to O (n)