Docsity

Activity Scheduling, Coin Changing - Notes | CSCI 212, Study notes of Computer Science

Toggle sidebar
bg1
CSCI 212 - Class Notes
02/17/09
Activity Scheduling Problem
Given: A set of nactivities A={ai←(si, fi)}n
i=1.
Objective: Find a schedule S⊆Aof activities with maximum cardinality that do not overlap.
The start time and finish time of activity aiare denoted by siand fi, respectively. Activities i
and joverlap if si< fjand sj< fi. Our algorithm initially sorts the activities by finish time (and
possibly relabels them) such that f1≤f2≤. . . ≤fn. The earliest finishing activity is added to the
schedule. The remaining activities are iterated over. Activities that overlap with the most recently
scheduled activity are ignored, while the next first-finishing activity that does not overlap with the
most recently scheduled activity is added to the schedule. The resulting schedule is optimal, i.e.,
it has a maximum number of activities.
Schedule(n)
1. Sort(A).
2. i←1, S← {a1}.
3. For j←2 to n,
If sj≥fi,S←S∪ {aj}and i←j.
4. Return S.
Iterating through all activities takes O(n) time complexity. However, the sorting has a time com-
plexity of O(nlog n). Therefore, the total time complexity is O(nlog n). We now prove the opti-
mality of the solution returned by our algorithm.
Proof: We must show that (1) some optimal schedule contains a1, i.e., the first-finishing activity
and (2) an optimal schedule for nactivities that includes the first-finishing activity a1is composed
of an optimal solution for the subproblem A0=A\(a1∪ {ai|si< f1}).
(1) Let S⊆Abe an optimal schedule such that a1/∈S. Therefore, there must exist some other
first-finishing activity ak∈A. By construction, we have fk≥f1, i.e., activity akwill finish at the
same time or later than a1. We can define a new solution S0= (S\{ak})∪ {a1}by replacing the
first-finishing activity in Swith a1.S0has the same cardinality as S. Further, f1≤fkimplies that
there are no new conflicts with the remaining elements. Therefore, there exists an optimal schedule
S0that contains the first-finishing activity a1.
(2) Assume an optimal solution for Acontains ksegments, including the first-finishing segment
a1. It must be the case that k−1 is the maximum number of segments for the subproblem
A0=A\(a1∪ {ai|si< f1}). Suppose that there exists a solution for A0that includes > k −1
segments. We could then replace our original (k−1)-segment schedule with the (> k −1)-segment
schedule. The resulting schedule includes >(k−1)+1 = ksegments in total. This contradicts are as-
sumed k-segment optimal schedule. Therefore, an optimal schedule that includes the first-finishing
1
pf3
pf4
pf5

Partial preview of the text

Download Activity Scheduling, Coin Changing - Notes | CSCI 212 and more Study notes Computer Science in PDF only on Docsity!

CSCI 212 - Class Notes

Activity Scheduling Problem

Given: A set of n activities A = {ai ← (si, fi)}ni=1. Objective: Find a schedule S ⊆ A of activities with maximum cardinality that do not overlap.

The start time and finish time of activity ai are denoted by si and fi, respectively. Activities i and j overlap if si < fj and sj < fi. Our algorithm initially sorts the activities by finish time (and possibly relabels them) such that f 1 ≤ f 2 ≤... ≤ fn. The earliest finishing activity is added to the schedule. The remaining activities are iterated over. Activities that overlap with the most recently scheduled activity are ignored, while the next first-finishing activity that does not overlap with the most recently scheduled activity is added to the schedule. The resulting schedule is optimal, i.e., it has a maximum number of activities.

Schedule(n)

  1. Sort(A).
  2. i ← 1, S ← {a 1 }.
  3. For j ← 2 to n,

If sj ≥ fi, S ← S ∪ {aj } and i ← j.

  1. Return S.

Iterating through all activities takes O(n) time complexity. However, the sorting has a time com- plexity of O(nlog n). Therefore, the total time complexity is O(nlog n). We now prove the opti- mality of the solution returned by our algorithm.

Proof: We must show that (1) some optimal schedule contains a 1 , i.e., the first-finishing activity and (2) an optimal schedule for n activities that includes the first-finishing activity a 1 is composed of an optimal solution for the subproblem A′^ = A(a 1 ∪ {ai|si < f 1 }).

