1 / 12

Greedy Algorithms & Dynamic Programming

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.

pravat
Download Presentation

Greedy Algorithms & Dynamic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Greedy Algorithms&Dynamic Programming Prof Amir Geva EitanNetzer

  2. Greedy algorithms Solve local optimization in hope it will bring to global optimization Often trade off with the best answer possible

  3. 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!!!

  4. 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)

  5. 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

  6. 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

  7. 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]

  8. Longest common subsequence Find Longest common subsequence (not have to be continuous) sequence X: AGCAT(n elements)sequence Y: GAC(m elements) # combinations

  9. Dynamic

  10. 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]

  11. ExampleAGCAT GAC

More Related