Fibonacci
F5
F4
F3
F2
F1
Fibonacci
Kruskal's algorithm
Prim's algorithm
Q
R
T
S
Longest path problem
(No-optimal substructure)
1) Describe optimal structure
2) Make recurrent relationship (connect optimal values)
3) Bottom-up approach
4) Use information and build solution
...
PROFIT!!!
F(n) = F(n-1) + F(n-2)
F0 = F1 = 1
F(2n) = F(n) + F(n-1),
F(2n+1) = F(n) – F(n-1),
F0 = F1 = 1
Recursion:
+ Easy implementation
- Slow (little bit)
Loop:
+ Fast
- Memory consumption
private static int f(int n){
if(n==0 || n==1) return 1;
if(n%2==0){
return f(n/2)+f(n/2-1);
}else{
return f((n-1)/2)-f((n-1)/2-1);
}
}
if(n<2) return 1;
int[] fs = int[n];
fs[0]=fs[1]=1;
for(int i=2; i<n; i++){
if(i%2==0){
fs[i]=fs[i/2]+fs[i/2-1];
}else{
fs[i]=fs[(i-1)/2]+fs[(i-1)/2-1]
}
}
return fs[n-1];
f(6)+f(5)
f(6)=f(3)+f(2)
f(5)=f(2)-f(1)
f(2)
# | 3 |
---|---|
# | 4 |
# | 5 |
... |
N steps
1) 1 step
2) 2 steps
3) 3 steps
F(N) = F(N-1)+F(N-2)+F(N-3)
private static int f(int n){
if(n==1) return 1;
if(n==2) return 2;
if(n==3) return 4;
return f(n-1)+f(n-2)+f(n-3);
}
int[] arr = new int[3];
arr[0]=1; arr[1]=2; arr[2]=4;
for(int i=3; i<N; i++){
vars[i%3] = arr[0]+arr[1]+arr[2];
}
}
N
M
(x, y-1)
(x-1, y)
F(x,y) = F(x-1, y)+F(x,y-1)
F(x,y) = F(x-1, y)+F(x,y-1)
for(int i=0; i<Imax; i++){
for(int j=0; j<Jmax; j++){
if(i==0 || j==0){
dp[i][j]=1;
}else{
dp[i][j]=dp[i-1][j]+dp[i][j-1];
}
}
}
A type
(especially dangerous)
B type
(non-hazardous)
N containers
A |
---|
B |
B |
A |
... |
BBB |
BBA |
BAB |
ABB |
ABA |
BB |
BA |
AB |
B |
A |
∆
B type => 2 combinations
A type => comes to B type (in 1 iteration)
Fibonacci numbers
1) Add 1
2) Multiply by 2
3) Multiply by 3
Input: X (int)
Output: min sequence of operations (1231123)
F(N) = min(F(N-1), F(N/2), F(N/3)) + 1
int min;
for(int i=2; i<N+1; i++){
min=a[i-1]+1;
if(i%2==0) min=Math.min(min,a[i/2]+1);
if(i%3==0) min=Math.min(min,a[i/3]+1);
a[i] = min;
}
def golden_pyramid(triangle, row=0, column=0, total=0):
global count
count += 1
if row == len(triangle) - 1:
return total + triangle[row][column]
return max(golden_pyramid(triangle, row + 1, column, total + triangle[row][column]),
golden_pyramid(triangle, row + 1, column + 1, total + triangle[row][column]))
def golden_pyramid_d(triangle):
tr = [row[:] for row in triangle] # copy
for i in range(len(tr) - 2, -1, -1):
for j in range(i + 1):
tr[i][j] += max(tr[i + 1][j], tr[i + 1][j + 1])
return tr[0][0]
Amortized analysis
Greedy algorithms
Thomas H. Cormen
Be happy and learn algorithms!
You are awesome!