15-251 Great Theoretical Ideas in Computer Science.

Slides:



Advertisements
Similar presentations
Grade School Revisited: How To Multiply Two Numbers Great Theoretical Ideas In Computer Science Anupam Gupta Danny Sleator CS Fall 2010 Lecture.
Advertisements

CS Divide and Conquer/Recurrence Relations1 Divide and Conquer.
Lecture 3: Parallel Algorithm Design
Grade School Again: A Parallel Perspective Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2004 Lecture 17Mar 16, 2004Carnegie.
Recursion Lecture 18: Nov 18.
Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS Fall 2005 Lecture 5Sept 13, 2005Carnegie.
Lecture 4. Paradigm #2 Recursion Last time we discussed Fibonacci numbers F(n), and Alg fib(n) if (n
Grade School Revisited: How To Multiply Two Numbers Great Theoretical Ideas In Computer Science Victor Adamchik Danny Sleator CS Spring 2010 Lecture.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Lecture 4: Divide and Conquer III: Other Applications and Examples Shang-Hua Teng.
Introduction Credits: Steve Rudich, Jeff Edmond, Ping Xuan.
Grade School Revisited: How To Multiply Two Numbers Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2004 Lecture 16March 4,
Recursion Credits: Jeff Edmonds, Ping Xuan. MULT(X,Y): If |X| = |Y| = 1 then RETURN XY Break X into a;b and Y into c;d e = MULT(a,c) and f =MULT(b,d)
On Time Versus Input Size Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2004 Lecture 15March 2, 2004Carnegie Mellon University.
Introduction By Jeff Edmonds York University COSC 3101 Lecture 1 So you want to be a computer scientist? Grade School Revisited: How To Multiply Two Numbers.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Grade School Revisited: How To Multiply Two Numbers Great Theoretical Ideas In Computer Science S. Rudich V. Adamchik CS Spring 2006 Lecture 18March.
Arithmetic.
COMPSCI 102 Introduction to Discrete Mathematics.
MA/CSSE 473 Day 03 Asymptotics A Closer Look at Arithmetic With another student, try to write a precise, formal definition of “t(n) is in O(g(n))”
Divide-and-Conquer 7 2  9 4   2   4   7
Mathematics Review and Asymptotic Notation
Computer Algorithms Ch. 2
Data Structures and Algorithms A. G. Malamos
Project 2 due … Project 2 due … Project 2 Project 2.
Recursive Algorithms Introduction Applications to Numeric Computation.
Recursion Jeff Edmonds York University COSC 6111 Lecture 3 Friends & Steps for Recursion Derivatives Recursive Images Multiplying Parsing Ackermann.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
COMPSCI 102 Introduction to Discrete Mathematics.
J. Elder COSC 3101N Thinking about Algorithms Abstractly Lecture 1. Introduction.
Introduction Pizzanu Kanongchaiyos, Ph.D.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
4/3/2003CSE More Math CSE Algorithms Euclidean Algorithm Divide and Conquer.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
COMPSCI 102 Discrete Mathematics for Computer Science.
COSC 3101Winter, 2004 COSC 3101 Design and Analysis of Algorithms Lecture 2. Relevant Mathematics: –The time complexity of an algorithm –Adding made easy.
Choose Your Representation! Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2005 Lecture 14 Feb 24, 2005 Carnegie Mellon University.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Introduction: Content And Method Professors Steven Rudich and Bruce Maggs Carnegie Mellon University CS Lecture 1.
1 How to Multiply Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. integers, matrices, and polynomials.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Grade School Revisited: How To Multiply Two Numbers CS Lecture 4 2 X 2 = 5.
Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS Fall 2006 Lecture 5Sept 12, 2006Carnegie.
On Time Versus Input Size Great Theoretical Ideas In Computer Science S. Rudich V. Adamchik CS Spring 2006 Lecture 17March 21, 2006Carnegie Mellon.
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Introduction to Analysis of Algorithms
Lecture 3: Parallel Algorithm Design
CSC 427: Data Structures and Algorithm Analysis
CS 3343: Analysis of Algorithms
Great Theoretical Ideas in Computer Science
Great Theoretical Ideas in Computer Science
Lecture 3: Divide-and-conquer
How To Multiply Two Numbers
CS 3343: Analysis of Algorithms
Grade School Revisited: How To Multiply Two Numbers
CS 3343: Analysis of Algorithms
By Jeff Edmonds York University
Algorithm design and Analysis
Data Structures Review Session
Grade School Revisited: How To Multiply Two Numbers
Introduction to Discrete Mathematics
Topic: Divide and Conquer
Introduction to Discrete Mathematics
Presentation transcript:

