CS 373: Combinatorial Algorithms, Fall 2000
Homework 1 (due September 12, 2000 at midnight)
Name:
Net ID:
Alias:
U
3
/
4
1
Name:
Net ID:
Alias:
U
3
/
4
1
Name:
Net ID:
Alias:
U
3
/
4
1
Starting with Homework 1, homeworks may be done in teams of up to three people. Each team
turns in just one solution, and every member of a team gets the same grade. Since 1unit graduate
students are required to solve problems that are worth extra credit for other students,
1unit grad
students may not be on the same team as 3/4unit grad students or undergraduates.
Neatly print your name(s), NetID(s), and the alias(es) you used for Homework 0 in the boxes
above. Please also tell us whether you are an undergraduate, 3/4unit grad student, or 1unit grad
student by circling U,
3
/
4
, or 1, respectively. Staple this sheet to the top of your homework.
Required Problems
1. Suppose we want to display a paragraph of text on a computer screen. The text consists of
n
words, where the
i
th word is
p
i
pixels wide. We want to break the paragraph into several
lines, each exactly
P
pixels long. Depending on which words we put on each line, we will
need to insert different amounts of white space between the words. The paragraph should be
fully justified, meaning that the first word on each line starts at its leftmost pixel, and
except
for the last line
, the last character on each line ends at its rightmost pixel. There must be at
least one pixel of whitespace between any two words on the same line.
Define the
slop
of a paragraph layout as the sum over all lines,
except the last
, of the cube
of the number of extra whitespace pixels in each line (not counting the one pixel required
between every adjacent pair of words). Specifically, if a line contains words
i
through
j
, then
the amount of extra white space on that line is
P
−
j
+
i
−
∑
j
k
=
i
p
k
. Describe a dynamic
programming algorithm to print the paragraph with minimum slop.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
CS 373
Homework 1 (due 9/12/00)
Fall 2000
2. Consider the following sorting algorithm:
S
TUPID
S
ORT
(
A
[0
.. n
−
1]) :
if
n
= 2
and
A
[0]
> A
[1]
swap
A
[0]
↔
A
[1]
else if
n >
2
m
←⌈
2
n/
3
⌉
S
TUPID
S
ORT
(
A
[0
.. m
−
1])
S
TUPID
S
ORT
(
A
[
n
−
m .. n
−
1])
S
TUPID
S
ORT
(
A
[0
.. m
−
1])
(a) Prove that S
TUPID
S
ORT
actually sorts its input.
(b) Would the algorithm still sort correctly if we replaced the line
m
←⌈
2
n/
3
⌉
with
m
←⌊
2
n/
3
⌋
?
Justify your answer.
(c) State a recurrence (including the base case(s)) for the number of comparisons executed
by S
TUPID
S
ORT
.
(d) Solve the recurrence, and prove that your solution is correct. [Hint: Ignore the ceiling.]
Does the algorithm deserve its name?
⋆
(e) Show that the number of
swaps
executed by S
TUPID
S
ORT
is at most
(
n
2
)
.
3. The following randomized algorithm selects the
r
th smallest element in an unsorted array
A
[1
.. n
]
. For example, to find the smallest element, you would call R
ANDOM
S
ELECT
(
A,
1)
; to
find the median element, you would call R
ANDOM
S
ELECT
(
A,
⌊
n/
2
⌋
)
. Recall from lecture that
P
ARTITION
splits the array into three parts by comparing the pivot element
A
[
p
]
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '09
 A
 Dynamic Programming, 2 m, θ, ANDOM S ELECT

Click to edit the document details