Greedy Algorithms - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Greedy Algorithms

Description:

Greedy Algorithms. Greedy Algorithms. For some problems, dynamic programming is overkill. Greedy algorithms make the choice that's best at that moment ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 17
Provided by: jeffch8
Category:

less

Transcript and Presenter's Notes

Title: Greedy Algorithms


1
Greedy Algorithms
2
Greedy Algorithms
  • For some problems, dynamic programming is
    overkill
  • Greedy algorithms make the choice thats best at
    that moment
  • makes locally optimal choice in hopes for
    globally optimal solution
  • will be useful when discussing MSTs, Dijkstras,
    etc...

3
The Activity-Selection Problem
  • Problem schedule several competing activities
    that require exclusive use of a common resource
  • Could develop a solution similar to that of
    matrix-chain multiplication
  • suppose the activity is included
  • assume that times to the left/right are optimal

4
Greedy Solution
  • Obviously, there are 2n combinations
  • Order activities by increasing finishing time
  • Pick an activity if its non-conflicting
  • Runs in linear time

5
a0
1 1 4
a1
a0
2 3 5
a2
a1
3 0 6
a3
a1
4 5 7
a4
a1
5 3 8
a5
a1
a4
6 5 9
a6
a1
a4
7 6 10
a7
a1
a4
8 8 11
a8
9 8 12
a9
a1
a4
a8
10 2 13
a10
a1
a4
a8
11 12 14
a11
12 ? -
a1
a4
a8
a11
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
6
Elements of a Greedy Algorithm
  • Determine the optimal substructure of the problem
  • Develop a recursive solution
  • Prove that at any stage of the recursion, the
    greedy choice is optimal
  • Show that all but one of the subproblems induced
    by having made the greedy choice arae empty

7
The Knapsack Problems
  • Assume a thief is robbing a store with various
    valued and weighted objects. Her bag can hold
    only W pounds.
  • The 0-1 knapsack problem must take all or
    nothing
  • The fractional knapsack problem can take all or
    part of the item

8
Where Greedy Fails
50
50
50
50
50
20
20 --- 30
30
30
30
20
20
knapsack
20
10
10
10
10
60
220
160
100
120
180
240
9
Huffman Codes
  • A widely used compression algorithm
  • Savings of 20 - 90
  • Uses the frequency of the characters
  • Fixed codes versus variable-length codes
  • Letter freq fixed code variable
  • a 45 000 0
  • b 13 001 101
  • c 12 010 100
  • d 16 011 111
  • e 9 100 1101
  • f 5 101 1101

10
Constructing a Huffman Tree
f5
e9
c12
b13
d16
a45
11
Constructing a Huffman Tree
14
c12
b13
d16
a45
0
1
f5
e9
12
Constructing a Huffman Tree
14
25
d16
a45
0
1
0
1
f5
e9
c12
b13
13
Constructing a Huffman Tree
25
30
a45
1
0
0
1
14
c12
b13
d16
0
1
f5
e9
14
Constructing a Huffman Tree
55
a45
1
0
25
30
1
0
0
1
14
c12
b13
d16
0
1
f5
e9
15
Constructing a Huffman Tree
100
1
0
55
a45
1
0
25
30
1
0
0
1
14
c12
b13
d16
0
1
f5
e9
16
Summary
  • For every greedy algorithm, there is a dynamic
    algorithm
  • Greedy Algorithms make quick (local) decisions
  • After a decision is made, problems of size n-1
    are left
  • Greedy does not always yield an optimal solution
Write a Comment
User Comments (0)
About PowerShow.com