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

Similar documents
Analysis of Algorithms

Elementary maths for GMT. Algorithm analysis Part I

Data Structures Lecture 8

Outline and Reading. Analysis of Algorithms 1

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

Data Structures and Algorithms

Algorithms and Data Structures

ANALYSIS OF ALGORITHMS

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

Data Structures and Algorithms

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Data structure and algorithm in Python

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

(Refer Slide Time: 1:27)

Analysis of Algorithms

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

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

Choice of C++ as Language

Assignment 1 (concept): Solutions

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

Scientific Computing. Algorithm Analysis

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

Partha Sarathi Mandal

Data Structures and Algorithms. Analysis of Algorithms

Asymptotic Analysis of Algorithms

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

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

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

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

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

L6,7: Time & Space Complexity

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

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

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

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

CSc 225 Algorithms and Data Structures I Case Studies

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

UNIT 1 ANALYSIS OF ALGORITHMS

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.

CS:3330 (22c:31) Algorithms

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

Analysis of Algorithm. Chapter 2

Chapter 2: Complexity Analysis

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Introduction to Analysis of Algorithms

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

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

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

csci 210: Data Structures Program Analysis

Introduction to Data Structure

Data Structures and Algorithms CSE 465

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

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

Analysis of Algorithms

Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4) Dr. Yingwu Zhu

ASYMPTOTIC COMPLEXITY

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

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

What is an algorithm?

Introduction to the Analysis of Algorithms. Algorithm

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

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

Introduction to Computer Science

Complexity Analysis of an Algorithm

Lecture 2: Algorithm Analysis

ASYMPTOTIC COMPLEXITY

Programming II (CS300)

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

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

Theory and Algorithms Introduction: insertion sort, merge sort

Lecture 5 Sorting Arrays

Data Structures and Algorithms. Part 2

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

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

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

Algorithm Analysis. CENG 707 Data Structures and Algorithms

Algorithms and Data Structures

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

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

Comparison of x with an entry in the array

Measuring the Efficiency of Algorithms

What is an Algorithm?

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

COMP Analysis of Algorithms & Data Structures

Algorithm Performance. (the Big-O)

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Data Structures and Algorithms Chapter 2

Organisation. Assessment

Introduction to Computer Science

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

IE 495 Lecture 3. Septermber 5, 2000

INTRODUCTION. Objective: - Algorithms - Techniques - Analysis. Algorithms:

CS S-02 Algorithm Analysis 1

Introduction to Algorithms 6.046J/18.401J/SMA5503

Introduction to Algorithms 6.046J/18.401J

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

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

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Lecture Notes for Chapter 2: Getting Started

Transcription:

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes. Input Algorithm Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time.

Algorithm Descriptions Nature languages: Chinese, English, etc. Pseudo-code: codes very close to computer languages, e.g., C programming language. Programs: C programs, C++ programs, Java programs. Goal: Allow a well-trained programmer to be able to implement. Allow an expert to be able to analyze the running time. Analysis of Algorithms 2

An Example of an Algorithm Algorithm sorting(x, n) Input array X of n integers Output array X sorted in a non-decreasing order for i 0 to n 1 do for j i+1 to n do if (X[i]>X[j]) then { s=x[i]; X[i]=X[j]; X[j]=s; } return X Analysis of Algorithms 3

Analysis of Algorithms Estimate the running time Estimate the memory space required. Depends on the input size. Analysis of Algorithms 4

Running Time Running Time ( 3.1) Most algorithms transform input objects into output objects. The running time of an algorithm typically grows with the input size. Average case time is often difficult to determine. We focus on the worst case running time. Easier to analyze Crucial to applications such as games, finance and robotics 120 100 80 60 40 20 0 best case average case worst case 1000 2000 3000 4000 Input Size Analysis of Algorithms 5

Time (ms) Experimental Studies Write a program implementing the algorithm Run the program with inputs of varying size and composition Use a method like System.currentTimeMillis() to get an accurate measure of the actual running time Plot the results 9000 8000 7000 6000 5000 4000 3000 2000 1000 0 0 50 100 Input Size Analysis of Algorithms 6

Limitations of Experiments It is necessary to implement the algorithm, which may be difficult Results may not be indicative of the running time on other inputs not included in the experiment. In order to compare two algorithms, the same hardware and software environments must be used Analysis of Algorithms 7

