* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Algorithms Lecture 5 Name:___________________________
Computational electromagnetics wikipedia , lookup
Corecursion wikipedia , lookup
Post-quantum cryptography wikipedia , lookup
Theoretical computer science wikipedia , lookup
Recursion (computer science) wikipedia , lookup
Inverse problem wikipedia , lookup
Lateral computing wikipedia , lookup
Pattern recognition wikipedia , lookup
Fisher–Yates shuffle wikipedia , lookup
Factorization of polynomials over finite fields wikipedia , lookup
Algorithm characterizations wikipedia , lookup
Travelling salesman problem wikipedia , lookup
Mathematical optimization wikipedia , lookup
Knapsack problem wikipedia , lookup
Multiple-criteria decision analysis wikipedia , lookup
Multiplication algorithm wikipedia , lookup
Simplex algorithm wikipedia , lookup
Computational complexity theory wikipedia , lookup
Genetic algorithm wikipedia , lookup
Recurrence relation wikipedia , lookup
Algorithms Lecture 5 Name:___________________________ In "divide-and-conquer" algorithms (e.g., recursive fibonacci, binary search, merge sort, ...): Large Problem Smaller Problem . . . 1) divide large problem into one or more smaller problems Smaller Problem 2) "conquer"/solve smaller problem(s) (usually recursively) 3) combine solution(s) of smaller problem(s) to get solution to larger problem 1. I want you to think about when a divide-and-conquer algorithm might be “fast enough.” a.) Why was recursive fibonacci so slow? b) Why is binary search so fast? c) Simple sorts (e.g., bubble, selection, insertion) are all Θ(n2), we can employ divide-and-conquer to develop Θ(nlog n) “advanced” sorting algorithms: Merge sort and Quick Sort. Consider why a divide-and-conquer sort might be more efficient. Assume that I had a simple Θ(n2) sorting algorithm with n = 100, then there is roughly 1002 / 2 or 5,000 amount of work. Suppose I split the problem down into two smaller problems of size 50. If I run the n2 algorithm on both smaller problems of size 50, then what would be the approximate amount of work? If I further solve the problems of size 50 by splitting each of them into two problems of size 25, then what would be the approximate amount of work? What other work do we need to consider in this analysis? Lecture 5 Page 1 Algorithms Lecture 5 Name:___________________________ 2. Quick sort is another advanced sort that often is quicker than merge sort (hence its name). The general idea is as follows. Assume “n” items to sort. Select a “random” item in the unsorted part as the pivot Rearrange (called partitioning) the unsorted items such that: Pivot Index All items < to Pivot Pivot Item All items >= to Pivot Quick sort the unsorted part to the left of the pivot Quick sort the unsorted part to the right of the pivot a) What base case(s) would we have? b) What would be the theta notation (Θ( )) of the partition function which returns the index of the pivot after this rearrangement? c) What would the recurrence relation be for the following cases? Worst-case recurrence Best-case recurrence Lecture 5 Page 2 Algorithms Lecture 5 Name:___________________________ 3. Section 2.6: Arithmetic with Large Integers: (too big for built-in integer representation) Representation: one decimal digit per array element with sign (0 means positive and 1 means negative) u = 432,581 6 5 4 3 2 1 4 3 2 5 8 1 0 v = 6,741,312 7 6 5 4 3 2 1 6 7 4 1 3 1 2 0 Let n be the number of digits in larger integers. What are the Θ( ) for the large integer operations: addition/subtraction? u ∗ 10m ? u divide 10m ? u rem 10m ? multiplication of two large integers using grammar school “long multiplication” algorithm? u = 432,581 v = 6,741,312 4 x 6 7 8 4 3 3 4 6 2 ... 2 1 5 5 4 5 3 1 8 3 8 1 1 2 6 2 1 .. + ... 2 7 2 4. To apply divide-and-conquer to multiplication, we can divide the n-digit value roughly in two where m = n/2. For example, u = 432,581 = 432 x 103 + 581 and v = 6741 x 103 + 312. In general, m m m v = w * 10 + z n n/2 digits digits m uv = (x * 10 + y) (w * 10 + z) u = x * 10 + y Complete: n/2 = m digits uv = 2m m * 10 + ( ) * 10 + 5. Write the recurrence for multiplying uv assuming n-digit values. T(n) = 6. Use the “Master Theorem” to determine the Θ( ) for the large-integer-multiplication recurrence. Suppose a complexity function T(n) is eventually nondecreasing and satisfies the recurrence T(n) = aT(n/b) + cn k for n > 1, n a power of b T(1) = d, where constant a > 0, b is an integer ≥ 2, c > 0, d ≥ 0, and k is an integer ≥ 0. Then T(n) ∈ { θ ( nk ) θ ( nk log n) θ ( nlogba ) if a < bk if a = bk if a > bk a= b= k= Lecture 5 Page 3 Algorithms Lecture 5 Name:___________________________ 7. The “trick” to improving performance: Let r = (x + y) (w + z) r = xw + (xz + wy) + yz (xz + wy) = r - xw - yz, substitute for r (xz + wy) = (x + y) (w + z) - xw - yz a) Rewrite the uv equation by replacing (xz + wy). Recall, u = x * 10m + y, v = w * 10m + z, and m = n/2. uv = xw * 102m + (xz + wy)*10m + yz uv = xw * 102m + ( )*10m + yz b) Write the recurrence for this version of multiplying uv assuming n-digit values. T(n) = c) Use the “Master Theorem” to determine the Θ( ) for this large-integer-multiplication recurrence. Lecture 5 Page 4