Analysis of Algorithms

Slides:



Advertisements
Similar presentations
 The running time of an algorithm as input size approaches infinity is called the asymptotic running time  We study different notations for asymptotic.
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
CPSC 320: Intermediate Algorithm Design & Analysis Asymptotic Notation: (O, Ω, Θ, o, ω) Steve Wolfman 1.
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Asymptotic Growth Rate
Cutler/HeadGrowth of Functions 1 Asymptotic Growth Rate.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
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.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Analysis of Algorithms
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Asymptotic Notations Iterative Algorithms and their analysis
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
1 ©2008 DEEDS Group Introduction to Computer Science 2 - SS 08 Asymptotic Complexity Introduction in Computer Science 2 Asymptotic Complexity DEEDS Group.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
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.
CS 3343: Analysis of Algorithms
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
Analysis of Algorithms
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Asymptotic Analysis-Ch. 3
CMPT 438 Algorithms Chapter 3 Asymptotic Notations.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Growth of Functions. 2 Analysis of Bubble Sort 3 Time Analysis – Best Case The array is already sorted – no swap operations are required.
CMSC 341 Asymptotic Analysis. 2 Complexity How many resources will it take to solve a problem of a given size? –time –space Expressed as a function of.
Asymptotic Analysis (based on slides used at UMBC)
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
2-0 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 2 Theoretical.
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
Spring 2015 Lecture 2: Analysis of Algorithms
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
13 February 2016 Asymptotic Notation, Review of Functions & Summations.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
Asymptotic Notation Faculty Name: Ruhi Fatima
Lecture 4 Jianjun Hu Department of Computer Science and Engineerintg University of South Carolina CSCE350 Algorithms and Data Structure.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
LECTURE 2 : fundamentals of analysis of algorithm efficiency Introduction to design and analysis algorithm 1.
Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
At the end of this session, learner will be able to:
Presentation transcript:

Analysis of Algorithms Asymptotic Notations, Analyzing non-recursive algorithms 4/13/2017

Outline Review of last lecture Continue on asymptotic notations Analyzing non-recursive algorithms 4/13/2017

Asymptotic notations O: <= o: < Ω: >= ω: > Θ: = (in terms of growth rate) 4/13/2017

Mathematical definitions O(g(n)) = {f(n):  positive constants c and n0 such that 0 ≤ f(n) ≤ cg(n)  n>n0} Ω(g(n)) = {f(n):  positive constants c and n0 such that 0 ≤ cg(n) ≤ f(n)  n>n0} Θ(g(n)) = {f(n):  positive constants c1, c2, and n0 such that 0  c1 g(n)  f(n)  c2 g(n)  n  n0} 4/13/2017

O, Ω, and Θ The definitions imply a constant n0 beyond which they are satisfied. We do not care about small values of n. Use definition to prove big-O (Ω,Θ): find constant c (c1 and c2) and n0, such that the definition of big-O (Ω,Θ) is satisfied 4/13/2017

Big-Oh Claim: f(n) = 3n2 + 10n + 5  O(n2) Proof by definition: (Hint: Need to find c and n0 such that f(n) <= cn2 for all n > n0. You can be sloppy about the constant factors. Pick a comfortably large c when proving big-O or small one when proving big-Omega.) (Note: you just need to find one concrete example of c and n0, but the condition needs to be met for all n > n0. So do not try to plug in a concrete value of n and show the inequality holds.) Proof: 3n2 + 10n + 5  3n2 + 10n2 + 5,  n > 1  3n2 + 10n2 + 5n2, n > 1  18 n2,  n > 1 If we let c = 18 and n0 = 1, we have f(n)  c n2,  n > n0. Therefore by definition 3n2 + 10n + 5  O(n2). 4/13/2017

Properties of asymptotic notations Textbook page 51 Transitivity f(n) = (g(n)) and g(n) = (h(n)) => f(n) = (h(n)) (holds true for o, O, , and  as well). Symmetry f(n) = (g(n)) if and only if g(n) = (f(n)) Transpose symmetry f(n) = O(g(n)) if and only if g(n) = (f(n)) f(n) = o(g(n)) if and only if g(n) = (f(n)) 4/13/2017

Binary Search In binary search we throw away half the possible number of keys after each comparison. How many times can we halve n before getting to 1? Answer: ceiling (lg n) 4/13/2017

Logarithms and Trees How tall a binary tree do we need until we have n leaves? The number of potential leaves doubles with each level. How many times can we double 1 until we get to n? Answer: ceiling (lg n) 4/13/2017

Logarithms and Bits How many numbers can you represent with k bits? Each bit you add doubles the possible number of bit patterns You can represent from 0 to 2k – 1 with k bits. A total of 2k numbers. How many bits do you need to represent the numbers from 0 to n? ceiling (lg (n+1)) 4/13/2017

