greedy algorithms & dynamic programming

14
Greedy Algorithms & Dynamic Programming Prof Amir Geva Eitan Netzer

Upload: pravat

Post on 23-Feb-2016

64 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Greedy Algorithms & Dynamic  Programming

Greedy Algorithms&Dynamic Programming

Prof Amir GevaEitan Netzer

Page 2: Greedy Algorithms & Dynamic  Programming

Greedy algorithms

Solve local optimization in hope it will bring to global optimization

Often trade off with the best answer possible

Page 3: Greedy Algorithms & Dynamic  Programming

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

Page 4: Greedy Algorithms & Dynamic  Programming

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

Page 5: Greedy Algorithms & Dynamic  Programming

this is an example of a huffman tree

Page 6: Greedy Algorithms & Dynamic  Programming

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

Page 7: Greedy Algorithms & Dynamic  Programming

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

Page 8: Greedy Algorithms & Dynamic  Programming

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

Page 9: Greedy Algorithms & Dynamic  Programming

Longest common subsequence

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

# combinations 2n

Page 10: Greedy Algorithms & Dynamic  Programming

Dynamic

Page 11: Greedy Algorithms & Dynamic  Programming

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

Page 12: Greedy Algorithms & Dynamic  Programming

ExampleAGCAT GAC

Page 13: Greedy Algorithms & Dynamic  Programming

ExampleAGCAT GAC

Page 14: Greedy Algorithms & Dynamic  Programming

ExampleAGCAT GAC