(f) Solve the recurrence relation for
L
(
n
)
, to obtain an asymptotic expression for
L
(
n
)
(up to a multiplicative
factor). In other words, find a function
f
(
n
)
such that
L
(
n
)
∈
Θ
(
f
(
n
))
.
Solution:
By the master theorem with
a
=
2,
b
=
4,
c
=
log
4
2
=
1
/
2, and
d
=
0, we have
L
(
n
) =
Θ
(
√
n
)
.
Alternate Solution:
By the master theorem with
a
=
1,
b
=
4,
c
=
log
4
1
=
0, and
d
=
1
/
2, we have
L
(
n
) =
Θ
(
√
n
)
.
(g) The total area of your scheme is given by
A
0
(
n
) =
L
(
n
)
2
. Find an asymptotic expression for
A
0
(
n
)
. In
other words, find a function
f
(
n
)
such that
A
0
(
n
)
∈
Θ
(
f
(
n
))
.
Solution:
We have
A
0
(
n
) =
Θ
(
√
n
)
2
=
Θ
(
n
)
.
(h) Which is better, for large
n
? Charlene’s layout depicted in part (a), or your layout from part (e)?
Solution:
The layout in part (e) is better, since its area grows only linearly with the number of leaves
desired (which is clearly optimal!), while the area of the layout in part (a) grows faster than linearly.
Applications:
The recursive Htree layout is used in chip design, e.g., for communicating a clock signal
to many parts of the chip.
5. (20 pts.)
Pattern matching, with tolerance for noise
We are given binary strings
s
,
t
;
s
is
m
bits long, and
t
is
n
bits long, and
m
<
n
. We are also given an integer
k
. We want to find whether
s
occurs as a substring of
t
, but with
≤
k
errors, and if so, find all such matches.
In other words, we want to determine whether there exists an index
i
such that
s
0
,
s
1
,...,
s
m

1
agrees with
t
i
,
t
i
+
1
,
t
i
+
2
,...,
t
i
+
m

1
in all but
k
bits; and if yes, find all such indices
i
.
(a) Describe an
O
(
mn
)
time algorithm for this string matching problem. Just show the pseudocode; you
don’t need to give a proof of correctness or show the running time.
Solution:
We try matching
s
against
t
at all possible shifts, counting how many errors there are at each
one:
Algorithm Match
(
s
[
0
...
m

1
]
,
t
[
0
...
n

1
]
,
k
)
:
1. Set
M
:
=
{}
.
2. For
i
:
=
0
,...,
n

m
:
3.
Set
e
:
=
0.
4.
For
j
:
=
0
,...,
m

1:
5.
If
s
[
j
]
6
=
t
[
i
+
j
]
, set
e
:
=
e
+
1.
6.
If
e
≤
k
, add
i
to
M
.
7. Return
M
.
The runtime of this algorithm is clearly
O
((
n

m
)
·
m
) =
O
(
nm
)
.
(b) Let’s work towards a faster algorithm. Suggest a way to choose polynomials
p
(
x
)
,
q
(
x
)
of degree
m

1
,
n

1, respectively, with the following property: the coefficient of
x
m

1
+
i
in
p
(
x
)
q
(
x
)
is
m

2
d
(
i
)
,
where
d
(
i
)
is the number of bits that differ between
s
0
,
s
1
,...,
s
m

1
and
t
i
,
t
i
+
1
,
t
i
+
2
,...,
t
i
+
m

1
.
Hint: use coefficients
+
1 and

1.
Solution:
Define
p
(
x
) =
m

1
∑
i
=
0
(

1
)
s
[
m

1

i
]
x
i
CS 170, Fall 2014, Sol 3
5
and
q
(
x
) =
n

1
∑
i
=
0
(

1
)
t
[
i
]
x
i
.
Multiplying, we get
p
(
x
)
·
q
(
x
) =
m

1
∑
i
=
0
n

1
∑
j
=
0
(

1
)
s
[
m

1

i
]+
t
[
j
]
x
i
+
j
.
Therefore the coefficient of
x
m

1
+
‘
in
p
(
x
)
·
q
(
x
)
is
∑
i
+
j
=
m

1
+
‘
0
≤
i
<
m
0
≤
j
<
n
(

1
)
s
[
m

1

i
]+
t
[
j
]
=
m

1
∑
i
=
0
(

1
)
s
[
m

1

i
]+
t
[
m

1

i
+
‘
]
=
m

1
∑
i
=
0
(

1
)
s
[
i
]+
t
[
‘
+
i
]
=
m

2
d
(
‘
)
,
using the fact that the sum of
m

d
(
‘
) +
1’s and
d
(
‘
)

1’s is
m

2
d
(
‘
)
. Also, we have used the fact
that
(

1
)
y
+
z
is
+
1 if
y
=
z
and

1 if
y
6
=
z
(when
y
,
z
are bits).
Alternatively, you could have written your answer as follows. Define
p
(
x
) = (

1
)
s
[
m

1
]
+(

1
)
s
[
m

2
]
x
+(

1
)
s
[
m

3
]
x
2
+
···
+(

1
)
s
[
0
]
x
m

1
q
(
x
) = (

1
)
t
[
0
]
+(

1
)
t
[
1
]
x
+(

1
)
t
[
2
]
x
2
+
···
+(

1
)
t
[
n

1
]
x
n

1
.