This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 2. Algorithm : Let S be the subsequence and S be the set that we want to check if S is a part of, then the following algorithm will check whether S is in S. 1. function f(S', S): 2. S'_i = 0 3. S_i = 0 4. while S_i < cardinality of S and S'_i < cardinality of S': 5. if S[S_i] == S'[S'_i]: 6. S'_i = S'_i + 1 7. S_i = S_i + 1 8. return S'_i == cardinality of S' Complexity analysis : We look at the while loop and notice that S_i increments by one on every iteration and that it is one of the induction variables in the loop condition of S_i < |S|. Since the precondition of the function ensures that |S| < |S|, the worst case number of iterations is |S|, hence the while loop terminates at worse after |S| steps, each of which consists of a finite number of idealistically constant time operations, so the worst case runtime complexity of f is O(n) where n is the cardinality of |S|. Proof : a. Termination : Since S_i increments on each iteration, and the loop terminates once S_i is no longer < |S|,...
View Full Document
- Spring '08