{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ps1num2 - 2 Algorithm Let S be the subsequence and S be the...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
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|, then the maximum number of iterations is |S|, after which it returns and terminates.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}