Foundations II: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Chapter 3 Growth of Functions
Asymptotic Growth Rate
Cutler/HeadGrowth of Functions 1 Asymptotic Growth Rate.
20-Jun-15 Analysis of Algorithms II. 2 Basics Before we attempt to analyze an algorithm, we need to define two things: How we measure the size of the.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 2 Elements of complexity analysis Performance and efficiency Motivation: analysis.
Algorithm analysis and design Introduction to Algorithms week1
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
Mathematics Review and Asymptotic Notation
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
CS 3343: Analysis of Algorithms
Asymptotic Analysis-Ch. 3
CMPT 438 Algorithms Chapter 3 Asymptotic Notations.
A Lecture /24/2015 COSC3101A: Design and Analysis of Algorithms Tianying Ji Lecture 1.
Asymptotic Notation (O, Ω, )
Analysis of Algorithms1 O-notation (upper bound) Asymptotic running times of algorithms are usually defined by functions whose domain are N={0, 1, 2, …}
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Time Complexity of Algorithms (Asymptotic Notations)
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.
David Meredith Growth of Functions David Meredith
21-Feb-16 Analysis of Algorithms II. 2 Basics Before we attempt to analyze an algorithm, we need to define two things: How we measure the size of the.
Asymptotic Notation Faculty Name: Ruhi Fatima
Mathematical Foundations (Growth Functions) Neelima Gupta Department of Computer Science University of Delhi people.du.ac.in/~ngupta.
Asymptotic Complexity
Lecture 2 Algorithm Analysis
CMPT 438 Algorithms.
Design and Analysis of Algorithms
Introduction to Algorithms: Asymptotic Notation
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
COMP108 Algorithmic Foundations Algorithm efficiency
Asymptotic Analysis.
Introduction to Algorithms
Introduction Algorithms Order Analysis of Algorithm
CS 3343: Analysis of Algorithms
ET 2006 : Data Structures & Algorithms
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
DATA STRUCTURES Introduction: Basic Concepts and Notations
CS 583 Fall 2006 Analysis of Algorithms
CS 3343: Analysis of Algorithms
Definition In simple terms, an algorithm is a series of instructions to solve a problem (complete a task) We focus on Deterministic Algorithms Under the.
CS 3343: Analysis of Algorithms
O-notation (upper bound)
CS 3343: Analysis of Algorithms
Asymptotic Notations Algorithms Lecture 9.
Asymptotes: Why? How to describe an algorithm’s running time?
CSC 413/513: Intro to Algorithms
Introduction to Algorithms Analysis
Asymptotic Growth Rate
Asymptotic Analysis.
Data Structures and Algorithms CSCE 221H
CS 3343: Analysis of Algorithms
CMPT 438 Algorithms Instructor: Tina Tian.
Analysis of Algorithms
Advanced Analysis of Algorithms
Chapter 2.
Analysis of Algorithms II
CS200: Algorithms Analysis
CSE 2010: Algorithms and Data Structures Algorithms
Searching, Sorting, and Asymptotic Complexity
Asst. Dr.Surasak Mungsing
Analysis of Algorithms II
O-notation (upper bound)
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
David Kauchak cs161 Summer 2009
Estimating Algorithm Performance
Analysis of Algorithms
Presentation transcript:

Foundations II: Data Structures and Algorithms Instructor : Yusu Wang Topic 1 : Introduction and Asymptotic notation CSE 2331 / 5331

Course Information Course webpage http://www.cse.ohio-state.edu/~yusu/courses/2331 Office hours Tu/Th 9:30 am – 11:00 am Grading policy: homework: 20%, two midterms: 40%, final: 40% CSE 2331 / 5331

Exams Midterm 1 Midterm 2 Final exam: Sept. 28th : 8 – 10:00pm, Location: JR 251 Midterm 2 Nov. 2th : 8 – 10:00pm, Location: JR 251 Final exam: Dec. 11th (Monday), 12:00pm--1:45pm, Room DL369 Mark you calendar. Contact me within first two full weeks if any schedule conflict for any exam. CSE 2331 / 5331

Notes All homework should be submitted before or in class on the due date. Late homework during the same day receives a 10% penalty. Late homework submitted the next day receives a 30% penalty. You may discuss homework with your classmates. It is very important that you write up your solutions individually, and acknowledge any collaboration / discussion with others. CSE 2331 / 5331

More Information Textbook Others Introduction to Algorithms by Cormen, Leiserson, Rivest and Stein (third edition) Others CSE 2331 course notes By Dr. Rephael Wenger (SBX) The Art of Computer Programming By Donald Knuth CSE 2331 / 5331

Introduction What is an algorithm? Step by step strategy to solve problems Should terminate Should return correct answer for any input instance CSE 2331 / 5331

This Course Various issues involved in designing good algorithms What do we mean by “good”? Algorithm analysis, language used to measure performance How to design good algorithms Data structures, algorithm paradigms Fundamental problems Graph traversal, shortest path etc. CSE 2331 / 5331

Asymptotic Notation CSE 2331 / 5331

Sorting problem Input: Output: permutation of A that is sorted a 2 1 A = < , , …. > Example: Input: < 3, 11, 6, 4, 2, 7, 9 > Output: < 2, 3, 4, 6, 7, 9, 11 > CSE 2331 / 5331

Insertion Sort 1 i i+1 n 3, 11, 6, 4, 2, 7, 9 3, 6, 11, 4, 2, 7, 9 3, 4, 6, 11, 2, 7, 9 2, 3, 4, 6, 11, 7, 9 CSE 2331 / 5331

Pseudo-code InsertionSort(A, n) for i = 1 to n-1 do key = A[i+1] j = i while j > 0 and A[j] > key do A[j+1] = A[j] j = j - 1 A[j+1] = key CSE 2331 / 5331

Analysis Termination Correctness Efficiency: time/space beginning of for-loop: if A[1.. i] sorted, then end of for-loop: A[1.. i+1] sorted. when i = n-1, A[1.. i+1] is sorted. Efficiency: time/space Depends on input size n Space: roughly n CSE 2331 / 5331

Running Time Depends on input size Depends on particular input Best case Worst case First Idea: Provide upper bound (as a guanrantee) Make it a function of n : T(n) CSE 2331 / 5331

Running Time Worst case Average case T(n) = max time of algorithm on any input of size n Average case T(n) = expected time of algorithm over all possible inputs of size n Need a statistical distribution model for input E.g, uniform distribution CSE 2331 / 5331

Insertion Sort T(n) =  ( + i ) = + n + c c c InsertionSort(A, n) for i = 1 to n-1 do key = A[i] j = i while j > 0 and A[j] > key do A[j+1] = A[j] j = j - 1 A[j+1] = key c 1 c 2 T(n) =  ( + i ) = + n + i=2 n c 2 1 3 5 4 CSE 2331 / 5331

Asymptotic Complexity Why shall we care about constants and lower order terms? Should focus on the relative growth w.r.t. n Especially when n becomes large ! E.g: 0.2n1.5 v.s 500n Second Idea: Ignore all constants and lower order terms Only order of growth --- Asymptotic time complexity CSE 2331 / 5331

Illustration 𝑓 𝑛 =𝑂 𝑔 𝑛 CSE 2331 / 5331

Big O Notation f(n)  O(g(n)) if and only if  c > 0, and n0 , so that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 f(n) = O(g(n)) means f(n)  O(g(n)) (i.e, at most) We say that g(n) is an asymptotic upper bound for f(n). It also means: E.g, f(n) = O(n2) if there exists c, n0 > 0 such that f(n) ≤ cn2, for all n ≥ n0. lim ≤ c n f(n) g(n) CSE 2331 / 5331

