CS 361: Algorithms and Data Structures
Homework 1


  1. Algorithm Design 1.1, 1.2
    These questions ask you to either disprove a claim using a counterexample or, if you believe it is true, give a short explanation. Please see the "Homework Guide" for an explanation of what a counterexample is. A counterexample has a precise mathematical meaning and is more than just words.

  2. Write by hand the definition for Big-O ten times. Your definition should begin with, "A function T(n) is O(f(n))..." Make sure you are writing the correct definition!

  3. Algorithm Design 2.2. (Be sure to show all of your work.)

  4. Use your answers from AD 2.2 to fill in the following table:

    Running Times (in ascending order) Largest input size (in 1 hour)
             
             
             
             
             
             

    Note: The first column should be the functions from AD 2.2 arranged in ascending order of growth rate. That is, if function g(n) immediately follows function f(n) in the table, then it should be the case that f(n) is O(g(n)). The second column should be the results you computed in AD 2.2

  5. Consider the following Java method that takes in an array and returns the position of the median element. (The median is the number that is bigger than half of the elements in the list and smaller than half of the elements in the list. For example, if the list was A = [4, -1, 9, 0, 3] then the median would be 3 since 3 is bigger than {-1, 0} and smaller than {4, 9}.)

    public int findMedian(int[] list){
            for(int i = 0; i < list.length; i++){
                int lessThan = 0;   // counts how many numbers are less than list[i]
                int grtThan = 0;    // counts how many numbers are greater than list[i]
                
                for(int j = 0; j < list.length; j++){
                    if(list[j] < list[i]){
                        lessThan++;
                    }
                    else if(list[j] > list[i]){
                        grtThan++;
                    }
                }
                
                // If the list has odd length, there is a unique median
                if(list.length%2 == 1 && lessThan == grtThan){
                    return i;
                }
                // If the list has even length, there are 2 medians. We return 
                // the first one
                else if( list.length%2 == 0 && lessThan == grtThan-1){
                    return i;
                }
            }
    
            return -1;
        }
    


    • What quantity best represents the "input size" for this problem?

    • Suppose the only primitive operations we are interested in counting are comparisons -- i.e., the number of times we compare two elements in the list. Given this, what is the running time T(n) of this algorithm in the worst case? That is, determine the number of comparisons of elements in the list performed by this algorithm in the worst case as a function of the input size.

    • Now give an upper-bound on T(n). That is, find a function f(n) such that T(n) is O(f(n)). Prove your upper-bound is correct by finding an appropriate c and n0 that fulfill the definition.


Last modified: Fri Jan 24 10:58:47 PST 2014