Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Combinatorial Search and Optimization: Dynamic Programming and Greedy Algorithms, Assignments of Computer Science

Solutions to two problems related to combinatorial search and optimization using dynamic programming and greedy algorithms. The first problem is single word reusable boggle (sworb), where the goal is to determine if there is a path in a grid that spells a given word. The second problem is about calculating the edit distance between two strings using various operations. The document also discusses the dynamic programming algorithm for finding the edit distance.

Typology: Assignments

2009/2010

Uploaded on 03/28/2010

koofers-user-cmx
koofers-user-cmx 🇺🇸

10 documents

1 / 2

Toggle sidebar

Related documents


Partial preview of the text

Download Combinatorial Search and Optimization: Dynamic Programming and Greedy Algorithms and more Assignments Computer Science in PDF only on Docsity! CSE 202 Homework 2 Combinatorial Search and Optimization: Dynammic Programming and Greedy Algorithms 1. Single Word Reusable Boggle (SWoRB) Problem You are given an 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 algorithm to 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 have gone. 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 fw1 : : : wkg ending at position (i; j) on the matrix. The computation begins by initializing the first plane of the matrix fS(i; j; 1) : i; j  ng with true if M(i; j) = w1, false otherwise. When all elements S(:; :; k) are filled in, elements of fS(i; j; k+1) : i; j  ng are set to true if M(i; j) = wk+1 and any of the neighbors fS(i0; j0; k) : i0 2 fi 1; i; i+ 1g; j0 2 fj 1; j; j + 1g; i0 6= i _ j0 6= jg 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(i0; j0; 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 nnm 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(n2m). 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 previous plane. 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 throughM that form a wordw. 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 randomly sample from the paths uniformly by selecting each backward step with probability proportional to its share of the paths. 2. Problem 16-3 from text (p. 325) Let ccopy, cdelete, creplace, cinsert, ctwiddle and ckill be the costs of the operations. Let ED(s1s2::sk; t1t2::tl) be the edit distance from s to t, and let EDop be the edit distance given that we perform operation op first. Then we get the recursive definitions: (a) ED(s; t) = 0ifk = l = 0;minopEDop(s; t)otherwise, (b) EDdelete(s1::sk; t1::tl) = cdelete +ED(s2; ::sk; t1::tl) (if k > 0, infinity otherwise); (c) EDreplace(s1::sk; t1::tl) = creplace + ED(s2::sk; t2::tl) 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 t1. Other interpretations make the algorithm rather more complicated.) (d) EDcopy(s; t) = ccopy +ED(s2::sk; t2::tl) if s1 = t1, infinity otherwise. (e) EDinsert(s; t) = cinsert +ED(s1::sk; t2::tl) if l > 0, infinity otherwise. (f) EDtwiddle(s; t) = ctwiddle +ED(s3::sk; t3::tl) if s1 = t2 and t1 = s2, infinity otherwise. (g) EDkill(s; t) = ckill 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(si::sk; tj ::tl), the second representing the first operation in 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
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved