Lecture 2 Analysis of Algorithms How to estimate time complexity? Analysis of algorithms Techniques based on Recursions ACKNOWLEDGEMENTS: Some contents.

Slides:



Advertisements
Similar presentations
Discrete Structures CISC 2315
Advertisements

Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Asymptotic Notation (O, Ω,  ) s Describes the behavior of the time or space complexity for large instance characteristics s Common asymptotic functions.
Chapter 1 – Basic Concepts
1 Data Structures Performance Analysis. 2 Fundamental Concepts Some fundamental concepts that you should know: –Dynamic memory allocation. –Recursion.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 3 Growth of Functions
Data Structures Performance Analysis.
Cmpt-225 Algorithm Efficiency.
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 2 Elements of complexity analysis Performance and efficiency Motivation: analysis.
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS. Definitions of Algorithm A mathematical relation between an observed quantity and a variable used in a step-by-step.
Algorithm Design and Analysis Liao Minghong School of Computer Science and Technology of HIT July, 2003.
Analysis of Algorithms Lecture 2
Program Performance & Asymptotic Notations CSE, POSTECH.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Basic Concepts 2014, Fall Pusan National University Ki-Joune Li.
Lecture 2 Computational Complexity
Analysis of Algorithms
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.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Analysis of Algorithm Efficiency Dr. Yingwu Zhu p5-11, p16-29, p43-53, p93-96.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Data Structure Introduction.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Asymptotic Analysis (based on slides used at UMBC)
Introduction to Analysis of Algorithms CS342 S2004.
General rules: Find big-O f(n) = k = O(1) f(n) = a k n k + a k-1 n k a 1 n 1 + a 0 = O(n k ) Other functions, try to find the dominant term according.
Algorithm Analysis Part of slides are borrowed from UST.
Big-O. Algorithm Analysis Exact analysis: produce a function f(n) measuring how many basic steps are needed for a given inputs n On any input of size.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 5: Algorithms.
Algorithm Analysis (Big O)
Spring 2015 Lecture 2: Analysis of Algorithms
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Data Structures & Algorithm CS-102 Lecture 12 Asymptotic Analysis Lecturer: Syeda Nazia Ashraf 1.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Analysis of Non – Recursive Algorithms
Analysis of Non – Recursive Algorithms
Complexity & the O-Notation
What is an Algorithm? Algorithm Specification.
Lecture 2 Analysis of Algorithms
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Chapter 2.
Fundamentals of the Analysis of Algorithm Efficiency
Searching, Sorting, and Asymptotic Complexity
David Kauchak cs161 Summer 2009
Presentation transcript:

Lecture 2 Analysis of Algorithms How to estimate time complexity? Analysis of algorithms Techniques based on Recursions ACKNOWLEDGEMENTS: Some contents in this lecture source from slides of Yuxi Fu

ROADMAP How to estimate time complexity? Analysis of algorithms ‣ worst case, best case? ‣ Big-O notation 12/24/2015Xiaojuan Cai2

Observation Problem: 3-Sum Input: N distinct integers Output: the number of triples sum to exactly zero? /24/2015Xiaojuan Cai3 a[i]a[j]a[k]sum

Observation System dependent effects. Hardware: CPU, memory, cache, … Software: compiler, interpreter, garbage collector, … System: operating system, network, other applications, … Easy, but difficult to get precise measurements. 12/24/2015Xiaojuan Cai4

Mathematical model Total running time = sum of cost × frequency for all operations. – Cost depends on machine, compiler. – Frequency depends on algorithm, input data. % 1-sum int count = 0; for (int i = 0; i < N; i++) if (a[i] == 0) count++; 12/24/2015Xiaojuan Cai5

‣ Relative rather than Absolute. ‣ Machine independent. ‣ About large input instance Time complexity of an algorithm is a function of the size of the input n. Mathematical model 12/24/2015Xiaojuan Cai6

Input size Algo 1.9Algo 1.10 input sizenlog n +1 timenn relationLinearExponential 12/24/2015Xiaojuan Cai7

Input size -- convention Sorting and searching: the size of the array Graph: the number of the vertices and edges Computational geometry: the number points or line segments Matrix operation: the dimensions of the input matrices Number theory and Cryptography: the number of bits of the input. 12/24/2015Xiaojuan Cai8

How to estimate time complexity? Analysis of algorithms ‣ worst case, best case? ‣ Big-O notation Where are we? 12/24/2015Xiaojuan Cai9

Searching 9, 8, 9, 6, 2, 56, 12, 5, 4, 30, 67, 93, 25, 44, 96 Does 45 exist in: 2, 4, 5, 6, 8, 9, 9, 12, 25, 30, 44, 56, 67, 93, 96 Linear searching v.s. Binary searching Problem: Searching Input: An integer array A[1...n], and an integer x Output: Does x exist in A? 12/24/2015Xiaojuan Cai10

Binary searching 2, 4, 5, 6, 8, 9, 9, 12, 25, 30, 44, 56, 67, 93, 96 low high mid 12/24/2015Xiaojuan Cai11

Quiz Linear Searching Binary Searching Best case11 Worst casenlog n+1 A. 1 B. logn. C. n D. none of above 12/24/2015Xiaojuan Cai12

Computational Complexity Linear SearchingBinary Searching Worst casenlog n s per instruction Linear Searching Binary Searching n= s s n= day s 12/24/2015Xiaojuan Cai13

