You are on page 1of 10

CSCI 5454 - Algorithms - Spring 2015

Problem Set #1
Due: Wed, Feb 4th

1. In each of the following situations, indicate whether f = O(g), or f = (g), or both


(in which case f = (g)).
(a) f(n) = n1.01, g(n) = n
(b) f(n) = lg n, g(n) = ln n
(c) f(n) = 2n, g(n) = 3n
2. The text spends a lot of time talking about fib(200). Compute this number exactly
using Python. Submit your code listing along with the value of fib(200), on paper.
3. Consider the following Python function:
def find(a, target):
x = 0
y = len(a)
while x < y:
m = (x+y)/2
if a[m] < target:
x = m+1
elif a[m] > target:
y = m
else:
return m
return -1

Suppose list a has n elements and is sorted. Using () notation, what is the best case
running time as function of n? Using () notation, what is the worst case running
time as function of n?
4. Modify the find function from problem 4 by changing the 5th line from m =
(x+y)/2 to m = (2*x+y)/3; now answer the same questions given in problem 3.
5. Consider this sorting algorithm, written in Python:
def bsort(a):
swapped = True
while swapped:
swapped = False

for i in range(0, len(a)-1):


if a[i] > a[i+1]:
a[i], a[i+1] = a[i+1], a[i]
swapped = True

(a) Is this sort randomized or deterministic?


(b) What is the shape of the best-case input?
(c) Using notation, what is the best-case running time of this program?
(d) What is the shape of the worst-case input?
(e) Using notation, what is the worst-case running time of this program?
6. Consider this sorting algorithm, written in Python:
import random
def luckysort(a):
sorted = False
while not sorted:
random.shuffle(a)
sorted = True
for i in range(0, len(a)-1):
if a[i] > a[i+1]:
sorted = False

You may assume the input array a contains distinct elements.


(a) Is this sort randomized or deterministic?
(b) What is the shape of the best-case input?
(c) Using notation, what is the best-case running time of this program?
(d) What is the shape of the worst-case input?
(e) Using notation, what is the worst-case running running time of this program?
(f) Using notation, what is the expected running time of this program?

CSCI 5454 - Algorithms - Spring 2015

Problem Set #2
Due: 1pm, Feb 18th, 2015

1. Suppose you wanted to determine if a decimal number is divisible by 101. Give a


