url
stringclasses
71 values
filename
stringclasses
71 values
success
bool
1 class
page_count
int64
1
917
pdf_text
stringlengths
0
4.81k
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
List of boxes Bases and logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Two’s complement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Is your social security number a prime? . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Hey, th...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Why P and NP? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 The two ways to use reductions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Unsolvable problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Entanglement . . . . ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Preface This book evolved over the past ten years from a set of lecture notes developed while teaching the undergraduate Algorithms course at Berkeley and U.C. San Diego. Our way of teaching this course evolved tremendously over these years in a number of directions, partly to address our students’ background (undevelo...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
and excursions for the mathematically sophisticated. 10
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Chapter 0 Prologue Look around you. Computers and networks are everywhere, enabling an intricate web of com- plex human activities: education, commerce, entertainment, research, manufacturing, health management, human communication, even war. Of the two main technological underpinnings of this amazing proliferation, on...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Since then, this decimal positional system and its numerical algorithms have played an enormous role in Western civilization. They enabled science and technology; they acceler- ated industry and commerce. And when, much later, the computer was finally designed, it explicitly embodied the positional system in its bits an...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
The first question is moot here, as this algorithm is precisely Fibonacci’s definition of Fn. But the second demands an answer. Let T(n) be the number of computer steps needed to compute fib1(n); what can we say about this function? For starters, if n is less than 2, the procedure halts almost immediately, after just a c...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 0.1 The proliferation of recursive calls in fib1.    Fn−3 Fn−1 Fn−4 Fn−2 Fn−4 Fn−6 Fn−5 Fn−4 Fn−2 Fn−3 Fn−3 Fn−4 Fn−5 Fn−5 Fn As with fib1, the correctness of this algorithm is self-evident because it directly uses the definition of Fn. How long does it take? The inner loop consists of a single computer s...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
for addition, which works on one digit at a time. Thus fib1, which performs about Fn ad- ditions, actually uses a number of basic steps roughly proportional to nFn. Likewise, the number of steps taken by fib2 is proportional to n2, still polynomial in n and therefore ex- ponentially superior to fib1. This correction to...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 0.2 Which running time is better? 1 2 3 4 5 6 7 8 9 10 0 10 20 30 40 50 60 70 80 90 100 n 2n+20 n2 Now another algorithm comes along, one that uses f3(n) = n + 1 steps. Is this better than f2? Certainly, but only by a constant factor. The discrepancy between f2 and f3 is tiny compared to the huge gap between f...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
4. Likewise, any polynomial dominates any logarithm: n dominates (log n)3. This also means, for example, that n2 dominates n log n. Don’t misunderstand this cavalier attitude toward constants. Programmers and algorithm developers are very interested in constants and would gladly stay up nights in order to make an algor...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Exercises 0.1. In each of the following situations, indicate whether f = O(g), or f = Ω(g), or both (in which case f = Θ(g)). f(n) g(n) (a) n −100 n −200 (b) n1/2 n2/3 (c) 100n + log n n + (log n)2 (d) n log n 10n log 10n (e) log 2n log 3n (f) 10 log n log(n2) (g) n1.01 n log2 n (h) n2/ log n n(log n)2 (i) n0.1 (log n)...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
and in general  Fn Fn+1  =  0 1 1 1 n ·  F0 F1  . So, in order to compute Fn, it suffices to raise this 2 × 2 matrix, call it X, to the nth power. (a) Show that two 2 × 2 matrices can be multiplied using 4 additions and 8 multiplications. But how many matrix multiplications does it take to compute X n? (b) Show th...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
20
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Chapter 1 Algorithms with numbers One of the main themes of this chapter is the dramatic contrast between two ancient problems that at first seem very similar: Factoring: Given a number N, express it as a product of its prime factors. Primality: Given a number N, determine whether it is a prime. Factoring is hard. Despi...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Bases and logs Naturally, there is nothing special about the number 10—we just happen to have 10 fingers, and so 10 was an obvious place to pause and take counting to the next level. The Mayans developed a similar positional system based on the number 20 (no shoes, see?). And of course today computers represent numbers ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
of this sum gets computed in a fixed amount of time. The total running time for the addition algorithm is therefore of the form c0 +c1n, where c0 and c1 are some constants; in other words, it is linear. Instead of worrying about the precise values of c0 and c1, we will focus on the big picture and denote the running tim...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 1.1 Multiplication `a la Franc¸ais. function multiply(x, y) Input: Two n-bit integers x and y, where y ≥0 Output: Their product if y = 0: return 0 z = multiply(x, ⌊y/2⌋) if y is even: return 2z else: return x + 2z rows, with lengths of up to 2n bits (taking the shifting into account). The total time taken to add...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 1.2 Division. function divide(x, y) Input: Two n-bit integers x and y, where y ≥1 Output: The quotient and remainder of x divided by y if x = 0: return (q, r) = (0, 0) (q, r) = divide(⌊x/2⌋, y) q = 2 · q, r = 2 · r if x is odd: r = r + 1 if r ≥y: r = r −y, q = q + 1 return (q, r) Is this algorithm correct? The p...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 1.3 Addition modulo 8. 0 0 0 + = 6 3 1 then x modulo N is equal to r. This gives an enhanced notion of equivalence between numbers: x and y are congruent modulo N if they differ by a multiple of N, or in symbols, x ≡y (mod N) ⇐⇒ N divides (x −y). For instance, 253 ≡13 (mod 60) because 253 −13 is a multiple of 60...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Taken together with the substitution rule, this implies that while performing a sequence of arithmetic operations, it is legal to reduce intermediate results to their remainders modulo N at any stage. Such simplifications can be a dramatic help in big calculations. Witness, for instance: 2345 ≡(25)69 ≡3269 ≡169 ≡1 (mod ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 1.4 Modular exponentiation. function modexp(x, y, N) Input: Two n-bit integers x and N, an integer exponent y Output: xy mod N if y = 0: return 1 z = modexp(x, ⌊y/2⌋, N) if y is even: return z2 mod N else: return x · z2 mod N division. For both tasks, the most obvious procedures take exponentially long, but with...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 1.5 Euclid’s algorithm for finding the greatest common divisor of two numbers. function Euclid(a, b) Input: Two integers a and b with a ≥b ≥0 Output: gcd(a, b) if b = 0: return a return Euclid(b, a mod b) We can package this idea in a particularly simple form: the recursive algorithm of Fig- ure 1.4, which works ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 1.6 A simple extension of Euclid’s algorithm. function extended-Euclid(a, b) Input: Two positive integers a and b with a ≥b ≥0 Output: Integers x, y, d such that d = gcd(a, b) and ax + by = d if b = 0: return (1, 0, a) (x′, y′, d) = Extended-Euclid(b, a mod b) return (y′, x′ −⌊a/b⌋y′, d) Lemma If a ≥b, then a mo...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Proof. The first thing to confirm is that if you ignore the x’s and y’s, the extended algorithm is exactly the same as the original. So, at least we compute d = gcd(a, b). For the rest, the recursive nature of the algorithm suggests a proof by induction. The recursion ends when b = 0, so it is convenient to do induction ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
We say x is the multiplicative inverse of a modulo N if ax ≡1 (mod N). There can be at most one such x modulo N (Exercise 1.23), and we shall denote it by a−1. However, this inverse does not always exist! For instance, 2 is not invertible modulo 6: that is, 2x ̸≡1 mod 6 for every possible choice of x. In this case, a a...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
1.3 Primality testing Is there some litmus test that will tell us whether a number is prime without actually trying to factor the number? We place our hopes in a theorem from the year 1640. Fermat’s little theorem If p is prime, then for every 1 ≤a < p, ap−1 ≡1 (mod p). Proof. Let S be the nonzero integers modulo p; th...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 1.7 An algorithm for testing primality. function primality(N) Input: Positive integer N Output: yes/no Pick a positive integer a < N at random if aN−1 ≡1 (mod N): return yes else: return no Is aN−1 ≡1 mod N? Pick some a “prime” “composite” Fermat’s test Pass Fail The problem is that Fermat’s theorem is not an if...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Fail Pass The set {1, 2, . . . , N −1} b a · b The one-to-one function b 7→a · b shows that at least as many elements fail the test as pass it. Hey, that was group theory! For any integer N, the set of all numbers mod N that are relatively prime to N constitute what mathematicians call a group: • There is a multiplicat...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 1.8 An algorithm for testing primality, with low error probability. function primality2(N) Input: Positive integer N Output: yes/no Pick positive integers a1, a2, . . . , ak < N at random if aN−1 i ≡1 (mod N) for all i = 1, 2, . . . , k: return yes else: return no This probability of error drops exponentially fa...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
long. What makes this task quite easy is that primes are abundant—a random n-bit number has roughly a one-in-n chance of being prime (actually about 1/(ln 2n) ≈1.44/n). For instance, about 1 in 20 social security numbers is prime! Lagrange’s prime number theorem Let π(x) be the number of primes ≤x. Then π(x) ≈ x/(ln x)...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Randomized algorithms: a virtual chapter Surprisingly—almost paradoxically—some of the fastest and most clever algorithms we have rely on chance: at specified steps they proceed according to the outcomes of random coin tosses. These randomized algorithms are often very simple and elegant, and their output is correct wit...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Alice and Bob are worried that the eavesdropper, Eve, will intercept e(x): for instance, she might be a sniffer on the network. But ideally the encryption function e(·) is so chosen that without knowing d(·), Eve cannot do anything with the information she has picked up. In other words, knowing e(x) tells her little or...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Alice’s encryption function is then a bitwise exclusive-or, er(x) = x ⊕r: each position in the encoded message is the exclusive-or of the corresponding positions in x and r. For instance, if r = 01110010, then the message 11110000 is scrambled thus: er(11110000) = 11110000 ⊕01110010 = 10000010. This function er is a bi...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
The security of AES has not been rigorously established, but certainly at present the gen- eral public does not know how to break the code—to recover x from er(x)—except using tech- niques that are not very much better than the brute-force approach of trying all possibilities for the shared string r. 1.4.2 RSA Unlike t...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 1.9 RSA. Bob chooses his public and secret keys. • He starts by picking two large (n-bit) random primes p and q. • His public key is (N, e) where N = pq and e is a 2n-bit number relatively prime to (p −1)(q −1). A common choice is e = 3 because it permits fast encoding. • His secret key is d, the inverse of e mo...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
entries, the vast majority of them blank. Or alternatively, we could use a linked list of just the 250 records. But then accessing records would be very slow, taking time proportional to 250, the total number of customers. Is there a way to get the best of both worlds, to use an amount of memory that is proportional to...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
There is nothing inherently wrong with these two functions. If our 250 IP addresses were uniformly drawn from among all N = 232 possibilities, then these functions would behave well. The problem is we have no guarantee that the distribution of IP addresses is uniform. Conversely, there is no single hash function, no ma...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Let us now prove the preceding property. Proof. Since x = (x1, . . . , x4) and y = (y1, . . . , y4) are distinct, these quadruples must differ in some component; without loss of generality let us assume that x4 ̸= y4. We wish to compute the probability Pr[ha(x1, . . . , x4) = ha(y1, . . . , y4)], that is, the probabili...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Exercises 1.1. Show that in any base b ≥2, the sum of any three single-digit numbers is at most two digits long. 1.2. Show that any binary integer is at most four times as long as the corresponding decimal integer. For very large numbers, what is the ratio of these two lengths, approximately? 1.3. A d-ary tree is a roo...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
1.15. Determine necessary and sufficient conditions on x and c so that the following holds: for any a, b, if ax ≡bx mod c, then a ≡b mod c. 1.16. The algorithm for computing ab mod c by repeated squaring does not necessarily lead to the minimum number of multiplications. Give an example of b > 10 where the exponentiatio...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
the depth of the circuit or the longest path from the input to the output of the circuit. This determines the total time taken for computing the function. To add two m-bit binary numbers naively, we must wait for the carry bit from position i−1 before we can figure out the ith bit of the answer. This leads to a circuit ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
(d) Unlike Fermat’s Little theorem, Wilson’s theorem is an if-and-only-if condition for primality. Why can’t we immediately base a primality test on this rule? 1.36. Square roots. In this problem, we’ll see that it is easy to compute square roots modulo a prime p with p ≡3 (mod 4). (a) Suppose p ≡3 (mod 4). Show that (...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
(c) Give an example of positive integers a and N such that x2 ≡a mod N has more than two solutions in {0, 1, . . ., N −1}. 1.42. Suppose that instead of using a composite N = pq in the RSA cryptosystem (Figure 1.9), we simply use a prime modulus p. As in RSA, we would have an encryption exponent e, and the encryption o...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Chapter 2 Divide-and-conquer algorithms The divide-and-conquer strategy solves a problem by: 1. Breaking it into subproblems that are themselves smaller instances of the same type of problem 2. Recursively solving these subproblems 3. Appropriately combining their answers The real work is done piecemeal, in three diffe...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
x = xL xR = 2n/2xL + xR y = yL yR = 2n/2yL + yR. For instance, if x = 101101102 (the subscript 2 means “binary”) then xL = 10112, xR = 01102, and x = 10112 × 24 + 01102. The product of x and y can then be rewritten as xy = (2n/2xL + xR)(2n/2yL + yR) = 2n xLyL + 2n/2 (xLyR + xRyL) + xRyR. We will compute xy via the expr...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 2.1 A divide-and-conquer algorithm for integer multiplication. function multiply(x, y) Input: Positive integers x and y, in binary Output: Their product n = max(size of x, size of y) if n = 1: return xy xL, xR = leftmost ⌈n/2⌉, rightmost ⌊n/2⌋bits of x yL, yR = leftmost ⌈n/2⌉, rightmost ⌊n/2⌋bits of y P1 = multi...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 2.2 Divide-and-conquer integer multiplication. (a) Each problem is divided into three subproblems. (b) The levels of recursion. (a) 10110010 × 01100011 1011 × 0110 0010 × 0011 1101 × 1001 (b) 2 1 1 1 2 1 1 1 2 1 1 1 2 1 1 1 Size n Size n/2 · · · · · · ...... log n levels Size n/4 Master theorem2 If T(n) = aT(⌈n/...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 2.3 Each problem of size n is divided into a subproblems of size n/b. Size 1 Size n/b2 Size n/b Size n Depth logb n Width alogb n = nlogb a Branching factor a subproblems, each of size n/bk (Figure 2.3). The total work done at this level is ak × O  n bk d = O(nd) ×  a bd k . As k goes from 0 (the root) to lo...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Binary search The ultimate divide-and-conquer algorithm is, of course, binary search: to find a key k in a large file containing keys z[0, 1, . . . , n −1] in sorted order, we first compare k with z[n/2], and depending on the result we recurse either on the first half of the file, z[0, . . . , n/2 −1], or on the second half...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 2.4 The sequence of merge operations in mergesort. 2 3 10 1 6 7 13 5 10 2 5 3 13 7 1 6 2 5 3 7 13 1 6 10 Input: 10 2 3 1 13 5 7 6 1 6 10 13 3 2 5 7 . merged in pairs, to yield arrays with two elements. Then pairs of these 2-tuples are merged, producing 4-tuples, and so on. Figure 2.4 shows an example. This viewp...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
58
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
An n log n lower bound for sorting Sorting algorithms can be depicted as trees. The one in the following figure sorts an array of three elements, a1, a2, a3. It starts by comparing a1 to a2 and, if the first is larger, compares it with a3; otherwise it compares a2 and a3. And so on. Eventually we end up at a leaf, and th...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
2.4 Medians The median of a list of numbers is its 50th percentile: half the numbers are bigger than it, and half are smaller. For instance, the median of [45, 1, 10, 30, 25] is 25, since this is the middle element when the numbers are arranged in order. If the list has even length, there are two choices for what the m...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
The three sublists SL, Sv, and SR can be computed from S in linear time; in fact, this compu- tation can even be done in place, that is, without allocating new memory (Exercise 2.15). We then recurse on the appropriate sublist. The effect of the split is thus to shrink the number of elements from |S| to at most max{|SL...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
This follows by taking expected values of both sides of the following statement: Time taken on an array of size n ≤ (time taken on an array of size 3n/4) + (time to reduce array size to ≤3n/4), and, for the right-hand side, using the familiar property that the expectation of the sum is the sum of the expectations. From...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
to be the best running time possible, and it was even proved that in certain models of com- putation no algorithm could do better. It was therefore a source of great excitement when in 1969, the German mathematician Volker Strassen announced a significantly more efficient algorithm, based upon divide-and-conquer. Matrix ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
2.6 The fast Fourier transform We have so far seen how divide-and-conquer gives fast algorithms for multiplying integers and matrices; our next target is polynomials. The product of two degree-d polynomials is a polynomial of degree 2d, for example: (1 + 2x + 3x2) · (2 + x + 4x2) = 2 + 5x + 12x2 + 11x3 + 12x4. More gen...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
65
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Why multiply polynomials? For one thing, it turns out that the fastest algorithms we have for multiplying integers rely heavily on polynomial multiplication; after all, polynomials and binary integers are quite similar—just replace the variable x by the base 2, and watch out for carries. But perhaps more importantly, m...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
2.6.1 An alternative representation of polynomials To arrive at a fast algorithm for polynomial multiplication we take inspiration from an impor- tant property of polynomials. Fact A degree-d polynomial is uniquely characterized by its values at any d + 1 distinct points. A familiar instance of this is that “any two po...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
The equivalence of the two polynomial representations makes it clear that this high-level approach is correct, but how efficient is it? Certainly the selection step and the n multiplica- tions are no trouble at all, just linear time.3 But (leaving aside interpolation, about which we know even less) how about evaluation?...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
The original problem of size n is in this way recast as two subproblems of size n/2, followed by some linear-time arithmetic. If we could recurse, we would get a divide-and-conquer pro- cedure with running time T(n) = 2T(n/2) + O(n), which is O(n log n), exactly what we want. But we have a problem: The plus-minus trick...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 2.6 The complex roots of unity are ideal for our divide-and-conquer scheme. θ Real Imaginary a b r The complex plane z = a + bi is plotted at position (a, b). Polar coordinates: rewrite as z = r(cos θ + i sin θ) = reiθ, denoted (r, θ). • length r = √ a2 + b2. • angle θ ∈[0, 2π): cos θ = a/r, sin θ = b/r. • θ can...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 2.7 The fast Fourier transform (polynomial formulation) function FFT(A, ω) Input: Coefficient representation of a polynomial A(x) of degree ≤n −1, where n is a power of 2 ω, an nth root of unity Output: Value representation A(ω0), . . . , A(ωn−1) if ω = 1: return A(1) express A(x) in the form Ae(x2) + xAo(x2) ca...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
A matrix reformulation To get a clearer view of interpolation, let’s explicitly set down the relationship between our two representations for a polynomial A(x) of degree ≤n −1. They are both vectors of n numbers, and one is a linear transformation of the other:   A(x0) A(x1) ... A(xn−1)   =   1 x0 x2 0 ·...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 2.8 The FFT takes points in the standard coordinate system, whose axes are shown here as x1, x2, x3, and rotates them into the Fourier basis, whose axes are the columns of Mn(ω), shown here as f1, f2, f3. For instance, points in direction x1 get mapped into direction f1. FFT x1 x3 x2 f3 f1 f2 the Fourier basis. ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
The orthogonality property can be summarized in the single equation MM ∗= nI, since (MM ∗)ij is the inner product of the ith and jth columns of M (do you see why?). This immediately implies M −1 = (1/n)M ∗: we have an inversion formula! But is it the same for- mula we earlier claimed? Let’s see—the (j, k)th entry of M ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 2.9 The fast Fourier transform function FFT(a, ω) Input: An array a = (a0, a1, . . . , an−1), for n a power of 2 A primitive nth root of unity, ω Output: Mn(ω) a if ω = 1: return a (s0, s1, . . . , sn/2−1) = FFT((a0, a2, . . . , an−2), ω2) (s′ 0, s′ 1, . . . , s′ n/2−1) = FFT((a1, a3, . . . , an−1), ω2) for j = ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
a0 a2 a3 j + n/2 j a1 an−1 rj+n/2 FFTn/2 FFTn/2 ... ... an−2 rj FFTn (input: a0, . . . , an−1, output: r0, . . . , rn−1) We’re using a particular shorthand: the edges are wires carrying complex numbers from left to right. A weight of j means “multiply the number on this wire by ωj.” And when two wires come into a junct...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 2.10 The fast Fourier transform circuit.               ! "# $% &' () *+ ,- ./ a0 a4 a2 a6 a1 a5 a7 A(ω1) A(ω2) A(ω3) A(ω4) A(ω5) A(ω6) A(ω7) a3 A(ω0) 1 4 4 4 4 6 6 7 4 4 2 2 6 3 2 5 4 000 100 010 110 001 101 011 111 111 110 101 100 011 010 001 000 77
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
The slow spread of a fast algorithm In 1963, during a meeting of President Kennedy’s scientific advisors, John Tukey, a math- ematician from Princeton, explained to IBM’s Dick Garwin a fast method for computing Fourier transforms. Garwin listened carefully, because he was at the time working on ways to detect nuclear ex...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Exercises 2.1. Use the divide-and-conquer integer multiplication algorithm to multiply the two binary integers 10011011 and 10111010. 2.2. Show that for any positive integers n and any base b, there must some power of b lying in the range [n, bn]. 2.3. Section 2.2 describes a method for solving recurrence relations whi...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
(e) T(n) = 8T(n/2) + n3 (f) T(n) = 49T(n/25) + n3/2 log n (g) T(n) = T(n −1) + 2 (h) T(n) = T(n −1) + nc, where c ≥1 is a constant (i) T(n) = T(n −1) + cn, where c > 1 is some constant (j) T(n) = 2T(n −1) + 1 (k) T(n) = T(√n) + 1 2.6. A linear, time-invariant system has the following impulse response:      ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
2.12. How many lines, as a function of n (in Θ(·) form), does the following program print? Write a recurrence and solve it. You may assume n is a power of 2. function f(n) if n > 1: print_line(‘‘still going’’) f(n/2) f(n/2) 2.13. A binary tree is full if all of its vertices have either zero or two children. Let Bn deno...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
2.21. Mean and median. One of the most basic tasks in statistics is to summarize a set of observations {x1, x2, . . . , xn} ⊆R by a single number. Two popular choices for this summary statistic are: • The median, which we’ll call µ1 • The mean, which we’ll call µ2 (a) Show that the median is the value of µ that minimiz...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
2.24. On page 62 there is a high-level description of the quicksort algorithm. (a) Write down the pseudocode for quicksort. (b) Show that its worst-case running time on an array of size n is Θ(n2). (c) Show that its expected running time satisfies the recurrence relation T(n) ≤O(n) + 1 n n−1 X i=1 (T(i) + T(n −i)). Then...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
(c) In fact, squaring matrices is no easier than matrix multiplication. In this part, you will show that if n × n matrices can be squared in time S(n) = O(nc), then any two n × n matrices can be multiplied in time O(nc). i. Given two n × n matrices A and B, show that the matrix AB + BA can be computed in time 3S(n) + O...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
(a) Show that the following rule is true. gcd(a, b) =    2 gcd(a/2, b/2) if a, b are even gcd(a, b/2) if a is odd, b is even gcd((a −b)/2, b) if a, b are odd (b) Give an efficient divide-and-conquer algorithm for greatest common divisor. (c) How does the efficiency of your algorithm compare to Euclid’s algorithm if a ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
86
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Chapter 3 Decompositions of graphs 3.1 Why graphs? A wide range of problems can be expressed with clarity and precision in the concise pictorial language of graphs. For instance, consider the task of coloring a political map. What is the minimum number of colors needed, with the obvious restriction that neighboring cou...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 3.1 (a) A map and (b) its graph. (a) (b) 2 3 4 5 6 12 1 8 7 9 13 11 10 necessary to use edges with directions on them. There can be directed edges e from x to y (written e = (x, y)), or from y to x (written (y, x)), or both. A particularly enormous example of a directed graph is the graph of all links in the Wor...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Therefore, each edge appears in exactly one of the linked lists if the graph is directed or two of the lists if the graph is undirected. Either way, the total size of the data structure is O(|E|). Checking for a particular edge (u, v) is no longer constant time, because it requires sifting through u’s adjacency list. B...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 3.2 Exploring a graph is rather like navigating a maze. A C B F D H I J K E G L H G D A C F K L J I B E Figure 3.3 Finding all nodes reachable from a particular node. procedure explore(G, v) Input: G = (V, E) is a graph; v ∈V Output: visited(u) is set to true for all nodes u reachable from v visited(v) = true pr...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 3.3.1 The previsit and postvisit procedures are optional, meant for performing operations on a vertex when it is first discovered and also when it is being left for the last time. We will soon see some creative uses for them. More immediately, we need to confirm that explore always works correctly. It certainly do...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 3.4 The result of explore(A) on the graph of Figure 3.2. I E J C F B A D G H Figure 3.5 Depth-first search. procedure dfs(G) for all v ∈V : visited(v) = false for all v ∈V : if not visited(v): explore(v) This loop takes a different amount of time for each vertex, so let’s consider all vertices to- gether. The tot...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 3.6 (a) A 12-node graph. (b) DFS search forest. (a) A B C D E F G H I J K L (b) A B E I J G K F C D H L 1,10 2,3 4,9 5,8 6,7 11,22 23,24 12,21 13,20 14,17 15,16 18,19 These regions are called connected components: each of them is a subgraph that is internally connected but has no edges to the remaining vertices....
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 3.7 DFS on a directed graph. A B C F D E G H A H B C E D F G 12,15 13,14 1,16 2,11 4,7 5,6 8,9 3,10 procedure postvisit(v) post[v] = clock clock = clock + 1 These timings will soon take on larger significance. Meanwhile, you might have noticed from Figure 3.4 that: Property For any nodes u and v, the two interval...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Tree edges are actually part of the DFS forest. Forward edges lead from a node to a nonchild descendant in the DFS tree. Back edges lead to an ancestor in the DFS tree. Cross edges lead to neither descendant nor ancestor; they therefore lead to a node that has already been completely explored (that is, already postvisi...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 3.8 A directed acyclic graph with one source, two sinks, and four possible lineariza- tions. A B C D E F Property A directed graph has a cycle if and only if its depth-first search reveals a back edge. Proof. One direction is quite easy: if (u, v) is a back edge, then there is a cycle consisting of this edge toge...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
the same thing. Since a dag is linearized by decreasing post numbers, the vertex with the smallest post number comes last in this linearization, and it must be a sink—no outgoing edges. Symmet- rically, the one with the highest post is a source, a node with no incoming edges. Property Every dag has at least one source ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 3.9 (a) A directed graph and its strongly connected components. (b) The meta-graph. (a) A D E C F B H G K L J I (b) A B,E C,F D J,K,L G,H,I 3.4.2 An efficient algorithm The decomposition of a directed graph into its strongly connected components is very infor- mative and useful. It turns out, fortunately, that it...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 3.10 The reverse of the graph from Figure 3.9. A D E C F B H G K L J I A B,E C,F D J,K,L G,H,I in C to a node in C′, then the highest post number in C is bigger than the highest post number in C′. Proof. In proving Property 3, there are two cases to consider. If the depth-first search visits component C before co...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
to successively output the second strongly connected component, the third strongly connected component, and so on. The resulting algorithm is this. 1. Run depth-first search on GR. 2. Run the undirected connected components algorithm (from Section 3.2.3) on G, and dur- ing the depth-first search, process the vertices in ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Exercises 3.1. Perform a depth-first search on the following graph; whenever there’s a choice of vertices, pick the one that is alphabetically first. Classify each edge as a tree edge or back edge, and give the pre and post number of each vertex. A B C D E F G H I 3.2. Perform depth-first search on each of the following g...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
(i) A B E G H I C D F J (ii) A B C D E F G H I In each case answer the following questions. (a) In what order are the strongly connected components (SCCs) found? (b) Which are source SCCs and which are sink SCCs? (c) Draw the “metagraph” (each meta-node is an SCC of G). (d) What is the minimum number of edges you must ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
(a) Model this as a graph problem: give a precise definition of the graph involved and state the specific question about this graph that needs to be answered. (b) What algorithm should be applied to solve the problem? (c) Find the answer by applying the algorithm. 3.9. For each node u in an undirected graph, let twodegre...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
3.17. Infinite paths. Let G = (V, E) be a directed graph with a designated “start vertex” s ∈V , a set VG ⊆V of “good” vertices, and a set VB ⊆V of “bad” vertices. An infinite trace p of G is an infinite sequence v0v1v2 · · · of vertices vi ∈V such that (1) v0 = s, and (2) for all i ≥0, (vi, vi+1) ∈E. That is, p is an infi...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
For instance, in the graph below (with prices shown for each vertex), the cost values of the nodes A, B, C, D, E, F are 2, 1, 4, 1, 4, 5, respectively. A B C D E F 1 5 4 6 2 3 Your goal is to design an algorithm that fills in the entire cost array (i.e., for all vertices). (a) Give a linear-time algorithm that works for...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
3.28. In the 2SAT problem, you are given a set of clauses, where each clause is the disjunction (OR) of two literals (a literal is a Boolean variable or the negation of a Boolean variable). You are looking for a way to assign a value true or false to each of the variables so that all clauses are satisfied – that is, the...