130 likes | 323 Views
Greedy Algorithms & Dynamic Programming. Prof Amir Geva Eitan Netzer. Greedy algorithms. Solve local optimization in hope it will bring to global optimization Often trade off with the best answer possible. Huffman coding.
E N D
Greedy Algorithms&Dynamic Programming Prof Amir Geva EitanNetzer
Greedy algorithms Solve local optimization in hope it will bring to global optimization Often trade off with the best answer possible
Huffman coding Huffman coding is an entropy encoding algorithm used for lossless data compression Main idea encode repeating symbols in as short word Input: file containing symbols or chars Output: compressed file * Optimal!!!
Huffman(C) n<-length(C) % insert the group into priority queue Q<- C for i<-1 to n-1 do z<-allocate-node() x<-left[z]<-extract-min(Q) y<-right[z]<-extract-min(Q) f[z]<-f[x]+f[y] Insert(Q,z) return extract-min(Q)
Dynamic Programming All the Torah on one leg dynamic programming is a method for solving complex problems by breaking them down into simpler sub problems. Solve only once Often trade off with memory complexity
Guide lines • Find recursive method • Discover that algorithm (top to bottom) is exponential time • If cause of exponential is repeating problems then Dynamic Programming is in order • Solve sub problems and keep there answer
ExampleFibonacci sequence function fib(n) if n = 0 return 0 if n = 1 return 1 return fib(n − 1) + fib(n − 2) function fib(n) if key n is not in map m m[n] := fib(n − 1) + fib(n − 2) return m[n]
Longest common subsequence Find Longest common subsequence (not have to be continuous) sequence X: AGCAT(n elements)sequence Y: GAC(m elements) # combinations
functionLCSLength(X[1..m], Y[1..n]) C = array(0..m, 0..n) fori := 0..m C[i,0] = 0 for j := 0..n C[0,j] = 0 fori := 1..m for j := 1..n if X[i] = Y[j] C[i,j] := C[i-1,j-1] + 1 else C[i,j] := max(C[i,j-1], C[i-1,j]) return C[m,n]