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

第一次课笔记

lecture:沈孝钧

record:孙相国

time: 2017/05/06

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

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 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[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 math


def 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,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 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)2n2 is 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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值