+ All Categories
Home > Documents > Greedy Algorithms

Greedy Algorithms

Date post: 23-Feb-2016
Category:
Upload: rowena
View: 87 times
Download: 0 times
Share this document with a friend
Description:
Greedy Algorithms. Greedy Algorithm. Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum is a part of the global optimum, we get the global optimum. Greedy Algorithm vs Dynamic Programming. Dynamic Programming. Greedy - PowerPoint PPT Presentation
Popular Tags:
42
S Greedy Algorithms
Transcript
Page 1: Greedy Algorithms

S

Greedy Algorithms

Page 2: Greedy Algorithms

Greedy Algorithm

Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum is a part of the global

optimum, we get the global optimum.

Page 3: Greedy Algorithms

Greedy Algorithm vs Dynamic Programming

DynamicProgramming

GreedyAlgorithm

Page 4: Greedy Algorithms

Knapsack Problem

n items a thief’s knapsack of

size W

Page 5: Greedy Algorithms

Knapsack Problem

0-1 knapsack problem - Each item must be either taken or left behind. Fractional knapsack problem - The thief can take fractions of items.

Page 6: Greedy Algorithms

Knapsack Problem

453

0201

0$60 $10

0$120

$135 knapsac

k($6/unit)

($5/unit)

($4/unit)

($3/unit)

n = 4,W = 50

50

Page 7: Greedy Algorithms

Fractional Knapsack Problem

5045 3

0 20 1

0$60$10

0

$120$13

5 knapsack

($6/unit)

($5/unit)

($4/unit)($3/

unit)

10

20

2030

Greedy algorithm:greatest value per unit

50

$240

Page 8: Greedy Algorithms

0-1 Knapsack Problem

45

30

20

10

$60

$100

$120

$135 optima

l

($6/unit)

($5/unit)

($4/unit)

($3/unit)

50 50

value

50

valueper unit

50

10

20

45

30

20

$160

$135

$220

Page 9: Greedy Algorithms

0-1 Knapsack Problem

optimal

50 50

value

50

valueper unit

50

10

20

45

30

20

$160

$135

$220Difficult to get the

optimal solution with a greedy strategy.Dynamic Programming :

Page 10: Greedy Algorithms

Optimal Substructure vsSubproblem Solution

48

5 7

4

1

3

2

T1,5 T1,6

T2,5 T2,6

Ti,j : the solution of a subproblem

A subproblem solution?

A local optimum?

Page 11: Greedy Algorithms

Greedy Algorithm vs Dynamic Programming

Dynamic Programming

Greedy Algorithm

Computes all subproblems

Find a local optimum

Always finds the optimal solution

May not be able to find the optimal solution

Less efficient More efficient

Page 12: Greedy Algorithms

Optimal Substructure vsSubproblem Solution

Subproblem solution - From all subproblem solutions Optimal substructure - With only constant number of parameters - Without subproblems or future choices

Usuallytop-down

UsuallyBottom-up

Page 13: Greedy Algorithms

Huffman Codes

A lossless data compression algorithm. It uses variable-length code

Page 14: Greedy Algorithms

Variable-Length Code

Six characters : a, b, c, d, e, f

How can we represent them with binary strings?

a b c d e fFixed-length

000 001 010 011 100 101

Page 15: Greedy Algorithms

Variable-Length Code

Six characters : a, b, c, d, e, f

How can we represent them with binary strings?

a b c d e fVariable-length

0 1 00 01 10 11

What 0010 menas?

0 0 1 0 = a a b a0 01 0 = a d a00 10 = c e…

Page 16: Greedy Algorithms

Prefix Code

Six characters : a, b, c, d, e, f

How can we represent them with binary strings?

a b c d e fVariable-length

0 101 100 111 1101 1100

No codeword is a prefix of another codeword.

Page 17: Greedy Algorithms

Prefix Code

Six characters : a, b, c, d, e, f

How can we represent them with binary strings?

a b c d e fVariable-length

0 101 100 111 1101 1100

No codeword is a prefix of another codeword.

Page 18: Greedy Algorithms

Variable-Length Code

a b c d e f Total

Frequency 45 13 12 16 9 5 100Fixed-length 000 001 010 011 100 101 300

Variable-length

0 101 100 111 1101

1100

224

Is it the optimal way?

Page 19: Greedy Algorithms

Variable-Length Code

a b c d e f Total

Frequency 45 13 12 16 9 5 100Fixed-length 000 001 010 011 100 101 300

Variable-length

0 101 100 111 1101

1100

224

Alternative? 0 1100

1101

10 1110

1111

231

Page 20: Greedy Algorithms

Huffman Tree

100cha

rcode

freq

a 000 45b 001 13c 010 12d 011 16e 100 9f 101 5

