Professional Documents
Culture Documents
2021 05 05 MM Irfan Subakti Greedy Algorithms
2021 05 05 MM Irfan Subakti Greedy Algorithms
司馬伊凡
Мисбакхул Мунир Ирфан Субакти
Greedy Algorithms: Why?
• Optimisation problems → sequence of steps of algorithms, a
set of choices at each steps
• Dynamic programming (DP) → overkill
• Simpler, more efficient algorithms will do
• Greedy algorithm → always makes the choice that looks best
at the moment
• Locally optimal choice → hopefully it will lead to a globally optimal
solution
• {a3, a9, a11} consists of mutually compatible activities → not max set
• {a1, a4, a8, a11} and {a2, a4, a9, a11} → largest subset, max set
• Solving the problem: relationship between greedy alg. & DP
• DP → several choices which subproblems to use in an optimal solution
• Only one choice: the greedy choice → when we make greedy choice, only one
subproblem remains
• Recursive greedy algorithm for the activity-selection problem
• Complete the process of developing a greedy solution by converting the
recursive algorithm to an iterative one
2020/2021(2) - IF184401 Design & Analysis of Algorithms - MM
5
Irfan Subakti
Activity-Selection Problem: Optimal
Substructure
• The activity-selection problem exhibits optimal substructure
• Sij the set of activities that start after activity ai finishes and that finish before activity aj starts
• Goal: max set of mutually compatible activities in Sij, suppose that such a max set is Aij, which
includes some activities ak
• By including ak in an optimal solution → left with 2 subproblems
• Finding mutually compatible activities in Sik
• Activities that start after activity ai finishes & that finish before activity ak starts
• Finding mutually compatible activities in Skj
• Activities that start after activity ak finishes & that finish before activity aj starts
• Aik = Aij ∩ Sik and Akj = Aij ∩ Skj, so that Aik contains the activities in Aij that finish before ak starts
and Akj contains the activities in Aij that start after ak finishes → Aij = Aik ∪ {ak} ∪ Akj, and so the
max-size set Aij of mutually compatible activities in Sij consists of |Aij| = |Aik| + |Akj| + 1 activities
• Cut-and-paste argument shows that the optimal solution Aij must also include optimal solutions
to the 2 subproblems for Sik and Skj
2020/2021(2) - IF184401 Design & Analysis of Algorithms - MM
6
Irfan Subakti
Activity-Selection Problem: Optimal
Substructure (continued)
• Contradiction
• If we could find a set A’kj of mutually compatible activities in Skj where |A’kj| > |Akj| → we
could use A’kj, rather than Akj, in a solution to the subproblem for Sij
• Constructed |Aik| + |A’kj| + 1 > |Aik| + |Akj| + 1 = |Aij| mutually compatible activities →
which contradicts the assumption that Aij is an optimal solution. A symmetric argument
applies to the activities in Sik
• DP: if the size of an optimal solution for the set Sij by c[i, j] → recurrence: c[i, j] =
c[i, k] + c[k, j] + 1
0 𝑖𝑓 𝑆𝑖𝑗 = ∅
• c[i, j] = ቐ max {𝑐 𝑖, 𝑘 + 𝑐 𝑘, 𝑗 + 1} 𝑖𝑓𝑆 ≠ ∅
𝑖𝑗
𝑎𝑘 ∈𝑆𝑖𝑗