(1) Let S ⊆ A be an optimal schedule such that a 1 ∈/ S. Therefore, there must exist some other first-finishing activity ak ∈ A. By construction, we have fk ≥ f 1 , i.e., activity ak will finish at the same time or later than a 1. We can define a new solution S′^ = (S{ak}) ∪ {a 1 } by replacing the first-finishing activity in S with a 1. S′^ has the same cardinality as S. Further, f 1 ≤ fk implies that there are no new conflicts with the remaining elements. Therefore, there exists an optimal schedule S′^ that contains the first-finishing activity a 1.

(2) Assume an optimal solution for A contains k segments, including the first-finishing segment a 1. It must be the case that k − 1 is the maximum number of segments for the subproblem A′^ = A(a 1 ∪ {ai|si < f 1 }). Suppose that there exists a solution for A′^ that includes > k − 1 segments. We could then replace our original (k − 1)-segment schedule with the (> k − 1)-segment schedule. The resulting schedule includes > (k−1)+1 = k segments in total. This contradicts are as- sumed k-segment optimal schedule. Therefore, an optimal schedule that includes the first-finishing

segment a 1 includes an optimal solution to the remaining subproblem A′^ = A(a 1 ∪ {ai|si < f 1 }).

By (1) and (2), there exists an optimal schedule that combines the first-finishing segment a 1 with an optimal solution to the remaining subproblem A(a 1 ∪ {ai|si < f 1 }). Therefore, our greedy algorithm is optimal.

Coin Changing Problem

Given: Coins with values of 1, 5, 10, 25, and 50, and number of cents n. Objective: Make change for n cents such the total number of coins used is minimized.

Our algorithm greedily includes as many coins of each value, in decreasing order of value, as possi- ble without exceeding n cents. The solution returned is a 5-tuple representing the optimal (fewest) quantity of coins used. Its elements correspond to the quantity of coins with values 1, 5, 10, 25, and 50, respectively. The time complexity is O(1).

Change(n)

  1. f ← b 50 n c, nf ← n%50.
  2. q ← b n 25 f c, nq ← nf %25.
  3. d ← b n 10 q c, nd ← nq%10.
  4. k ← b n 5 d c, nk ← nd%5.
  5. Return (nk, k, d, q, f ).

Proof: Let G = (g 1 , g 2 ,... , g 5 ) be our greedy solution and let P = (p 1 , p 2 ,... , p 5 ) be an optimal solution such that

i=1 pi^ <^

i=1 gi.^ Let^ k^ denote the index of the rightmost quantity where gk 6 = pk, i.e., the largest coin value where the quantities differ. It must be the case that gk > sk. Otherwise, gk = sk or gk < sk, which contradicts our assumption on them differing or yields a number of cents greater than n. Observe than an optimal solution necessarily has 0 ≤ nk ≤ 4, 0 ≤ k ≤ 1, 0 ≤ d ≤ 2, and 0 ≤ q ≤ 1. Otherwise, a solution S′^ using less coins could be formed (e.g., 3 value 10 coins replaced by 1 value 5 and 1 value 25 coin). Consider all possible values of the index k. If k = 5, at least 50 must compensated for by the quantity of coins in p 1 , p 2 ,... , p 4. At least 4 additional coins must be used, which implies there exists a solution S′^ with at least three coins less than the optimal solution P by using one value 50 coin. Similarly, if k = 4, a solution S′ exists with at least two coins less than P by using one value 25 coin. For k = 3, at least a value of 10 must be compensated for by coins in p 1 and p 2. However, the highest value that an optimal solution can obtained with such coins is 9. The case for k = 2 is similar. For k = 1, the optimal solution cannot obtain a total of n cents. We see that each value of k for an optimal solution P that uses less coins than G leads to a contradiction, which implies that such a solution P with less coins than G cannot exist. Therefore, our greedy solution is optimal.

Proof (alternative): Consider an alternative Change(n) algorithm that includes a coin of largest value c, with c ≤ n, and recursively solves the problem for n − c cents. We must show that (1) some optimal solution contains a coin of largest value c ≤ n and (2) an optimal solution for n cents that includes a coin of value c is composed of an optimal solution for n − c cents.

