-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_programming.py
More file actions
191 lines (176 loc) · 5.95 KB
/
Copy pathdynamic_programming.py
File metadata and controls
191 lines (176 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import numpy as np
def recursive_matching(P: str, T: str, i: int, j: int) -> int:
if i == 0:
return len(T[:j])
if j == 0:
return len(P[:i])
swap = recursive_matching(P, T, i - 1, j - 1) + (P[i] != T[j])
insert = recursive_matching(P, T, i, j - 1) + 1
delete = recursive_matching(P, T, i - 1, j) + 1
return min(swap, insert, delete)
def PD_matching(P: str, T: str) -> int:
D = np.zeros((len(P), len(T)))
D[0] = np.array([i for i in range(len(T))])
for i in range(len(P)):
D[i, 0] = i
for i in range(1, D.shape[0]):
for j in range(1, D.shape[1]):
swap = D[i - 1, j - 1] + (P[i] != T[j])
insert = D[i, j - 1] + 1
delete = D[i - 1, j] + 1
min_cost = min(swap, insert, delete)
D[i, j] = min_cost
return int(D[len(P) - 1, len(T) - 1])
def PD_matching_with_path(P: str, T: str) -> str:
D = np.zeros((len(P), len(T)))
D[0] = np.array([i for i in range(len(T))])
for i in range(len(P)):
D[i, 0] = i
parent = []
for i in range(len(P)):
parent.append(['X' for _ in range(len(T))])
parent = np.array(parent)
parent[0, 1:] = np.array(['I' for _ in range(1, len(T))])
for i in range(1, len(P)):
parent[i, 0] = 'D'
for i in range(1, D.shape[0]):
for j in range(1, D.shape[1]):
swap = D[i - 1, j - 1] + (P[i] != T[j])
insert = D[i, j - 1] + 1
delete = D[i - 1, j] + 1
min_cost = min(swap, insert, delete)
D[i, j] = min_cost
if min_cost == swap:
if P[i] != T[j]:
parent[i, j] = 'S'
else:
parent[i, j] = 'M'
elif min_cost == insert:
parent[i, j] = 'I'
else:
parent[i, j] = 'D'
path = []
i = len(P) - 1
j = len(T) - 1
while parent[i, j] != 'X':
operation = parent[i, j]
path.append(operation)
if operation == 'M' or operation == 'S':
i -= 1
j -= 1
elif operation == 'I':
j -= 1
elif operation == 'D':
i -= 1
path.reverse()
return ''.join(path)
def sub_match(P: str, T: str) -> int:
D = np.zeros((len(P), len(T)))
for i in range(len(P)):
D[i, 0] = i
parent = []
for i in range(len(P)):
parent.append(['X' for _ in range(len(T))])
parent = np.array(parent)
for i in range(1, len(P)):
parent[i, 0] = 'D'
for i in range(1, D.shape[0]):
for j in range(1, D.shape[1]):
swap = D[i - 1, j - 1] + (P[i] != T[j])
insert = D[i, j - 1] + 1
delete = D[i - 1, j] + 1
min_cost = min(swap, insert, delete)
D[i, j] = min_cost
if min_cost == swap:
if P[i] != T[j]:
parent[i, j] = 'S'
else:
parent[i, j] = 'M'
elif min_cost == insert:
parent[i, j] = 'I'
else:
parent[i, j] = 'D'
i = len(P) - 1
j = 0
for k in range(1, len(T)):
if D[i, k] < D[i, j]:
j = k
return j
def lcs(P: str, T: str) -> str:
D = np.zeros((len(P), len(T)))
D[0] = np.array([i for i in range(len(T))])
for i in range(len(P)):
D[i, 0] = i
parent = []
for i in range(len(P)):
parent.append(['X' for _ in range(len(T))])
parent = np.array(parent)
parent[0, 1:] = np.array(['I' for _ in range(1, len(T))])
for i in range(1, len(P)):
parent[i, 0] = 'D'
for i in range(1, D.shape[0]):
for j in range(1, D.shape[1]):
swap = D[i - 1, j - 1] + float('inf') if P[i] != T[j] else 0
insert = D[i, j - 1] + 1
delete = D[i - 1, j] + 1
min_cost = min(swap, insert, delete)
D[i, j] = min_cost
if min_cost == swap:
if P[i] != T[j]:
parent[i, j] = 'S'
else:
parent[i, j] = 'M'
elif min_cost == insert:
parent[i, j] = 'I'
else:
parent[i, j] = 'D'
res = ""
i = len(P) - 1
j = len(T) - 1
while parent[i, j] != 'X':
operation = parent[i, j]
if operation == 'M':
res += T[j]
i -= 1
j -= 1
elif operation == 'I':
j -= 1
elif operation == 'D':
i -= 1
return res[::-1]
def main():
P = ' kot'
T = ' pies'
min_cost = recursive_matching(P, T, len(P) - 1, len(T) - 1)
print(f"Dla wyrazów - {P} i {T}, minimalny koszt to: {min_cost}")
print('------------------------------------------------------------------------')
P = ' biały autobus'
T = ' czarny autokar'
min_cost = PD_matching(P, T)
print(f"Dla wyrazów - {P} i {T}, minimalny koszt to: {min_cost}")
print('------------------------------------------------------------------------')
P = ' thou shalt not'
T = ' you should not'
p = PD_matching_with_path(P, T)
print(f'Ścieżka: {p}')
print('------------------------------------------------------------------------')
P = ' bin'
T = ' mokeyssbanana'
idx_start = sub_match(P, T)
print(f"Indeks najlepszego dopasowania podciągu {P} w {T}: {idx_start - len(P.strip()) + 1}")
print('------------------------------------------------------------------------')
P = ' democrat'
T = ' republican'
LCS = lcs(P, T)
print(f"Najdłuższa wspólna sekwencja dla {P} i {T}: {LCS}")
print('------------------------------------------------------------------------')
T = ' 243517698'
T_new = T.strip()
P = list(T_new)
P = sorted(P, key=int)
P = " " + ''.join(P)
LMS = lcs(P, T)
print(f"Najdłuższa podsekwencja monotoniczna dla {P} i {T}: {LMS}")
print('------------------------------------------------------------------------')
if __name__ == '__main__':
main()