Guest User

Untitled

a guest
Jun 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. L = (d)
  2. R = (a,b,c)
  3. Result = ((NULL,d), (a,NULL), (b,NULL), (c,NULL))
  4.  
  5. L = (a,b,c,d,e)
  6. R = (b,q,c,d,g,e)
  7. Result = ((a,NULL), (b,b), (NULL,q), (c,c), (d,d), (NULL,g), (e,e))
  8.  
  9. def FindMatches(listL, listR):
  10. result=[]
  11. lookupR={}
  12. for i in range(0, len(listR)):
  13. lookupR[listR[i]] = i
  14. unprocessedR = 0
  15. for left in listL:
  16. if left in lookupR:
  17. for right in listR[unprocessedR:lookupR[left]]:
  18. result.append((None,right))
  19. result.append((left,left))
  20. unprocessedR = lookupR[left] + 1
  21. else:
  22. result.append((left,None))
  23. for right in listR[unprocessedR:]:
  24. result.append((None,right))
  25. return result
  26.  
  27. >>> FindMatches(('d'),('a','b','c'))
  28. [('d', None), (None, 'a'), (None, 'b'), (None, 'c')]
  29. >>> FindMatches(('a','b','c','d','e'),('b','q','c','d','g','e'))
  30. [('a', None), ('b', 'b'), (None, 'q'), ('c', 'c'), ('d', 'd'), (None, 'g'), ('e','e')]
  31.  
  32. //this is very rough pseudocode
  33. stack aList;
  34. stack bList;
  35. List resultList;
  36. char aVal;
  37. char bVal;
  38.  
  39. while(aList.Count > 0 || bList.Count > 0)
  40. {
  41. aVal = aList.Peek; //grab the top item in A
  42. bVal = bList.Peek; //grab the top item in B
  43.  
  44. if(aVal < bVal || bVal == null)
  45. {
  46. resultList.Add(new Tuple(aList.Pop(), null)));
  47. }
  48. if(bVal < aVal || aVal == null)
  49. {
  50. resultList.Add(new Tuple(null, bList.Pop()));
  51. }
  52. else //equal
  53. {
  54. resultList.Add(new Tuple(aList.Pop(), bList.Pop()));
  55. }
  56. }
  57.  
  58. //pseudo code... will not compile
  59. //Note, this modifies aList and bList, so make copies.
  60. List aList;
  61. List bList;
  62. List resultList;
  63. var aVal;
  64. var bVal;
  65.  
  66. while(aList.Count > 0)
  67. {
  68. aVal = aList.Pop();
  69. for(int bIndex = 0; bIndex < bList.Count; bIndex++)
  70. {
  71. bVal = bList.Peek();
  72. if(aVal.RelevantlyEquivalentTo(bVal)
  73. {
  74. //The bList items that come BEFORE the match, are definetly not in aList
  75. for(int tempIndex = 0; tempIndex < bIndex; tempIndex++)
  76. {
  77. resultList.Add(new Tuple(null, bList.Pop()));
  78. }
  79. //This 'popped' item is the same as bVal right now
  80. resultList.Add(new Tuple(aVal, bList.Pop()));
  81.  
  82. //Set aVal to null so it doesn't get added to resultList again
  83. aVal = null;
  84.  
  85. //Break because it's guaranteed not to be in the rest of the list
  86. break;
  87. }
  88. }
  89. //No Matches
  90. if(aVal != null)
  91. {
  92. resultList.Add(new Tuple(aVal, null));
  93. }
  94. }
  95. //aList is now empty, and all the items left in bList need to be added to result set
  96. while(bList.Count > 0)
  97. {
  98. resultList.Add(new Tuple(null, bList.Pop()));
  99. }
  100.  
  101. int score(char x, char y){
  102. if ((x == ' ') || (y == ' ')){
  103. return 0;
  104. }
  105. else if (x != y){
  106. return -1;
  107. }
  108. else if (x == y){
  109. return 1;
  110. }
  111. else{
  112. puts("Error!");
  113. exit(2);
  114. }
  115. }
  116.  
  117. #include <stdio.h>
  118. #include <stdbool.h>
  119.  
  120. int max(int a, int b, int c){
  121. bool ab, ac, bc;
  122. ab = (a > b);
  123. ac = (a > c);
  124. bc = (b > c);
  125. if (ab && ac){
  126. return a;
  127. }
  128. if (!ab && bc){
  129. return b;
  130. }
  131. if (!ac && !bc){
  132. return c;
  133. }
  134. }
  135.  
  136. int score(char x, char y){
  137. if ((x == ' ') || (y == ' ')){
  138. return 0;
  139. }
  140. else if (x != y){
  141. return -1;
  142. }
  143. else if (x == y){
  144. return 1;
  145. }
  146. else{
  147. puts("Error!");
  148. exit(2);
  149. }
  150. }
  151.  
  152.  
  153. void print_table(int **table, char str1[], char str2[]){
  154. unsigned int i, j, len1, len2;
  155. len1 = strlen(str1) + 1;
  156. len2 = strlen(str2) + 1;
  157. for (j = 0; j < len2; j++){
  158. if (j != 0){
  159. printf("%3c", str2[j - 1]);
  160. }
  161. else{
  162. printf("%3c%3c", ' ', ' ');
  163. }
  164. }
  165. putchar('n');
  166. for (i = 0; i < len1; i++){
  167. if (i != 0){
  168. printf("%3c", str1[i - 1]);
  169. }
  170. else{
  171. printf("%3c", ' ');
  172. }
  173. for (j = 0; j < len2; j++){
  174. printf("%3d", table[i][j]);
  175. }
  176. putchar('n');
  177. }
  178. }
  179.  
  180. int **optimal_global_alignment_table(char str1[], char str2[]){
  181. unsigned int len1, len2, i, j;
  182. int **table;
  183. len1 = strlen(str1) + 1;
  184. len2 = strlen(str2) + 1;
  185. table = malloc(sizeof(int*) * len1);
  186. for (i = 0; i < len1; i++){
  187. table[i] = calloc(len2, sizeof(int));
  188. }
  189. for (i = 0; i < len1; i++){
  190. table[i][0] += i * score(str1[i], ' ');
  191. }
  192. for (j = 0; j < len1; j++){
  193. table[0][j] += j * score(str1[j], ' ');
  194. }
  195. for (i = 1; i < len1; i++){
  196. for (j = 1; j < len2; j++){
  197. table[i][j] = max(
  198. table[i - 1][j - 1] + score(str1[i - 1], str2[j - 1]),
  199. table[i - 1][j] + score(str1[i - 1], ' '),
  200. table[i][j - 1] + score(' ', str2[j - 1])
  201. );
  202. }
  203. }
  204. return table;
  205. }
  206.  
  207. void prefix_char(char ch, char str[]){
  208. int i;
  209. for (i = strlen(str); i >= 0; i--){
  210. str[i+1] = str[i];
  211. }
  212. str[0] = ch;
  213. }
  214.  
  215. void optimal_global_alignment(int **table, char str1[], char str2[]){
  216. unsigned int i, j;
  217. char *align1, *align2;
  218. i = strlen(str1);
  219. j = strlen(str2);
  220. align1 = malloc(sizeof(char) * (i * j));
  221. align2 = malloc(sizeof(char) * (i * j));
  222. align1[0] = align2[0] = '';
  223. while((i > 0) && (j > 0)){
  224. if (table[i][j] == (table[i - 1][j - 1] + score(str1[i - 1], str2[j - 1]))){
  225. prefix_char(str1[i - 1], align1);
  226. prefix_char(str2[j - 1], align2);
  227. i--;
  228. j--;
  229. }
  230. else if (table[i][j] == (table[i - 1][j] + score(str1[i-1], ' '))){
  231. prefix_char(str1[i - 1], align1);
  232. prefix_char('_', align2);
  233. i--;
  234. }
  235. else if (table[i][j] == (table[i][j - 1] + score(' ', str2[j - 1]))){
  236. prefix_char('_', align1);
  237. prefix_char(str2[j - 1], align2);
  238. j--;
  239. }
  240. }
  241. while (i > 0){
  242. prefix_char(str1[i - 1], align1);
  243. prefix_char('_', align2);
  244. i--;
  245. }
  246. while(j > 0){
  247. prefix_char('_', align1);
  248. prefix_char(str2[j - 1], align2);
  249. j--;
  250. }
  251. puts(align1);
  252. puts(align2);
  253. }
  254.  
  255. int main(int argc, char * argv[]){
  256. int **table;
  257. if (argc == 3){
  258. table = optimal_global_alignment_table(argv[1], argv[2]);
  259. print_table(table, argv[1], argv[2]);
  260. optimal_global_alignment(table, argv[1], argv[2]);
  261. }
  262. else{
  263. puts("Reqires to string arguments!");
  264. }
  265. return 0;
  266. }
  267.  
  268. $ cc dynamic_programming.c && ./a.out aab bba
  269. __aab
  270. bb_a_
  271. $ cc dynamic_programming.c && ./a.out d abc
  272. ___d
  273. abc_
  274. $ cc dynamic_programming.c && ./a.out abcde bqcdge
  275. ab_cd_e
  276. _bqcdge
Add Comment
Please, Sign In to add comment