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

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

Introduction to Computers & Programming

Lecture 2: Algorithm Analysis

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

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

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Chapter 2: Complexity Analysis

Algorithm Analysis Part 2. Complexity Analysis

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

Introduction to Data Structure

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

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

Complexity Analysis of an Algorithm

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

Analysis of Algorithm. Chapter 2

Choice of C++ as Language

CS S-02 Algorithm Analysis 1

Data Structures and Algorithms

UNIT 1 ANALYSIS OF ALGORITHMS

Complexity of Algorithms. Andreas Klappenecker

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

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Analysis of Algorithms

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

CS:3330 (22c:31) Algorithms

Complexity of Algorithms

Data Structures and Algorithms. Part 2

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

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

Searching, Sorting. Arizona State University 1

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Elementary maths for GMT. Algorithm analysis Part I

Algorithms and Data Structures

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

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

Data Structures and Algorithms Chapter 2

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

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

Data Structures and Algorithms

ANALYSIS OF ALGORITHMS

4.4 Algorithm Design Technique: Randomization

CS171:Introduction to Computer Science II. Algorithm Analysis. Li Xiong

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

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

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

Recall from Last Time: Big-Oh Notation

Data Structures and Algorithms(1)

Another Sorting Algorithm

Outline and Reading. Analysis of Algorithms 1

Internal Sorting by Comparison

Adam Blank Lecture 2 Winter 2017 CSE 332. Data Structures and Parallelism

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

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Algorithmic Analysis. Go go Big O(h)!

Data Structures and Algorithms Key to Homework 1

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

University of Petra Faculty of Information Technology

Data Structures Lecture 8

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

Internal Sorting by Comparison

Classic Data Structures Introduction UNIT I

Quiz 1 Practice Problems

Lecture 5: Running Time Evaluation

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

1. Attempt any three of the following: 15

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

INTRODUCTION. An easy way to express the idea of an algorithm (very much like C/C++, Java, Pascal, Ada, )

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

Assignment 1 (concept): Solutions

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

csci 210: Data Structures Program Analysis


Data Structures and Algorithms. Course slides: String Matching, Algorithms growth evaluation

Measuring the Efficiency of Algorithms

Introduction to Computers & Programming

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

ENGI 4892: Data Structures Assignment 2 SOLUTIONS

Data structure and algorithm in Python

Round 1: Basics of algorithm analysis and data structures

Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

Computational biology course IST 2015/2016

Math 501 Solutions to Homework Assignment 10

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

HOMEWORK FILE SOLUTIONS

Chapter 3. The Efficiency of Algorithms INVITATION TO. Computer Science

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

Data Structures and Algorithms

Asymptotic Analysis of Algorithms

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

COMP Analysis of Algorithms & Data Structures

IE 495 Lecture 3. Septermber 5, 2000

DESIGN AND ANALYSIS OF ALGORITHMS

Algorithm Analysis. CENG 707 Data Structures and Algorithms

L6,7: Time & Space Complexity

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

Transcription:

Algorithm Analysis Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity ITCS 2214:Data Structures 1 Mathematical Fundamentals

Measuring Algorithm Efficiency Empirical comparison (run programs) - has difficulties: Programming effort Implementation differences Exhaustive testing infeasible At best, want to keep 1 implementation Asymptotic Algorithm Analysis. Measure efficiency as a function of input size. ITCS 2214:Data Structures 2 Mathematical Fundamentals

Measuring Algorithm Efficiency Critical resources: Running time. Space (disk, RAM). Programmer s effort. Ease of use (user s effort). Running time depends on Machine load. OS, Compiler. Problem size. Input values. For most algorithms, running time, T (n) depends on some function of size n, of the input. T (n) f(n) ITCS 2214:Data Structures 3 Mathematical Fundamentals

