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