CMSC 341 Asymptotic Anaylsis1 CMSC 341 Asymptotic Analysis.

Slides:



Advertisements
Similar presentations
CMSC 341 Asymptotic Analysis. 2 Mileage Example Problem: John drives his car, how much gas does he use?
Advertisements

Analysis of Algorithms
Discrete Structures CISC 2315
Lecture: Algorithmic complexity
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
Chapter 10 Algorithm Efficiency
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Asymptotic Growth Rate
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
Analysis of Algorithm.
Elementary Data Structures and Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
Asymptotic Notations Iterative Algorithms and their analysis
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.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Mathematics Review and Asymptotic Notation
CMSC 341 Asymptotic Analysis. 8/3/07 UMBC CMSC 341 AA2-color 2 Complexity How many resources will it take to solve a problem of a given size?  time 
CS 3343: Analysis of Algorithms
Iterative Algorithm Analysis & Asymptotic Notations
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Analysis of Algorithms
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.
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.
Asymptotic Analysis-Ch. 3
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright ©
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program.
Asymptotic Growth Rates  Themes  Analyzing the cost of programs  Ignoring constants and Big-Oh  Recurrence Relations & Sums  Divide and Conquer 
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.
Algorithm Analysis Part of slides are borrowed from UST.
Analysis of Algorithm. Why Analysis? We need to know the “behavior” of algorithms – How much resource (time/space) does it use So that we know when to.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
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.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Searching Topics Sequential Search Binary Search.
8/3/07CMSC 341 Asymptotic Anaylsis1 CMSC 341 Asymptotic Analysis.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
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.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Analysis of Algorithms
CMSC 341 Asymptotic Analysis.
CMSC 341 Asymptotic Analysis.
At the end of this session, learner will be able to:
Estimating Algorithm Performance
Presentation transcript:

CMSC 341 Asymptotic Anaylsis1 CMSC 341 Asymptotic Analysis

CMSC 341 Asymptotic Anaylsis 2 Complexity How many resources will it take to solve a problem of a given size?  time  space Expressed as a function of problem size (beyond some minimum size)  how do requirements grow as size grows? Problem size  number of elements to be handled  size of thing to be operated on

CMSC 341 Asymptotic Anaylsis 3 The Goal of Asymptotic Analysis How to analyze the running time (aka computational complexity) of an algorithm in a theoretical model. Using a theoretical model allows us to ignore the effects of  Which computer are we using?  How good is our compiler at optimization We define the running time of an algorithm with input size n as T ( n ) and examine the rate of growth of T( n ) as n grows larger and larger and larger.

CMSC 341 Asymptotic Anaylsis 4 Growth Functions Constant T(n) = c ex: getting array element at known location any simple C++ statement (e.g. assignment) Linear T(n) = cn [+ possible lower order terms] ex: finding particular element in array of size n (i.e. sequential search) trying on all of your n shirts

CMSC 341 Asymptotic Anaylsis 5 Growth Functions (cont.) Quadratic T(n) = cn 2 [ + possible lower order terms] ex: sorting all the elements in an array (using bubble sort) (trying all your n shirts with all your n ties) Polynomial T(n) = cn k [ + possible lower order terms] ex:maximum matching problems in graph (we will talk about this later in the semester)

CMSC 341 Asymptotic Anaylsis 6 Growth Functions (cont.) Exponential T(n) = c n [+ possible lower order terms] ex:(constructing all possible orders of array elements) - Recursively calculating n th Fibonacci number (2 n ) Logarithmic T(n) = lg n [ + possible lower order terms] ex: (algorithms that continually divides a problem in half) - binary search (find the index of an item in a presorted array)

8/3/07 CMSC 341 Asymptotic Anaylsis 7 Int fibo(int n) { If (n==0 || n==1) return 1; Else return fibo(n-1) + fibo (n-2); } Add the result of two recursive method calls: 1 op Each recursive call will add the value of two further recursive calls (4 in all), 2 ops; Each of those call will add the value of two further recursive calls (8 in all): 4 ops; And so on, until we reach fibo(1) and fibo (0) It will take n-1 steps to reach the basis case. T(n) = … + 2^(n-2) = o(2^n)

CMSC 341 Asymptotic Anaylsis 8 Growth Functions (cont.) Exponential T(n) = c n [+ possible lower order terms] ex:(constructing all possible orders of array elements) - Recursively calculating n th Fibonacci number (2 n ) Logarithmic T(n) = lg n [ + possible lower order terms] ex: (algorithms that continually divides a problem in half) - binary search (find the index of an item in a presorted array)

8/3/07 CMSC 341 Asymptotic Anaylsis 9 int binarySearch (int [ ] a, int x) { int low=0, high=a.length-1; while (low<=high) { int mid = (low+high)/2; if(a[mid] < x) low = mid+1; else if (a[mid > x) high = mid -1; else return mid; // found }

CMSC 341 Asymptotic Anaylsis 10 A Graph of Growth Functions

CMSC 341 Asymptotic Anaylsis 11 Expanded Scale

CMSC 341 Asymptotic Anaylsis 12 Asymptotic Analysis How does the time (or space) requirement grow as the problem size grows really, really large?  We are interested in “order of magnitude” growth rate.  We are usually not concerned with constant multipliers. For instance, if the running time of an algorithm is proportional to (let’s suppose) the square of the number of input items, i.e. T(n) is c*n 2, we won’t (usually) be concerned with the specific value of c.  Lower order terms don’t matter.

CMSC 341 Asymptotic Anaylsis 13 Analysis Cases What particular input (of given size) gives worst/best/average complexity? Best Case: If there is a permutation of the input data that minimizes the “run time efficiency”, then that minimum is the best case run time efficiency Worst Case: If there is a permutation of the input data that maximizes the “run time efficiency”, then that maximum is the best case run time efficiency Average case is the “run time efficiency” over all possible inputs. Mileage example: how much gas does it take to go 20 miles?  Worst case: all uphill  Best case: all downhill, just coast  Average case: “average terrain

CMSC 341 Asymptotic Anaylsis 14 Cases Example Consider sequential search on an unsorted array of length n, what is time complexity? Best case: Worst case: Average case:

8/3/07 CMSC 341 Asymptotic Anaylsis 15 int sequentialSearch (int a[], int x, int n) { for( i=0; i< n; i++) { if(a[i]==x) return i; } return -1; // not found } Best case: 1 Worst case: n Average case: the key is equally likely to be in any position in the array: (1+2+… +n) /n = T((n+1)/2) = O(n)

CMSC 341 Asymptotic Anaylsis 16 Definition of Big-Oh T(n) = O(f(n)) (read “T( n ) is in Big-Oh of f( n )” ) if and only if T(n)  cf(n) for some constants c, n 0 and n  n 0 This means that eventually (when n  n 0 ), T( n ) is always less than or equal to c times f( n ). The growth rate of T(n) is less than or equal to that of f(n) Loosely speaking, f( n ) is an “upper bound” for T ( n ) NOTE: if T(n) =O(f(n)), there are infinitely many pairs of c’s and n 0 ’ s that satisfy the relationship. We only need to find one such pair for the relationship to hold.

CMSC 341 Asymptotic Anaylsis 17 Big-Oh Example Suppose we have an algorithm that reads N integers from a file and does something with each integer. The algorithm takes some constant amount of time for initialization (say 500 time units) and some constant amount of time to process each data element (say 10 time units). For this algorithm, we can say T( N ) = N. The following graph shows T( N ) plotted against N, the problem size and 20N. Note that the function N will never be larger than the function T( N ), no matter how large N gets. But there are constants c 0 and n 0 such that T( N ) = n 0, namely c 0 = 20 and n 0 = 50. Therefore, we can say that T( N ) is in O( N ).

CMSC 341 Asymptotic Anaylsis 18 Simplifying Assumptions 1. If f(n) = O(g(n)) and g(n) = O(h(n)), then f(n) = O(h(n)) 2. If f(n) = O(kg(n)) for any k > 0, then f(n) = O(g(n)) 3. If f 1 (n) = O(g 1 (n)) and f 2 (n) = O(g 2 (n)), then f 1 (n) + f 2 (n) = O(max (g 1 (n), g 2 (n))) 4. If f 1 (n) = O(g 1 (n)) and f 2 (n) = O(g 2 (n)), then f 1 (n) * f 2 (n) = O(g 1 (n) * g 2 (n))

8/3/07 CMSC 341 Asymptotic Anaylsis 19 for (i=0; i<n; i++) { for(j=0; j<n; j++) a+=1; } O(n^2)

CMSC 341 Asymptotic Anaylsis 20 Example Code: sum = 0; for (i = 1; i <= n; i++) sum += n; Complexity: O(n): T(n) = n

CMSC 341 Asymptotic Anaylsis 21 Example Code: sum1 = 0; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) sum1++; Complexity: T(n) =n^2 O(n^2)

CMSC 341 Asymptotic Anaylsis 22 Example Code: sum1 = 0; for (i = 1; i <= m; i++) for (j = 1; j <= n; j++) sum1++; Complexity: T = m*n, O(m*n) or O(m^2 or n^2)

CMSC 341 Asymptotic Anaylsis 23 Example Code: sum2 = 0; for (i = 1 ; i <= n; i++) for (j = 1; j <= i; j++) sum2++; Complexity: T (n) = … + n = n(n+1) / 2 O(n^2)

CMSC 341 Asymptotic Anaylsis 24 Example Code: sum = 0; for (j = 1; j <= n; j++) for (i = 1; i <= j; i++) sum++; for (k = 0; k < n; k++) a[ k ] = k; Complexity:

CMSC 341 Asymptotic Anaylsis 25 Example Code: sum1 = 0; for (k = 1; k <= n; k *= 2) for (j = 1; j <= n; j++) sum1++; Complexity:

CMSC 341 Asymptotic Anaylsis 26 Example Using Horner’s rule to convert a string to an integer static int convertString(String key) { int intValue = 0; // Horner’s rule for (int i = 0; i < key.length(); i++) intValue = 37 * intValue + key.charAt(i); return intValue }

CMSC 341 Asymptotic Anaylsis 27 Example Square each element of an N x N matrix Printing the first and last row of an N x N matrix Finding the smallest element in a sorted array of N integers Printing all permutations of N distinct elements

CMSC 341 Asymptotic Anaylsis 28 Space Complexity Does it matter? What determines space complexity? How can you reduce it? What tradeoffs are involved?

CMSC 341 Asymptotic Anaylsis 29 Constants in Bounds (“constants don’t matter”) Theorem: If T(x) = O(cf(x)), then T(x) = O(f(x)) Proof:  T(x) = O(cf(x)) implies that there are constants c 0 and n 0 such that T(x)  c 0 (cf(x)) when x  n 0  Therefore, T(x)  c 1 (f(x)) when x  n 0 where c 1 = c 0 c  Therefore, T(x) = O(f(x))

CMSC 341 Asymptotic Anaylsis 30 Sum in Bounds (the “sum rule”) Theorem: Let T 1 (n) = O(f(n)) and T 2 (n) = O(g(n)). Then T 1 (n) + T 2 (n) = O(max (f(n), g(n))). Proof:  From the definition of O, T 1 (n)  c 1 f (n) for n  n 1 and T 2 (n)  c 2 g(n) for n  n 2  Let n 0 = max(n 1, n 2 ).  Then, for n  n 0, T 1 (n) + T 2 (n)  c 1 f (n) + c 2 g(n)  Let c 3 = max(c 1, c 2 ).  Then, T 1 (n) + T 2 (n)  c 3 f (n) + c 3 g (n)  2c 3 max(f (n), g (n))  c max(f (n), g (n)) = O (max (f(n), g(n)))

CMSC 341 Asymptotic Anaylsis 31 Products in Bounds (“the product rule”) Theorem: Let T 1 (n) = O(f(n)) and T 2 (n) = O(g(n)). Then T 1 (n) * T 2 (n) = O(f(n) * g(n)). Proof:  Since T 1 (n) = O(f(n)), then T 1 (n)  c 1 f(n) when n  n 1  Since T 2 (n) = O(g(n)), then T 2 (n)  c 2 g(n) when n  n 2  Hence T 1 (n) * T 2 (n)  c 1 * c 2 * f(n) * g(n) when n  n 0 where n 0 = max (n 1, n 2 )  And T 1 (n) * T 2 (n)  c * f (n) * g(n) when n  n 0 where n 0 = max (n 1, n 2 ) and c = c 1* c 2  Therefore, by definition, T 1 (n)*T 2 (n) = O(f(n)*g(n)).

CMSC 341 Asymptotic Anaylsis 32 Polynomials in Bounds Theorem: If T (n) is a polynomial of degree k, then T(n) = O(n k ). Proof:  T (n) = n k + n k-1 + … + c is a polynomial of degree k.  By the sum rule, the largest term dominates.  Therefore, T(n) = O(n k ).

CMSC 341 Asymptotic Anaylsis 33 L’Hospital’s Rule Finding limit of ratio of functions as variable approaches  Use this rule to prove other function growth relationships f(x) = O(g(x)) if

CMSC 341 Asymptotic Anaylsis 34 Polynomials of Logarithms in Bounds Theorem: lg k n = O(n) for any positive constant k (i.e. logarithmic functions grow slower than linear functions) Proof:  Note that lg k n means (lg n) k.  Need to show lg k n  cn for n  n 0. Equivalently, can show lg n  cn 1/k  Letting a = 1/k, we will show that lg n = O(n a ) for any positive constant a. Use L’Hospital’s rule: Ex: lg (n) = O(n)

CMSC 341 Asymptotic Anaylsis 35 Polynomials vs Exponentials in Bounds Theorem: n k = O(a n ) for a > 1 (e.g. polynomial functions grow slower than exponential functions) Proof:  Use L’Hospital’s rule = 0 Ex: n = O( n ) Theorem: n k = O(a n ) for a > 1 (e.g. polynomial functions grow slower than exponential functions) Proof:  Use L’Hospital’s rule = 0

CMSC 341 Asymptotic Anaylsis 36 Little-Oh and Big-Theta In addition to Big-O, there are other definitions used when discussing the relative growth of functions Big-Theta – T(n) = Θ( f(n) ) if c 1 *f(n) ≤ T(n) ≤ c 2 *f(n) This means that f(n) is both an upper- and lower-bound for T(n) In particular, if T(n) = Θ( f(n) ), then T(n) = O( f(n) ) Little-Oh – T(n) = o( f(n) ) if for all constants c there exist n 0 such that T(n) < c*f(n). Note that this is more stringent than the definition of Big-O and therefore if T( n ) = o( f(n) ) then T(n) = O( f(n) )

CMSC 341 Asymptotic Anaylsis 37 Determining Relative Order of Growth Given the definitions of Big-Theta and Little-o, we can compare the relative growth of any two functions using limits. See text pages 29 – 31. f(x) = o(g(x)) if By definition, if f(x) = o(g(x)), then f(x) = O(g(x)). f(x) = Θ(g(x)) if for some constant c > 0. By definition if f(x) = Θ(g(x)), then f(x) = O(g(x))

CMSC 341 Asymptotic Anaylsis 38 Determining relative order of Growth Often times using limits is unnecessary as simple algebra will do. For example, if f(n) = n log n and g(n) = n 1.5 then deciding which grows faster is the same as determining which of f(n) = log n and g(n) = n 0.5 grows faster (after dividing both functions by n), which is the same as determining which of f(n) = log 2 n and g(n) = n grows faster (after squaring both functions). Since we know from previous theorems that n (linear functions) grows faster than any power of a log, we know that g(n) grows faster than f(n).

CMSC 341 Asymptotic Anaylsis 39 Relative Orders of Growth An Exercise n (linear) log k n for 0 < k < 1 constant n 1+k for k > 0 (polynomial) 2 n (exponential) n log n log k n for k > 1 n k for 0 < k < 1 log n

CMSC 341 Asymptotic Anaylsis 40 Relative Orders of Growth Answers constant log k n for 0 < k < 1 log n log k n for k> 1 n k for k < 1 n (linear) n log n n 1+k for k > 0 (polynomial) 2 n (exponential)

CMSC 341 Asymptotic Anaylsis 41 Big-Oh is not the whole story Suppose you have a choice of two approaches to writing a program. Both approaches have the same asymptotic performance (for example, both are O(n lg(n)). Why select one over the other, they're both the same, right? They may not be the same. There is this small matter of the constant of proportionality. Suppose algorithms A and B have the same asymptotic performance, T A (n) = T B (n) = O(g(n)). Now suppose that A does 10 operations for each data item, but algorithm B only does 3. It is reasonable to expect B to be faster than A even though both have the same asymptotic performance. The reason is that asymptotic analysis ignores constants of proportionality. The following slides show a specific example.

CMSC 341 Asymptotic Anaylsis 42 Algorithm A Let's say that algorithm A is { initialization// takes 50 units read in n elements into array A;// 3 units/element for (i = 0; i < n; i++) { do operation1 on A[i]; // takes 10 units do operation2 on A[i]; // takes 5 units do operation3 on A[i]; // takes 15 units } } T A (n) = n + ( )n = n

CMSC 341 Asymptotic Anaylsis 43 Algorithm B Let's now say that algorithm B is { initialization// takes 200 units read in n elements into array A; // 3 units/element for (i = 0; i < n; i++) { do operation1 on A[i]; // takes 10 units do operation2 on A[i]; //takes 5 units } } T B (n) = n + (10 + 5)n = n

CMSC 341 Asymptotic Anaylsis 44 T A ( n ) vs. T B ( n )

CMSC 341 Asymptotic Anaylsis 45 A concrete example NT(n) = nT(n) = nlgnT(n) = n 2 T(n) = n 3 Tn = 2 n  s0.01  s0.03  s0.13  s0.03  s  s0.03  s0.1  s1  s  s0.09  s0.4  s8  s 1 ms  s0.28  s2.5  s125  s 13 days  s0.66  s10  s 1 ms4 x years The following table shows how long it would take to perform T(n) steps on a computer that does 1 billion steps/second. Note that a microsecond is a millionth of a second and a millisecond is a thousandth of a second. Notice that when n >= 50, the computation time for T(n) = 2 n has started to become too large to be practical. This is most certainly true when n >= 100. Even if we were to increase the speed of the machine a million-fold, 2 n for n = 100 would be 40,000,000 years, a bit longer than you might want to wait for an answer.

CMSC 341 Asymptotic Anaylsis 46 Amortized Analysis Sometimes the worst-case running time of an operation does not accurately capture the worst-case running time of a sequence of operations. What is the worst-case running time of ArrayList’s add( ) method that places a new element at the end of the ArrayList? The idea of amortized analysis is to determine the average running time of the worst case.

CMSC 341 Asymptotic Anaylsis 47 Amortized Example – add() In the worst case, there is no room in the ArrayList for the new element, X. The ArrayList then doubles its current size, copies the existing elements into the new ArrayList, then places X in the next available slot. This operation is O( N ) where N is the current number of elements in the ArrayList. But this doubling happens very infrequently. (how often?) If there is room in the ArrayList for X, then it is just placed in the next available slot in the ArrayList and no doubling is required. This operation is O( 1 ) – constant time To discuss the running time of add( ) it makes more sense to look at a long sequence of add( ) operations rather than individual operations since not all individual operations A sequence of N add( ) operations can always be done in O(N), so we say the amortized running time of per add( )operation is O(N) / N = O(1) or constant time. We are willing to perform a very slow operation (doubling the vector size) very infrequently in exchange for frequently having very fast operations.

CMSC 341 Asymptotic Anaylsis 48 Amortized Analysis Example What is the average number of bits that are changed when a binary number is incremented by 1? For example, suppose we increment We will change just 1 bit to get Incrementing again produces , but this time 2 bits were changed. Some increments will be “expensive”, others “cheap”. How can we get an average? We do this by looking at a sequence of increments. When we compute the total number of bits that change with n increments, divide that total by n, the result will be the average number of bits that change with an increment. The table on the next slide shows the bits that change as we increment a binary number.(changed bits are shown in red).

CMSC 341 Asymptotic Anaylsis 49 Analysis Total bits changed 00000Start = We see that bit position 2 0 changes every time we increment. Position 2 1 every other time (1/2 of the increments), and bit position 2 J changes each 1/2 J increments. We can total up the number of bits that change:

CMSC 341 Asymptotic Anaylsis 50 Analysis, continued The total number of bits that are changed by incrementing n times is: When we perform n increments, the total number of bit changes is <= 2n. The average number of bits that will be flipped is 2n/n = 2. So the amortized cost of each increment is constant, or O(1). We can simplify the summation: