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

Similar documents
PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

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

Elementary maths for GMT. Algorithm analysis Part I

Outline and Reading. Analysis of Algorithms 1

Analysis of Algorithms

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Data Structures Lecture 8

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Choice of C++ as Language

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

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

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

Chapter 2: Complexity Analysis

CS240 Fall Mike Lam, Professor. Algorithm Analysis

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

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

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

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

Algorithms and Data Structures

Data Structures and Algorithms

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

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

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Data Structures and Algorithms CSE 465

CS 261 Data Structures. Big-Oh Analysis: A Review

Data Structures and Algorithms

Lecture 2: Algorithm Analysis

Asymptotic Analysis of Algorithms

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Algorithmic Analysis. Go go Big O(h)!

ANALYSIS OF ALGORITHMS

Introduction to the Analysis of Algorithms. Algorithm

Sorting. Bubble Sort. Selection Sort

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

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

csci 210: Data Structures Program Analysis

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

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

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

What is an Algorithm?

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

CS 310: Order Notation (aka Big-O and friends)

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

Another Sorting Algorithm

Recursion & Performance. Recursion. Recursion. Recursion. Where Recursion Shines. Breaking a Problem Down

Algorithm Analysis. October 12, CMPE 250 Algorithm Analysis October 12, / 66

Computer Algorithms. Introduction to Algorithm

Sum this up for me. Let s write a method to calculate the sum from 1 to some n. Gauss also has a way of solving this. Which one is more efficient?

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

9/3/12. Outline. Part 5. Computational Complexity (1) Software cost factors. Software cost factors (cont d) Algorithm and Computational Complexity

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

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

Algorithm Analysis. CENG 707 Data Structures and Algorithms

CS S-02 Algorithm Analysis 1

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Classic Data Structures Introduction UNIT I

CS/COE 1501

CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction

Lecture 5: Running Time Evaluation

Algorithms and Data Structures

S.E. (Computer) (First Semester) EXAMINATION, 2011 DATA STRUCTURES AND ALGORITHM (2008 PATTERN) Time : Three Hours Maximum Marks : 100

Analysis of Algorithms. 5-Dec-16

Scientific Computing. Algorithm Analysis

CSI33 Data Structures

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

Is This Algorithm Fast? Algorithm Analysis. Attendance Question 1. Grading Algorithms

Data Structures Lecture 3 Order Notation and Recursion

Analysis of algorithms

Data Structures and Algorithms. Part 2

Introduction to Programming I

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

Analysis of Algorithm. Chapter 2

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Introduction to Analysis of Algorithms

Recall from Last Time: Big-Oh Notation

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

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Programming II (CS300)

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

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

UNIT 1 ANALYSIS OF ALGORITHMS

Data structure and algorithm in Python

Topic Number 2 Efficiency Complexity Algorithm Analysis

4.4 Algorithm Design Technique: Randomization

CSCA48 Winter 2018 Week 10:Algorithm Analysis. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

(Refer Slide Time: 1:27)

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux

Introduction to Computers & Programming

Theory and Algorithms Introduction: insertion sort, merge sort

Analysis of Algorithms Part I: Analyzing a pseudo-code

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Lesson 12: Recursion, Complexity, Searching and Sorting. Modifications By Mr. Dave Clausen Updated for Java 1_5

Lecture 5 Sorting Arrays

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

Algorithms and Complexity

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 7 Complexity and Orders of Growth TODAY PROBLEM SOLVING: ALGORITHMS COMPARISON OF ALGORITHMS 7/10/2012

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

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

Advanced Algorithms and Data Structures

Transcription:

