Algorithm Topic 1

Source: Internet
Author: User

1. Greedy algorithm, change problem

#Greedy Algorithm Money= [100,50,20,5,1]defChange_money (x): change=[0,0,0,0,0] forI,minchEnumerate (Money): Change[i]= x//Money[i] x= x%Money[i]ifX >0:Print("still left%s"%x)return ChangePrint(Change_money (356.2))

2. Greedy algorithm: Digital stitching problem

3. Dynamic planning: Longest ascending subsequence

3. Dynamic planning: The longest common string

def fib (n):
"Fibonacci Series"
f = [All]
For I in range (2, n+1):
F.append (F[-1]+f[-2])
Print (f)
return F[n]

FIB (5)

def LIS (x):
' Longest common subsequence '
F = [0 for _ in range (len (x))]
p = [-1 for _ in range (len (x))]
# initialization
F[0] = 1
P[0] = 1
for k in range (1, Len (F)):
Max_loc =-1
Max_num = 0
# inner loop represents the maximum value of f[i] for all locations less than x[k] in f[0:k]
For I in range (0, K):
If x[i] < X[k]:
If f[i] > max_num:
Max_loc = i
Max_num = F[i]
F[k] = max_num + 1
P[k] = Max_loc

max_i = 0
For I in range (1,len (F)):
If f[i] > f[max_i]:
Max_i = i

Lis = []
i = Max_i
While I >= 0:
Lis.append (X[i])
i = P[i]
Lis.reverse ()
return LIS

Print (LIS ([9,7,2,8,3,5,2]))

def LCS (x, y):
# The longest common subsequence (two-dimensional)
F = [[0 for _ in range (len (y) +1)] ' for ' in range (len (x) +1)]
p = [[0 for _ in range (len (y) +1)] for _ in range (len (x) +1)]
For I in range (1, len (x) +1):
P[i][0] = 2
For j in range (1, len (y) +1):
P[0][J] = 1

# 0 Oblique 1 Transverse j-1 2 vertical i-1
For I in range (1, len (x) +1):
For j in range (1, len (y) +1):
If x[i-1] = = Y[j-1]:
F[I][J] = f[i-1][j-1]+1
P[I][J] = 0
Else
#F [I][j] = max (F[i-1][j], f[i][j-1])
If F[I-1][J] > f[i][j-1]:
F[I][J] = F[i-1][j]
P[I][J] = 2
Else
F[I][J] = f[i][j-1]
P[I][J] = 1

LCS = []
i = Len (x)
j = Len (y)
While I > 0 or J > 0:
If p[i][j] = = 0:
Lcs.append (X[i-1])
I-= 1
J-= 1
Elif P[i][j] = = 1:
J-= 1
Else
I-= 1
Lcs.reverse ()
return LCS
#return F[i][j]

Print (LCS ("Abbcbde", "dbbcdb"))

def lcss_bf (x, y):
# the longest common string (violence)
m = Len (x)
n = Len (y)
Max_len = 0
Max_str = ""
for k in range (0, min (m,n)):
For I in range (0, m-k+1):
For j in range (0, n-k+1):
If x[i:i+k] = = Y[j:j+k]:
If k > Max_len:
Max_len = k
MAX_STR = X[i:i+k]
Return MAX_STR

Print (LCSS_BF ("Abbcbde", "dbbcdb"))

def lcss (x, y):
# the longest common string
F = [[0 for _ in range (len (y) +1)] ' for ' in range (len (x) +1)]
p = [[0 for _ in range (len (y) +1)] for _ in range (len (x) +1)]
# 0 does not match 1 matches
For I in range (1, len (x) +1):
For j in range (1, len (y) +1):
If x[i-1] = = Y[j-1]:
F[I][J] = f[i-1][j-1]+1
P[I][J] = 1
Else
F[I][J] = 0
P[I][J] = 0
Max_val = 0
max_i = 0
Max_j = 0
For I in range (1, len (x) +1):
For j in range (1, len (y) +1):
If F[I][J] > Max_val:
Max_val = F[i][j]
Max_i = i
Max_j = J
#tracback
LCSS = []
i = Max_i
j = Max_j
While p[i][j] = = 1:
Lcss.append (X[i-1])
I-= 1
J-= 1

Lcss.reverse ()
Return LCSS

Print (LCSs ("Abbcbde", "dbbcdb"))

Algorithm Topic 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.