greedy algorithms & dynamic programming
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
Greedy Algorithms&Dynamic Programming
Prof Amir GevaEitan Netzer
Greedy algorithms
Solve local optimization in hope it will bring to global optimization
Often trade off with the best answer possible
Huffman codingHuffman 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 charsOutput: 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) logT n O n n
this is an example of a huffman tree
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 onceOften trade off with memory complexity
Guide lines
1.Find recursive method2.Discover that algorithm (top to bottom) is
exponential time3.If cause of exponential is repeating problems
then Dynamic Programming is in order4.Solve sub problems and keep there answer
ExampleFibonacci sequence
function fib(n) 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]
T n O n
Longest common subsequence
Find Longest common subsequence (not have to be continuous)sequence X: AGCAT(n elements)sequence Y: GAC(m elements)
# combinations 2n
Dynamic
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] T n O nm
ExampleAGCAT GAC
ExampleAGCAT GAC
ExampleAGCAT GAC