Recursive : Divide and Conquer(meta-algo II)

本文深入探讨了递归算法的设计与应用,通过实例讲解如何利用递归解决复杂问题,并对比了几种不同递归排序算法的时间复杂度。同时,文章还讨论了如何验证二叉搜索树的正确性。
  • Assume you have an algorithm that works.
  • Use it to write an algorithm that works.
  • till use brute force to get into the smallest problem(base case).

 meta-recursive algorithm

def recursiveAlgo(a,b,c){
//pre-cond: a,b,c
//post-cond: x,y,z

    if((a,b,c) is sufficently small instance) return (0,0,0)
    else{
        (asub1,bsub1,csub1) =a smaller part of (a,b,c)
        (xsub1,ysub1,zsub1) = recursiveAlgo(asub1,bsub1,csub1)

        (asub2,bsub2,csub2) =a diff smaller part of (a,b,c)
        (xsub2,ysub2,zsub2) = recursiveAlgo(asub2,bsub2,csub2)

        (x,y,z) = combine((xsub1,ysub1,zsub1),(xsub2,ysub2,zsub2))

        return (x,y,z)
    }

}

 

Trust your friends to solve sub-instances.

The sub-instance must be smaller and the same problem.

Combine solution given by friend to construct your own solution for your instance.

Focus on only one step.

Do not trace out the entire computation.

Consider your input instance:

Remember that you know nothing other than what is given to you by the precondition.

Be sure each path in your code returns what is required by the postcondition.

Do not worry about who your boss is OR how your friends solve their instance.

No global variable or effects.

From you instance, construct one or more subinstances.

Prove that the subinstances that you give to your friends are in some way "smaller" than your instance.

And you instance has a finite size.

Assume by magic (strong induction) your friends give you a correct solution for each of these.

If your instance is sufficiently small, sovle it yourself as a base case.

Your solution for the base case meets the postcondition.

T(n) = aT(n/b) + f(n)

evaluating: T(n) = a T(n/b) + f(n)

Level Instance Sizework in stack frame#stack frameswork in Level
0.nf(n)11 * f(n)
1....n/bf(n/b)aa * f(n/b)
2 n/b2f(n/b2)a2a2 * f(n/b2)
      
i................n/bif(n/bi)ai 
      
h=logn/logb.................................n/bh=1T(1)ah=alogbn=nlogbanlogba * T(1)

 Total work: T(n) = Σi=0..h ai * f(n/bi)

 

ProsCons
Worry about one step at a timeStudent resist it.

An abstraction within which to develop, think about,and describe algorithms

in such way that their correctness is transparent.

expect too much from their friends.
expect too much from their boss.

 

T(1)=1

T(2)=2

T(n)=T(n-1)+T(n-2)+3

//TowersOfHanoi
towersOfHanoi(n,source,dest,spare){
//pre-cond: the n smallest disks are on pole source
//post-cond: they are moved to pole dest

if(n==1)
move the single disk from source to dest

else{
towersOfHanoi(n-1,source,spare,dest);
towersOfHanoi(1,source,dest,spare);
towersOfHanoi(n-1,spare,dest,source);
}

//T(n)=2T(n-1)+1
//exponential
}

Four Recursive sorts

 Size of sublists
 n/2,n/2n-1,1

Minimal effort splitting

Lots of effort recombining

merge sortinsertion sort

Lots of effort splitting

Minimal effort recombining

Quick sortselection sort

  

Quick Sort

Best Time: T(n)=T(n/2)+T(n/2)+O(n):O(nlogn)

Worst Time: T(n)=T(0)+T(n-1)+O(n):O(n2)

Expected Time: T(n)=T(n/3)+T(2n/3)+O(n):O(nlogn)

 

Kth element problem

partition set into two using randomly chosen pivot, left OR right?

Best Time: T(n)=T(n/2)+O(n):O(n)

Worse Time:T(n)=T(n-1)+O(n):O(n2)

Expected Time: T(n)=T(2n/3)+O(n):O(n)

 

T(n)=T(n/2)+1:O(logN)


Recursion on Trees

a binary tree:

  • the empty tree
  • a node with a right and a left sub-tree

Most programmers don't use empty tree as a base case, This causes a lot more work.

Empty tree is cool as zero or -∞, to provide a base state.

3+4+8+2+...

              | Sum so far is 17

3+4+8+2+...

| Sum so far is 0

NewSum=OldSum+nextValue

            = 0        +3

            =3

 

 3*4*2*3...

           |Product so far is 72

3*4*2*3...

|Product so far is 1

NewProd=OldProd*newValue 

            =1 * 3

True and True and False...

               | So far is True

|So far is True 

 

new = Old and next

       =True and True

       =True 

 max(3,4,2...)

              | max so far is 4

       |max so far is -∞           

max(tree)=max(left,right)+1 
  

 

Time: T(n)=2T(n/2)+O(n)=O(nlogn)

                =2T(n/2)+O(n1)=O(nlognn)

 

def isBSTTree(tree,[min,max]):

"""pre-cond: tree is a binary tree. In addition,[min,max] is a range of values.
"""post-cond: the output indicates whether it is a binary search tree with values within this range.

    if(tree=emptyTree):
        return YES;
    elseif(rootkey(tree) in [min,max] and isBSTTree(leftSubTree(tree,[min,rootkey(tree)]) and isBSTTree(rightSubTree(tree),[rootkey(tree),max])):
        return YES;
    else:
        return NO;

 

转载于:https://www.cnblogs.com/grep/archive/2012/02/24/2367240.html

标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值