Great Theoretical Ideas in Computer Science

Grade School Revisited: How To Multiply Two Numbers Lecture 23 (November 13, 2007)

Gauss (a+bi)

Gauss’ Complex Puzzle Can you do better than $4.02? Remember how to multiply two complex numbers a + bi and c + di? (a+bi)(c+di) = [ac –bd] + [ad + bc] i Input: a,b,c,d Output: ac-bd, ad+bc If multiplying two real numbers costs $1 and adding them costs a penny, what is the cheapest way to obtain the output from the input?

Gauss’ $3.05 Method Input:a,b,c,d Output: ac-bd, ad+bc X 1 = a + b X 2 = c + d X 3 = X 1 X 2 = ac + ad + bc + bd X 4 = ac X 5 = bd X 6 = X 4 – X 5 = ac - bd X 7 = X 3 – X 4 – X 5 = bc + ad c $ $ $ c c cc

The Gauss optimization saves one multiplication out of four. It requires 25% less work.

+ T(n) = amount of time grade school addition uses to add two n-bit numbers * * * * * * * * * * * * * * * * Time complexity of grade school addition We saw that T(n) was linear T(n) = Θ (n)

Time complexity of grade school multiplication T(n) = The amount of time grade school multiplication uses to add two n-bit numbers We saw that T(n) was quadratic T(n) = Θ (n 2 ) X * * * * * * * * * * * * * * * * * * * * * * * * * * * * n2n2

# of bits in the numbers timetime Grade School Addition: Linear time Grade School Multiplication: Quadratic time No matter how dramatic the difference in the constants, the quadratic curve will eventually dominate the linear curve

Is there a sub-linear time method for addition?

Any addition algorithm takes Ω (n) time Claim: Any algorithm for addition must read all of the input bits Proof: Suppose there is a mystery algorithm A that does not examine each bit Give A a pair of numbers. There must be some unexamined bit position i in one of the numbers

* * * * * * * * * * * * * * A did not read this bit at position i Any addition algorithm takes Ω (n) time If A is not correct on the inputs, we found a bug If A is correct, flip the bit at position i and give A the new pair of numbers. A gives the same answer as before, which is now wrong.

Grade school addition can’t be improved upon by more than a constant factor

Grade School Addition: Θ (n) time. Furthermore, it is optimal Grade School Multiplication: Θ (n 2 ) time Is there a clever algorithm to multiply two numbers in linear time? Despite years of research, no one knows! If you resolve this question, Carnegie Mellon will give you a PhD!

Can we even break the quadratic time barrier? In other words, can we do something very different than grade school multiplication?

Divide And Conquer An approach to faster algorithms: DIVIDE a problem into smaller subproblems CONQUER them recursively GLUE the answers together so as to obtain the answer to the larger problem

X = Y = ab cd X = a 2 n/2 + b n/2 bits n bits X × Y = ac 2 n + (ad + bc) 2 n/2 + bd X Y Multiplication of 2 n-bit numbers Y = c 2 n/2 + d

Multiplication of 2 n-bit numbers X = Y = ab cd n/2 bits X × Y = ac 2 n + (ad + bc) 2 n/2 + bd MULT(X,Y): If |X| = |Y| = 1 then return XY else break X into a;b and Y into c;d return MULT(a,c) 2 n + (MULT(a,d) + MULT(b,c)) 2 n/2 + MULT(b,d)

