Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic programming & Greedy algorithms

Similar presentations


Presentation on theme: "Dynamic programming & Greedy algorithms"— Presentation transcript:

1 Dynamic programming & Greedy algorithms

2 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

3 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

4 Example Fibonacci sequence
function fib(n) if n = 0 return 0 if n = 1 return 1 return fib(n − 1) + fib(n − 2) if key n is not in map m m[n] := fib(n − 1) + fib(n − 2) return m[n]

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

6 Dynamic

7 function LCSLength(X[1. m], Y[1. n]) C = array(0. m, 0. n) for i := 0
function LCSLength(X[1..m], Y[1..n]) C = array(0..m, 0..n) for i := 0..m C[i,0] = 0 for j := 0..n C[0,j] = 0 for i := 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]

8 Example AGCAT GAC "G" Row Completed Ø A G C T (G)

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

10 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!!!

11 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)

12


Download ppt "Dynamic programming & Greedy algorithms"

Similar presentations


Ads by Google