CISC 235: Topic 1 Complexity of Iterative Algorithms.

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.
HST 952 Computing for Biomedical Scientists Lecture 10.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Analysys & Complexity of Algorithms Big Oh Notation.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
Chapter 2: Algorithm Analysis
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
Analysis of Algorithms (Chapter 4)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms
Fall 2006CSC311: Data Structures1 Chapter 4 Analysis Tools Objectives –Experiment analysis of algorithms and limitations –Theoretical Analysis of algorithms.
Analysis of Algorithm.
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Algorithm Analysis (Big O)
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
Analysis of Performance
CS2210 Data Structures and Algorithms Lecture 2:
Analysis of Algorithms Algorithm Input Output © 2014 Goodrich, Tamassia, Goldwasser1Analysis of Algorithms Presentation for use with the textbook Data.
Analysis of Algorithms Lecture 2
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
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
1 ©2008 DEEDS Group Introduction to Computer Science 2 - SS 08 Asymptotic Complexity Introduction in Computer Science 2 Asymptotic Complexity DEEDS Group.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Analysis of Algorithms
Analysis of Algorithms1 The Goal of the Course Design “good” data structures and algorithms Data structure is a systematic way of organizing and accessing.
Mathematics Review and Asymptotic Notation
Data Structures Lecture 8 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Analysis of Algorithms
Analysis of Algorithm Efficiency Dr. Yingwu Zhu p5-11, p16-29, p43-53, p93-96.
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.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Analysis of Algorithms [ Section 4.1 ] Examples of functions important in CS: the constant function:f(n) =
Data Structure Introduction.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Introduction to Analysis of Algorithms CS342 S2004.
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
Analysis of Algorithms Algorithm Input Output © 2010 Goodrich, Tamassia1Analysis of Algorithms.
Algorithm Analysis Part of slides are borrowed from UST.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
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.
Algorithm Analysis (Big O)
تصميم وتحليل الخوارزميات عال311 Chapter 3 Growth of Functions
Algorithm Complexity L. Grewe 1. Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them?
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Announcement We will have a 10 minutes Quiz on Feb. 4 at the end of the lecture. The quiz is about Big O notation. The weight of this quiz is 3% (please.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
1 COMP9024: Data Structures and Algorithms Week Two: Analysis of Algorithms Hui Wu Session 2, 2014
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Introduction to Algorithms
CSC 413/513: Intro to Algorithms
Analysys & Complexity of Algorithms
Programming and Data Structure
Programming and Data Structure
Algorithm Analysis Bina Ramamurthy CSE116A,B.
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Presentation transcript:

CISC 235: Topic 1 Complexity of Iterative Algorithms

Outline Complexity Basics Big-Oh Notation Big-Ω and Big-θ Notation Summations Limitations of Big-Oh Analysis 2CISC 235 Topic 1

Complexity Complexity is the study of how the time and space to execute an algorithm vary with problem size. The purpose is to compare alternative algorithms to solve a problem to determine which is “best”. Time Complexity: Let T( n ) denote the time to execute an algorithm with input size n How can we measure T( n )? 3CISC 235 Topic 1

Experimental Study Implement the alternative algorithms and then time them with various benchmarks, measuring running time with a method like Java’s System.currentTimeMillis( ) T( n ) n Too much coding & not general results 4CISC 235 Topic 1

Mathematical Analysis Analyze alternative algorithms mathematically prior to coding –Define the amount of time taken by an algorithm to be a function of the size of the input data: T( n ) = ? –Count the key instructions that are executed to obtain the value of the function in terms of the size of the input data –Compare algorithms by comparing how fast their running time functions grow as the input size increases 5CISC 235 Topic 1

Finding an Algorithm’s Running Time Function Count the key instructions that are executed to obtain the running time in terms of the size of the input data. Important Decisions: What is the measure of the size of input? Which are the key instructions? 6CISC 235 Topic 1

Example: Find smallest value in array A of length n int small = 0; for ( int j = 0; j < n; j++ ) if ( A[j] < A[small] ) small = j; Counting Assignments: Line 1: 1 Line 2: n + 1 Line 3: 0 Line 4: best case: 0 worst case: n So, T(n) = 2n + 2 7CISC 235 Topic 1

Example: Find smallest value in array A of length n int small = 0; for ( int j = 0; j < n; j++ ) if ( A[j] < A[small] ) small = j; Substitute constants a & b to reduce analysis time: Line 1: b (constant time for everything outside loop) Line 2: n (variable number of times through loop) Lines 2, 3, 4: a (constant time for everything inside loop, including loop test & increment) So, T(n) = an + b 8CISC 235 Topic 1

For large input sizes, constant terms are insignificant Program A with running time T A ( n )= 100 n Program B with running time T B ( n )= 2 n 2 TP(n)TP(n) T A ( n ) = 100 n T B ( n ) = 2 n Input Size n 9CISC 235 Topic 1

Big-Oh Notation Purpose: Establish a relative ordering among functions by comparing their relative rates of growth Example: f ( x ) = 4 x + 3 Big-Oh Notation: f ( x ) O( x ) or f ( x ) is O( x ) 10CISC 235 Topic 1

Definition of Big-Oh Notation f ( x ) is O(g( x )) if two constants C and k can be found such that: for all x  k, f ( x ) ≤ Cg( x ) Note: Big-Oh is an upper bound 11CISC 235 Topic 1

Meaning of Big-Oh Notation The function f (x) is one of the set of functions that has an order of magnitude growth rate ≤ the growth rate of g( x ) OR f ( x ) is at most a constant times g( x ), except possibly for some small values of x 12CISC 235 Topic 1

Graph of Big-Oh for a Program T(n)T(n) n T(n)T(n) Cg(n)Cg(n) Running Time Input Size For all n  k, T( n ) ≤ Cg( n ) g(n)g(n) K or K or … 13CISC 235 Topic 1

jpg If x > 1, then x x + 1  x x 2 + x 2 = 4 x 2 If x > 2, then x x + 1  x 2 + x 2 + x 2 = 3 x 2 So, we can take k = 1 and C = 4 to show that f(x) = x 2 + 2x + 1 is O(x 2 ) Show that f ( x ) = x x + 1 is O( x 2 ) 14CISC 235 Topic 1

Rules for Big-Oh We want to find the closest upper bound –if f ( x ) = 100 x, we choose f ( x ) is O( x ), not f ( x ) is O( x 2 ) Never include constants or lower-order terms within a Big-Oh (so also don’t include the base of a logarithm) –if f ( x ) = 2 x 2 + x we choose f ( x ) is O( x 2 ), not f ( x ) is O(2 x 2 ) and not f ( x ) is O( x 2 + x ) 15CISC 235 Topic 1

Order of Magnitude Growth Rates FunctionDescriptor Big-Oh C Constant O( 1 ) log n Logarithmic O( log n ) n Linear O( n ) n log n O( n log n ) n 2 Quadratic O( n 2 ) n 3 Cubic O( n 3 ) n k Polynomial O( n k ) 2 n Exponential O( 2 n ) n! Factorial O( n! ) 16CISC 235 Topic 1

jpg Order of Magnitude Growth Rates 17CISC 235 Topic 1

Change to Running Times When Input Size n is Doubled? Function n = 100 n = 200 Change C C C + ____ log n ~5 ~5 + ____ n * ____ n2n * ____ n3n * ____ 2n2n ~ ~ * ____ 18CISC 235 Topic 1

Growth of Combinations of Functions If f 1 ( x ) is O( g 1 ( x ) ) and f 2 ( x ) is O( g 2 ( x ) ), Then ( f 1 + f 2 )( x ) is O( max( g 1 ( x ), g 2 ( x ) )) and ( f 1 f 2 )( x ) is O( g 1 ( x )g 2 ( x ) ) 19CISC 235 Topic 1

Deriving Big-Oh of Functions f ( x ) Big-Oh 3 x O( x 2 ) 2 x 3 - x O( x 3 ) log 2 x + x O( x ) (5 + log 2 x )(3 x - 3) O( x log x ) 4 ( 2 x - x 3 ) O(2 x ) 4c - 6d + 17a O( 1 ) 20CISC 235 Topic 1

Big-Omega (Big- Ω) Notation Expresses a lower-bound of a function f ( x ) is Ω(g( x )) if f ( x ) is at least a constant times g( x ), except possibly for some small values of x. Formally: f ( x ) is Ω(g( x )) if two constants C and k can be found such that for all x  k, f ( x ) ≥ Cg( x ) 21CISC 235 Topic 1

Graph of Big-Ω for a Program Running Time T(n)T(n) n T(n)T(n) Cg(n)Cg(n) Input Size K For all n  k, T( n ) ≥ Cg( n ) 22CISC 235 Topic 1

Big-Theta (Big-  ) Notation Expresses a tight bound of a function f ( x ) is  (g( x )) if f ( x ) is both O(g( x )) and Ω(g( x )) T(n)T(n) n T(n)T(n) C1g(n)C1g(n) Input Size C2g(n)C2g(n) 23CISC 235 Topic 1

Example Input: An n +1 element array A of coefficients and x, a number Output: pVal, the value of the polynomial A[0] + A[1] x + A[2] x 2 +… + A[ n ] x n 24CISC 235 Topic 1

Algorithm 1 pVal = A[0] for (int i = 1; i <= n; i++) pVal = pVal + A[i] * Math.pow(x,i); What is the Big-Oh analysis of this algorithm? 25CISC 235 Topic 1

Algorithm 2 pVal = A[0] for ( int i = 1; i <= n; i++ ) { powX = 1 for ( int j = 0; j < i; j++ ) powX = x * powX pVal = pVal + A[i] * powX } What is the Big-Oh analysis of this algorithm? 26CISC 235 Topic 1

Algorithm 3 Horner’s method: To evaluate a 0 + a 1 x + a 2 x 2 + … + a n x n, use: P(x) = a 0 + (x (a 1 + x(a 2 + … + x(a n-1 + xa n ) …))) pVal = x * A[n] for (int i = n -1; i > 0; i - -) pVal = x * ( A[i] + pVal ) pVal = pVal + A[0] What is the Big-Oh analysis of this algorithm? 27CISC 235 Topic 1

Example: Selection Sort for ( int i = 0; i < n-1; i++ ) { int small = i; for ( int j = i + 1; j < n; j++ ) if ( A[j] < A[small] ) small = j; int temp = A[small]; A[small] = A[i]; A[i] = temp; } 28CISC 235 Topic 1

Summation Notation n ∑ a i i = m Represents a m + a m+1 + … + a n 29CISC 235 Topic 1

t jpg 30CISC 235 Topic 1

Summation Manipulations 1. Take out constant terms: n n ∑ k/2 = ½ ∑ k k=1 k=1 2.Decompose large terms: n n n n ∑ n-k+k 2 = ∑ n – ∑ k + ∑ k 2 k=1 k=1 k=1 k=1 31CISC 235 Topic 1

Summation Manipulations 3.Partial Sums: n n j ∑ k = ∑ k – ∑ k k=j+1 k=1 k=1 4. Practice: n ∑ kj + a = j=i ________________________ 32CISC 235 Topic 1

Number of Terms in a Summation Upper Bound – Lower Bound + 1 n ∑ 1 = … + 1 = n – = n – 1 k=2 n-1 ∑ 1 = … + 1 = (n–1) – (j+1) + 1 k=j+1 = n – j – 1 Note: This is equivalent to calculating the number of iterations in a for loop 33CISC 235 Topic 1

Calculating Arithmetic Summations ((First Term + Last Term) * Number of Terms) / 2 n ∑ k = … + n = (1+n) * n/2 = n(n+1) k=1 2 n-2 ∑ n – i – 1 = (n–1) + (n–2) + … + 1 i=0 = ((n-1) + 1) * (n-1)/2 = n( n-1) 2 34CISC 235 Topic 1

Example: Max Subsequence Sum int maxSum = 0; int besti = 0; int bestj = 0; for ( int i = 0; i < n; i++ ) for ( int j = i; j < n; j++ ) { int thisSum = 0; for ( int k = i; k <= j; k++ ) thisSum = thisSum + A[k]; if ( thisSum > maxSum ) { maxSum = thisSum; besti = i; bestj = j; } 35CISC 235 Topic 1

Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) Insert( L, x ) Delete( L, x ) 36CISC 235 Topic 1

Limitations of Big-Oh Analysis Not appropriate for small amounts of input –Use the simplest algorithm Large constants can affect which algorithm is more efficient –e.g., 2nlogn versus 1000n Assumption that all basic operations take 1 time unit is faulty –e.g., memory access versus disk access (1000s of time more ) Big-Oh can give serious over-estimate –e.g., loop inside an if statement that seldom executes 37CISC 235 Topic 1