0% found this document useful (0 votes)
10 views20 pages

Week6 Chap4 Greedy

Uploaded by

Seriyuu Hòa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

Week6 Chap4 Greedy

Uploaded by

Seriyuu Hòa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

APPLIED ALGORITHMS

GREEDY ALGORITHMS

1
CONTENT

• Basis of Greedy algorithms


• Coin exchange
• Knapsack
• Disjoint segments

2
Basis of Greedy algorithms

• Notations Greedy() {
• 𝑺: solution under construction S = ;
• 𝑪: set of candidates while C   and not solution(S){
x = select(C);
• 𝒔𝒆𝒍𝒆𝒄𝒕(𝑪): select a potential candidate for inserting to
C = C \ {x};
the solution
if feasible(S  {x}) {
• 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏(𝑺): return TRUE if S is a complete accepted S = S  {x};
solution, and return FALSE, otherwise }
• 𝒇𝒆𝒂𝒔𝒊𝒃𝒍𝒆(𝑺): return TRUE if 𝑆 does not violate given }
constraints, and return FALSE, otherwise return S;
}

3
Basis of Greedy algorithms

• Travelling Salesman Problem (TSP) GreedyTSP() {


• Find the shortest closed tour starting from point 1, S = [1]; cur = 1;
visiting 2, . . ., n (each point is visited exactly once) and C = {2, 3, . . ., n};
while C   do {
coming back to 1.
x = selectNearest(C, cur);
• Nearest neighbor selection
C = C \ {x};
• At each step, find the nearest point to the current S = S::x; // append S with x
point (the last point of the solution under cur = x;
construction) }
return S;
}

4
Basis of Greedy algorithms

• In general, greedy algorithms cannot ensure to find optimal solutions in all cases
• In some cases, there exist greedy algorithms and can find optimal solutions
• Coin exchange with denominations 1, 2, 5, 10
• Disjoint segments
• Kruskal, Prim algorithms for finding minimum spanning tree of a given undirected weighted graph
• Huffman code

5
Coin exchange problem

• Given coins of denominations 1, 2, 5, 10. Greedy(Y) {


D = [1, 2, 5, 10];
Given a positive integer Y, how to get an
res = [];
amount of money Y from the coins such that
while Y > 0 do {
the number of coins used is minimal.
x = select max item from D such that x  Y;
Y = Y - x;
res = res::x; // append res with x
}
return res;
}

6
Knapsack Problem

• There are n items S ={1, 2, 3, . . ., n}. Item j has weight Wj and value Cj (j = 1, 2, . . . n). Given a bin
with capacity B. Select a subset S of items from the given items such that the sum of weights of
items of S is not greater than B and the sum of values of the items is maximal.

7
Knapsack Problem: Greedy 1

• Sort the items in a non-increasing of values Greedy1(B, W, C) {


• Explore the items from left to right in the L = sort items in a non-increasing of values;
sorted list, select the item if it can be res = {};
inserted into the bin without violating the for j in L do {
capacity constraint
if Wj <= B then {
res = res  {j}; B = B – Wj;
}
}
return res;
}

8
Knapsack Problem: Greedy 1

• Counter example
• Number of items n = 3
• Capacity of the bin B = 19

Items 1 2 3
Ci 20 16 8
Wi 14 6 10

• Solution returned by Greedy1: S1 = {1} with total values 20


• Optimal solution S* = {2, 3} with total values 24

9
Knapsack Problem: Greedy 2

• Sort the items in a non-decreasing of Greedy2(B, W, C) {


weights L = sort items in a non-decreasing of weights;
• Explore the items from left to right in the res = {};
sorted list, select the item if it can be for j in L do {
inserted into the bin without violating the
capacity constraint if Wj <= B then {
res = res  {j}; B = B – Wj;
}
}
return res;
}

10
Knapsack Problem: Greedy 2

• Counter example
• Number of items n = 3
• Capacity of the bin B = 11

Items 1 2 3
Cj 10 16 28
Wj 5 6 10

• Solution returned by the Greedy2: S2 = {1, 2} with total values 26


• Optimal solution S* = {3} with total values 28

11
Knapsack Problem: Greedy 3

• Sort the items in a non-increasing of Greedy3(B, W, C) {


weights: 𝑪𝒊
L = sort items in a non-increasing of ;
𝑪𝟏 𝑪𝟐 𝑪𝒏 𝑾𝒊
≥ ≥... res = {};
𝑾𝟏 𝑾𝟐 𝑾𝒏
• Explore the items from left to right in the for j in L do {
sorted list, select the item if it can be if Wj <= B then {
inserted into the bin without violating the res = res  {j}; B = B – Wj;
capacity constraint
}
}
return res;
}

12
Knapsack Problem: Greedy 3

• Counter example
• Number of items n = 2
• Capacity of the bin B ≥ 2

Item 1 2
Ci 10 10B – 1
Wi 1 B
𝐶1 10 10𝐵−1 𝐶2
• Clearly: = ≥ =
𝑊1 1 𝐵 𝑊2
• Solution returned by the Greedy3: S3 = {1} with value 10
• Optimal solution S* = {2} with value 10B - 1

13
Knapsack Problem: Greedy 4

• Let Sj be the solution obtained by Greedyj, (j = 1, 2, 3). Let S4 be the best solution
among S1, S2, S3:
1
• Then we have σ𝑖 𝑆4 𝐶𝑖 ≥ 𝑂𝑃𝑇 (in which OPT is the total values of the optimal
2
solution)

14
Knapsack Problem: Greedy 4

• Counter example
• Number of items n = 4
• Capacity of the bin B = 11

Items 1 2 3 4
Ci 9 10 18 27
Wi 4 5 6 10
Ci/Wi 2.25 2 3 2.7
Greedyi 27 19 27 7

• Solution returned by the Greedy4 has value 27


• Optimal solution S* = {2, 3} with value 28

15
Disjoint segments

• There are n jobs 1, 2, . . ., n. Job j starts at time-point Sj and finishes at time-point Fj


• Two jobs i and j are compatible if [Si, Fi] and [Sj, Fj] are not overlap.
• Goal: Find a subset of the given jobs such that all pair of two jobs of the are pair-
wise compatible.

16
Greedy algorithm ideas

• Greedy 1: Sort the jobs in non-decreasing order of start time Si.


• Greedy 2: Sort the jobs in non-decreasing order of finish time Fi.
• Greedy 3: Sort the jobs in non-decreasing order of duration Fi – Si.
• Greedy 4: Sort the jobs in non-decreasing order of conflict (conflict of a job j is the
number of jobs that are not compatible with j)

17
Greedy algorithm ideas

• Counter examples

Greedy 1

Greedy 3

Greedy 4

18
Disjoint segments: Greedy 2 is correct

• Algorithm Greedy 2 ensures to Greedy2([S1, F1], . . ., [Sn, Fn]) {


find an optimal solution L = sort segments in a non-decreasing of 𝑭𝒋;
• Độ phức tạp O(nlog n) res = {};
for j in L do {
if [Sj, Fj] is compatible with segments in res then {
res = res  {j};
}
}
return res;
}

19
THANK YOU !

20

You might also like