2c03-review - 00013

2c03-review - 00013 - Solution type 2 (specific) For...

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

View Full Document Right Arrow Icon
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.
Background image of page 1
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

This note was uploaded on 12/10/2009 for the course CAS 2c03 taught by Professor Janicki during the Spring '03 term at McMaster University.

Ask a homework question - tutors are online