CS 361: Algorithms and Data Structures
Homework 1


  1. Algorithm Design 1.1, 1.2
    • These questions are asking about the Stable Matching problem, not the Gale-Shapley Algorithm (stop and think about what this means). As such, your answer should not reference or depend upon the GSA.

    • 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. Consider the following Java method that takes in a (possibly unsorted) 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 the number 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;
        }
    


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

    2. 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 worst-case running time T(n) of this algorithm? Justify your answer.

    3. What is the best-case running time T(n) of this algorithm? Justify your answer.


  3. Consider the following Java method that reads in a text file line-by-line and then prints the set of unique tokens found in the file. You can assume that adding to a hash set takes constant time and that iterating over the contents of a hash set (e.g. in order to print) takes time linear in the size of the hash set.

    public static void main(String[] args){        
            String filename = args[0];  
            HashSet<String> uniqueTokens = new HashSet<>();
            
            try{
                Scanner scan = new Scanner(new File(filename));
    
                // Read in each line in the file
                while(scan.hasNextLine()){
                    String line = scan.nextLine();
    
                    // Split the line into individual tokens
                    String[] tokens = line.split(" ");  
                    
                    // Add each token to the hash set
                    for(int i = 0; i < tokens.length; i++){
                        uniqueTokens.add(tokens[i]);
                    }
                }
                scan.close();
            }
            catch(IOException e){
               System.out.println(e);
               System.exit(-1);
            }
             
            // Print out the set of unique tokens from the file
            for(String token : uniqueTokens){
                System.out.println(token);
            }        
        }    
    


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

    2. What is the worst-case running time T(n) of this algorithm? Justify your answer.

    3. What is the best-case running time T(n) of this algorithm? Justify your answer.


  4. 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.

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

  6. 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


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