{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2c03-review - 00013

# 2c03-review - 00013 - Solution type 2(specific For example...

This preview shows page 1. Sign up to view the full content.

3 solution 2 The average code length is 0.08 × 3+0.12 × 3+0.2 × 3+0.2 × 3+0.4 × 1=2.2 3.[15] a) 1 2 3 4 5 6 a b a a b a f(i) 0 0 1 1 2 3 g(i) 0 1 1 2 2 3 not needed for (a). b) solution type1 (general) First note that if n=1 then mn = m < m+1 = m+n, so an algorithm with complexity O(m+n) could be sometimes slower than an algorithm with complexity O(mn). Consider any case when |pat|=1. Then the naïve algorithm and the second part of KMP are identical, but KMP requires computing the functions f and g first, so its complexity is greater.
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Solution type 2 (specific) For example, the string x is ababab, the pattern is “c”, so f(1)=0, g(1)=0, in this case, if we use naive algorithm, we need 6 times comparing, and if we use KMP algorithm, we need 6 times comparing and 1 time computing for g(1), so the total time is O(m+n)=6+1=7, it is greater than naive algorithm which is O(mn)=6. 4.[10] a. Consider a tree 1 / \ 2 3 / | \ | 4 5 6 7...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online