Same thing for numbers in decimal! X = Y = ab cd X = a 10 n/2 + b Y = c 10 n/2 + d n/2 digits n digits X × Y = ac 10 n + (ad + bc) 10 n/2 + bd

Multiplying (Divide & Conquer style) X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd 1234* * *21 12*39 34*21 34*39 1*2 1*1 2*2 2* Hence: 12*21 = 2* (1 + 4) = * * *4276

Multiplying (Divide & Conquer style) X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd 1234* * * * * *21 12*39 34*21 34* * * * *1=

Multiplying (Divide & Conquer style) X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd 1234* * * * * * * * *1 =

Multiplying (Divide & Conquer style) X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd * =

Divide, Conquer, and Glue MULT(X,Y)

if |X| = |Y| = 1 then return XY, else… Divide, Conquer, and Glue MULT(X,Y):

X=a;b Y=c;d Divide, Conquer, and Glue MULT(X,Y): Mult( a,c ) Mult( a,d ) Mult( b,c ) Mult( b,d )

X=a;b Y=c;d Divide, Conquer, and Glue MULT(X,Y): Mult(a,c) Mult( a,d ) Mult( b,c ) Mult( b,d )

X=a;b Y=c;d Divide, Conquer, and Glue MULT(X,Y): ac Mult( a,d ) Mult( b,c ) Mult( b,d )

X=a;b Y=c;d Divide, Conquer, and Glue MULT(X,Y): ac Mult(a,d) Mult( b,c ) Mult( b,d )

X=a;b Y=c;d Divide, Conquer, and Glue MULT(X,Y): ac ad Mult( b,c ) Mult( b,d )

X=a;b Y=c;d Divide, Conquer, and Glue MULT(X,Y): ac ad Mult(b,c) Mult( b,d )

X=a;b Y=c;d Divide, Conquer, and Glue MULT(X,Y): ac ad bc Mult( b,d )

X=a;b Y=c;d Divide, Conquer, and Glue MULT(X,Y): ac ad bc Mult(b,d)

X=a;b Y=c;d Divide, Conquer, and Glue MULT(X,Y): ac ad bc bd XY = ac2 n +(ad+bc)2 n/2 + bd

Time required by MULT T(n) = time taken by MULT on two n-bit numbers What is T(n)? What is its growth rate? Big Question: Is it Θ (n 2 )? T(n) = 4 T(n/2) conquering time divide and glue + (k’n + k’’)

Recurrence Relation T(1) = k for some constant k T(n) = 4 T(n/2) + k’n + k’’ for constants k’ and k’’

Simplified Recurrence Relation T(1) = 1 T(n) = 4 T(n/2) + n conquering time divide and glue

n = T(n) T(n/2)

n = T(n) T(n/2) n/2 T (n/4) T (n/4) T (n/4) T (n/4)

n = T(n) T(n/2) n/2 T (n/4) T (n/4) T (n/4) T (n/4) n/2 T (n/4) T (n/4) T (n/4) T (n/4)

n n/2 + n/2 + n/2 + n/ i log 2 (n) Level i is the sum of 4 i copies of n/2 i

n n/2 + n/2 + n/2 + n/2 Level i is the sum of 4 i copies of n/2 i n = 4n = 2 i n = (n)n = 1n = n( n) = n(2n-1) = 2n 2 -n

Divide and Conquer MULT: Θ (n 2 ) time Grade School Multiplication: Θ (n 2 ) time Bummer!

MULT revisited MULT(X,Y): If |X| = |Y| = 1 then return XY else break X into a;b and Y into c;d return MULT(a,c) 2 n + (MULT(a,d) + MULT(b,c)) 2 n/2 + MULT(b,d) MULT calls itself 4 times. Can you see a way to reduce the number of calls?

Input:a,b,c,d Output: ac-bd, ad+bc X 1 = a + b X 2 = c + d X 3 = X 1 X 2 = ac + ad + bc + bd X 4 = ac X 5 = bd X 6 = X 4 – X 5 = ac - bd X 7 = X 3 – X 4 – X 5 = bc + ad c $ $ $ c c cc Gauss’ optimization

