MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
MA/CSSE 473 Day 13 Divide and Conquer Closest Points Convex Hull Answer Quiz Question 1.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Chapter 4: Solution of recurrence relationships
The Design and Analysis of Algorithms
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Arithmetic.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
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))”
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
CS Algorithm Analysis1 Algorithm Analysis - CS 312 Professor Tony Martinez.
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
Analysis of Algorithms
Project 2 due … Project 2 due … Project 2 Project 2.
MA/CSSE 473 Day 17 Permutations by lexicographic order number.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
MS 101: Algorithms Instructor Neelima Gupta
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
Chapter 4: Solution of recurrence relationships Techniques: Substitution: proof by induction Tree analysis: graphical representation Master theorem: Recipe.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
MA/CSSE 473 Day 05 More induction Factors and Primes Recursive division algorithm.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
Prepared by John Reif, Ph.D.
CMPT 438 Algorithms.
Introduction to Algorithms: Divide-n-Conquer Algorithms
Design and Analysis of Algorithms
Analysis of Algorithms
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
The Design and Analysis of Algorithms
Analysis of Algorithms
Foundations of Algorithm 유관우
Introduction to Algorithms
Unit 1. Sorting and Divide and Conquer
CS 3343: Analysis of Algorithms
MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer
Algorithm Analysis (for Divide-and-Conquer problems)
CSCE 411 Design and Analysis of Algorithms
CS 3343: Analysis of Algorithms
Objective of This Course
Data Structures Review Session
CS 201 Fundamental Structures of Computer Science
CSE 373 Data Structures Lecture 5
Divide and Conquer (Merge Sort)
CSC 380: Design and Analysis of Algorithms
David Kauchak cs161 Summer 2009
Recurrences.
Divide-and-Conquer 7 2  9 4   2   4   7
Time Complexity and the divide and conquer strategy
Recursion.
Presentation transcript:

MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis Don’t print slide 14 putting on-line: solution to Matrix mult problem.

Student questions on … Syllabus? Course procedures, policies, or resources? Course materials? Homework assignments? Anything else? Roll call notation: lg n means log2 n Also, log n without a specified base will usually mean log2 n

Leftovers Algorithm definition: Sequence of instructions (appropriate for audience) For solving a problem Unambiguous (including order) Can depend on input Terminates in a finite amount of time Session #  day of week algorithm from yesterday

Levitin Algorithm picture

Algorithm design Process Note the iterative process

Interlude What we become depends on what we read after all of the professors have finished with us. The greatest university of all is a collection of books.   - Thomas Carlyle

Review: The Master Theorem The Master Theorem for Divide and Conquer recurrence relations: Consider the recurrence T(n) = aT(n/b) +f(n), T(1)=c, where f(n) = Ѳ(nk) and k≥0 , The solution is Ѳ(nk) if a < bk Ѳ(nk log n) if a = bk Ѳ(nlogba) if a > bk For details, see Levitin pages 490-491 [483-485] or Weiss section 7.5.3. Grimaldi's Theorem 10.1 is a special case of the Master Theorem. Note that page numbers in brackets refer to Levitin 2nd edition Note that we can replace big theta by big O everywhere and it is still true. What are a, b, k for binary search, merge sort (on ClassNotes handout) We will use this theorem often. You should review its proof soon (Weiss's proof is a bit easier than Levitin's). Binary Search Merge sort

Arithmetic algorithms For the next few days: Reading: mostly review from CSSE 230 and DISCO In-class: Some review, but mainly arithmetic algorithms Examples: Fibonacci numbers, addition, multiplication, exponentiation, modular arithmetic, Euclid’s algorithm, extended Euclid. Lots of problems to do some over review material Some over arithmetic algorithms.

Fibonacci Numbers F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … Straightforward recursive algorithm: Correctness is obvious. Why? Because the algorithm exactly mirrors the definition

Analysis of the Recursive Algorithm What do we count? For simplicity, we count basic computer operations Let T(n) be the number of operations required to compute F(n). T(0) = 1, T(1) = 2, T(n) = T(n-1) + T(n-2) + 3 What can we conclude about the relationship between T(n) and F(n)? How bad is that? How long to compute F(200) on an exaflop machine (10^18 operations per second)? http://slashdot.org/article.pl?sid=08/02/22/040239&from=rss Where does the 3 come from? Two comparisons plus a sum. Conclusion: T(N) >= F(N) Show this by Mathematical induction F(N) = (1 / sqrt(5)) * ( (1 + sqrt(5))/2)^N - (1 - sqrt(5))/2)^N ) ) This formula comes from an exercise in Chapter 7 of Weiss. It is tedious but straightforward to prove it by induction. What happens to the second term as N gets large? (1 + sqrt(5))/2 ≈ 1.618, so it is exponential. 1.618^N = 2^(0.694*N) How long would it take to compute F(200)? T(200) >= F(200) >= 2^138. Last question 2^(138-18) = 2^120 seconds. How many seconds in a year? (less than 2^22). So time for F(200) is at least 2^98 years!

A Polynomial-time algorithm? Correctness is obvious because it again directly implements the Fibonacci definition. Analysis? Now (if we have enough space) we can quickly compute F(14000) This algorithm is clearly linear. Or is it? (actually, not linear) Addition is NOT a constant-time operation unless there is a limit on the size of the numbers involved.

A more efficient algorithm? Let X be the matrix Then also How many additions and multiplications of numbers are needed to compute the product of two 2x2 matrices? If n = 2k, how many matrix multiplications does it take to compute Xn? What if n is not a power of 2? Implement it with a partner (details on next slide) Then we will analyze it But there is a catch! The file fib3.py has the solution, and fib3-incomplete.py has it partially completed. Solution for matrix_power: def matrix_power(m, n): result = identity_matrix power = x while n > 0: if n % 2 == 1: result = matrix_multiply(result, power) power = matrix_multiply(power, power) # print "In loop:", n, power, result n = n /2 return result

identity_matrix = [[1,0],[0,1]] #a constant x = [[0,1],[1,1]] #another constant def matrix_multiply(a, b): #why not do loops? return [[a[0][0]*b[0][0] + a[0][1]*b[1][0], a[0][0]*b[0][1] + a[0][1]*b[1][1]], [a[1][0]*b[0][0] + a[1][1]*b[1][0], a[1][0]*b[0][1] + a[1][1]*b[1][1]]] def matrix_power(m, n): #efficiently calculate mn result = identity_matrix # Fill in the details return result def fib (n) : return matrix_power(x, n)[0][1] # Test code print ([fib(i) for i in range(11)]) The file fib3.py has the solution, and fib3-incomplete.py has it partially completed. Solution for matrix_power: def matrix_power(m, n): result = identity_matrix power = x while n > 0: if n % 2 == 1: result = matrix_multiply(result, power) power = matrix_multiply(power, power) # print "In loop:", n, power, result n = n /2 return result