We apply the Knuth-Morris-Pratt (KMP) algorithm which solves the problem in O(n) time. As an example, consider the problem of finding a pattern p = 010100 in some text t. It is possible to construct a finite state machine (FSM) that finds p such as the one shown in Fig. 1. Observe the following properties of the FSM. When a match occurs, a transition to a state with a higher index i is made. The next text character is then compared with the character dictating the transitions from the new state. When a mismatch occurs, the next text character is not necessarily checked immediately. Instead, the FSM transitions to a state at least one index lower than the current state and compares the current text character with another pattern character. The backward transitions occur until either a match is found or a mismatch occurs in state i = 0, i.e., with the first character of the pattern, at which point the next text character is checked.

(^0 1 010 ) i=0 i=1 i=2 i=3 i=4 i=5 i=

mismatch

mismatch

mismatch

mismatch

mismatch/ mismatch next text

Figure 1: A FSM that searches for the pattern 010100.

The states that the backward transitions transition to are determined by properties of the pattern. A mismatch at state i = k implies that we’ve already matched the first k characters of the pattern. The characters form a prefix of the pattern. It also implies that we’ve mismatched on what would have been the k + 1 character of our prefix of the pattern. Therefore, we transition back to a state i = h, where h is the length of the longest substring of the length k pattern prefix that is both a prefix and suffix of the pattern prefix. The reason is that after mismatching with the k + 1 pattern character we do not necessarily have to return to the beginning of the pattern. Instead, we are allowed to attempt to find a match with the h + 1 pattern character. The reason is that the last h characters of the k-character pattern prefix we matched can also form the first h characters of a new pattern match. Such a match is possible because our k-character pattern prefix contained a length h substring that is both a prefix and a suffix of it. KMP finds the longest such substring for each possible length of pattern prefix during the preprocessing phase described below.

Preprocess()

  1. i ← 0, j ← −1, s[i] ← j.
  2. While i < m,

While (j ≥ 0) ∧ (p[i] 6 = p[j]), j ← s[j]. i ← i + 1, j ← j + 1. s[i] ← j.

The element p[i], with 0 ≤ i ≤ m − 1, is the ith character of the length m pattern p. The element s[i] contains the length of the longest substring that is both a prefix and a suffix of a prefix of length i of the pattern. The algorithm iterates through each of the m elements of the pattern. Successive longest prefix/sufix substring lengths s[i + 1] are computed by checking if p[s[i]] = p[i],

i.e., if the character after the longest prefix/suffix substring is equal to the i + 1 character of the pattern. If not, it keeps checking the remaining longest prefix/suffix substrings in decreasing length similarly. It stops when the character after a longest prefix/suffix substring matches or no more substrings remain. For our example pattern p, we obtain s[0... m] = {− 1 , 0 , 0 , 1 , 2 , 3 , 1 }. Observe how s[0... m] compactly represents the backwards transitions of our FSM. Note that the value s[m] = 1 would be used if we wanted our FSM to find multiple occurrences of the pattern.

The outer while loop executes for m iterations and the inner loop iterates until we decrease j past

  1. Each time it executes it decreases j by at least 1. Observe that j is only increased (by 1) in each iteration of the outer loop. Since j cannot be decreased more times than it has been increased, the number of iterations of the iterations of the inner loop is at most m. One comparison is made during each iteration of the loop. Therefore, the time complexity of the KMP preprocessing is O(m).

Searching for the pattern is similar to the preprocessing. The basic idea is to view the text as the concatenation of the pattern and the text. For our example, we would have 010100 t[0..n − 1]. Therefore, an occurrence of the pattern in the text is found when we find for some prefix of length i + 1 of the text a longest prefix/suffix subsequence of length m. KMP searches as described below.

Search()

  1. i ← 0, j ← 0.
  2. While i < n,

While (j ≥ 0) ∧ (t[i] 6 = p[j]), j ← s[j]. i ← i + 1, j ← j + 1. If j = m, return i − j. // the index of the first character of the match in the text

The analysis of Search() follows that of Preprocess(). However, here we increase j up to n times in the outer while loop. Using similar reasoning as before, the time complexity of Search() is O(n). With preprocessing, KMP has a time complexity O(m + n). Therefore, when m ≤ n, the time complexity of KMP is O(n).