CSCI 3104 - Algorithms - Fall 2013

Problem Set #1

Due: Tue, Sep 10th



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
(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?