Optimization Problems. Optimization problem: a problem of finding the best solution from all feasible solutions. Two common techniques: Greedy Algorithms (local) Dynamic Programming (global). Greedy Algorithms. Greedy algorithms typically consist of A set of candidate solutions
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Greedy algorithms typically consist of
Cost ?
O(V+E)
priority queue Q.
Cost:
?
O(V2)
Minimal Spanning Tree (MST) Problem:
Input: An undirected, connected graph G.
Output: The subgraph of G that
(the sum of the values of the edges in the subset is at the minimum)
A greedy algorithm.
Problem:
a,e,i,o,u, blanks, newlines, few q, x, z’s
Solution:
(Can take advantage of relative freq of letters to save space)
E:120
L:42
D:42
U:37
C:32
M:24
K:7
Z:2
Example (cont.)Char
Code
0
1
0
1
1
0
0
1
0
1
0
1
0
1
Complete binary tree
(Can take advantage of relative freq of letters to save space)
 Huffman codes
Char
Code
Freq
C
32
128
D
42
126
E
120
120
M
24
120
K
7
42
L
42
126
U
37
111
Z
2
12
785
Assigning CodesCompare with: 918
~15% less
Code
Bits
1110
101
0
11111
111101
110
100
111100
Char
Code
010000000010
101011100110
Char
Code
10100101
111111001110111101
A set of codes is said to meet the prefix property if no code in the set is the prefix of another. Such codes are called prefix codes.
Huffman codes are prefix codes.
Char
Code
Sort coins denominations by value: c1 < c2 < … < cn.
S
while (x 0) {
let k be largest integer such that ck x
if (k = 0)
return "no solution found"
x x  ck
S S {k}
}
return S
coins selected
CoinChanging: Greedy AlgorithmGreedy algorithm
failed!
ck
All optimal solutionsmust satisfy
Max value of coins1, 2, …, k1 in any OPT
1
1
P 4

2
5
N 1
4
3
10
N + D2
4 + 5 = 9
4
25
Q 3
20 + 4 = 24
5
100
no limit
75 + 24 = 99
CoinChanging: Analysis of Greedy Algorithmk
ck
ck
All optimal solutionsmust satisfy
All optimal solutionsmust satisfy
Max value of coins1, 2, …, k1 in any OPT
Max value of coins1, 2, …, k1 in any OPT
1
1
1
1
P 4
P 9


2
5
N 1
4
2
10
P + D8
9
3
10
N + D2
4 + 5 = 9
3
25
Q 3
40 + 4 = 44
4
25
Q 3
20 + 4 = 24
4
100
no limit
75 + 44 = 119
5
100
no limit
75 + 24 = 99
CoinChanging: Analysis of Greedy AlgorithmKevin’s problem
Dynamic programming
Value
Weight
1
1
1
2
6
2
Let W=11
3
18
5
4
22
6
5
28
7
Fractional Knapsack ProblemAnswer for 01 knapsack problem:
OPT: { 4, 3 }
value = 22 + 18 = 40
> 45
b
c
d
e
f
g
h
Time
0
1
2
3
4
5
6
7
8
9
10
11
Activityselection ProblemGreedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken.
Earliest start time:
Consider jobs in ascending order of start time sj.
Earliest finish time:
Consider jobs in ascending order of finish time fj.
Shortest interval:
Consider jobs in ascending order of interval length fj  sj.
Fewest conflicts:
For each job, count the number of conflicting jobs cj. Schedule in ascending order of conflicts cj.
…
Sort jobs by finish times so that f1 f2 ... fn.
A
for j = 1 to n {
if (job j compatible with A)
A A {j}
}
return A
jobs selected
Interval Scheduling: Greedy AlgorithmTheorem. Greedy algorithm is optimal.
Proof: (by contradiction)
job ir+1 finishes before jr+1
Greedy:
i1
i1
ir
ir+1
ir+1
j1
j2
jr
jr+1
. . .
OPT:
Still optimal with a bigger value than r : ir+1=jr+1 contradiction!
why not replace job jr+1 with job ir+1?
b
c
d
e
f
g
h
Time
0
1
2
3
4
5
6
7
8
9
10
11
Weighted Interval SchedulingGreedy algorithm?
For each town t, St = {towns that are within 30 miles of it}
 a Walmart center at t will cover all towns in St .
while (not all covered)
Pick Si with largest uncovered elements
Proof: Let nt be # of elements not covered after t iterations. There must be a set with ≥ nt/k elements:
nt+1 ≤ nt  nt/k ≤ n0(11/k)t+1 ≤ n0(e1/k)t+1
nt < 1 when t=klnn.