高级算法日记2:第1次课笔记

来源:互联网 发布:软件验收报告模板 编辑:程序博客网 时间:2024/06/08 14:53

第一次课笔记

lecture:沈孝钧

record:孙相国

time: 2017/05/06

    • 排序算法比较
    • Divide and Conquer
      • 1 Principle of Divide and Conquer
      • 2 Examples
        • Example 1 Binary Search
        • Example 2 Find max and min
        • 摸底作业题第23题dominating number

复杂度分析;分治法;排序及下界

1.排序算法比较:

这里写图片描述

2. Divide and Conquer

2.1 Principle of Divide and Conquer

When the input size of a problem is small, one or two numbers for example, then this problem can be easily solved. However, when the input size is large, many problems become difficult to solve. Therefore, a basic methodology is to 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 that follows this methodology. We will see two more approaches later, namely, the greedy approach and the dynamic programming which are also following this methodology but differ in the ways of implementation.
Briefly speaking, Divide and Conquer method follows the following steps:

  1. Divide the problem with a large input size into a number of subproblems that are smaller instances of the same problem.

  2. Conquer the subproblems by solving them recursively. If the size of a subproblem is small enough, then solve it in a straightforward manner which is called “bottom out.”

  3. Combine the solutions to the subproblems into the solution for the original problem.

2.2 Examples

Suppose we have a sorted array of n numbers, A[1]A[2]A[n]. Now, we need to design an algorithm that searches the array to see if this array contains a particular number x. If A[i]=x, then report index i, otherwise report 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, denoted by A[p],A[p+1],,A[r], where 1prn. This notation allows us to represent any subproblem. When, p=1,r=n, this sequence represents the original problem.

import mathdef 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[1n], we wish to design a divide-and-conquer algorithm that finds both the maximum 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 mathdef Max_Min(A,p,r):    if p == r:        Max = A[p]        Min = A[p]        return Max,Min    if p == r-1:        Max = 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,MinA=[1,3,4,5,6,7,8]print(Max_Min(A,0,6))

By calling Max-Min (A[1.. n], Max, Min) , we will 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.

We can prove by induction that for any n,T(n)2n2.

Basis. When n=1 or n=2, T(n)2n2is obviously true.
Induction step. When n>2, we have
T(n)=T(n/2)+T(n/2)+2
By induction,

T(n)(2n/22)+(2n/22)+2=2(n/2+n/2)4+2=2n2.

Obviously, in the worst case, T(n)=2n2.

另一种方法:

每相邻两个为一组,得到每一组的max,和min,再和上一次得到的max,min做比较,一共要做3次比较。

T(n)=3×n22

摸底作业题第2/3题:dominating number

巴基斯坦老兄的解法(Boyer-Moore Algorithm)

用stack数据结构,算法思想同第3题

证明:因为配对一共n/2,这意味着,任何配对的数字都不可能是dominating number.只有剩下的数字才是dominating number

note:

another version Boyer-Moore Algorithm is used for string matching

原创粉丝点击