Examples of Growth Rate Example 1:Largest Number in an Array static int largest(int[] array) { // Find largest val int currlargest = 0; // Store largest val for (int i=0; i<array.length; i++) if (array[i] > currlargest) // if largest currlargest = array[i]; // remember it } Analysis return currlargest; Basic Operation: Examine a single integer (Cost = c). Size: length of the array (n). T (n) = cn // Return largest val ITCS 2214:Data Structures 4 Mathematical Fundamentals

Examples of Growth Rate Example 2: An assignment of the first element of an array Example 3: Summing up the elements of an n n matrix. Example 4: Matrix multiplication Example 5: Inner Product of two vectors of length n ITCS 2214:Data Structures 5 Mathematical Fundamentals

Growth Rates ITCS 2214:Data Structures 6 Mathematical Fundamentals

Growth Rates ITCS 2214:Data Structures 7 Mathematical Fundamentals

Best, Worst and Average Cases Not all inputs of a given size take the same time Example:Searching for an element in an array Can be the first element (Best Case) Can be the last element (Worst Case) On the average, search half the elements(average Case) Issues/Applications Best Case: Not very likely, too optimistic Worst Case: Real time applications (air traffic control) Average Case: Assumes uniform distribution (equal likelihood) ITCS 2214:Data Structures 8 Mathematical Fundamentals

Asymptotic Analysis: Big-oh Definition: T n is in the set O(f(n)) if there exist two positive constants c and n 0 such that T (n) c f(n), for all n > n 0. For all data sets (i.e., n > n 0 ), the algorithm always executes in less than c f(n) steps (in best, average or worst case). Big-oh provides an Upper Bound Prefer the tightest upper bound: T n O(n 2 ). = 3n 2 is in O(n 3 ), but prefer ITCS 2214:Data Structures 9 Mathematical Fundamentals

Big-oh Example Example 1: Finding value X in an array T n = c s n/2 (average case). For all values of n > 1, c s.n/2 c s.n Therefore, by definition, T n is in O(n) for n 0 = 1, c = c s. ITCS 2214:Data Structures 10 Mathematical Fundamentals

Big-oh Example Example 2 (Average Case) T n = c 1 n 2 + c 2 n, in average case c 1 n 2 + c 2 n c 1 n 2 + c 2 n 2 (c 1 + c 2 )n 2, n > 1 Therefore, T n T n cn 2, for c = c 1 + c 2, n 0 = 1., and O(n 2 ), by definition Example 3 T n = c We say T n O(1). ITCS 2214:Data Structures 11 Mathematical Fundamentals

Definition: Big-Omega Notation T n is in the set Ω(g(n) if there exist two positive constants c and n 0 such that T n cg(n) for all n > n 0. For all data sets big enough (i.e., n > n 0 ), the algorithm always executes in more than cg(n) steps. Big Omega provides a lower bound. Always want greatest lower bound. Example T n = c 1 n 2 + c 2 n c 1 n 2 + c 2 n c 1 n 2, n > 1 T n cn 2, for c = c 1, n 0 = 1. Therefore, T n is in Ω(n 2 ) by definition. ITCS 2214:Data Structures 12 Mathematical Fundamentals

Big-Theta Notation When Big-Oh and Ω meet, we indicate using Θ (Big-Theta) notation. Definition An algorithm is said to be Θ(h(n)) if it is in O(h(n)) and it is in Ω(h(n)). For polynomial equations on T n, we always have Θ. Simplifying Rules 1. If f(n) is in O(g(n) and g(n) is in O(h(n), then f(n) is in O(h(n) 2. If f(n) is in O(kg(n)) for any constant k > 0, then f(n) is in O(g(n) 3. If f 1 (n) is in O(g 1 (n)) and f 2 (n) is in O(g 2 (n)), then (f 1 + f 2 )(n) is in O(max(g 1 (n), g 2 (n))). 4. If f 1 (n) is in O(g 1 (n)) and f 2 (n) is in O(g 2 (n)) then f 1 (n)f 2 (n) is in O(g 1 (n)g 2 (n)). ITCS 2214:Data Structures 13 Mathematical Fundamentals

Running Time of a Program Example 1: Assignment Example 2: Single Loop Example 3: Double Loop Example 4: Multiple double loops Example 5: Multiple double loops ITCS 2214:Data Structures 14 Mathematical Fundamentals

Binary Search static int bsearch(int K, int[] array, int left, int right) { // Return position in array (if any) with value K int l = left-1; int r = right+1; // l and r are beyond array bounds while (l+1!= r) // Stop when l and r meet { int i = (l+r)/2; // Look at middle of subarray } if (K < array[i]) r = i; // In left half if (K == array[i]) return i; // Found it if (K > array[i]) l = i; // In right half } return UNSUCCESSFUL; // Search value not in array Analysis: How many elements can be examined in the worst case? ITCS 2214:Data Structures 15 Mathematical Fundamentals

Space Complexity Space bounds can also be analyzed with asymptotic complexity analysis Time Complexity is generally relevant for algorithms Space Complexity is generally relevant for data structures. Space/Time Tradeoff: Reduction in computation time by sacrificing space, or vice versa. Encoding or packing information, eg., Boolean flags Table lookup, eg., Factorials, trig functions Disk Based Space/Time Tradeoff: Smaller disk storage requirements, the faster the algorithm. ITCS 2214:Data Structures 16 Mathematical Fundamentals