Algorithmic Toolbox__5
Dynamic Programming_1
一、Change Problem
The problem is just the simple changing coins game, imagine that there are some money and several different denominations then we will try to find the smallest point.
1.Greedy Algorithm
To find the most efficient method of changing problem, we can find the largest denomination of each coins which is not larger than the last total money.
def gred(money,coins):
coins=sorted(coins,reverse=True)
num_p=0
while money>0:
for i in coins:
if i<=money:
num_p+=1
money-=i
break
return num_p
print(gred(100,[5,10,2,1]))
2.Recursive Algorithm
Another way is finding all the possible way that can finish the problem, however, it is obviously not a efficient way.
def rca(money, coins):
if money == 0:
return 0
min_change = 10000000 # just a very big number
for i in range(len(coins)):
if money-coins[i]>=0:
num_change = rca(money - coins[i], coins)
if num_change + 1 < min_change:
min_change= num_change+1
return min_change
print(rca(16,[10,5,1]))
We can find that if the number of money become larger than 100 and coins is [10,5,1], the running time will be a very long time, so it is obviously not a good method.
3.Dynamic Change Algorithm
Imagine that if we already know all the denominations and filled them in a list coins, then we should calculate all the best plan of changing money from 0 to a optional number.
def Dyc(money,coins):
min_change=[0]
for i in range(money+1):
min_change.append(100000)
for j in range(len(coins)):
if i>=coins[j]:
num_change=min_change[i-coins[j]]+1
if num_change<min_change[i]:
min_change[i]=num_change
return min_change[:len(min_change)-1]
print(Dyc(11,[1,2,5]))
To sum up, this question is a very simple example, but the algorithm we used is not. To save the running time, we should not calculate the thing that we have already calculate or have impossible result.
二、Edit Distance
1.Input and Output
Input: Two string
Output: The minimum number of operation
Binary search can be used when the original list has already been sorted, then we can do as the fake code shown below:
2.String Comparison
The edit distance between two strings is the minimum number of operations (insertions, deletions, and
substitutions of symbols) to transform one string into another. It is a measure of similarity of two strings.
Edit distance has applications, for example, in computational biology, natural language processing, and spell
checking. Your goal in this problem is to compute the edit distance between two strings.
So we can do it as the code shown below:
import numpy as np
def SC(A,B):
A=[i for i in A]
B=[i for i in B]
la,lb=len(A),len(B)
D=np.zeros([la+1,lb+1])
for i in range(la+1):
D[i,0]=i
for i in range(lb+1):
D[0,i]=i
for i in range(1,la+1):
for j in range(1,lb+1):
insertion=D[i-1,j]+1
deletion=D[i,j-1]+1
match=D[i-1,j-1]
dismatch=D[i-1,j-1]+1
if A[i-1]==B[j-1]:
D[i,j]=min(insertion,deletion,match)
else:
D[i,j]=min(insertion,deletion,dismatch)
return int(D[len(A),len(B)])
print(SC("distance","dstapce"))
From this we can find that whether it is a string comparison problem or a route planning problem, the core idea of using algorithms to solve this problem is to stipulate tiny rules in a unit and deduct this rule to the whole. We do not It is necessary to analyze whether this local operation is absurd from a global perspective, as long as it is logical.
In the end, I’d like to discuss some private understanding of dynamic programming, as its name said, the fundamental part of dynamic programming is using the original part to use certain rules to push to the next part, and the next part is again a member of the original part, and we treat it as a kind of cogitation of solving dynamic programming problem.