quick method for doing this. (Hint: convert the number to base-100 by grouping pairs
of digits together.) Justify your answer.
2. Text problem 1.12. Use only paper. Show your work.
3. Text problem 1.13. Use only paper. Show your work.
4. One of the largest naturally-occurring integer constants we know of
is M=808017424794512875886459904961710757005754368000000000. This is the
size of an object of mathematics that is just there (humans didn't invent it).
Just for fun, compute MMmod22015. Use a computer for this one; include source code
with your solution.
5. Text problem 1.18. Show your work.
6. Suppose I have an n-bit positive integer N stored in memory in binary, and I want
to print it. What is the running time to print N if
(a) I output N in hexadecimal?
(b) I output N in binary?
(c) I output N in unary?

CSCI 5454 - Algorithms - Spring 2015


Problem Set #3
Due: 1pm, Feb 25th, 2015

1. Text problem 1.16.

2. Text problem 1.20. Do not use a computer; show your work.


3. Write a python function nextprime(i) to find the next prime i. What
is nextprime(2015**50)? Include the answer along with your source code.
4. Using your code from above, write a python program that computes the average
distance from a random 100-digit integer to the nextprime after it. Use 1 thousand
samples to obtain your average. In other words, define a function getrand100() that
generates random 100-digit random positive integers and let s=getrand100(); then
compute the average value of nextprime(s)-s using 1000 samples. Include source
code with your answer.
CSCI 5454 - Algorithms - Spring 2015
Problem Set #4

Due: 1pm, Mar 9th, 2015

1. Text problem 2.4. Justify your answer.


2. Text problem 2.12. Justify your answer.
3. Text problem 2.17. Justify your answer.
4. Let A and B be arrays of integers. Each array contains n elements, and each array is
in sorted order (ascending). A and B do not share any elements in common. Give a
O(lg n)-time algorithm which finds the median of A union B. In other words, find the
median of the 2n elements given by putting A and B together into one array.
(Note: Remember the definition from our book (section 2.4) for the median of a list
with an even number of elements.)
5. The text doesn't implement their algorithm for Selection. Please do so. Then,
compute the median of the list of words given here using your algorithm. (Note: Use
the standard comparison for strings (just like strcmp() in C). All words in this list are
distinct.)

What is the median of the list? Run your program and show the
output.
Run your program 1000 times on the word list and compute
statistics on the number of recursive calls you make for each
run: what is the min, the max, and the average number of
calls?

CSCI 5454 - Algorithms - Spring 2015


Problem Set #5

Due: 1pm, Mar 30th, 2015

1. Text problem 3.8. (If using on-line book, ignore part (c))
2. Suppose you are trying to buy a digraph from a salesman Joe. You tell Joe that your
graph cannot have any odd-length cycles, and must be strongly-connected. Joe says he
has just the thing, and he shows you just three edges of it: (a, b), (b, c) and (a, c).
Explain why Joe must be lying. (Note: there might be a LOT more edges and a LOT
more vertices, but you have to base your answer on just the three edges you can see.)
3. Text problem 3.21.
4. Text problem 3.24. Justify the correctness and running time of your algorithm.
(Note that it says dag and not digraph. If we ask the same problem about a digraph,
there is no known polynomial-time algorithm!)
5. A chain of words is a list of words where the i-th word is the (i-1)st word with one
extra character and some mixing of letters. For example, AN, TAN, RANT, TRAIN,
RETINA, NASTIER is a chain of length 6. Find the longest chain you can in
our wordlist.
In order to do this, first build a dag. The dag will consist of a node for each word (you
might want to collapse words into a single node when it makes sense to), and an edge

from word x to word y if y can follow x in a chain. Then run DFS from each source
node in the dag and keep track of the maximum depth you reach. Print out an example
chain that has maximum length (there will be a TON... just give one chain).
Please hand in your source code along with the longest chain your code found.
CSCI 5454 - Algorithms - Spring 2015
Problem Set #6

Due: 1pm, Apr 13th, 2015

1. Text problem 4.1.


2. Text problem 4.4.
3. Text problem 4.8.
4. Text problem 4.9. (Assume that the graph has no negative-weight cycles.)
5. Text problem 4.18.
6. Text problem 4.19.
7. Consider the digraph given by this graph where each line of the file indicates an
edge. There are 77,360 nodes and 905,468 directed edges. What is the number of
nodes in the largest SCC? What is the number of edges in the largest SCC? Include
your code with your answer.
By the way, here's a rendering of the graph from above. You can see that it's fairly
tightly clustered (in other words, the shortest paths are not very long). The graph was
constructed from 77,360 users of the popular SlashDot website; each user named

other users as either Friend or Foe, thereby creating a digraph.

CSCI 5454 - Algorithms - Spr 2015

Problem Set #7

Due: 1pm, Apr 27th, 2015

1. Text problem 5.3.


2. Text problem 5.9. (omit (j))
3. Text problem 5.14.
4. Text problem 5.34
5. Text problem 6.1.
6. Text problem 6.4.
7. Text problem 6.8.
CSCI 3104 - Algorithms - Fall 2011
Problem Set #8

Due: Never

1. Text problem 6.21.


2. Text problem 6.22.
3. Text problem 6.25.
4. Text problem 6.26.
5. Text Problem 8.4 (For part d, your algorithm should run in O(|V|) not O(|V| 4) as the
pdf book says.)

6. Text Problem 8.9


7. Text Problem 8.11
8. Text Problem 8.12

You might also like