How to estimate time complexity? Analysis of algorithms ‣ worst case, best case? ‣ Big-O notation Where are we? 12/24/2015Xiaojuan Cai14

Big-O Notation Definition (O-notation) Let f(n) and g(n) : f(n) is said to be O(g(n)), written f (n) = O(g(n)), if ∃ c>0. ∃ n 0. ∀ n ≥ n 0.f (n) ≤ cg(n) Definition (O-notation) Let f(n) and g(n) : f(n) is said to be O(g(n)), written f (n) = O(g(n)), if ∃ c>0. ∃ n 0. ∀ n ≥ n 0.f (n) ≤ cg(n) The O-notation provides an upper bound of the running time; f grows no faster than some constant times g. 12/24/2015Xiaojuan Cai15

Examples 10n n = O(n 2 ). logn 2 =O(logn). log(n!) = O(nlogn). Hanoi: T(n) = O(2 n ), n is the input size. Searching: T(n) = O(n) 12/24/2015Xiaojuan Cai16

Big-Ω Notation Definition (Ω-notation) Let f(n) and g(n) : f(n) is said to be Ω(g(n)), written f (n) = Ω(g(n)), if ∃ c>0. ∃ n 0. ∀ n ≥ n 0.f (n) ≥ cg(n) The Ω-notation provides an lower bound of the running time; f(n) = O(g(n)) iff g(n) = Ω(f(n)). 12/24/2015Xiaojuan Cai17

Examples log n k = Ω(logn). log n! = Ω(nlogn). n! = Ω(2 n ). Searching: T(n) = Ω(1) Does there exist f,g, such that f = O(g) and f = Ω(g)? 12/24/2015Xiaojuan Cai18

Big-Θ notation log n! = Θ(nlogn). 10n n = Θ(n 2 ) Definition (Θ-notation) f(n)=Θ(g(n)), if both f(n)=O(g(n)) and f(n) = Ω (g(n)). 12/24/2015Xiaojuan Cai19

Small o-notation log n! = o(nlogn)? 10n n = o(n 2 )? nlog n = o(n 2 )? Definition (o-notation) Let f(n) and g(n) : f(n) is said to be o(g(n)), written f (n) = o(g(n)), if ∀ c. ∃ n 0. ∀ n ≥ n 0.f (n) < cg(n) 12/24/2015Xiaojuan Cai20

Definition (ω-notation) Let f(n) and g(n) : f(n) is said to be ω(g(n)), written f (n) = ω(g(n)), if ∀ c. ∃ n 0. ∀ n ≥ n 0.f (n) > cg(n) Small ω-notation 12/24/2015Xiaojuan Cai21

Quiz f(n)g(n) n 1.01 nlog 2 n nlognlog(n!) 1+1/2+1/ / n logn n2 n 3n3n A. O B. Ω. C. Θ D. none of above 12/24/2015Xiaojuan Cai22

Logarithms 12/24/2015Xiaojuan Cai23

Pigeonhole principle Johann Dirichlet (source from wikipedia) Prove there exists a number consisting of 7 and 0 that can divide 13. (source from wikipedia) 12/24/2015Xiaojuan Cai24

Summation 12/24/2015Xiaojuan Cai25

Approximation by Integration 12/24/2015Xiaojuan Cai26

In terms of limits Suppose exists 12/24/2015Xiaojuan Cai27

Complexity class Definition (Complexity class) An equivalence relation R on the set of complexity functions is defined as follows: f Rg if and only if f (n) = Θ(g (n)). 12/24/2015Xiaojuan Cai28

Order of growth ordernameexampleN = 1000N = constant add two numbers instant log Nlogarithmic binary search instant Nlinear find the maximum instant N log Nlinearithmic mergesort instant N2N2 quadratic 2-sum ~1s~2s 2N2N exponential Hanoi forever Assume the computer consumes s per instruction 12/24/2015Xiaojuan Cai29

Order of growth 12/24/2015Xiaojuan Cai30

Quiz A. Θ(n 2 ) B. Θ(n). C. Θ(nlogn) D. none of above 12/24/2015Xiaojuan Cai31

Quiz A. Θ(n 2 ) B. Θ(n). C. Θ(nlogn) D. none of above 12/24/2015Xiaojuan Cai32

Quiz 12/24/2015Xiaojuan Cai33 A. Θ(n 2 ) B. Θ(n). C. Θ(nlogn) D. none of above

Quiz 12/24/2015Xiaojuan Cai34 A. Θ(n 2 ) B. Θ(n). C. Θ(nlogn) D. none of above

Quiz 12/24/2015Xiaojuan Cai35 A. Θ(n 2 ) B. Θ(n). C. Θ(nlogn) D. none of above

Quiz 12/24/2015Xiaojuan Cai36 A. Θ(n 2 ) B. Θ(n). C. Θ(nlogn) D. none of above θ(nloglogn)

Amortized analysis The total number of append is n. The total number of delete is between 0 and n-1. So the time complexity is O(n). 12/24/2015Xiaojuan Cai37

Memory Time: Time complexity ‣ faster, better. Memory: Space complexity ‣ less, better. Space complexity <= Time complexity 12/24/2015Xiaojuan Cai38

Conclusion How to estimate time complexity? Analysis of algorithms ‣ worst case, best case? ‣ Big-O notation 12/24/2015Xiaojuan Cai39

3-Sum, better solution O(N 2 ) Problem: 3-Sum Input: N distinct integers Output: the number of triples sum to exactly zero? a[i]a[j]a[k]sum /24/2015Xiaojuan Cai40