



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




UC Berkeley—CS 170: Efficient Algorithms and Intractable Problems Handout 1 Lecturer: Michael Jordan August 28, 2005
An algorithm is a recipe or a well-defined procedure for transforming some input into a desired output. Perhaps the most familiar algorithms are those for adding and multiplying integers. Here is a multiplication algorithm that is different the algorithm you learned in school: write the multiplier and multiplicand side by side, say, 13 × 15 = 195. Repeat the following operations: divide the first number by 2 (throw out any fractions) and multiply the second by 2, until the first number is 1. This results in two columns of numbers. Now cross out all rows in which the first entry is even, and add all entries of the second column that haven’t been crossed out. The result is the product of the two numbers. In this course we will ask a number of basic questions about algorithms:
The history of algorithms for simple arithmetic is quite fascinating. Although we take them for granted, their widespread use is surprisingly recent. The key to good algorithms for arithmetic was the positional number system (such as the decimal system). Roman numerals (I, II, III, IV, V, VI, etc) were just the wrong data structure for performing arithmetic efficiently. The positional number system was first invented by the Mayan Indians in Central America about 2000 years ago. They used a base 20 system, and it is unknown whether they had invented algorithms for performing arithmetic, since the Spanish conquerors destroyed most of the Mayan books on science and astronomy. The decimal system that we use today was invented in India in roughly 600 AD. This positional number system, together with algorithms for performing arithmetic were trans- mitted to Persia around 750 AD, when several important Indian works were translated into arabic. In particular, it was around this time that the Persian mathematician Al-Khwarizmi wrote his arabic textbook on the subject. The word “algorithm” comes from Al-Khwarizmi’s name. Al-Khwarizmi’s work was translated into Latin around 1200 AD, and the positional number system was propagated throughout Europe from 1200 to 1600 AD. The decimal point was not invented until the 10th century AD, by a Syrian mathemati- cian al-Uqlidisi from Damascus. His work was soon forgotten, and five centuries passed before decimal fractions were re-invented by the Persian mathematician al-Kashi. With the invention of computers in this century, the field of algorithms has seen explosive growth. There are a number of major successes in this field, not all of which we can discuss in CS 170:
in this class). So, what is T (n)? We shall be interested in the order of growth of T (n), ignoring constants. If n ≤ 1, then we do constant amount of work to obtain Fn; since we are suppressing constants, we can write T (n) = 1. Otherwise, if n ≥ 2, we have:
T (n) = T (n − 1) + T (n − 2) + 1,
because in this case the running time of F on input n is just the running time of F on input n − 1—which is T (n − 1)—plus T (n − 2), plus the time for the addition. This is nearly the Fibonacci equation. We can change it to be exactly the Fibonacci equation by noting that the new variable T ′(n) ≡ T (n) + 1 satisfies T ′(n) = T ′(n − 1) + T ′(n − 2), T ′(1) = T (1) + 1 = 2 and T ′(0) = T (0) + 1 = 2. Comparing this recurrence for T ′(n) to the Fibonacci recurrence, we see T ′(n) = 2Fn and so T (n) = 2Fn − 1. In other words, the running time of our Fibonacci program grows as the Fibonacci numbers—that is to say, way too fast. Can we do better? This is the question we shall always ask of our algorithms. The trouble with the naive algorithm is wasteful use of recursion: The function F is called with the same argument over an over again, exponentially many times (try to see how many times F (1) is called in the computation of F (5)). A simple trick for improving this performance is to memoize the recursive algorithm, by remembering the results from previous calls. Specifically, we can maintain an array A[0... n], initially all zero, except that A[0] = A[1] = 1. This array is updated before the return step of the F function, which now becomes A[n] := A[n − 1] + A[n − 2]; return A[n]. More importantly, it is consulted in the beginning of the F function, and, if its value is found to be non-zero—that is to say, defined— it is immediately returned. But then of course, there would be little point in keeping the recursive structure of the algorithm, we could just write:
function F(n: integer): integer array A[0...n] of integers, initially all 0 A[0] := A[1] := 1 for i=2 to n do A[i] := A[i-1] + A[i-2] return A[n]
This algorithm is correct, because it is just another way of implementing the definition of Fibonacci numbers. The point is that its running time is now much better. We have a single “for” loop, executed n − 1 times. And the body of the loop takes only constant time (one addition, one assignment). Hence, the algorithm takes only O(n) operations. This is usually an important moment in the study of a problem: We have come from the naive but prohibitively exponential algorithm to a polynomial one. Now, let us be more precise in our accounting of the time requirements for all these methods. We have made a grave and common error: we have been too liberal about what constitutes an elementary step of the algorithm. In general, in analyzing our algorithms we shall assume that each arithmetic step takes unit time, because the numbers involved will be typically small enough—about n, the size of the problem— that we can reasonably expect them to fit within a computer’s word. But in the present case, we are doing arithmetic on huge numbers, with about n bits—remember, a Fibonacci number has about. 694... n
bits—and of course we are interested in the case n is truly large. When dealing with such huge numbers, and if exact computation is required, we have to use sophisticated long integer packages. Such algorithms take O(n) time to add two n-bit numbers—hence the complexity of the two methods was not really O(Fn) and O(n), but instead O(nFn) and O(n^2 ), respectively; notice that the latter is still exponentially faster.
Every algorithmic situation (every computational problem) is different, and there are no hard and fast algorithm design rules that work all the time. But the vast majority of algorithms can be categorized with respect to the concept of progress they utilize. In an algorithm you want to make sure that, after each execution of a loop, or after each recursive call, some kind of progress has been made. In the absence of such assurance, you cannot be sure that your algorithm will even terminate. For example, in depth-first search, after each recursive call you know that another node has been visited. And in Dijkstra’s algorithm you know that, after each execution of the main loop, the correct distance to another node has been found. And so on. Divide-and-Conquer Algorithms. These algorithms have the following outline: To solve a problem, divide it into subproblems. Recursively solve the subproblems. Finally glue the resulting solutions together to obtain the solution to the original problem. Progress here is measured by how much smaller the subproblems are compared to the original problem. You have already seen an example of a divide and conquer algorithm in cs61B: mergesort. The idea behind mergesort is to take a list, divide it into two smaller sublists, conquer each sublist by sorting it, and then combine the two solutions for the subproblems into a single solution. These three basic steps—divide, conquer, and combine—lie behind most divide and conquer algorithms. With mergesort, we kept dividing the list into halves until there was just one element left. In general, we may divide the problem into smaller problems in any convenient fashion. Also, in practice it may not be best to keep dividing until the instances are completely trivial. Instead, it may be wise to divide until the instances are reasonably small, and then apply an algorithm that is faster on small instances. For example, with mergesort, it might be best to divide lists until there are only four elements, and then sort these small lists quickly by other means. We will consider these issues in some of the applications we consider.
Suppose we wish to find the minimum and maximum items in a list of numbers. How many comparisons does it take? The obvious loop takes 2n − 2:
m = L[1]; M = L[1]; for i=2, length(L) m = min(m, L[i]) M = max(M, L[i])