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

Similar documents
Programming II (CS300)

Elementary maths for GMT. Algorithm analysis Part I

Analysis of Algorithms

Data Structures Lecture 8

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

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

CSCA48 Winter 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

Data Structures and Algorithms

Algorithms and Data Structures

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

Data Structures and Algorithms

Algorithms A Look At Efficiency

Analysis of Algorithm. Chapter 2

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

Introduction to the Analysis of Algorithms. Algorithm

Outline and Reading. Analysis of Algorithms 1

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

ECE6095: CAD Algorithms. Optimization Techniques

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

Lecture 5: Running Time Evaluation

Computer Algorithms. Introduction to Algorithm

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Introduction to Analysis of Algorithms

IT 4043 Data Structures and Algorithms

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

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

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

CS:3330 (22c:31) Algorithms

Chapter 11. Analysis of Algorithms

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

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

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

Choice of C++ as Language

Algorithm Performance. (the Big-O)

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

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

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

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

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

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

CS240 Fall Mike Lam, Professor. Algorithm Analysis

(Refer Slide Time: 1:27)

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

The Running Time of Programs

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

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

Practical Questions CSCA48 Winter 2018 Week 11

csci 210: Data Structures Program Analysis

CS61B Fall 2014 Guerrilla Section 2 This worksheet is quite long. Longer than we ll cover in guerrilla section.

Week 12: Running Time and Performance

Theory and Frontiers of Computer Science. Fall 2013 Carola Wenk

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Data Structures Lecture 3 Order Notation and Recursion

What is an algorithm?

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

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

Algorithmic Analysis. Go go Big O(h)!

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

ANALYSIS OF ALGORITHMS

Algorithm Analysis. Performance Factors

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

ECE 2400 Computer Systems Programming Fall 2018 Topic 8: Complexity Analysis

ASYMPTOTIC COMPLEXITY

Algorithm Analysis. CENG 707 Data Structures and Algorithms

Analysis of Algorithms. CSE Data Structures April 10, 2002

Algorithm Efficiency and Big-O

CSCA08 Winter 2018 Week 2: Variables & Functions. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

Data Structures and Algorithms

Lecture 5 Sorting Arrays

Algorithm Analysis. Big Oh

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Asymptotic Analysis of Algorithms

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Principles of Algorithm Analysis. Biostatistics 615/815

17 February Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough.

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

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

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;

Measuring algorithm efficiency

Recitation 9. Prelim Review

Computer Science Approach to problem solving

Another Sorting Algorithm

Algorithms: Efficiency, Analysis, techniques for solving problems using computer approach or methodology but not programming

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

CSE 373: Data Structures and Algorithms

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

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

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

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

CS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started

Classic Data Structures Introduction UNIT I

UNIT 1 ANALYSIS OF ALGORITHMS

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

Scientific Computing. Algorithm Analysis

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

ASYMPTOTIC COMPLEXITY

Introduction to Computers and Programming. Today

CSCA08 Winter Week 12: Exceptions & Testing. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

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

Transcription:

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

Algorithm Definition: Solving a problem step-by-step in finite amount of time. Analysis: How much of computer resources does an algorithm use? Memory Space We are not interested in this for now CPU time Dependent to input size How to measure running time? Experimental method Theoretical analysis 2

Experimental Method Implement the algorithm using one of the programming languages. Start the timer Execute the algorithm Stop the timer Plot the results to compare. 3

Disadvantage of experimental method It needs implementing the algorithms Results are only indicative of the running time on data included in the experiment The same hardware and software in similar execution atmosphere must be used to compare two algorithms. 4

Theoretical Analysis Requirement Uses a pseudo-code instead of a code to describe the algorithm Allows us to compute the running time of the algorithm independent of the environment and language. Takes into account the worst case scenario for inputs Best case is not the reality. Average case coverage is difficult to determine Describe the execution time as a function of input size. Assumes that every primitive operation takes one unit of time in a certain machine model. expression evaluation, assignment, indexing into an array, function call, returning from a function, comparison, follow an object reference 5

Example In worst case scenario: Function find_max(a_list, n) max a_list[0] for i 1 to n 1 do if a_list[i] > max max = a_list[i] return max a_list is a list containing n comparable data Code # of Operations max a_list[0] 2 for i 1 to n 1 2n + 1 if a_list[i] > max 2(n-1) max = a_list[i] 2(n-1) Increment i 2(n-1) return max 1 8n - 2 If n = 100 then 798 operations is required to execute this code If n = 10 then 78 operations is required 6

Time Complexity Is the time that is taken to run an algorithm when maximum amount of resources is required. i.e. worst case complexity Since complexity varies with different input size, it is shown by a function: T(n) = r, where n: input size, r: running time, T(n): worst case complexity. n ϵ N, r ϵ R + For previous example: T(n) = 8n 2 We don t need to know the exact value of T(n), instead an upper bound (i.e. dominant term) is used as an estimate. 7

Upper-bound (1) Suppose T(n) = 8n 2 We define the order of magnitude as the dominant part of T(n) that increases fastest as the value of n increases. Order of magnitude is n (i. e. O n ) since c, n N: T n c. f n T n = 8n 2, f n = n, c = 8 Big-O notation is used to show the order of magnitude. T has order n T is big- oh of n 8

Upper-bound (2) Suppose T(n) = 2n + 10, then 2n is only dominant if n is large enough So we define asymptotic upper-bound: Let f: N R +, T: N R + be functions then: O(f(n)) = {T(n) c, b, n b: T(n) c. f(n)} T n = 2n + 10, f n = n, c = 4, n 11 9

Example: If T n = 3n 2 + 5n 7 then running time is O(n 2 ) Find c and b such that 3n 2 + 5n 7 cn 2 c = 4, b = 2 So 3n 2 + 5n 7 O(n 2 ) 10

Important Functions Constant : O(1) Logarithmic: O(log n) Linear: O(n) N-Log-N: O(n log n) Quadratic: O(n 2 ) Cubic : O(n 3 ) Exponential: O(2 n ) 4500 4000 3500 3000 2500 2000 1500 1000 500 0 14 12 10 8 6 4 2 0 1 2 3 4 5 6 7 8 9 10 11 12 N O(1) O(log n) O(n) O( n * log n) 1 2 3 4 5 6 7 8 9 10 11 12 N O(1) O(n^2) O( n^3) O( 2^n) 11

Examples: Find the running time of Insertion & removal into/from a list at a certain index where it is implemented by a single linked list (with a pointer to tail and without a pointer to tail) the end of a list, where it is implemented by a single linked list (with and without tail) the end of a list, where it is implemented by a double linked list Appending to a list, implemented by a single linked list Finding an item in the list Insertion & removal into/from a dictionary PQ BST Heap Bubble sort 12