Activity (A): 1 2 3 4 5 6 7 8
Start (s): 3 2 2 1 8 6 4 7
Finish (f): 5 5 3 5 9 9 5 8
4
3 7 8
2 6
1 5
1 2 3 4 5 6 7 8 9 10
The Activity Selection problem.
Given a set A of activities, wish to maximize the number of
compatible activities.
Activity selection A
Sort A by increasing order of fi
S := {1}
j := 1
from i = 2 to n do
if si > fj
S := S ∪ {i} and j := i
return S.
A:3127856
f :3555899
SOL: 3 1 8 5
4
3 7 8
2 6
1 5
Theorem
The previous algorithm produces an optimal solution to the activity
selection problem.
Proof.
Given A = {1, . . . , n} sorted by finishing time, we must show there
is an optimal solution that begins with activity 1. Lest
S = {k, . . . , m} be a solution. If k = 1 done.
Otherwise, define B = (S − {k}) ∪ {1}. As f1 ≤ fk the activities in
B are disjoint. As |B| = |S|, B is also an optimal solution.
If S is an optimal solution to A, then S 0 = A − {1} is an optimal
solution for A0 = {i ∈ A|si ≥ fi }.
Therefore, after each greedy choice we are left with an optimization
problem of the same form as the original. Induction on the number
of choices, the gredy strategy produces an optimal solution
The satisfiability problem (SAT)
(x̄1 ∨x2 )∧(x̄1 ∨x̄3 ∨x4 )∧(x1 ∨x̄2 ∨x̄3 ∨x̄4 )∧(x1 )∧(x̄1 ∨x̄2 ∨x̄4 )∧(x̄3 )∧(x̄1 ∨x̄2 ∨x4
(x ∨ ȳ ∨ z̄ ∨ w̄ ) ≡ (y ∧ z ∧ w ∧) → x
(x2 ∧ x3 ∧ x4 → x1 )
This fact can be used to develop a greedy algorithm for Horn
formulae
Linear time Greedy algorithm for Horn formulae
Greedy Horn F
for all i ∈ [n] do T (xi ) = 0
while there is false implication · · · → y
y := 1
if all clauses with negatives are 1
return F = 1
else F = 0.
Example: