Kinds Of Complexity Worst-case complexity. Average complexity. Amortized complexity.

Slides:



Advertisements
Similar presentations
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M
Advertisements

C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
The Game of Algebra or The Other Side of Arithmetic The Game of Algebra or The Other Side of Arithmetic © 2007 Herbert I. Gross by Herbert I. Gross & Richard.
Analysis Of Binomial Heaps. Operations Insert  Add a new min tree to top-level circular list. Meld  Combine two circular lists. Remove min  Pairwise.
Mathematical Induction II Lecture 14: Nov
Induction Lecture 5: Sep 21 (chapter of the book and chapter of the notes)
Asymptotic Notation (O, Ω,  ) s Describes the behavior of the time or space complexity for large instance characteristics s Common asymptotic functions.
Advanced Data Structures Sartaj Sahni
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 4 By Herb I. Gross and Richard A. Medeiros next.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2010 Lecture 3 Tuesday, 2/9/10 Amortized Analysis.
CPSC 411, Fall 2008: Set 6 1 CPSC 411 Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Jennifer Welch Fall 2008.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2005 Lecture 3 Tuesday, 2/8/05 Amortized Analysis.
Analysis Of Binomial Heaps. Operations Insert  Add a new min tree to top-level circular list. Meld  Combine two circular lists. Delete min  Pairwise.
Tirgul 9 Amortized analysis Graph representation.
Complexity Analysis (Part I)
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2009 Lecture 3 Tuesday, 2/10/09 Amortized Analysis.
Andreas Klappenecker [based on the slides of Prof. Welch]
17.Amortized analysis Hsu, Lih-Hsing. Computer Theory Lab. Chapter 17P.2 The time required to perform a sequence of data structure operations in average.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 2 (Part 2) Tuesday, 9/11/01 Amortized Analysis.
Amortized Analysis Not just consider one operation, but a sequence of operations on a given data structure. Average cost over a sequence of operations.
Algebra Problems… Solutions
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
Chapter 28 Direct Current Circuits. Electromotive Force An electromotive force device, or emf device, is a source of constant potential. The emf describes.
Ajinkya Nene, Lynbrook CS Club. 04/21/2014. Definition Allows you to figure the worst-case bound for the performance of an algorithm (most useful for.
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
1 Introduction to Abstract Mathematics Chapter 4: Sequences and Mathematical Induction Instructor: Hayk Melikya 4.1- Sequences. 4.2,
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Rational Expressions Much of the terminology and many of the techniques for the arithmetic of fractions of real numbers carry over to algebraic fractions,
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Amortized Analysis The problem domains vary widely, so this approach is not tied to any single data structure The goal is to guarantee the average performance.
Amortized Complexity Aggregate method. Accounting method. Potential function method.
Amortized Analysis Typically, most data structures provide absolute guarantees on the worst case time for performing a single operation. We will study.
1 Section 5.5 Solving Recurrences Any recursively defined function ƒ with domain N that computes numbers is called a recurrence or recurrence relation.
Asymptotic Notation (O, Ω, )
A grammar for arithmetic expressions involving the four basic operators and parenthesized expressions. Parenthesized expressions have the highest precedence.
1 Chapter 17: Amortized Analysis I. 2 About this lecture Given a data structure, amortized analysis studies in a sequence of operations, the average time.
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
Amortized Analysis We examined worst-case, average-case and best-case analysis performance In amortized analysis we care for the cost of one operation.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Amortized Analysis Some of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore.
CSCE 411H Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 6 1 * Slides adapted.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
CHP-3 STACKS.
Condition Testing. Condition testing is a test case design method that exercises the logical conditions contained in a program module. A simple condition.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Amortized Analysis. Problem What is the time complexity of n insert operations into an dynamic array, which doubles its size each time it is completely.
Introduction to Algorithms Amortized Analysis My T. UF.
Amortized Analysis In amortized analysis, the time required to perform a sequence of operations is averaged over all the operations performed Aggregate.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
CS 461 – Sept. 23 Context-free grammars Derivations Ambiguity Proving correctness.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Andreas Klappenecker [partially based on the slides of Prof. Welch]
Mathematical Induction II
CSCE 411 Design and Analysis of Algorithms
Analysis Of Binomial Heaps
Chapter 17: Amortized Analysis I
Amortized Analysis The problem domains vary widely, so this approach is not tied to any single data structure The goal is to guarantee the average performance.
Algorithms and Data Structures
Lecture 3 Expressions Richard Gesick.
Sorting … and Insertion Sort.
CSCE 411 Design and Analysis of Algorithms
Copyright © Cengage Learning. All rights reserved.
Induction Rosen 5 Lecture 8: Oct 29, 30.
Assignment Operators Topics Increment and Decrement Operators
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

Kinds Of Complexity Worst-case complexity. Average complexity. Amortized complexity.

Task Sequence Suppose that a sequence of n tasks is performed. The worst-case cost of a task is c wc. Let c i be the (actual) cost of the i th task in this sequence. So, c i <= c wc, 1 <= i <= n. n * c wc is an upper bound on the cost of the sequence. j * c wc is an upper bound on the cost of the first j tasks.

Task Sequence Let c avg be the average cost of a task in this sequence. So, c avg =  c i /n. n * c avg is the cost of the sequence. j * c avg is not an upper bound on the cost of the first j tasks. Usually, determining c avg is quite hard.

Task Sequence At times, a better upper bound than j * c wc or n * c wc on sequence cost is obtained using amortized complexity.

Amortized Complexity The amortized complexity of a task is the amount you charge the task. The conventional way to bound the cost of doing a task n times is to use one of the expressions  n*(worst-case cost of task)   worst-case cost  of task i  The amortized complexity way to bound the cost of doing a task n times is to use one of the expressions  n*(amortized cost of task)   amortized cost of task i 

Amortized Complexity The amortized complexity/cost of individual tasks in any task sequence must satisfy:  actual cost  of task i   amortized cost of task i  So, we can use  amortized cost of task i  as a bound on the actual complexity of the task sequence.

Amortized Complexity The amortized complexity of a task may bear no direct relationship to the actual complexity of the task.

Amortized Complexity In conventional complexity analysis, each task is charged an amount that is >= its cost.  actual cost  of task i   worst-case cost of task i) In amortized analysis, some tasks may be charged an amount that is < their cost.  actual cost  of task i   amortized cost of task i)

Arithmetic Statements Rewrite an arithmetic statement as a sequence of statements without using parentheses. a = x+((a+b)*c+d)+y; is equivalent to the sequence: z1 = a+b; z2 = z1*c+d; a = x+z2+y;

Arithmetic Statements The rewriting is done using a stack and a method processNextSymbol. create an empty stack; for (int i = 1; i <= n; i++) // n is number of symbols in statement processNextSymbol(); a = x+((a+b)*c+d)+y;

Arithmetic Statements processNextSymbol extracts the next symbol from the input statement. Symbols other than ) and ; are simply pushed on to the stack. a = x+((a+b)*c+d)+y; a = x + ( ( a + b

Arithmetic Statements If the next symbol is ), symbols are popped from the stack up to and including the first (, an assignment statement is generated, and the left hand symbol is added to the stack. a = x+((a+b)*c+d)+y; a = x + ( ( a + b z1 = a+b;

Arithmetic Statements a = x+((a+b)*c+d)+y; a = x + ( z1 z1 = a+b; * c + d z2 = z1*c+d; If the next symbol is ), symbols are popped from the stack up to and including the first (, an assignment statement is generated, and the left hand symbol is added to the stack.

Arithmetic Statements a = x+((a+b)*c+d)+y; a = x + z2 z1 = a+b; z2 = z1*c+d; + y If the next symbol is ), symbols are popped from the stack up to and including the first (, an assignment statement is generated, and the left hand symbol is added to the stack.

Arithmetic Statements If the next symbol is ;, symbols are popped from the stack until the stack becomes empty. The final assignment statement a = x+z2+y; is generated. a = x+((a+b)*c+d)+y; z1 = a+b; a = x + z2 + y z2 = z1*c+d;

Complexity Of processNextSymbol O(number of symbols that get popped from stack) O(i), where i is for loop index. a = x+((a+b)*c+d)+y;

Overall Complexity (Conventional Analysis) So, overall complexity is O(  i) = O(n 2 ). Alternatively, O(n*n) = O(n 2 ). Although correct, a more careful analysis permits us to conclude that the complexity is O(n). create an empty stack; for (int i = 1; i <= n; i++) // n is number of symbols in statement processNextSymbol();

Ways To Determine Amortized Complexity Aggregate method. Accounting method. Potential function method.

Aggregate Method Somehow obtain a good upper bound on the actual cost of the n invocations of processNextSymbol() Divide this bound by n to get the amortized cost of one invocation of processNextSymbol() Easy to see that  actual cost  amortized cost 

Aggregate Method The actual cost of the n invocations of processNextSymbol() equals number of stack pop and push operations. The n invocations cause at most n symbols to be pushed on to the stack. This count includes the symbols for new variables, because each new variable is the result of a ) being processed. Note that no )s get pushed on to the stack.

Aggregate Method The actual cost of the n invocations of processNextSymbol() is at most 2n. So, using 2n/n = 2 as the amortized cost of processNextSymbol() is OK, because this cost results in  actual cost  amortized cost  Since the amortized cost of processNextSymbol() is 2, the actual cost of all n invocations is at most 2n.

Aggregate Method The aggregate method isn’t very useful, because to figure out the amortized cost we must first obtain a good bound on the aggregate cost of a sequence of invocations. Since our objective was to use amortized complexity to get a better bound on the cost of a sequence of invocations, if we can obtain this better bound through other techniques, we can omit dividing the bound by n to obtain the amortized cost.

Potential Function P(i) = amortizedCost(i) – actualCost(i) + P(i – 1)  (P(i) – P(i–1)) =  (amortizedCost(i) –actualCost(i)) P(n) – P(0) =  (amortizedCost(i) –actualCost(i)) P(n) – P(0) >= 0 When P(0) = 0, P(i) is the amount by which the first i tasks/operations have been over charged.

Potential Function Example a = x + ( ( a + b ) * c + d ) + y ; actual cost amortized cost potential Potential = stack size except at end.

Accounting Method Guess the amortized cost. Show that P(n) – P(0) >= 0.

Accounting Method Example Guess that amortized complexity of processNextSymbol is 2. Start with P(0) = 0. Can show that P(i) >= number of elements on stack after ith symbol is processed. create an empty stack; for (int i = 1; i <= n; i++) // n is number of symbols in statement processNextSymbol();

Accounting Method Example Potential >= number of symbols on stack. Therefore, P(i) >= 0 for all i. In particular, P(n) >= 0. a = x + ( ( a + b ) * c + d ) + y ; actual cost amortized cost potential

Potential Method Guess a suitable potential function for which P(n) – P(0) >= 0 for all n. Derive amortized cost of ith operation using  P = P(i) – P(i–1) = amortized cost – actual cost amortized cost = actual cost +  P

Potential Method Example Guess that the potential function is P(i) = number of elements on stack after i th symbol is processed (exception is P(n) = 2). P(0) = 0 and P(i) – P(0) >= 0 for all i. create an empty stack; for (int i = 1; i <= n; i++) // n is number of symbols in statement processNextSymbol();

i th Symbol Is Not ) or ; Actual cost of processNextSymbol is 1. Number of elements on stack increases by 1.  P = P(i) – P(i–1) = 1. amortized cost = actual cost +  P = = 2

i th Symbol Is ) Actual cost of processNextSymbol is #unstacked + 1. Number of elements on stack decreases by #unstacked –1.   P = P(i) – P(i–1) = 1 – #unstacked. amortized cost = actual cost +  P = #unstacked (1 – #unstacked) = 2

i th Symbol Is ; Actual cost of processNextSymbol is #unstacked = P(n–1). Number of elements on stack decreases by P(n–1).   P = P(n) – P(n–1) = 2 – P(n–1). amortized cost = actual cost +  P = P(n–1) + (2 – P(n–1)) = 2