Introduction Credits: Steve Rudich, Jeff Edmond, Ping Xuan.

Slides:



Advertisements
Similar presentations
5/1/20151 Analysis of Algorithms Introduction. 5/1/20152 Are you want to be a computer scientist?
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Grade School Revisited: How To Multiply Two Numbers Great Theoretical Ideas In Computer Science Victor Adamchik Danny Sleator CS Spring 2010 Lecture.
Spring 2015 Lecture 5: QuickSort & Selection
Asymptotic Growth Rate
CS421 - Course Information Website Syllabus Schedule The Book:
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.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Introduction to Algorithm design and analysis
Grade School Revisited: How To Multiply Two Numbers Great Theoretical Ideas In Computer Science S. Rudich V. Adamchik CS Spring 2006 Lecture 18March.
COMPSCI 102 Introduction to Discrete Mathematics.
CSE 3101E Design and Analysis of Algorithms Prof. J. Elder.
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
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))”
Program Performance & Asymptotic Notations CSE, POSTECH.
1 Welcome Jeff Edmonds York University Lecture 0 COSC 2011 Jeff Edmonds CSB 3044, ext
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Lecture 2 Computational Complexity
Mathematics Review and Asymptotic Notation
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Great Theoretical Ideas in Computer Science.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
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.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Getting Started Introduction to Algorithms Jeff Chastine.
Asymptotic Notation (O, Ω, )
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
COMPSCI 102 Introduction to Discrete Mathematics.
J. Elder COSC 3101N Thinking about Algorithms Abstractly Lecture 1. Introduction.
Introduction Pizzanu Kanongchaiyos, Ph.D.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
COSC 3101Winter, 2004 COSC 3101 Design and Analysis of Algorithms Lecture 2. Relevant Mathematics: –The time complexity of an algorithm –Adding made easy.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 5: Algorithms.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Vishnu Kotrajaras, PhD.1 Data Structures
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Grade School Revisited: How To Multiply Two Numbers CS Lecture 4 2 X 2 = 5.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
CMPT 438 Algorithms.
Introduction to Algorithms
Growth of functions CSC317.
Great Theoretical Ideas in Computer Science
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
Data Structures Review Session
Programming and Data Structure
Grade School Revisited: How To Multiply Two Numbers
Ch. 2: Getting Started.
At the end of this session, learner will be able to:
Introduction to Discrete Mathematics
Algorithms and Data Structures Lecture II
Presentation transcript:

Introduction Credits: Steve Rudich, Jeff Edmond, Ping Xuan

So you want to be a bioinformatician /mathematician /computer scientist? What is an Algorithm ? Grade school revisited: How to multiply two numbers

So you want to be a computer scientist?

Is your goal to be a mundane programmer?

Or a great leader and thinker?

Original Thinking

Boss assigns task: –Given today’s prices of pork, grain, sawdust, … –Given constraints on what constitutes a hotdog. –Make the cheapest hotdog. Everyday industry asks these questions.

Um? Tell me what to code. With more suffocated software engineering systems, the demand for mundane programmers will diminish. Your answer:

I learned this great algorithm that will work. Soon all known algorithms will be available in libraries.

Your answer: I can develop a new algorithm for you. Great thinkers will always be needed.

–Content: An up to date grasp of fundamental problems and solutions –Method: Principles and techniques to solve the vast array of unfamiliar problems that arise in a rapidly changing field The future belongs to the computer scientist who has

