CSE 326: Data Structures Lecture #3 Asymptotic Analysis Steve Wolfman Winter Quarter 2000.

Slides:



Advertisements
Similar presentations
CSE 373 Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III.
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.
Recitation on analysis of algorithms. runtimeof MergeSort /** Sort b[h..k]. */ public static void mS(Comparable[] b, int h, int k) { if (h >= k) return;
CSE 326 Analyzing Recursion David Kaplan Dept of Computer Science & Engineering Autumn 2001.
CSE 326 Asymptotic Analysis David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Introduction to Analysis of Algorithms
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. Reminder: Recursive algorithm Reduce problem to one or more sub-problems of smaller sizes (linear or tree.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
CSE 326: Data Structures Lecture #2 Analysis of Algorithms Alon Halevy Fall Quarter 2000.
Analysis of Recursive Algorithms
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Analysis of Recursive Algorithms October 29, 2014
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
CS221: Algorithms and Data Structures Lecture #1 Complexity Theory and Asymptotic Analysis Steve Wolfman 2014W1 1.
Week 2 CS 361: Advanced Data Structures and Algorithms
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
CS 3343: Analysis of Algorithms
Iterative Algorithm Analysis & Asymptotic Notations
Recitation 11 Analysis of Algorithms and inductive proofs 1.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
CS221: Algorithms and Data Structures Lecture #1 Complexity Theory and Asymptotic Analysis Steve Wolfman 2011W2 1.
CSC 413/513: Intro to Algorithms Merge Sort Solving Recurrences.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
CS221: Algorithms and Data Structures Lecture #1 Complexity Theory and Asymptotic Analysis Steve Wolfman 2013W2 1.
1 CSE 326 Data Structures: Complexity Lecture 2: Wednesday, Jan 8, 2003.
Asymptotic Growth Rates  Themes  Analyzing the cost of programs  Ignoring constants and Big-Oh  Recurrence Relations & Sums  Divide and Conquer 
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Recitation on analysis of algorithms. Formal definition of O(n) We give a formal definition and show how it is used: f(n) is O(g(n)) iff There is a positive.
Algorithm Analysis Part of slides are borrowed from UST.
CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000.
CS221: Algorithms and Data Structures Lecture #1 Complexity Theory and Asymptotic Analysis Steve Wolfman 2009W1 1.
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.
Foundations II: Data Structures and Algorithms
Complexity Analysis. 2 Complexity The complexity of an algorithm quantifies the resources needed as a function of the amount of input data size. The resource.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
CSE 326: Data Structures Asymptotic Analysis Hannah Tang and Brian Tjaden Summer Quarter 2002.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
CSE 326: Data Structures Class #4 Analysis of Algorithms III Analysis of Recursive Algorithms Henry Kautz Winter 2002.
Vishnu Kotrajaras, PhD.1 Data Structures
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
Lecture 2 Algorithm Analysis
Fundamental Data Structures and Algorithms
Analysis of Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
CS 213: Data Structures and Algorithms
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
Algorithm Analysis (not included in any exams!)
CS 3343: Analysis of Algorithms
Analysis of Algorithms
DS.A.1 Algorithm Analysis Chapter 2 Overview
CSE 373 Optional Section Led by Yuanwei, Luyi Apr
CSE 326: Data Structures Lecture #6 Asymptotic Analysis
How much does your program take ?
At the end of this session, learner will be able to:
David Kauchak cs161 Summer 2009
Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

CSE 326: Data Structures Lecture #3 Asymptotic Analysis Steve Wolfman Winter Quarter 2000

Today’s Outline Silicon Downs Asymptotic Analysis Return Quiz

Silicon Downs Post #1 n 3 + 2n 2 n 0.1 n + 100n 0.1 5n 5 n n / log n mn 3 Post #2 100n log n 2n + 10 log n n! 1000n 15 3n 7 + 7n 2 m n Winner O(n 2 ) O(log n) TIE O(n) O(n 5 ) O(n 15 ) O(n 6 ) IT DEPENDS

Race I n 3 + 2n 2 100n vs.

Race II n 0.1 log n vs.

Race III n + 100n 0.1 2n + 10 log n vs.

Race IV 5n 5 n! vs.

Race V n n / n 15 vs.

Race VI 8 2log(n) 3n 7 + 7n vs.

FBI Finds Silicon Downs Fixed The fix sheet (typical growth rates in order) –constant:O(1) –logarithmic:O(log n)(log k n, log n 2  O(log n)) –poly-log:O(log k n) –linear:O(n) –log-linear:O(n log n) –superlinear:O(n 1+c )(c is a constant > 0) –quadratic:O(n 2 ) –cubic:O(n 3 ) –polynomial:O(n k )(k is a constant) –exponential:O(c n )(c is a constant > 1)

Terminology Given an algorithm whose running time is T(n) –T(n)  O(f(n)) if there are constants c and n 0 such that T(n)  c f(n) for all n  n 0 1, log n, n, 100n  O(n) –T(n)   (f(n)) if there are constants c and n 0 such that T(n)  c f(n) for all n  n 0 n, n 2, n, n 3 log n   (n) –T(n)   (f(n)) if T(n)  O(f(n)) and T(n)   (f(n)) n, 2n, 100n, 0.01 n + log n   (n) –T(n)  o(f(n)) if T(n)  O(f(n)) and T(n)   (f(n)) 1, log n, n 0.99  o(n) –T(n)   (f(n)) if T(n)  O(f(n)) and T(n)   (f(n)) n 1.01, n 2, n, n 3 log n   (n)

Types of analysis Orthogonal axes –bound flavor upper bound (O, o) lower bound ( ,  ) asymptotically tight (  ) –analysis case worst case (adversary) average case best case “common” case –analysis quality loose bound (any true analysis) tight bound (no better bound which is asymptotically different)

Analyzing Code C++ operations - constant time consecutive stmts- sum of times conditionals- sum of branches, condition loops- sum of iterations function calls- cost of function body Above all, use your head!

More Examples Than You Can Shake a Stick At (#1) for i = 1 to n do for j = 1 to n do sum = sum + 1

METYCSSA (#2) for i = 1 to n do for j = i to n do sum = sum + 1

METYCSSA (#3) Conditional if C then S 1 else S 2 Loops while C do S

METYCSSA (#4) Recursion Almost always yields a recurrence Recursive max T(0) <= b T(n) 0 Analysis T(n) <= c + c + T(n - 2) (by substitution) T(n) <= c + c + c + T(n - 3) (by substitution, again) T(n) <= kc + T(n - k) (extrapolating 0 < k  n ) T(n) <= nc + T(0) = nc + b (for k = n ) T(n) 

METYCSSA (#5): Mergesort Mergesort algorithm –split list in half, sort first half, sort second half, merge together T(1) <= b T(n) 1 Analysis T(n) <= 2T(n/2) + cn <= 2(2T(n/4) + c(n/2)) + cn = 4T(n/4) + cn + cn <= 4(2T(n/8) + c(n/4)) + cn + cn = 8T(n/8) + cn + cn + cn <= 2 k T(n/2 k ) + kcn (extrapolating 1 < k  n ) <= nT(1) + cn log n (for 2 k = n or k = log n ) T(n) 

METYCSSA (#6): Fibonacci Recursive Fibonacci: int Fib(n) if (n == 0 or n == 1) return 1 else return Fib(n - 1) + Fib(n - 2) Lower bound analysis T(0), T(1) >= b T(n) 1 Analysis let  be (1 +  5)/2 which satisfies  2 =  + 1 show by induction on n that T(n) >= b  n - 1

Example #6 continued Basis: T(0)  b > b  -1 and T(1)  b = b  0 Inductive step: Assume T(m)  b  m - 1 for all m < n T(n)  T(n - 1) + T(n - 2) + c  b  n-2 + b  n-3 + c  b  n-3 (  + 1) + c = b  n-3  2 + c  b  n-1 T(n)  Why? Same recursive call is made numerous times.

Example #6: Learning from Analysis To avoid recursive calls –store all basis values in a table –each time you calculate an answer, store it in the table –before performing any calculation for a value n check if a valid answer for n is in the table if so, return it This strategy is called “memoization” and is closely related to dynamic programming How much time does this version take? Ask Zasha about me!

Final Example (#7): Longest Common Subsequence Problem: given two strings (m and n), find the longest sequence of characters which appears in order in both strings –lots of applications, DNA sequencing, blah, blah, blah Example: –“ search me ” and “ insane method ” = “ same ”

Asymptotic Analysis Summary Determine what characterizes a problem’s size Express how much resources (time, memory, etc.) an algorithm requires as a function of input size using O(),  (),  () –worst case –best case –average case –common case –overall

Remind Darth Wolfman to Hand Back the Quizzes Well, what are you waiting for?

To Do Write up homework (for Wed. beginning of class) Finalize Project I teams Finish Project I (for Friday 10PM) Read chapter 6 in the book Keep thinking about whether you like this homework format

Coming Up Priority Qs Self-balancing trees First homework assignment due (January 12th) First project due (January 14th) A day off (January 17 th )!