Anda di halaman 1dari 9

The activity selection problem

Given a set S = {1, 2, . . . , n} of activities that wish to use a


resource, which can be used only by one activity at the time.
Activity i has a start time si and a finishing time fi , fi > si .
Activities i and j are comparable if [si fi ) ∩ (sj fj ] = ∅.
The goal is to maximize the set of mutually compatible activities.
Notice, F = {A|A ⊂ S, A is compatible } and f (A) = |A|.
Example.

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)

A boolean variable x can take values 0,1. A litteral is a is a


boolean variable x or x̄. A clause is a disjunction of litterals.
An instance of SAT: X = {x1 , . . . , xn }, a set of clauses {Ci }m
i=1 .
and a boolen formula F in Conjuntive Normal Form (CNF). The
problem consists in obtaining a truth assignment of X :
(φ : X → {0, 1}) s.t.
m
^
φ(F ) = φ(Ci ) = 1.
i=1

Notice: for F = 1, ⇒ ∀i, φ(Ci ) = 1 ⇒ ∃x ∈ Ci , φ(x) = 1.


Horn formulae

A SAT instance, where each Ci has at most one positive literal.


Example:

(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

Recall that clauses with one positive literal can be rewritten as


implications:

(x ∨ ȳ ∨ z̄ ∨ w̄ ) ≡ (y ∧ z ∧ w ∧) → x

So the previous example can be rewritten as


(x1 → x2 ), (x1 ∧ x3 → x4 ), (x2 ∧ x3 ∧ x4 → x1 ), (→ x1 ),
(x̄1 ∨ x¯2 ∨ x̄4 ), (x̄3 ), (x1 ∧ x2 → x4 ).
Give boolean formulaes A and B, A → B is false iff A = 1 and
B = 0 . Therefore, if we have a → defined from a clause with no
negations in a Horn formulae, for the implication to be false, all
variables at the left must be 1 and the variable to the right must
be 0

(x2 ∧ x3 ∧ x4 → x1 )
This fact can be used to develop a greedy algorithm for Horn
formulae
Linear time Greedy algorithm for Horn formulae

Given a Horn formulae F :

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:

(x1 → x2 ), (x1 ∧ x3 → x4 ), (x2 ∧ x3 ∧ x4 → x1 ), (→ x1 ),


(x̄1 ∨ x¯2 ∨ x̄4 ), (x̄3 ), (x1 ∧ x2 → x4 )
Greedy-Horn will make φ(x1 ) = φ(x2 ) = φ(x3 ) = φ(x4 )
then φ(x1 ) = 1 because (→ x1 ),
then φ(x2 ) = 1 because (x1 → x2 ),
then φ(x4 ) = 1 because (x1 ∧ x2 → x4 ),
But then (x̄1 ∨ x¯2 ∨ x̄4 ) = 1.
Therefore, the original formula has no truth assignment.

Anda mungkin juga menyukai