Fixed-length code

86 14

58 1428

a: 45b: 13

c: 12d: 16

e: 9f: 5

0 1

0 1 0

0 1 0 1 0 1

Page 21: Greedy Algorithms

Huffman Tree

char

code

freq

a 0 45b 101 13c 100 12d 111 16e 110

19

f 1100

5

Variable-length code

100

55

25

14

30

a: 45

b: 13c: 12 d: 16

f: 5 e: 9

0 1

0 1

0 1

10

0 1

Page 22: Greedy Algorithms

Huffman’s Algorithm

A

B0

0

Every non-leaf node has two children.

B

0

Observation 1The longest code : at least 2 characters

Page 23: Greedy Algorithms

Huffman’s Algorithm

C H

I J

G10

A10

F10

Observation 1Observation 2

The longest code : at least 2 charactersThe longest 2 codes : the least frequent two characters

D E

B10

Page 24: Greedy Algorithms

Huffman’s Algorithm

Observation 1Observation 2

Observation 3

The longest code : at least 2 charactersThe longest 2 codes : the least frequent two charactersA non-leaf node : handled like a leaf node

d: 1614

30 x: 30T T ’

Page 25: Greedy Algorithms

Huffman’s Algorithm

d: 1614

30 x: 30

A non-leaf node : handled like a leaf node

Observation 3

e: 9 f: 510

T T ’10

Total length in T = Total length in T ‘ + 16 * 1 + (9 + 5) * 2

Page 26: Greedy Algorithms

Huffman’s Algorithm

Observation 1Observation 2

Observation 3

The longest code : at least 2 charactersThe longest 2 codes : the least frequent two charactersA non-leaf node : handled like a leaf node

d: 1614

30 x: 30T T ’

Page 27: Greedy Algorithms

Huffman’s Algorithm

a: 45 b: 13 c: 12 d: 16 e: 9 f: 5

Merging two least frequent nodes.

Page 28: Greedy Algorithms

Huffman’s Algorithm

a: 45 b: 13 c: 12 d: 16

e: 9 f: 5

Merging two least frequent nodes.

1410

Page 29: Greedy Algorithms

Huffman’s Algorithm

a: 45

c: 12 b: 13

d: 16

e: 9 f: 5

Merging two least frequent nodes.

1410

2510

Page 30: Greedy Algorithms

Huffman’s Algorithm

a: 45

c: 12 b: 13 d: 16

e: 9 f: 5

Merging two least frequent nodes.

1410

2510

3010

Page 31: Greedy Algorithms

Huffman’s Algorithm

a: 45

c: 12 b: 13 d: 16

e: 9 f: 5

Merging two least frequent nodes.

1410

2510

3010

550 1

Page 32: Greedy Algorithms

Huffman’s Algorithm

a: 45

c: 12 b: 13 d: 16

e: 9 f: 5

Merging two least frequent nodes.

1410

2510

3010

550 1

1000 1 cha

rcode

a 0b 101c 100d 111e 110

1f 110

0

Page 33: Greedy Algorithms

Greedy Algorithm

Optimization Algorithms : finds a proper local optimum.

Not as powerful as Dynamic Programming, but simpler.

Greedy algorithms - Knapsack Problem - Huffman Code

Page 34: Greedy Algorithms

S

Graphs

Page 35: Greedy Algorithms

What is a graph?

1 2

3 4

5

V = { 1, 2, 3, 4, 5 }E = { {1,2}, {1,3}, {2,3}, {2,4}, {2,5}, {3,4} }

G = ( V, E )

Page 36: Greedy Algorithms

Directed and Undirected

1 2

3 4

5

Directed graph

1 2

3 4

5

Undirected graph

Page 37: Greedy Algorithms

Representations of Graphs

1 2

3 4

5

Directed graph

12345

2 /3122 /

4 /3 /3 /

Adjacency-list

Page 38: Greedy Algorithms

Representations of Graphs

1 2

3 4

5

Directed graph Adjacency-Matrix1 2 3 4

5

5

34

21

Page 39: Greedy Algorithms

Representations of Graphs

Adjacency-Matrix

Adjacency-list

Adjacency List Adjacency Matrix

space |V| + |E| |V|2

Finding all edges

|V| + |E| |V|2

Finding one edge

num of edges 1

Page 40: Greedy Algorithms

Adjacency Matrix of Graphs

1 2

3 4

5

Directed graph Adjacency-Matrix1 2 3 4

5

5

34

21

Page 41: Greedy Algorithms

Adjacency Matrix of Graphs

A = AT =

Page 42: Greedy Algorithms

Weighted Graphs

1 2

3 4

5

1.7

0.4 2.0

-0.33.1

-0.23.6-

2.1


Recommended