Chapter 2: Complexity Analysis

Similar documents
PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

CS:3330 (22c:31) Algorithms

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

Another Sorting Algorithm

Introduction to Data Structure

Complexity of Algorithms. Andreas Klappenecker

csci 210: Data Structures Program Analysis

UNIT 1 ANALYSIS OF ALGORITHMS

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

Test 1 SOLUTIONS. June 10, Answer each question in the space provided or on the back of a page with an indication of where to find the answer.

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Algorithm Analysis. Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Introduction to Computers & Programming

Complexity of Algorithms

Choice of C++ as Language

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017

Data Structure Lecture#5: Algorithm Analysis (Chapter 3) U Kang Seoul National University

Analysis of Algorithms

Introduction to the Analysis of Algorithms. Algorithm

Algorithmic Analysis. Go go Big O(h)!

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

CS S-02 Algorithm Analysis 1

Algorithms and Data Structures

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Complexity Analysis of an Algorithm

Data Structures Lecture 8

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Analysis of Algorithm. Chapter 2

Lecture 5: Running Time Evaluation

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

4.4 Algorithm Design Technique: Randomization

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

Analysis of Algorithms

Lecture 2: Algorithm Analysis

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

CS240 Fall Mike Lam, Professor. Algorithm Analysis

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

Data Structures and Algorithms Chapter 2

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

Introduction to Analysis of Algorithms

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments

Introduction to Computer Science

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions. John Edgar 2

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Data Structures and Algorithms. Part 2

Big-O-ology. Jim Royer January 16, 2019 CIS 675. CIS 675 Big-O-ology 1/ 19

Algorithm Analysis. CENG 707 Data Structures and Algorithms

1. Answer the following questions about each code fragment below. [8 points]

CS 360 Exam 1 Fall 2014 Solution. 1. Answer the following questions about each code fragment below. [8 points]

University of Petra Faculty of Information Technology

1. Answer the following questions about each code fragment below. [8 points]

Data Structures and Algorithms

Analysis of Algorithms. CSE Data Structures April 10, 2002

Measuring algorithm efficiency

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

CS240 Fall Mike Lam, Professor. Algorithm Analysis

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

(Refer Slide Time: 1:27)

Lecture 5 Sorting Arrays

Scientific Computing. Algorithm Analysis

Lecture Notes on Sorting

Internal Sorting by Comparison

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

Outline and Reading. Analysis of Algorithms 1

Recall from Last Time: Big-Oh Notation

Computational Complexity: Measuring the Efficiency of Algorithms

Sorting. Bubble Sort. Selection Sort

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Asymptotic Analysis of Algorithms

Data Structures and Algorithms Key to Homework 1

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

Round 1: Basics of algorithm analysis and data structures

Ways to Fail this Class. Welcome! Questions? Course Policies. CSC 9010: Natural Language Processing. Who / what / where / when / why / how 1/9/2014

Algorithms and Data Structures

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

CS 3410 Ch 5 (DS*), 23 (IJP^)

CS 360 Exam 1 Fall 2014 Name. 1. Answer the following questions about each code fragment below. [8 points]

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

O(n): printing a list of n items to the screen, looking at each item once.

Introduction to Computer Science

IT 4043 Data Structures and Algorithms

Assignment 1 (concept): Solutions

Data Structures Question Bank Multiple Choice

Introduction to Algorithms and Complexity Theory

Math 501 Solutions to Homework Assignment 10

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

Transcription:

Chapter 2: Complexity Analysis

Objectives Looking ahead in this chapter, we ll consider: Computational and Asymptotic Complexity Big-O Notation Properties of the Big-O Notation Ω and Θ Notations Possible Problems with the Notation Edited from: Data Structures and Algorithms in C++, Fourth Edition 2

Objectives (continued) Examples of Complexities Finding Asymptotic Complexity Best, Average, and Worst Cases Amortized Complexity NP-Completeness Data Structures and Algorithms in C++, Fourth Edition 3

Computational and Asymptotic Complexity Algorithms are an essential aspect of data structures Data structures are implemented using algorithms Some algorithms are more efficient than others Efficiency is preferred; we need metrics to compare them An algorithm s complexity is a function describing the efficiency of the algorithm in terms of the amount of data the algorithm must process There are two main complexity measures of efficiency Data Structures and Algorithms in C++, Fourth Edition 4