Theoretical Analysis Uses a high-level description of the algorithm instead of an implementation Characterizes running time as a function of the input size, n. Takes into account all possible inputs Allows us to evaluate the speed of an algorithm independent of the hardware/software environment Analysis of Algorithms 8

Pseudocode ( 3.2) High-level description of an algorithm More structured than English prose Less detailed than a program Preferred notation for describing algorithms Hides program design issues Example: find max element of an array Algorithm arraymax(a, n) Input array A of n integers Output maximum element of A currentmax A[0] for i 1 to n 1 do if A[i] currentmax then currentmax A[i] return currentmax Analysis of Algorithms 9

Pseudocode Details Control flow if then [else ] while do repeat until for do Indentation replaces braces Method declaration Algorithm method (arg [, arg ]) Input Output Expressions Assignment (like in Java) Equality testing (like in Java) n 2 Superscripts and other mathematical formatting allowed Analysis of Algorithms 10

Primitive Operations Basic computations performed by an algorithm Identifiable in pseudocode Largely independent from the programming language Exact definition not important (we will see why later) Assumed to take a constant amount of time in the RAM model Examples: Evaluating an expression Assigning a value to a variable Indexing into an array Calling a method Returning from a method Analysis of Algorithms 11

Counting Primitive Operations ( 3.4) 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 arraymax(a, n) # operations currentmax A[0] 2 for (i =1; i<n; i++) 2n (i=1 once, i<n n times, i++ (n-1) times) if A[i] currentmax then 2(n 1) currentmax A[i] 2(n 1) return currentmax 1 Total 6n 1 Analysis of Algorithms 12

Estimating Running Time Algorithm arraymax executes 6n 1 primitive operations in the worst case. Define: a = Time taken by the fastest primitive operation b = Time taken by the slowest primitive operation Let T(n) be worst-case time of arraymax. Then a (8n 2) T(n) b(8n 2) Hence, the running time T(n) is bounded by two linear functions Analysis of Algorithms 13

Growth Rate of Running Time Changing the hardware/ software environment Affects T(n) by a constant factor, but Does not alter the growth rate of T(n) The linear growth rate of the running time T(n) is an intrinsic property of algorithm arraymax Analysis of Algorithms 14

n logn n nlogn n 2 n 3 2 n 4 2 4 8 16 64 16 8 3 8 24 64 512 256 16 4 16 64 256 4,096 65,536 32 5 32 160 1,024 32,768 4,294,967,296 64 6 64 384 4,094 262,144 1.84 * 10 19 128 7 128 896 16,384 2,097,152 3.40 * 10 38 256 8 256 2,048 65,536 16,777,216 1.15 * 10 77 512 9 512 4,608 262,144 134,217,728 1.34 * 10 154 1024 10 1,024 10,240 1,048,576 1,073,741,824 1.79 * 10 308 The Growth Rate of the Six Popular functions Analysis of Algorithms 15

Big-Oh Notation To simplify the running time estimation, for a function f(n), we ignore the constants and lower order terms. Example: 10n 3 +4n 2-4n+5 is O(n 3 ). Analysis of Algorithms 16

Big-Oh Notation (Formal Definition) Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constants c and n 0 such that 10,000 1,000 100 3n 2n+10 n f(n) cg(n) for n n 0 Example: 2n + 10 is O(n) 2n + 10 cn (c 2) n 10 n 10/(c 2) Pick c 3 and n 0 10 10 1 1 10 100 1,000 n Analysis of Algorithms 17

Big-Oh Example Example: the function n 2 is not O(n) n 2 cn n c The above inequality cannot be satisfied since c must be a constant n 2 is O(n 2 ). 1,000,000 100,000 10,000 1,000 100 10 n^2 100n 10n n 1 1 10 100 1,000 n Analysis of Algorithms 18

More Big-Oh Examples 7n-2 7n-2 is O(n) need c > 0 and n 0 1 such that 7n-2 c n for n n 0 this is true for c = 7 and n 0 = 1 3n 3 + 20n 2 + 5 3n 3 + 20n 2 + 5 is O(n 3 ) need c > 0 and n 0 1 such that 3n 3 + 20n 2 + 5 c n 3 for n n 0 this is true for c = 4 and n 0 = 21 3 log n + 5 3 log n + 5 is O(log n) need c > 0 and n 0 1 such that 3 log n + 5 c log n for n n 0 this is true for c = 8 and n 0 = 2 Analysis of Algorithms 19