Algorithm (03F) Lecture3: Algorithm Analysis A step by step procedure to solve a problem Start from an initial state and input Proceed through a finite number of successive states Stop when reaching a final state and producing output Lamp doesn t work. Bohyung Han CSE, POSTECH bhhan@postech.ac.kr Lamp plugged in? Bulb burned out? Plug in lamp. Replace bulb. Repair lamp. Algorithm Performance Algorithm performance is measured by the amount of computer memory and running time required to run a algorithm. Performance measurement Empirical analysis Compute memory space in use and running time Results are not general valid only for tested inputs. Same machine environment must be used to compare two algorithms. Theoretical analysis Compute asymptotic bound of space and running time Sometimes, it is difficult to measure average cases; worst case analysis are often used. Machine independent Measurement criteria Actual memory space in use Running time of algorithms Input Empirical Analysis Algorithm Algorithm Output Algorithm and took 0.3 and 0.5 milliseconds for Input, respectively. Algorithm may be faster than Algorithm. Algorithm and took.0 and 0.8 milliseconds for Input, respectively. How about now? 3

Theoretical Analysis Measurement criteria Space: amount of memory in bytes that algorithm occupies Time Running time Typically measured by the number of primitive operations Algorithm analysis Use a pseudocode of the algorithm Need to take all possible inputs into account Evaluate the algorithm speed independent of the machine environment Pseudocode High level description of an algorithm Independent of any programming language More structured than English proses Less detailed than program codes Hiding program design issues Easy to understand Algorithm arraymax(a, n) Output maximum element of A currentmax < A[0] for i < to n do if A[i] = currentmax then currentmax < A[i] return currentmax 5 6 Space Complexity Space Complexity Amount of necessary memory for an algorithm The space complexity may define an upper bound on the data that the algorithm uses. Why do we care about space complexity? We may not have sufficient memory space in our computer. When we solve a large scale problem, memory space is often a critical bottleneck. Data space depends on Computer architecture and compiler Implementation double array[00]; int array[00]; // The size of array is twice bigger than that of array. Algorithm: e.g., dense matrix vs. sparse matrix Dense matrix: 8 0 integers Sparse matrix: (index, value) (,), (5,8), (9,) 6 integers 7 8

Time Complexity : Insertion Sort Amount of time required to run an algorithm Why do we care about time complexity? Some applications require real time responses. If there are many solutions for a problem, we typically prefer the quickest one. How do we measure? Count a particular operation (operation counts) e.g., comparisons in sorting Count the number of steps (step counts) Asymptotic complexity How many comparisons are made? Algorithm insertionsort(a, n) Output the sorted array A for i < to n do t< A[i] for j < i to 0 do if t < A[j] then A[j+] < A[j] else break A[j+] < t return A Comparison happen here. n=3 3 j=0 i= 3 i= 3 9 0 Worst Case Analysis Primitive Operations Why using the worst case? Average case is sometimes difficult to analyze. The time complexity of the worst case is often very important. An example: insertion sort. All elements are reversely sorted. Total number of comparisons: Algorithm insertionsort(a, n) Output the sorted array A for i < to n do t< A[i] for j < i to 0 do if t < A[j] then A[j+] < A[j] else break A[j+] < t return A Basic computations performed by an algorithm Constant time assumed for execution Identifiable in pseudocode Largely independent from the programming language s Assignments: a < b Comparisons: a < b Arithmetic operations: a+b Dereference: A[i] Returning from a method Algorithm insertionsort(a, n) Output the sorted array A for i < to n do t< A[i] for j < i to 0 do if t < A[j] then A[j+] < A[j] else break A[j+] < t return A

Primitive Operation Counts By inspecting the pseudocode, we can determine the maximum number of primitive operations executed by an algorithm, as a function of the input size. Algorithm insertionsort(a, n) Output the sorted array A # operations total # operations Running time, Running Time Analysis Computational complexity of an algorithm in a theoretical model The total number of operations of insertion sort is. 5 0.5. The theoretical running time may be bounded by other functions:. 5 0.5. 5 0.5, for i < to n do t< A[i] for j < i to 0 do if t < A[j] then A[j+] < A[j] else break A[j+] < t return A Total (n ) (n ) 0.5(n )n (n )n (n ). 5 0.5 where and are times taken by the fastest and slowest primitive operations. Running time analysis Growth function characterizes running time with respect to number of inputs. We are interested in the growth rate of the running time. 3 Growth Functions Growth Functions Constant Growth is independent of the input size, e.g., accessing array element at known location, assignment Linear Growth is directly proportional to e.g., finding a particular array element (linear search) Logarithmic Growth increases slowly compared to log e.g., finding particular array element (binary search) Quadratic e.g., typical in nested loops (bubble sort) Cubic e.g., matrix multiplication Exponential Growth is extremely rapid and possibly impractical e.g., Towers of Hanoi ( ), Fibonacci number ( ) 5 6

