递归和搜索算法

Algorithm

  • Recursion/递归
  • Search/搜索

Recursion

  • A function that call itself!
  • Solve a problem where the solution depends on solutions to smaller instances of the same problem.
  • Can generally be solved by iteration.

Factorial Funvtion

在这里插入图片描述

递归

def fact(n):
    if n != 0:
        return n * fact(n-1)
    else:
        return 1
fact(3)
6

注意: 使用递归函数需要防止栈溢出。 在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,就会导致栈溢出。
**解决办法:**通过尾递归优化
**尾递归:**在函数返回的时候,调用自身本身,并且,return语句不能包含表达式。这样,编译器或者解释器就可以把尾递归做优化,使递归本身无论调用多少次,都只会占用一个栈帧,不会出现栈溢出的情况。如下:

def fact(n,acc=1):
    if n==0:
        return acc
    return fact(n-1,n*acc)

问题是python标准的解释器没有针对尾递归做优化,任何递归函数都存在栈溢出的问题

循环

n=3
mul = 1
for i in range(1,n+1):
    mul = mul*i
print(mul)
6

Hanoi Problem

在这里插入图片描述

思路

在这里插入图片描述

  • 把n-1个盘子从起始柱移到工具柱
  • 把最大的盘子从起始柱移到目标柱
  • 把n-1个盘子从工具柱移到目标柱
count = 0
def hanoi(source,target,tool,n):
    global count
    if n==1:
        count += 1
        mov(source,target,1)
    else:
        hanoi(source,tool,target,n-1)
        count += 1
        mov(source,target,n)
        hanoi(tool,target,source,n-1)

def mov(source,target,n):
    print("Rond %d move disk %d from %s to %s.\n"%(count,n,source,target))
hanoi("source","target","tool",5)
Rond 1 move disk 1 from source to target.

Rond 2 move disk 2 from source to tool.

Rond 3 move disk 1 from target to tool.

Rond 4 move disk 3 from source to target.

Rond 5 move disk 1 from tool to source.

Rond 6 move disk 2 from tool to target.

Rond 7 move disk 1 from source to target.

Rond 8 move disk 4 from source to tool.

Rond 9 move disk 1 from target to tool.

Rond 10 move disk 2 from target to source.

Rond 11 move disk 1 from tool to source.

Rond 12 move disk 3 from target to tool.

Rond 13 move disk 1 from source to target.

Rond 14 move disk 2 from source to tool.

Rond 15 move disk 1 from target to tool.

Rond 16 move disk 5 from source to target.

Rond 17 move disk 1 from tool to source.

Rond 18 move disk 2 from tool to target.

Rond 19 move disk 1 from source to target.

Rond 20 move disk 3 from tool to source.

Rond 21 move disk 1 from target to tool.

Rond 22 move disk 2 from target to source.

Rond 23 move disk 1 from tool to source.

Rond 24 move disk 4 from tool to target.

Rond 25 move disk 1 from source to target.

Rond 26 move disk 2 from source to tool.

Rond 27 move disk 1 from target to tool.

Rond 28 move disk 3 from source to target.

Rond 29 move disk 1 from tool to source.

Rond 30 move disk 2 from tool to target.

Rond 31 move disk 1 from source to target.

Fibonacci Numbers

在这里插入图片描述

def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n-1) + fib(n-2)

时间复杂度为O(2n),循环为O(n)

Search

  • Retrieve information stored within some data structure, or calculated in the search space of a problem domain, either with discrete or continuous values

Eight Queens Puzzle

在这里插入图片描述

思路

在这里插入图片描述

递归

非递归

Seven Bridges of Konigsberg

在这里插入图片描述

思路

简化为数学图形
在这里插入图片描述

Eulerian Path

A trial that visit every edge exactly once

在这里插入图片描述

七桥问题无解

八桥问题有解

在这里插入图片描述

Shortest Path

在这里插入图片描述

思路

在这里插入图片描述

do better

在这里插入图片描述

Deep-First Search and Breadth-first Search

DFS

  • explore as far as possible
  • use a stack(implicitly)

BFS

  • explore all of the neighbor nodes at the present depth
  • use a queue

Game

  • use DFS to generate a maze

  • use DFS/BFS to find the solution of that maze

Practice Tasks

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值