Big-Oh and Growth Rate The big-oh notation gives an upper bound on the growth rate of a function The statement f(n) is O(g(n)) means that the growth rate of f(n) is no more than the growth rate of g(n) We can use the big-oh notation to rank functions according to their growth rate Analysis of Algorithms 20

Big-Oh Rules If f(n) is a polynomial of degree d, then f(n) is O(n d ), i.e., 1. Drop lower-order terms 2. Drop constant factors Use the smallest possible class of functions Say 2n is O(n) instead of 2n is O(n 2 ) Use the simplest expression of the class Say 3n + 5 is O(n) instead of 3n + 5 is O(3n) Analysis of Algorithms 21

Growth Rate of Running Time Consider a program with time complexity O(n 2 ). For the input of size n, it takes 5 seconds. If the input size is doubled (2n), then it takes 20 seconds. Consider a program with time complexity O(n). For the input of size n, it takes 5 seconds. If the input size is doubled (2n), then it takes 10 seconds. Consider a program with time complexity O(n 3 ). For the input of size n, it takes 5 seconds. If the input size is doubled (2n), then it takes 40 seconds. Analysis of Algorithms 22

Asymptotic Algorithm Analysis The asymptotic analysis of an algorithm determines the running time in big-oh notation To perform the asymptotic analysis We find the worst-case number of primitive operations executed as a function of the input size We express this function with big-oh notation Example: We determine that algorithm arraymax executes at most 6n 1 primitive operations We say that algorithm arraymax runs in O(n) time Since constant factors and lower-order terms are eventually dropped anyhow, we can disregard them when counting primitive operations Analysis of Algorithms 23

Computing Prefix Averages We further illustrate asymptotic analysis with two algorithms for prefix averages The i-th prefix average of an array X is average of the first (i + 1) elements of X: A[i] (X[0] + X[1] + + X[i])/(i+1) Computing the array A of prefix averages of another array X has applications to financial analysis 35 30 25 20 15 10 5 0 X A 1 2 3 4 5 6 7 Analysis of Algorithms 24

Prefix Averages (Quadratic) The following algorithm computes prefix averages in quadratic time by applying the definition Algorithm prefixaverages1(x, n) Input array X of n integers Output array A of prefix averages of X #operations A new array of n integers n for i 0 to n 1 do n { s X[0] n for j 1 to i do 1 + 2 + + (n 1) s s + X[j] 1 + 2 + + (n 1) A[i] s / (i + 1) } n return A 1 Analysis of Algorithms 25

Arithmetic Progression The running time of prefixaverages1 is O(1 + 2 + + n) The sum of the first n integers is n(n + 1) / 2 There is a simple visual proof of this fact Thus, algorithm prefixaverages1 runs in O(n 2 ) time Analysis of Algorithms 26

Prefix Averages (Linear) The following algorithm computes prefix averages in linear time by keeping a running sum Algorithm prefixaverages2(x, n) Input array X of n integers Output array A of prefix averages of X A new array of n integers n s 0 1 for i 0 to n 1 do n {s s + X[i] n A[i] s / (i + 1) } n return A 1 Algorithm prefixaverages2 runs in O(n) time #operations Analysis of Algorithms 27

Exercise: Give a big-oh characterization Algorithm Ex1(A, n) Input an array X of n integers Output the sum of the elements in A s A[0] for i 0 to n 1 do s s + A[i] return s Analysis of Algorithms 28

Exercise: Give a big-oh characterization Algorithm Ex2(A, n) Input an array X of n integers Output the sum of the elements at even cells in A s A[0] for i 2 to n 1 by increments of 2 do s s + A[i] return s Analysis of Algorithms 29

Exercise: Give a big-oh characterization Algorithm Ex1(A, n) Input an array X of n integers Output the sum of the prefix sums A s 0 for i 0 to n 1 do s s + A[0] for j 1 to i do s s + A[j] return s Analysis of Algorithms 30

Remarks: In the first tutorial, ask the students to try programs with running time O(n), O(n log n), O(n 2 ), O(n 2 log n), O(2 n ) with various inputs. They will get intuitive ideas about those functions. for (i=1; i<=n; i++) for (j=1; j<=n; j++) { x=x+1; delay(1 second); } Analysis of Algorithms 31