CPSC 110-08: Computing on Mobile Phones
Spring 2012

Solutions to Exercises

Algorithm Analysis

This lecture focuses on algorithm analysis, focusing on the following learning objectives:

In Class Work

Do the following exercises and add your answers to your Portfolio page for today's homework.

For each of the following problems, decide whether it can be solved by a linear algorithm and explain briefly why or why not.

  1. Compute the sum of a list containing N random integers.
    Computing the sum of a list of random numbers can be solved by a linear time algorithm. To compute the sum you need to traverse the list once. So the amount of time it takes is proportional to the length of the list. If the list doubles in size, it should take twice as long to compute the sum.

  2. Determining if duplicate numbers occur in a list of N random integers.
    This problem cannot be solved by a linear time algorithm unless we use lots more memory -- i.e., a bucket for each possible number in the list. Here's how: Traverse the list placing each number in its own bucket. A duplicate will be detected if a bucket already has a number in it.

    If we don't use all those buckets, this problem can be solved by a quadratic time algorithm: For each number in the list, go through all the other numbers to see if it has a duplicated. So, each of n numbers in the list is compared to n-1 other numbers. That would be n × (n-1) = n2 - n comparisons, which is quadratic. As the size of the list doubles, the amount of time this would take would increase by 22.

For each of the following problems, decide whether it can be solved by a logarithmic algorithm and explain briefly why or why not.

  1. Guessing a number between 1 and 100 if your guesses are characterized as "too high" or "too low".
    This problem is like search a list where the numbers (or names) are in no special order. The only guaranteed way of finding the number is to search sequentially, starting at the first number and traversing the list. If the list doubles in size, it will take twice as long (on average) to guess the right number. So this is linear growth.

  2. Guessing a number between 1 and 100 if your guesses are characterized as "wrong" or "right".
    This is the classic binary search algorithm and it takes logarithmic time. So to guess a number between 1 and 100 requires 7 guesses, because 100 is less than 128, which is 27. That means that a list of 100 numbers can be divided in half at most 7 times. To guess a number between 1 and 1000 requires 10 guesses because 1000 is less than 1024, which is 210. A list of 1000 numbers can be divided in half at most 10 times. Thus, although the list grew by a factor of 10, the number of guesses increased by just 3. This is logarithmic growth.

For each of the following problems, decide whether or not it is intractable and explain briefly why or why not.

  1. Computing the sum of all the numbers in a square matrix of dimension N × N.
    This problem is tractable because it can be solved by an algorithm that grows quadratically as the size of N increases. For example, if N is 3, then you have to compute the sum of 9 numbers, which requires 9 additions. But if N doubles in size to 6, you would require 36 additions, which is 4 times as many. So doubling N, increases the time by N2. This is quadratic growth.

  2. Given n integers does there exists a subset of them that sum exactly to B? For example, suppose the integers are {4, 5, 8, 13, 15, 24, 33}. If B = 36 then the answer is yes (and 4, 8, 24 is a solution). If B = 14 the answer is no.
    This problem is NOT tractable because it requires an algorithm that grows exponentially as the size of N increases. To determine whether some subset of numbers sums to B would require that you look through every possible subset (in the worst case). And there are 2N subsets for a set of N things.

    To see this more clearly, let's count the subsets. There are 4 (22) subsets of {1, 2}: {}, {1,2}, {1}, {2}.

    There are 8 (23) subsets of {1,2,3}: {}, {1,2,3}, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}.

    And there are 16 (24) subsets of {1,2,3,4}: {}, {1,2,3,4}, {1}, {2},{3},{4},{1,2}, {1,3},{1,4},{2,3},{2,4},{3,4},{1,2,3},{1,2,4},{1,3,4},{2,3,4}.

    In general, the the number of subsets of a set of N things is 2N, which is exponential.