Course Content A list of algoirthms. –Learn their code. –Trace them until you are convenced that they work. –Impliment them. class InsertionSortAlgorithm extends SortAlgorithm { void sort(int a[]) throws Exception { for (int i = 1; i < a.length; i++) { int j = i; int B = a[i]; while ((j > 0) && (a[j-1] > B)) { a[j] = a[j-1]; j--; } a[j] = B; }}

Course Content A survey of algorithmic design techniques. Abstract thinking. How to develop new algorithms for any problem that may arise.

Study: Many experienced programmers were asked to code up binary search.

Study: Many experienced programmers were asked to code up binary search. 80% got it wrong Good thing is was not for a nuclear power plant.

What did they lack?

Formal proof methods?

What did they lack? Formal proof methods? Yes, likely Industry is starting to realize that formal methods are important. But even without formal methods …. ?

What did they lack? Fundamental understanding of the algorithmic design techniques. Abstract thinking.

Course Content Notations, analogies, and abstractions for developing, thinking about, and describing algorithms

You will see some Math … Recurrence Relations T(n) = a T(n/b) + f(n) Time Complexity t(n) =  (n 2 )

What is an Algorithm?  A step-by-step description of procedures performing certain task. Example: Sorting. Given a list of numbers, put them in increasing (non-decreasing) order.  Properties: Generality, Termination, Correctness, Feasibility.  Feasibility  Analysis of Algorithm Complexity theory

Analysis of Algorithm  Running time analysis Input size N Running time is a function of N: T(N)  Worst case, average case, …, etc  Time measured by number of computer operations  Each basic operation (add, load, write, etc) count as 1  Actual clock time differs by a constant factor  Usually very complex The use of asymptotic bounds  To study the rate of growth of T(N) compared to simpler functions f(N), such as N, N 2, log(N), etc  A constant factor can be ignored

Some Definitions: Big O Notation  T(N) = O( f(N) ) Exists constant c and n 0 such that when N>n 0, T(N) <= c * f(N) Asymptotic Upper bound c f(N) T(N) N n0n0

Some Definitions: Big Omega  T(N) = (g(N)) Exists constant c and n 0 such that when N>n 0, T(N) >= c * g(N) Asymptotic Lower bound c g(N) T(N) N n0n0

Some Definitions: Big Theta  T(N) = ( h(N) ) if and only if T(N)=O(h(N)) and T(N)= (h(N)) tight bound c 2 h(N) T(N) N c 1 h(N)

Some Definitions: Little “o”  T(N) = o(p(N)) if lim N  ∞ = o. E.g. log(N) = o(N). c p(N) T(N) N

Example: Insertion Sort Algorithm  class InsertionSortAlgorithm extends SortAlgorithm {  void sort(int a[ ]) throws Exception {  for (int i = 1; i < a.length; i++) {  int j = i;  int B = a[i];  while ((j > 0) && (a[j-1] > B)) {  a[j] = a[j-1];  j--; }  a[j] = B;  }}

Iterative Algorithms i-1 i i i 0 T+1 codeA loop exit when codeB codeC 9 km 5 km Code Relay Race One step at a time

Problem Specification Precondition: The input is a list of n values with the same value possibly repeated. Post condition: The output is a list consisting of the same n values in non-decreasing order ,23,25,30,31,52,62,79,88,98

Define Step Select arbitrary element from side. Insert it where it belongs ,31,52, ,31,52,62,

Making progress while Maintaining the loop invariant ,31,52, ,31,52,62, km75 km 5 elements to school 6 elements to school Exit Sorted sub-list

n elements to school 14,23,25,30,31,52,62,79,88,98 0 elements to school Beginning & Ending Exit 0 kmExit

Running Time Inserting an element into a list of size i takes O (i) time in the worst case. Total = … +n = n(n+1)/2 =  (n 2 ) ,31,52, ,31,52,62,

Explaining Insertion Sort We maintain a subset of elements sorted within a list. The remaining elements are off to the side somewhere. Initially, think of the first element in the array as a sorted list of length one. One at a time, we take one of the elements that is off to the side and we insert it into the sorted list where it belongs. This gives a sorted list that is one element longer than it was before. When the last element has been inserted, the array is completely sorted.

Insertion Sort The input consists of an array of integers 52,23,88,31,25,30,98,62,14,7923,31,52,88 We read in the i+1 st object. We will pretend that this larger prefix is the entire input. We extend the solution we have by one for this larger prefix. 23,25,31,52,88

Insertion Sort Algorithm: Pseudo-Code  Input: an array (or list) a of numbers // data structure: a[0] … a[n-1]  Operations: Leave the first element (location 0) untouched. for each element from location 1 on insert it to the appropriate place in the (sorted) sub-array to its left. a 012n-1

Operations in an Algorithm  Decision Making branching operation if … then … else …  Repetition loops: for each element do { … } conditional loops: while ( …. ) do { … }

Insertion Sort Algorithm: Code  class InsertionSortAlgorithm extends SortAlgorithm {  void sort(int a[ ]) throws Exception {  for (int i = 1; i < a.length; i++) {  int j = i;  int b = a[i];  while ( (j > 0) && (a[j-1] > b) ) {  a[j] = a[j-1];  j--; }  a[j] = B;  }}}

Grade School Revisited: How To Multiply Two Numbers 2 X 2 = 5 Another Algorithm

Complex Numbers Remember how to multiply 2 complex numbers? (a+bi)(c+di) = [ac –bd] + [ad + bc] i Input: a,b,c,d Output: ac-bd, ad+bc If a real multiplication costs $1 and an addition cost a penny. What is the cheapest way to obtain the output from the input? Can you do better than $4.02?

Gauss’ $3.05 Method: Input: a,b,c,d Output: ac-bd, ad+bc m 1 = ac m 2 = bd A 1 = m 1 – m 2 = ac-bd m 3 = (a+b)(c+d) = ac + ad + bc + bd A 2 = m 3 – m 1 – m 2 = ad+bc

Question: The Gauss “hack” saves one multiplication out of four. It requires 25% less work. Could there be a context where performing 3 multiplications for every 4 provides a more dramatic savings?

Odette Bonzo

How to add 2 n-bit numbers. ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** +

*** *** ** ** ** ** ** ** ** ** ** ** *** *** ** ** ** ** ** ** ** ** +

*** *** ** ** ** ** ** ** ** ** *** *** **** **** ** ** ** ** ** ** ** ** +

*** *** ** ** ** ** ** ** *** *** **** **** **** **** ** ** ** ** ** ** ** ** +

*** *** ** ** ** ** *** *** **** **** **** **** **** **** ** ** ** ** ** ** ** ** +

*** *** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** * * * * + * ** *

Time complexity of grade school addition *** *** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** + * ** * **** **** T(n) = The amount of time grade school addition uses to add two n-bit numbers = θ(n) = linear time. On any reasonable computer adding 3 bits can be done in constant time.

# of bits in numbers timetime f = θ (n) means that f can be sandwiched between two lines

Please feel free to ask questions!

Is there a faster way to add? QUESTION: Is there an algorithm to add two n-bit numbers whose time grows sub- linearly in n?

Any algorithm for addition must read all of the input bits –Suppose there is a mystery algorithm that does not examine each bit –Give the algorithm a pair of numbers. There must be some unexamined bit position i in one of the numbers –If the algorithm is not correct on the numbers, we found a bug –If the algorithm is correct, flip the bit at position i and give the algorithm the new pair of numbers. It give the same answer as before so it must be wrong since the sum has changed

So any algorithm for addition must use time at least linear in the size of the numbers. Grade school addition is essentially as good as it can be.

How to multiply 2 n-bit numbers. X * * * * * * * * * * * * * * * * * * * * * * * * * * * * n2n2

I get it! The total time is bounded by cn 2.

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 # of bits in numbers timetime

Neat! We have demonstrated that multiplication is a harder problem than addition. Mathematical confirmation of our common sense.

Don’t jump to conclusions! We have argued that grade school multiplication uses more time than grade school addition. This is a comparison of the complexity of two algorithms. To argue that multiplication is an inherently harder problem than addition we would have to show that no possible multiplication algorithm runs in linear time.

Grade School Addition: θ (n) time 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, Tunghai will give you a PhD!

Is there a faster way to multiply two numbers than the way you learned in grade school?

Divide and Conquer (an approach to faster algorithms) DIVIDE my instance to the problem into smaller instances to the same problem. Have a friend (recursively) solve them. Do not worry about it yourself. GLUE the answers together so as to obtain the answer to your larger instance.

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

Multiplication of 2 n-bit numbers X = Y = XY = ac 2 n + (ad+bc) 2 n/2 + bd ab cd MULT( X, Y ) : If |X| = |Y| = 1 then RETURN XY 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)

Time required by MULT T( n ) = time taken by MULT on two n-bit numbers What is T(n)? What is its growth rate? Is it θ(n 2 )?

Recurrence Relation T(1) = k for some constant k T(n) = 4 T(n/2) + k n + l for some constants k and l MULT(X,Y): If |X| = |Y| = 1 then RETURN XY 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)