Growth Functions A Table of Growth Functions Other polynomial e.g., typical in more nested loops Others log e.g., sorting using divide and conquer approach 0 3 8 0 8 6 6 8 6 5 6 56 5 6 3 6 60 56 0 096 3768 65536 996796 Overly complex algorithm may be impractical in some system 7 8 A Graph of Growth Functions Asymptotic Complexity Asymptotic Definition: approaching a given value as an expression containing a variable tends to infinity Asymptotic complexity Describe the growth rate of the time or space complexity with respect to input size Is not directly related to the exact running time Three notations Big O (O) notation provides an upper bound for the growth rate of the given function. Big Omega (Ω) notation provides a lower bound. Big Theta () notation is used when an algorithm is bounded both above and below by the same kind of function. 9 0

Big O More Big O s,, Note: and may not be unique. 0 00 80 3*n^ 5 n^ 7 7 for 7, 3 0 5 for, Rule of thumb Ignore less dominating term. Ignore constant 60 0 3 log 5 8 log for 8, 3 5. 3 5 & 3 0 0 0 3 5 6 Big Omega Big Theta,, Note: and may not be unique. 0.5 Ω 0.5 0. 0. & 0 60 50 0 30 0 0 0.5n^ n^,,, c for Note:, and may not be unique. The total number of primitive operations of an algorithm is. The upper bound is O The lower bound is Ω Therefore, Θ 0 0 3 5 6 3

Caveat The relative behavior of two function is compared only asymptotically, for large. The asymptotic complexity may make no sense for small. : two algorithms with the same complexity, Algorithm A executes 3 primitive operations in a loop. Algorithm B executes 0 primitive operations in a loop. Then the algorithm A is faster than the algorithm B although their complexities are same. : two algorithms with and With a small number of inputs, algorithm with quadratic complexity may be faster than algorithm with linear complexity. Think about the definition of Big O notation. int (int[][] a) int sum = 0; for (int i=0; i<a.length; i++) for (int j=0; j<i; j++) sum = sum + a[i][j]; 0 : number of rows 5 6 int (int[] a) int sum = 0; for (int i=a.length; i>0; i/=) sum = sum + a[i]; log log : number of rows int 3(int[] a) int sum = 0; for (int i=0; i<a.length; i++) for (int j=a.length; j>0; j/=) sum = sum + a[i] + a[j]; log log log log : number of elements 7 8

int (int[][] a) int sum = 0; for (int i=0; i<a.length; i++) for (int j=0; j<00; j++) sum = sum + a[i][j]; 00 00 00 00 int 5(int n) int sum = 0; int sum = 0; for (int i=0; i<n; i++) sum += i; for (int j=0*n; j<00*n; j++) sum += sum; 90 90 90 90 9 30 int[][] multiplysquarematrices(int[][] a, int[][] b) int[][] c = new int[a.length][a.length]; int n = a.length; for (int i=0; i<n; i++) for (int j=0; j<n; j++) for (int k=0; k<n; k++) c[i][j] = c[i][j] + a[i][k] * b[k][j]; return c; Trade off Space complexity and time complexity may not be independent. There is a trade off between the two complexities. Algorithm A may be faster than Algorithm B, but consume memory space more than Algorithm B. Dense matrix A: Sparse matrix A: 8 (index, value) (,), (5,8), (9,) Space complexity Time complexity of A[i] Dense matrix *0 bytes O() Sparse matrix *6 bytes O(n) 3 3

Summary Complexity analysis mostly involves examining loops. Attempt to characterize growth of functions. This will be important when we examine searching and sorting Big O notation helps us simplify complexity definitions worst case It ignores constant and lower order terms. Big Omega (Ω) examines lower bounds. Big Theta (Θ) examines specific case. Computer scientists often use proofs to defend complexity claims. 33 3