Karatsuba, Anatolii Alexeevich (1937-) Sometime in the late 1950’s Karatsuba had formulated the first algorithm to break the n 2 barrier!

Gaussified MULT (Karatsuba 1962) T(n) = 3 T(n/2) + n Actually: T(n) = 2 T(n/2) + T(n/2 + 1) + kn MULT(X,Y): If |X| = |Y| = 1 then return XY else break X into a;b and Y into c;d e : = MULT(a,c) f := MULT(b,d) e 2 n + (MULT(a+b,c+d) – e – f) 2 n/2 + f return

n = T(n) T(n/2)

n = T(n) T(n/2) n/2 T (n/4) T (n/4) T (n/4)

n = T(n) T(n/2) n/2 T (n/4) T (n/4) T (n/4) n/2 T (n/4) T (n/4) T (n/4)

n = T(n) n/2 T (n/4) T (n/4) T (n/4) n/2 T (n/4) T (n/4) T (n/4) n/2 T (n/4) T (n/4) T (n/4)

n n/2 + n/2 + n/ i log 2 (n) Level i is the sum of 3 i copies of n/2 i

n n/2 + n/2 + n/2 Level i is the sum of 3 i copies of n/2 i /2n = 9/4n = (3/2) i n = (3/2) log n n = 1n = n(1+3/2+(3/2) (3/2) log 2 n ) = 3n 1.58… – 2n

Dramatic Improvement for Large n T(n) = 3n log 2 3 – 2n = Θ (n log 2 3 ) = Θ (n 1.58… ) A huge savings over Θ (n 2 ) when n gets large.

n n2n2

Multiplication Algorithms Kindergartenn2 n Grade Schooln2n2 Karatsuban 1.58… Fastest Knownn logn loglogn

n2n2 n n log(n) loglog(n) n 1.584

A case study Anagram Programming Task. You are given a 70,000 word dictionary. Write an anagram utility that given a word as input returns all anagrams of that word appearing in the dictionary.

Examples Input: CAT Output: ACT, CAT, TAC Input: SUBESSENTIAL Output: SUITABLENESS

(Novice Level Solution) Loop through all possible ways of rearranging the input word Use binary search to look up resulting word in dictionary. If found, output it

Performance Analysis Counting without executing On the word “microphotographic”, we loop 17!  3 * times. Even at 1 microsecond per iteration, this will take 3 *10 8 seconds. Almost a decade! (There are about  seconds in a nanocentury.)

“Expert” Level Solution Module ANAGRAM(X,Y) returns TRUE exactly when X and Y are anagrams. (Works by sorting the letters in X and Y) Input: X Loop through all dictionary words Y If ANAGRAM(X,Y) output Y

The hacker is satisfied and reflects no further Comparing an input word with each of 70,000 dictionary entries takes about 15 seconds

The master keeps trying to refine the solution The master’s program runs in less than 1/1000 seconds.

Master Solution Don’t just keep the dictionary in sorted order! Rearranging the dictionary into “anagram classes” makes the original problem simpler.

Suppose the dictionary was the list below. ASP DOG LURE GOD NICE RULE SPA

After each word, write its “signature” (sort its letters) ASPAPS DOGDGO LUREELRU GODDGO NICECEIN RULEELRU SPAAPS

Sort by the signatures ASPAPS SPAAPS NICECEIN DOGDGO GODDGO LUREELRU RULEELRU

The Master’s Program Input word W X := signature of W (sort the letters) Use binary search to find the anagram class of W and output it.

A useful tool: preprocessing… Of course, it takes about 30 seconds to create the dictionary, but it is perfectly fair to think of this as programming time. The building of the dictionary is a one-time cost that is part of writing the program.

Here’s What You Need to Know… Gauss’s Multiplication Trick Proof of Lower bound for addition Divide and Conquer Solving Recurrences Karatsuba Multiplication Preprocessing