Algorithm Design and Analysis-Chapter 3 Recursion and Divide and Conquer

The design idea of ​​the divide-and-conquer method
divides a big problem that is difficult to solve directly into some smaller-scale sub-problems, so that each can be broken down and divided and ruled. More generally, the original problem to be solved is divided into k smaller-scale sub-problems, and these k sub-problems are solved separately. If the scale of the sub-problem is still not small enough, divide each sub-problem into k smaller sub-problems and decompose in this way until the scale of the problem is small enough to easily find its solution, and then solve the sub-problem Combined into a solution to a larger problem, the solution to the original problem is gradually found from the bottom up.
Heuristic rules:
1. Balance sub-problems : it is best to make the sub-problems about the same size. That is to divide a problem into k sub-problems of equal size (usually k = 2). This approach of making sub-problems roughly equal is derived from the idea of ​​a balancing (sub-problem), which is almost always better than sub-problems. The practice of unequal scale is better.
2. Independent sub-problems : Each sub-problem is independent of each other, which involves the efficiency of the divide-and-conquer method. If each sub-problem is not independent, the divide-and-conquer method needs to repeatedly solve common sub-problems.
The solution process of divide and conquer

  1. Division : Since it is a divide-and-conquer, of course, the original problem of size n needs to be divided into k smaller sub-problems, and try to make these k sub-problems roughly the same size.
  2. Solving sub-problems : The solution of each sub-problem is usually the same as that of the original problem. Recursive methods can be used to solve each sub-problem, and sometimes recursive processing can also be achieved with a loop.
  3. Merger : The solutions of the various sub-problems are merged. The cost of the merger varies greatly depending on the situation. The effectiveness of the divide-and-conquer algorithm depends largely on the implementation of the merger.

Not all divide and conquer methods are more effective than simple brute force methods.

Recursive
recursive There are two basic elements :
boundary conditions : recursion to determine when to terminate;
recursive mode : The big question is how to break into a small problem.

The maximum sub-segment and problem are
given a sequence of n integers (a1, a2,…, an). The maximum sub-segment and problem find the maximum value of the sequence. When all integers in the sequence are negative integers, the maximum sub The segment sum is 0. For example, the sum of the largest subsegments of the sequence (-20, 11, -4, 13, -5, -2) is: 20

int MaxSum(int a[ ], int left, int right)    
{        
	sum=0;        
	if (left= =right) 
	{ 
		if (a[left]>0) sum=a[left];            
		else sum=0;        
	}
	else 
	{           
		center=(left+right)/2;
		leftsum=MaxSum(a, left, center);                                                          
		rightsum=MaxSum(a, center+1, right);
		s1=0; 
		lefts=0;
		for (i=center; i>=left; i--)         
		{             
			lefts+=a[i];             
			if (lefts>s1) s1=lefts;         
		}
		s2=0; 
		rights=0;  
		for (j=center+1; j<=right; j++)         
		{              
			rights+=a[j];             
			if (rights>s2) s2=rights;         
		}
		sum=s1+s2;
		if (sum<leftsum) sum=leftsum; 
		if (sum<rightsum) sum=rightsum;      
	}
	return sum; 
}                                                

The time complexity of the algorithm is O (nlog2n)

Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/105500426