CSE 2320 Section 501 Fall 1998

Homework 4

Due: November 10, 1998, in class (November 11, 1998, 5:00pm for -10%)

1.
Consider the problem of defragmenting a disk file that is divided into n sequential, non-contiguous locations on the disk, where location i consists of si contiguous sectors. The mechanism for defragmenting a file is to repeatedly compact two sequential locations i and i+1 together by copying the smaller location next to the larger at a cost of . This operation creates a larger location of size (si + si+1). After n-1 pairwise compactions, the file will be in contiguous sectors on the disk. The goal is to find the sequence of pairwise compactions that minimizes the total cost.

(a)
Let c(i,j) be the optimal cost of compacting the locations . Assuming the optimal compaction of locations consists of compacting locations and , give an expression for c(i,j) in terms of c(i,k) and c(k+1,j).

(b)
Prove that the file defragmentation problem exhibits optimal substructure.

(c)
Define a recursive solution for computing c(i,j) and write pseudocode for a divide-and-conquer algorithm implementing your solution.

(d)
Give a recurrence T(n) for the running time of your recursive solution in part c, where n=j-i+1, and show that .

(e)
Show that the recursive solution has overlapping subproblems and compute the number of unique subproblems in terms of n.

(f)
Write pseudocode for a O(n4) bottom-up, dynamic programming solution to the file defragmentation problem. Justify the O(n4) running time of your solution. Note that your solution must keep track of which locations to compact and must include a procedure for actually defragmenting the file. You may assume the existence of the procedure COMPACT(i,j) that compacts the two locations i and j with a running time proportional to the number of sectors in the smaller location.

2.
Consider a greedy algorithm for the file defragmentation problem where the greedy choice is to compact the smallest remaining location to either of its neighboring locations. Give a counterexample showing that this greedy choice does not satisfy the greedy choice property, and explain why.

3.
Consider a greedy algorithm for the file defragmentation problem where the greedy choice is to compact the largest remaining location to either of its neighboring locations.

(a)
Prove that this greedy choice satisfies the greedy choice property.

(b)
Write pseudocode for a O(n2) running time algorithm that uses this greedy choice to solve the file defragmentation problem. Justify the O(n2) running time of your solution.