Let’s be concrete T(1) = 1 T(n) = 4 T(n/2) + n How do we unravel T(n) so that we can determine its growth rate?

Technique 1 Guess and Verify Recurrence Relation: T(1) = 1 & T(n) = 4T(n/2) + n Guess: G(n) = 2n 2 – n Verify: Left Hand SideRight Hand Side T(1) = 2(1) 2 – 1 T(n) = 2n 2 – n 1 4T(n/2) + n = 4 [2( n / 2 ) 2 – ( n / 2 )] + n = 2n 2 – n

Technique 2: Decorate The Tree T(n) = n + 4 T(n/2) n T(n/2) T(n) = T(n) = n + 4 T(n/2) n T(n/2) T(n) = T(1) T(1) = 1 1 =

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

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

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

n T(n) = n/ n/4

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

=1  n = 4  n/2 = 16  n/4 = 4 i  n/2 i Total: θ( n log4 ) = θ( n 2 ) = 4 logn  n/2 logn =n log4  1

Divide and Conquer MULT: θ (n 2 ) time Grade School Multiplication: θ (n 2 ) time All that work for nothing!

MULT revisited MULT calls itself 4 times. Can you see a way to reduce the number of calls? MULT(X,Y): If |X| = |Y| = 1 then RETURN XY 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)

Gauss’ Hack: Input: a,b,c,d Output: ac, ad+bc, bd A 1 = ac A 3 = bd m 3 = (a+b)(c+d) = ac + ad + bc + bd A 2 = m 3 – A 1 - A 3 = ad + bc

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 Break X into a;b and Y into c;d e = MULT(a,c) and f =MULT(b,d) RETURN e2 n + (MULT(a+b, c+d) – e - f) 2 n/2 + f

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

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

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

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

n n/2 + n/2 + n/2 Level i is the sum of 3 i copies of n/2 i n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/ i

=1  n = 3  n/2 = 9  n/4 = 3 i  n/2 i Total: θ( n log3 ) = θ( n ) = 3 logn  n/2 logn =n log3  1

Dramatic improvement for large n Not just a 25% savings! θ( n 2 ) vs θ( n )

Multiplication Algorithms Kindergarten ? n2 n Grade Schooln2n2 Karatsuban 1.58… Fastest Knownn log(n) log(log(n)) Homework 3*4=

You’re cool! Are you free sometime this weekend? Not interested, Bonzo. I took the initiative and asked out a guy in my 4277 class.