Computational and Asymptotic Complexity Time complexity describes the amount of time an algorithm takes in terms of the amount of input Space complexity describes the amount of memory (space) an algorithm takes in terms of the amount of input For both measures, we are interested in the algorithm s asymptotic complexity This asks: when n (number of input items) goes to infinity, what happens to the algorithm s performance? Data Structures and Algorithms in C++, Fourth Edition 5

Computational and Asymptotic Complexity To illustrate this, consider f(n) = n 2 + 100n + log 10 n + 1000 As the value of n increases, the importance of each term shifts until for large n, only the n 2 term is significant Fig. 2-1 The growth rate of all terms of function f (n) = n 2 + 100n + log 10 n + 1,000. Data Structures and Algorithms in C++, Fourth Edition 6

Big-O Notation The most commonly used notation for asymptotic complexity used is "big-o" notation In the previous example we would say n 2 + 100n + log 10 n + 1000 = O(n 2 ) (read "big-oh of n squared") Definition: Let f(n) and g(n) be functions, where n ε Z is a positive integer. We write f(n) = O(g(n)) if and only if there exists a real number c and positive integer N satisfying 0 < f(n) < cg(n) for all n > N. (And we say, "f of n is big-oh of g of n. ) This means that functions like n 2 + n, 4n 2 - n log n + 12, n 2 /5-100n, n log n, and so forth are all O(n 2 ) Data Structures and Algorithms in C++, Fourth Edition 7

Properties of Big-O Notation The following is a list of useful facts you can use to simplify big-o calculations Big-O is transitive: if f(n) = O(g(n)) and g(n) is O(h(n)), then f(n) = O(h(n)) If f(n) = O(h(n)) and g(n) is O(h(n)), then f(n) + g(n) = O(h(n)) A function an k = O(n k ) for any a > 0 Any k th degree polynomial is O(n k+j ) for any j > 0 Data Structures and Algorithms in C++, Fourth Edition 8

Properties of Big-O Notation (continued) f(n) = O(g(n)) is true if lim n-> f(n)/g(n) is a constant. Put another way, if f(n) = cg(n), then f(n) = O(g(n)) log a n = O(log b n) for any a, b > 1. This means, except for a few cases, we don t care what base our logarithms are Given the preceding, we can use just one base and rewrite the relationship as log a n = O(lg n) for positive a 1 and lg n = log 2 n Data Structures and Algorithms in C++, Fourth Edition 9

Ω and Θ Notations Big-O only gives us the upper bound of a function So if we ignore constant factors and let n get big enough, some function will never be bigger than some other function This can give us too much freedom Consider that selection sort is O(n 3 ), since n 2 is O(n 3 ) - but O(n 2 ) is a more meaningful upper bound We need a lower bound, a function that always grows more slowly than f(n), and a tight bound, a function that grows at about the same rate as f(n) Section 2.4 gives a good introduction to these concepts; let s look at a different way to approach this Data Structures and Algorithms in C++, Fourth Edition 10

Ω and Θ Notations (continued) Big-Ω is for lower bounds what big-o is for upper bounds Definition: Let f(n) and g(n) be functions, where n is a positive integer. We write f(n) = Ω(g(n)) if and only if g(n) = O(f(n)). We say "f of n is omega of g of n. So g is a lower bound for f ; after a certain n, and without regard to multiplicative constants, f will never go below g Finally, theta notation combines upper bounds with lower bounds to get tight bound Definition: Let f(n) and g(n) be functions, where n is a positive integer. We write f(n) = Θ(g(n)) if and only if g(n) = O(f(n)) and g(n) = 0(f(n)). We say "f of n is theta of g of n." Data Structures and Algorithms in C++, Fourth Edition 11

Examples of Complexities Since we examine algorithms in terms of their time and space complexity, we can classify them this way, too This is illustrated in the next figure Fig. 2.4 Classes of algorithms and their execution times on a computer executing 1 million operations per second (1 sec = 10 6 μsec = 10 3 msec) Data Structures and Algorithms in C++, Fourth Edition 12

Examples of Complexities (continued) Fig. 2.4 (concluded) Data Structures and Algorithms in C++, Fourth Edition 13

Finding the Complexity As we have seen, asymptotic bounds are used to determine the time and space efficiency of algorithms Generally, we are interested in time complexity, which is based on assignments and comparisons in a program We ll focus on assignments for the time being Consider a simple loop: for (i = sum = 0; i < n; i++) sum = sum + a[i] Two assignments are executed once (sum = 0 and i = sum) during initialization In the loop, sum = sum + a[i] is executed n times Data Structures and Algorithms in C++, Fourth Edition 14

Finding Asymptotic Complexity (continued) In addition, the i++ in the loop header is executed n times So there are 2 + 2n assignments in this loop s execution and it is O(n) Typically, as loops are nested, the complexity grows by a factor of n, although this isn t always the case Consider for (i = 0; i < n; i++) { for (j = 1, sum = a[0]; j <= i; j++) sum += a[j]; cout << sum for subarray 0 through << i << is <<sum<<end1; } Data Structures and Algorithms in C++, Fourth Edition 15

Finding Asymptotic Complexity (continued) The outer loop initializes i, then executes n times During each pass through the loop, the variable i is updated, and the inner loop and cout statement are executed The inner loop initializes j and sum each time, so the number of assignments so far is 1 + 3n The inner loop executes i times, where i ranges from 1 to n 1, based on the outer loop (when i is 0, it doesn t run) Each time the inner loop executes, it increments j, and assigns a value to sum n 1 So the inner loop executes i=1 2i = 2(1 + 2 + + n 1) = 2n(n 1) assignments Data Structures and Algorithms in C++, Fourth Edition 16

Finding Asymptotic Complexity (continued) The total number of assignments is then 1 + 3n + 2n(n - 1), which is O(1) + O(n) + O(n 2 ) = O(n 2 ) As mentioned earlier, not all loops increase complexity, so care has to be taken to analyze the processing that takes place However, additional complexity can be involved if the number of iterations changes during execution This can be the case in some of the more powerful searching and sorting algorithms Data Structures and Algorithms in C++, Fourth Edition 17

Best, Average, and Worst Cases If we want to truly get a handle on the complexity of more complicated algorithms, we need to distinguish three cases: Worst case the algorithm takes the maximum number of steps Best case the algorithm takes the fewest number of steps Average case performance falls between the extremes For simple situations we can determine the average case by adding together the number of steps required for each input and dividing by the number of inputs However, this is based on each input occurring with equal probability, which isn t always likely Data Structures and Algorithms in C++, Fourth Edition 18

Best, Average, and Worst Cases (continued) To be more precise, we need to weight the number of steps that occur for a given input by the probability of that input occurring, and sum this over the number of inputs: p input steps input i In probability theory, this defines the expected value, which assumes the probabilities can be determined and their distribution known Because p is a probability distribution, it satisfies two constraints: The function p can never be negative The sum of all the probabilities is equal to 1 i i Data Structures and Algorithms in C++, Fourth Edition 19

Best, Average, and Worst Cases (continued) Consider the example of sequentially searching an unordered array to find a target value The best and worst cases are straightforward: Best case occurs when we find the target in the first cell Worst case occurs when we find the target in the last cell, or not at all (but end up searching the entire array) For the average case, we first have to consider the probability of finding the target If we assume a uniform distribution of n values, then the probability of finding the target in any one location is 1 n Data Structures and Algorithms in C++, Fourth Edition 20

Best, Average, and Worst Cases (continued) So we would find the target in the first location with p = 1/n, in the second location with p = 1/n, etc. Since the number of steps required to get to each location is the same as the location itself, our sum becomes: 1/n * (1 + 2 + + n) = (n + 1) / 2 Again, this is based on an equally likely chance of finding the target in any cell If the probabilities differ, then the computation becomes more involved Data Structures and Algorithms in C++, Fourth Edition 21

Selection Sort Selection Sorting Algorithm: During the j-th pass (j = 0, 1,, n 2), we will examine the elements of the array a[j], a[j+1],, a[n- 1] and determine the index min of the smallest key. Swap a[min] and a[j]. selection_sort(int_array a) { if (a.size() == 1) return; n = a.size(); for (int j = 0; j < n 1; ++j) { min = j; for (int k= j+1; k<=n-1; ++k) if (a[k] < a[min]) min = k; swap a[min] and a[j]; } } http://math.hws.edu/tmcm/java/xsortlab/

Analysis of selection sorting Consider the program to find the min number in an array: min = 0; for (j = 1; j < n; ++j) if (A[j] > min) min = j; The number of comparisons performed is n 1. loop starts with j = 1 and ends with j = n so the number of iterations = n 1. In each iteration, one comparison is performed.

The inner loop: Selection sorting analysis n 1 comparisons during the first iteration of the inner loop n 2 comparisons during the 2nd iteration of the inner loop.... 1 comparison during the last iteration of the inner loop Total number of comparisons = 1 + 2 + + (n 1) = n(n 1)/ 2 (best as well as the worst-case)