More Examples 5 𝑛 2 +6𝑛+8=𝑂 𝑛 3 ? 100 𝑛 2 −1000 𝑛=𝑂( 𝑛 2 ) ? 5 𝑛 2 +6𝑛+8=𝑂 𝑛 3 ? 100 𝑛 2 −1000 𝑛=𝑂( 𝑛 2 ) ? 6 𝑛 3 +7 𝑛 2 +3𝑛 =𝑂( 𝑛 1.5 ) ? 2 𝑛 =𝑂 𝑛 2 ? 3lg 𝑛 =𝑂(𝑛) ? 3 𝑛 =𝑂 2 𝑛 ? 3 𝑛 =𝑂 2 2𝑛 log 3 𝑛 =𝑂 log 2 𝑛 ? 𝑛 lg 𝑛 =𝑂(𝑛) CSE 2331 / 5331

Omega  Notation f(n)   (g(n)) if and only if  c > 0, and n0 , so that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 f(n) =  (g(n)) means f(n)   (g(n)) (i.e, at least) We say g(n) is an asymptotic lower bound of f(n). It also means: lim ≥ c n f(n) g(n) CSE 2331 / 5331

Illustration 𝑓 𝑛 =Ω 𝑔 𝑛 CSE 2331 / 5331

More Examples 5 𝑛 2 +6𝑛+8=Ω 𝑛 3 ? 5 𝑛 2 +6𝑛+8=Ω 𝑛 2 ? 5 𝑛 2 +6𝑛+8=Ω 𝑛 3 ? 5 𝑛 2 +6𝑛+8=Ω 𝑛 2 ? 6 𝑛 3 +7 𝑛 2 +3𝑛 =Ω( 𝑛 1.5 ) ? 2 𝑛 =Ω 𝑛 2 ? 3lg 𝑛 =Ω(𝑛) ? 3 𝑛 =Ω 2 𝑛 ? log 3 𝑛 =Ω log 2 𝑛 ? 2 log 3 𝑛 =Ω( 2 log 2 𝑛 ) Prove for sqrt root one. Prove for exponential one. Prove for log. CSE 2331 / 5331

Theta  Notation Combine lower and upper bound Means tight: of the same order 𝑓 𝑛 ∈Θ 𝑔 𝑛 if and only if ∃ 𝑐 1 , 𝑐 2 >0, and 𝑛 0 , such that 𝑐 1 𝑔 𝑛 ≤𝑓 𝑛 ≤ 𝑐 2 𝑔 𝑛 for any 𝑛≥ 𝑛 0 𝑓 𝑛 =𝛩 𝑔 𝑛 means 𝑓 𝑛 ∈𝛩 𝑔 𝑛 We say g(n) is an asymptotically tight bound for f(n). It also means: 𝑐 1 ≤ lim 𝑛→∞ 𝑓(𝑛) 𝑔(𝑛) ≤ 𝑐 2 CSE 2331 / 5331

𝑓 𝑛 =𝑂(𝑔 𝑛 ) 𝑓 𝑛 =Ω(𝑔 𝑛 ) 𝑓 𝑛 =Θ(𝑔 𝑛 ) CSE 2331 / 5331

Remarks -- 1 𝑓 𝑛 =Θ(𝑔 𝑛 ) if and only if 𝑓 𝑛 =𝑂(𝑔 𝑛 ), and 𝑓 𝑛 =Ω 𝑔 𝑛 . Insertion sort algorithm as described earlier has time complexity Θ( 𝑛 2 ). Transpose symmetry: If 𝑓 𝑛 =𝑂(𝑔 𝑛 ), if and only if 𝑔 𝑛 =Ω 𝑓 𝑛 Transitivity: If 𝑓 𝑛 =𝑂(𝑔 𝑛 ) and 𝑔 𝑛 =𝑂(ℎ 𝑛 ), then 𝑓 𝑛 =𝑂 ℎ 𝑛 . Same for Ω and Θ CSE 2331 / 5331

If 𝑓 𝑛 ∉𝑂(𝑔 𝑛 ), this does not imply that 𝑓 𝑛 =Ω(𝑔 𝑛 ). Remarks -- 2 It is convenient to think that Big-O is smaller than or equal to Big-Ω is larger than or equal to Big-Θ is equal However, These relations are modulo constant factor scaling Not every pair of functions have such relations If 𝑓 𝑛 ∉𝑂(𝑔 𝑛 ), this does not imply that 𝑓 𝑛 =Ω(𝑔 𝑛 ). CSE 2331 / 5331

Remarks -- 3 Provide a unified language to measure the performance of algorithms Give us intuitive idea how fast we shall expect the alg. Can now compare various algorithms for the same problem Constants hidden! O( n lg lg n ), 2n lg n T(n) = + O (n) means T(n) = + h(n), and h(n) = O(n) n 2 n 2 CSE 2331 / 5331

It can be an arbitrarily large constant … Pitfalls OK O(n) + O(n) = O(n) T(n) = n +  O(n) = n + O(n) k ? i = 1 k should not depend on n ! It can be an arbitrarily large constant … CSE 2331 / 5331

Yet Another Notation Small o notation: f(n) = o(g(n)) means for any 𝑐>0, ∃ 𝑛 0 s.t. for all 𝑛≥ 𝑛 0 ,0≤𝑓 𝑛 <𝑐𝑔 𝑛 . In other words, lim = 0 n f(n) g(n) Examples: Is n - lg n = o(n) ? Is n = o (n lg n) ? CSE 2331 / 5331

Yet Another Notation Small ω notation: f(n) = ω(g(n)) means for any 𝑐>0, ∃ 𝑛 0 s.t. for all 𝑛≥ 𝑛 0 ,0≤𝑐𝑔 𝑛 <𝑓 𝑛 . In other words, lim = ∞ n f(n) g(n) lim = 0 n g(n) f(n) or Examples: Is n - lg n = ω(n) ? Is n = ω(n / lg n) ? CSE 2331 / 5331

Review some basics Chapter 3.2 of CLRS See the board CSE 2331 / 5331

Recall Asymptotic Notation CSE 2331 / 5331

Another View CSE 2331 / 5331

Some Special Cases CSE 2331 / 5331

More Examples 𝑛 3 vs. 1.0001 𝑛 𝑛 3 vs. 1.0001 𝑛 2 𝑛 𝑎 vs. 𝑏 𝑛 ln (𝑛 4 ) vs. log 3 2𝑛 2lg 𝑛 vs. 𝑛 lg 𝑛 vs. 𝑛 𝜖 𝑛 lg 𝑛 vs. 𝑛 lg 𝑛 𝑛 vs. log 2 3 𝑛 𝑛 vs. 2 log 2 𝑛 +4 𝑛 log 2 𝑛 vs. 2 2 log 2 𝑛 CSE 2331 / 5331

In general, we can safely ignore low order terms. More Examples lg 𝑛 2 vs. (lg 𝑛) 2 −100 lg 𝑛 10 10 vs. ln 𝑛 3 log 2 𝑛 vs. 0.5𝑛 lg 2 𝑛 2 +𝑛 lg 𝑛 vs lg ( n 3 −100n) 𝑛 − 𝑛 lg 𝑛 vs. 2 𝑛 2 +𝑛 lg 𝑛 In general, we can safely ignore low order terms. CSE 2331 / 5331

Hierarchy CSE 2331 / 5331

More Example Rank the following functions by order of growth: 2 𝑛−4 , 𝑛+3 lg (3 𝑛 2 −4𝑛) , 𝑛 𝑛+5 , 𝑛 ln 𝑛 , 3⋅ 2 𝑛 , 4𝑛 lg 𝑛 , 𝑛+ lg 𝑛 CSE 2331 / 5331

Summary Algorithms are useful Example: sorting problem Insertion sort Asymptotic complexity Focus on the growth of complexity w.r.t input size CSE 2331 / 5331