Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage....
Transcript of Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage....
![Page 1: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/1.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/2.jpg)
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 Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/3.jpg)
Greedy Algorithm vs Dynamic Programming
DynamicProgramming
GreedyAlgorithm
![Page 4: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/4.jpg)
Knapsack Problem
n items a thief’s knapsack of
size W
![Page 5: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/5.jpg)
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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/6.jpg)
Knapsack Problem
453
0201
0$60 $10
0$120
$135
knapsack($6/
unit)($5/unit)
($4/unit)
($3/unit)
n = 4,W = 50
50
![Page 7: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/7.jpg)
Fractional Knapsack Problem
5045
30
20 1
0$60
$100
$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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/8.jpg)
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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/9.jpg)
0-1 Knapsack Problem
optimal
50 50
value
50
valueper unit
50
10
20
45
30
20
$160
$135
$220
Difficult to get the optimal solution with a greedy strategy.
Dynamic Programming :
![Page 10: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/10.jpg)
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 Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/11.jpg)
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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/12.jpg)
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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/13.jpg)
Huffman Codes
A lossless data compression algorithm.
It uses variable-length code
![Page 14: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/14.jpg)
Variable-Length Code
Six characters : a, b, c, d, e, f
How can we represent them with binary strings?
a b c d e f
Fixed-length
000 001 010 011 100 101
![Page 15: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/15.jpg)
Variable-Length Code
Six characters : a, b, c, d, e, f
How can we represent them with binary strings?
a b c d e f
Variable-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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/16.jpg)
Prefix Code
Six characters : a, b, c, d, e, f
How can we represent them with binary strings?
a b c d e f
Variable-length
0 101 100 111 1101 1100
No codeword is a prefix of another codeword.
![Page 17: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/17.jpg)
Prefix Code
Six characters : a, b, c, d, e, f
How can we represent them with binary strings?
a b c d e f
Variable-length
0 101 100 111 1101 1100
No codeword is a prefix of another codeword.
![Page 18: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/18.jpg)
Variable-Length Code
a b c d e f Total
Frequency 45 13 12 16 9 5 100
Fixed-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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/19.jpg)
Variable-Length Code
a b c d e f Total
Frequency 45 13 12 16 9 5 100
Fixed-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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/20.jpg)
Huffman Tree
100cha
rcode
freq
a 000 45
b 001 13
c 010 12
d 011 16
e 100 9
f 101 5
Fixed-length code
86 14
58 1428
a: 45
b: 13
c: 12
d: 16
e: 9
f: 5
0 1
0 1 0
0 1 0 1 0 1
![Page 21: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/21.jpg)
Huffman Tree
char
code
freq
a 0 45
b 101 13
c 100 12
d 111 16
e 1101
9
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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/22.jpg)
Huffman’s Algorithm
A
B
0
0
Every non-leaf node has two children.
B
0
Observation 1The longest code : at least 2 characters
![Page 23: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/23.jpg)
Huffman’s Algorithm
C H
I J
G10
A10
F10
Observation 1
Observation 2
The longest code : at least 2 charactersThe longest 2 codes : the least frequent two characters
D E
B10
![Page 24: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/24.jpg)
Huffman’s Algorithm
Observation 1
Observation 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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/25.jpg)
Huffman’s Algorithm
d: 1614
30 x: 30
A non-leaf node : handled like a leaf node
Observation 3
e: 9 f: 5
10
T T ’
10
Total length in T = Total length in T ‘ + 16 * 1 + (9 + 5) * 2
![Page 26: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/26.jpg)
Huffman’s Algorithm
Observation 1
Observation 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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/27.jpg)
Huffman’s Algorithm
a: 45 b: 13 c: 12 d: 16 e: 9 f: 5
Merging two least frequent nodes.
![Page 28: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/28.jpg)
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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/29.jpg)
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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/30.jpg)
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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/31.jpg)
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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/32.jpg)
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 0
b 101
c 100
d 111
e 1101
f 1100
![Page 33: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/33.jpg)
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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/34.jpg)
S
Graphs
![Page 35: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/35.jpg)
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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/36.jpg)
Directed and Undirected
1 2
3 4
5
Directed graph
1 2
3 4
5
Undirected graph
![Page 37: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/37.jpg)
Representations of Graphs
1 2
3 4
5
Directed graph
1
2
3
4
5
2 /
3
1
2
2 /
4 /
3 /
3 /
Adjacency-list
![Page 38: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/38.jpg)
Representations of Graphs
1 2
3 4
5
Directed graph Adjacency-Matrix1 2 3 4
5
5
3
4
2
1
![Page 39: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/39.jpg)
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. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/40.jpg)
Adjacency Matrix of Graphs
1 2
3 4
5
Directed graph Adjacency-Matrix1 2 3 4
5
5
3
4
2
1
![Page 41: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/41.jpg)
Adjacency Matrix of Graphs
A = AT =
![Page 42: Greedy Algorithms. Greedy Algorithm Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum.](https://reader038.fdocuments.net/reader038/viewer/2022110101/56649eb55503460f94bbdfc7/html5/thumbnails/42.jpg)
Weighted Graphs
1 2
3 4
5
1.7
0.42.0
-0.3
3.1
-0.23.6-
2.1