First Lesson Notes
Lecture: Shen Xiaojun
Record: Sun Xiang
Time:2017/05/06
Sorting algorithm comparison Divide and conquer 1 Principle of Divide and Conquer 2 Examples Example 1 Binary Search Example 2 Find Max and Min The 23rd question dominating number of the problem of mapping work
complexity analysis; Sorting and the lower bound 1. Comparison of sorting algorithms:
2. Divide and Conquer 2.1 Principle of Divide and conquer
When the input size of a problem is small, one or both numbers for example and then this problem can easily solved. However, when the input size was large, many problems become difficult to solve. Therefore, a basic methodology is-find the relationship between the solution for a large sized input and the solutions For small sized inputs. Divide and conquer is a particular approach the follows this methodology. We'll see both more approaches later, namely, the greedy approach and the dynamic programming which is also following th is methodology but differ in the ways of implementation.
Briefly speaking, Divide and conquer method follows the following steps:
Divide the problem with a large input size to a number of subproblems that is smaller instances of the same problem.
Conquer the subproblems by solving them recursively. If the size of a subproblem is small enough, then solve it in a straightforward manner which are called "bottom out."
Combine the solutions to the subproblems into the solution for the original problem. 2.2 Examples Example 1:binary Search
Suppose we have a sorted array of n n numbers, A[1]⩽a[2]⩽⋯⩽a[n] a[1] \leqslant a[2] \leqslant \cdots \leqslant a[n]. Now, we need to design an algorithm that searches the array to see if this array contains a particular number x x. If a[i]=x A[i] = x, then report index i I, otherwise report nil nil. The following algorithm called binary search uses the divide and conquer approach . Note that a smaller subproblem corresponds to a segment of the array a A, denoted by a[p],a[p+1],⋯,a[r] a[p], a[p+1], \CDO TS, A[r], where 1⩽p⩽r⩽n 1 \leqslant p \leqslant r \leqslant N. This notation allows us to represent any subproblem. When, p=1,r=n p = 1, R = N, this sequence represents the original problem.
Import Math
def binarysearch (a,p,r,x):
if p > r:
return None
midpoint = Math.floor ((p+r)/2)
if A [Midpoint] = = x:
return midpoint
elif x < A[midpoint]:
return BinarySearch (a,p,midpoint-1,x)
else:
return BinarySearch (a,midpoint+1,r,x)
a=[1,2,3,5,6,7]
print (BinarySearch (a,0,5,2))
Example 2:find Max and Min
Given n numbers stored in a[1⋯n] a[1\cdots n], we wish to design A Divide-and-conquer algorithm that finds both the Maximu M number and the minimum number in this array.
Solution:
We Design a procedure that finds the maximum and minimum numbers in the range of A[p, R].
Import Math
def max_min (a,p,r):
if p = = r:
Max = a[p]
Min = a[p]
return max,min
if p = = r-1:
M AX = max (A[p],a[r])
Min = min (a[p],a[r])
return max,min
q = Math.floor ((p+r)/2)
Max1, Min1 = Max_min (A, P,Q)
max2,min2 = Max_min (a,p+1,r)
max = max (max1,max2)
min = min (min1,min2)
return max,min
A =[1,3,4,5,6,7,8]
print (Max_min (a,0,6))
by calling Max-min (a[1. N], Max, Min) , we'll get the maximum and minimum numbers in a[1. N].
The complexity follows the recurrence relation T (n) =t (⌊n/2⌋) +t (⌈n/2⌉) +2 t (n) = t (\lfloor n/2\rfloor) + t (\lceil N/2\rceil) + 2.
We can prove by induction, N, t (n) ⩽2n–2 t (n) \leqslant 2n–2.
Basis. When n=1 n = 1 or n=2 n = 2, T (n) ⩽2n–