logarithms lg n = log2 n ln n = loge n, e ≈ 2.718 lgkn = (lg n)k lg lg n = lg (lg n) = lg(2)n lg(k) n = lg lg lg … lg n lg24 = ? lg(2)4 = ? Compare lgkn vs lg(k)n? 4/13/2017

The iterated logarithm function lg * n is the least positive integer i such that lg(i) <= 1. Also known as (n). The number of times you need to take the logarithm of something until it becomes smaller than or equal to 1 lg * 256 = ? lg 256 = 8 lg 8 = 3 lg 3 < 2 lg lg 3 < 1 lg*2 = 1 lg*4 = 2 lg*16 = 3 lg*65536 = 4 lg*265536 = lg*(1019728) = 5 For any practical purpose, lg*(n) can be considered as a constant. 4/13/2017

Useful rules for logarithms For all a > 0, b > 0, c > 0, the following rules hold logba = logca / logcb = lg a / lg b logban = n logba blogba = a log (ab) = log a + log b lg (2n) = ? log (a/b) = log (a) – log(b) lg (n/2) = ? lg (1/n) = ? logba = 1 / logab 4/13/2017

More advanced dominance ranking 4/13/2017

Analyzing the complexity of an algorithm 4/13/2017

Kinds of analyses Worst case Provides an upper bound on running time Best case – not very useful, can always cheat Average case Provides the expected running time Very useful, but treat with care: what is “average”? 4/13/2017

General plan for analyzing time efficiency of a non-recursive algorithm Decide parameter (input size) Identify most executed line (basic operation) worst-case = average-case? T(n) = i ti T(n) = Θ (f(n)) 4/13/2017

Example repeatedElement (A, n) // determines whether all elements in a given // array are distinct for i = 1 to n-1 { for j = i+1 to n { if (A[i] == A[j]) return true; } return false; 4/13/2017

Example repeatedElement (A, n) // determines whether all elements in a given // array are distinct for i = 1 to n-1 { for j = i+1 to n { if (A[i] == A[j]) return true; } return false; 4/13/2017

Best case? Worst-case? Average case? 4/13/2017

Best case Worst-case Average case? A[1] = A[2] T(n) = Θ (1) No repeated elements T(n) = (n-1) + (n-2) + … + 1 = n (n-1) / 2 = Θ (n2) Average case? What do you mean by “average”? Need more assumptions about data distribution. How many possible repeats are in the data? Average-case analysis often involves probability. 4/13/2017

Find the order of growth for sums T(n) = i=1..n i = Θ (n2) T(n) = i=1..n log (i) = ? T(n) = i=1..n n / 2i = ? T(n) = i=1..n 2i = ? … How to find out the actual order of growth? Math… Textbook Appendix A.1 (page 1058-60) 4/13/2017

Arithmetic series An arithmetic series is a sequence of numbers such that the difference of any two successive members of the sequence is a constant. e.g.: 1, 2, 3, 4, 5 or 10, 12, 14, 16, 18, 20 In general: Recursive definition 4/13/2017

Sum of arithmetic series If a1, a2, …, an is an arithmetic series, then e.g. 1 + 3 + 5 + 7 + … + 99 = ? (series definition: ai = 2i-1) This is ∑ i = 1 to 50 (ai) = 50 * (1 + 99) / 2 = 2500 4/13/2017

Closed form, or explicit formula Geometric series A geometric series is a sequence of numbers such that the ratio between any two successive members of the sequence is a constant. e.g.: 1, 2, 4, 8, 16, 32 or 10, 20, 40, 80, 160 or 1, ½, ¼, 1/8, 1/16 In general: Recursive definition Closed form, or explicit formula Or: 4/13/2017

Sum of geometric series if r < 1 if r > 1 if r = 1 4/13/2017

Sum of geometric series if r < 1 if r > 1 if r = 1 4/13/2017

Important formulas 4/13/2017

Sum manipulation rules Example: 4/13/2017

Sum manipulation rules Example: 4/13/2017

using the formula for geometric series: i=1..n n / 2i = n * i=1..n (½)i = ? using the formula for geometric series: i=0..n (½)i = 1 + ½ + ¼ + … (½)n = 2 Application: algorithm for allocating dynamic memories 4/13/2017

Application: algorithm for selection sort using priority queue i=1..n log (i) = log 1 + log 2 + … + log n = log 1 x 2 x 3 x … x n = log n! = (n log n) Application: algorithm for selection sort using priority queue 4/13/2017

Recursive definition of sum of series T (n) = i=0..n i is equivalent to: T(n) = T(n-1) + n T(0) = 0 T(n) = i=0..n ai is equivalent to: T(n) = T(n-1) + an T(0) = 1 Recurrence Boundary condition Recursive definition is often intuitive and easy to obtain. It is very useful in analyzing recursive algorithms, and some non-recursive algorithms too. 4/13/2017

Recursive definition of sum of series How to solve such recurrence or more generally, recurrence in the form of: T(n) = aT(n-b) + f(n) or T(n) = aT(n/b) + f(n) 4/13/2017