ΑΛΓΟΡΙΘΜΟΙ http://eclass.aueb.gr/courses/inf161/ Άνοιξη 216 - I. ΜΗΛΗΣ ΔΥΝΑΜΙΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 1
Dynamic Programming ΓΕΝΙΚΗ ΙΔΕΑ 1. Ορισμός υπο-προβλήματος/ων 2. Optimal substructure 3. Αναδρομική σχέση για τη βέλτιστη λύση Η αναδρομική σχέση δίνει μία σειρά (order) των υποπροβλημάτων τέτοια ώστε καθένα μπορεί να λυθεί βέλτιστα δεδομένης των βέλτιστων λύσεων μικρότερων υπο-προβλημάτων (που εμφανίζονται νωρίτερα στη σειρά). ΑΛΓΟΡΙΘΜΟΣ 1. Μετάφραση της αναδρομικής σχέσης σε έναν επαναληπτικό αλγόριθμο (ΠΟΤΕ αναδρομικό) 2. Εύρεση πρώτα της τιμής της βέλτιστης λύσης 3. Εύρεση της ίδιας της βέλτιστης λύσης ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 2
Algorithm design methods DIVIDE AND CONQUER Non overlapping sub-problems Recursion can be used Tree GREEDY A sub-problem defines the next one A single (greedy) choice Chain DYNAMIC PROGRAMMING Overlapping sub-problems Recursion is forbidden Many choices for a sub-problem OPTIMAL SUB-STRUCTURE DAG ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 3
Longest Common Subsequence (LCS) ΕΙΣΟΔΟΣ: Δύο συμβολοσειρές X=x 1, x 2,,x m και Y=y 1, y 2,,y n ΕΡΩΤΗΣΗ: H LCS (όχι συνεχόμενη) των X και Y Εφαρμογή: Βιολογία, σύγκριση συμβολοσειρών DNA Παράδειγμα X = A B C B D A B Y = B D C A B A LCS(X,Y) = B C B A Η LCS δεν είναι συνεχόμενη ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 4
Longest Common Subsequence (LCS) Brute force: Σύγκρινε κάθε υπο-ακολουθία της X με την Y X = m, Y = n Υπάρχουν 2 m κάθε υπο-ακολουθίες της X Σύγκρινε κάθε μία με την Y (n συγκρίσεις - γιατί?) Πολυπλοκότητα O(n2 m ) ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 5
Longest Common Subsequence (LCS) X 1 i m Y 1 j n X i, Y j : προθέματα των X και Y με μήκη i και j Υπο-πρόβλημα: c[i,j] = Μήκος της LCS(X i, Y j ) Αρχικό πρόβλημα: c[m,n]=μήκος της LCS(X,Y) Optimal substructure: Έστω Z=z 1, z 2,,z k η LCS των X και Y, Z =k (i) If x m = y n,, then z k = x m = y n : η Z k-1 είναι LCS των X m-1 and Y n-1 (ii) If x m y n,, then (a) z k x m : η Z είναι LCS των X m-1 και Y (b) z k y n : η Z είναι LCS των X και Y n-1 Πρώτα θα βρούμε το μήκος της LCS(X,Y), και μετά την ίδια την LCS(X,Y) ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 6
Longest Common Subsequence (LCS) Υπολογισμός του c[i,j] Μελετάμε δύο περιπτώσεις/επιλογές: 1) x i = y j Ένα ακόμη σύμβολο των X και Y ταιριάζει c [ i, j ] = c [ i-1, j-1 ] +1 2) x i y j Τα σύμβολα x i και y j δεν ταιριάζουν, η λύση δεν βελτιώνεται Το μήκος της LCS(X i, Y j ) είναι ίδιο με πριν c [ i, j ] = max { c [ i, j-1 ], c [ i-1, j ] Γιατί δεν παίρνουνε μόνο το μήκος της LCS(X i-1, Y j-1 )? Παράδειγμα: BBD A c(3,3)=2 BBA C c(4,4)= max{ c(4,3), c(3,4) } = max {3, 2) = 3 ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 7
Longest Common Subsequence (LCS) H LCS δύο κενών συμβολοσειρών είναι κενή: c[,] = Η LCS μιας κενής συμβολοσειράς και οποιασδήποτε άλλης συμβολοσειράς είναι κενή: c[, j] = c[i,] =, i m, j n Αναδρομική σχέση: c[ i, j] c[ i max( c[ i, 1, j 1] 1 j 1], c[ i 1, j]) if if if i or j i, i, j and x j and x i i y y j j ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 8
Longest Common Subsequence (LCS) LCS-Length(X, Y) m = length(x) n = length(y) // # of symbols in X // # of symbols in Y for i = 1 to m: c[i,] = // special case: Y for j = 1 to n: c[,j] = // special case: X for i = 1 to m: // for all X i for j = 1 to n: // for all Y j if x i = y j : c[i,j] = c[i-1,j-1] + 1 else : c[i,j] = max(c[i-1,j], c[i,j-1]) Πολυπλοκότητα: O(mn) ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 9
LCS example X = A B C B Y = B D C A B LCS(X, Y) = BCB i if x i = y j : c[i,j] = c[i-1,j-1] + 1 else : c[i,j] = max(c[i-1,j], c[i,j-1]) j 1 2 3 4 5 y j B D C A B x i 1 A 1 1 2 B 1 1 1 1 2 3 C 1 1 2 2 2 4 B 1 1 2 2 3 ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 1
Longest Common Subsequence (LCS) Μέχρι τώρα έχουμε βρει το μήκος, c[m,n], της LCS(Χ,Υ) αλλά όχι την ίδια την LCS(Χ,Υ) Επέκταση του αλγορίθμου για να βρίσκει και την LCS(X,Y) Κάθε c[i,j] προκύπτει είτε από το c[i-1, j-1] ή από τα c[i-1,j] και c[i,j-1] Κοιτάζοντας τον πίνακα c μπορούμε να βρούμε πως προέκυψε κάθε c[i,j] Θυμηθείτε την αναδρομική σχέση: if i or j c [ i, j] c[ i 1, j 1] 1 if i, j and x i y j max( c [ i, j 1], c[ i 1, j]) if i, j and xi y j 2 2 2 3 If x i =y j then c[i,j] = c[i-1, j-1]+1 1 1 2 2 If x i y j then c[i,j] = max{c[i-1,j], c[i,j-1]} ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 11
Longest Common Subsequence (LCS) Αναδρομική σχέση c[ i, j] c[ i 1, j 1] 1 max( c[ i, j 1], c[ i 1, j]) if i or j if if i, i, j and j and x i x i y y j j Άρχισε από το c[m,n] και πήγαινε προς τα πίσω: If x i =y j then c[i,j] = c[i-1, j-1]+1) : κράτησε το x i, το x i ανήκει στην LCS(X,Y) πήγαινε στο c[i-1, j-1] If x i y j then c[i,j] = max{c[i-1,j], c[i,j-1]} If c[i,j] = c[i-1, j] < c[i, j-1]: πήγαινε στο c[i-1,j] If c[i,j] = c[i, j-1] < c[i-1, j] : πήγαινε στο c[i, j-1] If c[i,j] = c[i-1, j] = c[i, j-1]: πήγαινε είτε στο c[i-1,j] ή στο c[i, j-1] Ιf i= or j= output την LCS(X,Y) σε αντίστροφη σειρά ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 12
LCS example i j 1 2 3 4 5 Y j B D C A B X i 1 A 1 1 2 B 1 1 1 1 2 3 C 1 1 2 2 2 4 B 1 1 2 2 3 LCS (reversed order): BCB LCS (straight order): BCB ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 13
Longest Common Subsequence (LCS) LCS (X, Y) m = length(x) n = length(y);... LCS length algorithm... k:=, i = m, j = n While i and j : if x i =y j : // c[i,j] = c[i-1, j-1]+1 k=k+1, LCS[k]=x i, i=i-1, j=j-1 else: // c[i,j] = max(c[i-1,j], c[i,j-1]) if c[i,j] = c[i-1, j]: i=i-1 else: j=j-1; for i= k downto 1: output LCS[i]; ΠΟΛΥΠΛΟΚΟΤΗΤΑ: Ο(mn) +? ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 14
Edit distance Edit distance between two strings = the minimum number of edits insertions deletions substitutions to transform the first string into the second Alignment: writing the strings one above the other using spaces (-) # of edits = # of columns in which the characters of the strings differ Edit distance: the minimum cost over all possible alignments ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 15
Edit distance Example # of edits=3 # of edits=5 insert U insert S substitute O with N substitute S with U delete W delete O delete W insert N Too many possible alignments between two strings! How can we find the best alignment? ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 16
Edit distance Dynamic Programming Strings X[1..m] and Y[1..n] X[1..i], i m-1 is a prefix of X Y[1..j], j n-1 is a prefix of Y Subproblem: E[i,j]) = Minimum Edit Distance between X[1..i] and Y[1..j] Optimal substructure: Consider the rightmost column of an optimal alignment In each of the cases (i) to (iv) below the Edit Distance of the prefixes in their right-hade side must be also optimal: (i) Match: E(i,j)= E(i-1,j-1) (ii) Substitute: E(i,j)= E(i-1,j-1) +1 (iii) Insert: E(i,j)= E(i,j-1)+1 (iv) Delete: E(i,j)= E(i-1,j) +1 Edit distance between X and Y = E[m,n] First we ll find the Edit distance, then the edits themselves ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 17
Edit distance Three choices for the rightmost column of an alignment: 1 + E(i-1, j) 1+ E(i, j-1) diff(i, j) + E(i-1, j-1) where diff 1 if x[i] y[j] if x[i] y[j] Base cases: E(,j) = j : alignment of empty string and Y[1..j] E(i,) = i : alignment of X[1..i] and empty string ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 18
Edit distance The subproblem E(7, 5), diff = N or - or N - N N E(7,5) = min {1+ E(6,5), 1+ E(7,4), +E(6,4) } ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 19
Edit distance Fill a two-dimensional array by solving subproblems Row by row or column by column match or subst delete insert x x x x x x = 6 edits ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 2
Edit distance Complexity: O(nm) Exercise: Extent the algorithm to find the actual edits ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 21
Longest Increasing Subsequence (LIS) ΕΙΣΟΔΟΣ: ακολουθία αριθμών Α=a 1, a 2,...,a n ΕΡΩΤΗΣΗ: υπο-ακολουθία ai, ai,..., ai, 1 i... ik n 1 2 k 2 τ.ω. a a... a και το μήκος της k είναι μέγιστο Παράδειγμα: Ακολουθία Α= i 1 i 2 i k LIS: 2, 3, 6, 9 k=4 ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 22
Longest Increasing Subsequence (LIS) Αναγωγή στο LCS Έστω Α=a 1, a 2,...,a n η ακολουθία μας Ταξινομούμε τους αριθμούς στην Α παραλείποντας τους επαναλαμβανόμενους, έστω Β αυτή η ταξινομημένη ακολουθία Βρίσκουμε την LCS των Α και Β Παράδειγμα A = 5, 2, 8, 6, 3, 6, 9, 7 B = 2, 3, 5, 6, 7, 8, 9 LCS (A, B) = 2,3,6,9 =LIS (A) ΠΟΛΥΠΛΟΚΟΤΗΤΑ: O(n 2 ) ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 23
Longest Increasing Subsequence (LIS) Αναγωγή στο Edit distance Έστω Α=a 1, a 2,...,a n η ακολουθία μας Ταξινομούμε τους αριθμούς στην Α παραλείποντας τους επαναλαμβανόμενους, έστω Β αυτή η ταξινομημένη ακολουθία Βρίσκουμε την Edit Distance των Α και Β Παράδειγμα A = 5, 2, 8, 6, 3, 6, 9, 7 Β = 2, 3, 5, 6, 7, 8, 9 Edit distance των Α και Β 5 2 8 6 3-6 - - 9 7-2 - - 3 5 6 7 8 9 - LIS= οι αριθμοί που είναι ίδιοι στο alignment! ΠΟΛΥΠΛΟΚΟΤΗΤΑ: O(n 2 ) ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 216 - Ι. ΜΗΛΗΣ 9 DP II 24