HW 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CSIT 5500 Advanced Algorithms

2023 Fall Semester


Written Assignment 2
Handed out: October 9, 2023
Due: 23:59 on October 23, 2023
Please submit a soft copy via the canvas system by the due date and time shown
above. Late assignments will not be graded.

1. (10 points) Consider the divide-and-conquer algorithm for the closest pair problem discussed
in class. Let n be the number of input points. Suppose that we have recursively determined
the closest pair distances δ1 and δ2 of the left and right halves. Let left-xlist and left-ylist be
the n/2 points in the left half sorted in increasing x-coordinates and increasing y-coordinates,
respectively. These two lists are doubly linked lists, and there are cross reference pointers
between them. Similarly, let right-xlist and right-ylist be the n/2 points in the right half
sorted in increasing x-coordinates and increasing y-coordinates, respectively. Again, they are
doubly linked lists, and there are cross reference pointers between them.
Write a pseudocode to implement the combine step as described in the class that runs in O(n)
time. You are free to store additional information in the entries of left-xlist, left-ylist, right-
xlist, and right-ylist. State it clearly if you do. You should insert comments, explanations,
and even figures to make sure that your pseudocode is understandable. You may lose points
if we cannot understand your pseudocode.

2. (10 points) Consider the following string

aabbabbaaabbaab

(a) Treat the above string as a pattern and show the next(·) table for it that supports the
KMP string matching algorithm. You do NOT need to show your steps for obtaining
the next(·) table.
(b) Treat the above string as the text. Consider the following pattern
bba
Show the next(·) table for bba. You do not need to show the intermediate steps for
obtaining the next(·) table. Run the KMP string matching algorithm to find the oc-
currence of bba in the above string. Show the intermediate steps of running the KMP
algorithm as follows:
• Initially, draw the pattern and the text so that their leftmost symbols are aligned.
• Whenever the KMP algorithm accesses the next(·) table,
– indicate which two symbols in the pattern and the text were just compared by
the KMP algorithm right before it accesses the next(·) table, and
– draw the alignment of the pattern and the text to show the effect of accessing
the next(·) table.
• Run the KMP algorithm to its completion to find all occurrences of the pattern.

3. (10 points) Consider the speeding up of the string searching using suffix array from a query
time of O(m log n) to O(m + log n) as discussed in the lecture slides. There are three cases
case 1.1, case 1.2, and case 1.3 under the assumption that l ≥ r. Explain how the search
algorithm handles each of these three cases and why the handling is correct.

1
4. (10 points) Let A[1..n, 1..n] be a 2D array of n2 non-negative integers. A cut is a sequence
of entries of A of the form A[1, j1 ], A[2, j2 ], . . . , A[n, jn ] such that for all i ∈ [1, n − 1], ji+1 is
equal to ji − 1 or ji + 1. The cost of a cut is the sum of entries in the cut. Design a dynamic
programming algorithm to determine a cut of the minimum cost. Explain the correctness of
your algorithm and its running time.

You might also like