
CSE 202 Homework 2
Combinatorial Search and Optimization:
Dynammic Programming and Greedy Algorithms
1. Single Word Reusable Boggle (SWoRB)
Problem Youaregivenan
n
n
matrix of letters from a finite alphabet
, and a target word
w
in
of length
m
. You want to determine whether there is a (not necessarily simple, i.e. letters can be reused) path in the
grid so that the letters along that path are
w
. Each step in the path can go from a point in the grid to any of
its 8 neighboring points, including diagonal moves. Give an efficient algorithmto play SWoRB.
Solution The important thing to note when spelling a word on the SWoRB matrix is that the exact path history is
not important, only your position on the matrix and how far into the word you havegone. So, intuitively,we
only need maintain information on the polynomially many matrix position
word position subproblems.
The subproblems will be exactly those: an
n
n
m
array,
S
, with element
S
(
i; j; k
)
indicating if it is
possible to spell word
f
w
1
:::w
k
g
ending at position
(
i; j
)
on the matrix.
The computation begins by initializing the first plane of the matrix
f
S
(
i; j;
1) :
i; j
n
g
with true if
M
(
i; j
)=
w
1
, false otherwise. When all elements
S
(
:; :; k
)
are filled in, elements of
f
S
(
i; j; k
+1) :
i; j
n
g
are set to
true
if
M
(
i; j
)=
w
k
+1
and any of the neighbors
f
S
(
i
0
;j
0
;k
):
i
0
2f
i
1
;i;i
+1
g
;j
0
2
f
j
1
;j;j
+1
g
;i
0
6
=
i
_
j
0
6
=
j
g
are set to true. The order of evaluation within a plane does not matter
(i.e.any
S
(
i; j; k
)
may be evaluted before any other
S
(
i
0
;j
0
;k
)
), but all
S
(
:; :; k
)
must be evaluated before
proceeding to
S
(
:; :; k
+1)
. The answer to the problem is
yes
if any of the elements
S
(
:; :; m
)
are true.
There are
n
n
m
elements in this subproblem array, and each requires a constant number of comparisons
or lookups (
9
) to determine its value. So, the running time is
O
(
n
2
m
)
. If we wanted to generate
an example word path, we could pick any ending element, (i,j), such that
S
(
i; j; m
)
is true, and proceed
backwards selecting any true neighbor on the previousplane. This could not be done in a forward direction,
since a
true
at position
S
(
i; j; k
)
does not guarantee that this word path will continue.
Note: A similar problem is that of counting the number of paths through
M
that form a word
w
. This could
be accomplished by a similar algorithm that used the same subproblems, but stored a count of successful
paths to a particular point instead of simply if one existed or not. The final step would sum over all
counts on the final plane. This could be further extended to randomlysample from the paths uniformly by
selecting each backward step with probability proportionalto its share of the paths.
2. Problem 16-3 from text (p. 325)
Let
c
copy
,
c
delete
,
c
replace
,
c
insert
,
c
twiddle
and
c
kill
be the costs of the operations. Let
ED
(
s
1
s
2
::s
k
;t
1
t
2
::t
l
)
be the edit distance from
s
to
t
,andlet
ED
op
be the edit distance given that we perform operation
op
first. Then
we get the recursive definitions:
(a)
ED
(
s; t
)=0
if k
=
l
=0
; min
op
ED
op
(
s; t
)
otherwise
,
(b)
ED
delete
(
s
1
::s
k
;t
1
::t
l
)=
c
delete
+
ED
(
s
2
; ::s
k
;t
1
::t
l
)
(if
k>
0
, infinity otherwise);
(c)
ED
replace
(
s
1
::s
k
;t
1
::t
l
) =
c
replace
+
ED
(
s
2
::s
k
;t
2
::t
l
)
if
k; l >
0
, infinity otherwise. (Note: this is
going from the use of replace in their example, where the replaced symbol is automatically copied to the
target and hence must be
t
1
. Other interpretations make the algorithm rather more complicated.)
(d)
ED
copy
(
s; t
)=
c
copy
+
ED
(
s
2
::s
k
;t
2
::t
l
)
if
s
1
=
t
1
, infinity otherwise.
(e)
ED
insert
(
s; t
)=
c
insert
+
ED
(
s
1
::s
k
;t
2
::t
l
)
if
l>
0
, infinity otherwise.
(f)
ED
twiddle
(
s; t
)=
c
twiddle
+
ED
(
s
3
::s
k
;t
3
::t
l
)
if
s
1
=
t
2
and
t
1
=
s
2
, infinity otherwise.
(g)
ED
kill
(
s; t
)=
c
kill
if
l
=0
, infinity otherwise.
Note that the recursive calls in this definition are all to suffixes of the two strings, and that one of the suffixes
is always proper. This leads to the following dynammic programming algorithm. Make an array of entries,
A
(
i; j
)
,
1
i
k
+1
,
0
j
l
+1
, where each entry has two fields, the first representing the edit distance
ED
(
s
i
::s
k
;t
j
::t
l
)
, the second representing the first operationin a sequence that achieves that edit distance. (In
the cases
i
=
k
+1
or
j
=
l
+1
, the distance is the edit distance to or from the empty string respectively.) Fill
1