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

Similar documents
Analysis of Algorithms

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.

Outline and Reading. Analysis of Algorithms 1

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Choice of C++ as Language

Elementary maths for GMT. Algorithm analysis Part I

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

Data Structures Lecture 8

Assignment 1 (concept): Solutions

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

Asymptotic Analysis of Algorithms

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

Algorithm Analysis and Design

Algorithmic Analysis. Go go Big O(h)!

Data structure and algorithm in Python

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

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

csci 210: Data Structures Program Analysis

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

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

Chapter 2: Complexity Analysis

UNIT 1 ANALYSIS OF ALGORITHMS

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

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

Programming II (CS300)

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

Introduction to Computers & Programming

Analysis of Algorithm. Chapter 2

Lecture 5: Running Time Evaluation

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Algorithm Analysis. CENG 707 Data Structures and Algorithms

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

Scientific Computing. Algorithm Analysis

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

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Introduction to Computer Science

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

Data Structures and Algorithms

Computer Algorithms. Introduction to Algorithm

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

Data Structures and Algorithms

What is an algorithm?

(Refer Slide Time: 1:27)

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

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

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

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Analysis of Algorithms 1 / 18

We will use the following code as an example throughout the next two topics:

Algorithmic Complexity

CS S-02 Algorithm Analysis 1

Complexity Analysis of an Algorithm

Algorithms and Data Structures

ANALYSIS OF ALGORITHMS

Algorithm Efficiency, Big O Notation, and Javadoc

What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation

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

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

Algorithm Analysis. Big Oh

ASYMPTOTIC COMPLEXITY

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

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

Introduction to Computer Science

Lecture 2: Algorithm Analysis

Chapter 2: Algorithm Complexity Analysis. Chaochun Wei Spring 2018

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

Advanced R Programming - Lecture 6

ECE6095: CAD Algorithms. Optimization Techniques

Data Structures and Algorithms

Measuring the Efficiency of Algorithms

Remember, to manage anything, we need to measure it. So, how do we compare two implementations? ArrayBag vs. LinkedBag Which is more efficient?

ALGORITHM ANALYSIS. cs2420 Introduction to Algorithms and Data Structures Spring 2015

Introduction to Data Structure

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?

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

CSCI 136 Data Structures & Advanced Programming. Lecture 7 Spring 2018 Bill and Jon

Closed Book Examination. One and a half hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Wednesday 20 th January 2010

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

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

ASYMPTOTIC COMPLEXITY

Algorithm Analysis. Review of Terminology Some Consequences of Growth Rates. The book s big example: Detecting anagrams. There be math here!

Analysis of Algorithms. CSE Data Structures April 10, 2002

Principles of Algorithm Analysis. Biostatistics 615/815

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Algorithm Analysis. Performance Factors

Analysis of Algorithms

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

Week 12: Running Time and Performance

Introduction to the Analysis of Algorithms. Algorithm

The Importance of Runtime Cost! Measuring Runtime Cost! How to Measure Runtime Cost?!

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

Computer Science Approach to problem solving

1.4 ANALYSIS OF ALGORITHMS

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

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

CS 112 Introduction to Programming

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

Introduction to Analysis of Algorithms

Another Sorting Algorithm

Transcription:

CS171:Introduction to Computer Science II Algorithm Analysis Li Xiong

Announcement/Reminders Hw3 due Friday Quiz 2 on October 17, Wednesday (after Spring break, based on class poll) Linked List, Algorithm Analysis

Algorithm Analysis Simple sorting Road Map

Algorithm Analysis An algorithm is a method for solving a problem expressed as a sequence of steps that is suitable for execution by a computer (machine) E.g. Search in an ordered array E.g. N-Queens problem We are interested in designing good algorithms Linear search vs. binary search Brute-force search vs. backtracking Good algorithms Running time Space usage (amount of memory required)

Running time of an algorithm Running time typically increases with the input size (problem size) Also affected by hardware and software environment We would like to focus on the relationship between the running time and the input size Input Algorithm Output

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

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

Algorithm Analysis -insight Total running time of a program is determined by two primary factors: Cost of executing each statement (property of computer, Java compiler, OS) Frequency of execution of each statement (property of program and input)

Algorithm analysis: Algorithm Analysis Determine frequency of primitive operations Characterizes it as a function of the input size A primitive operation: corresponds to a low-level (basic) computation with a constant execution time E.g. Evaluating an expression; assigning a value to a variable; indexing into an array Benefit: Takes into account all possible inputs independent of the hardware/software environment

Average-case vs. worst-case An algorithm may run faster on some inputs than it does on others (with the same input size) Average case: taking the average over all possible inputs of the same size Depends on input distribution Best case Worst case Easier analysis Typically leads to better algorithms

Misleading Average A statistician who put her head in the oven and her feet in the refrigerator. She said, On average, I feel just fine.

Misleading Average Statistician drowning in a pond with an average depth of 3ft

Loop Analysis Programs typically use loops to enumerate through input data items Count number of operations or steps in loops Each statement within the loop is counted as a step

Example1 double sum = 0.0; for (int i = 0; i < n; i ++) { sum += array[i]; } How many steps? Only count the loop statements(update to the loop variable i is ignored)

Example1 double sum = 0.0; for (int i = 0; i < n; i ++) { sum += array[i]; } How many steps? Only count the loop statements(update to the loop variable i is ignored) n

Example2 double sum = 0.0; for (int i = 0; i < n; i += 2) { sum += array[i]; } How many steps?

Example2 double sum = 0.0; for (int i = 0; i < n; i += 2) { sum += array[i]; } How many steps? Loops will be executed n/2 times: n/2

Example3 Multiple Loops for (int i = 0; i < n; i ++) { for (int j = 0; j < n; j ++) { int x = i*j; sum += x; } } How many steps?

Example3 Multiple Loops for (int i = 0; i < n; i ++) { for (int j = 0; j < n; j ++) { int x = i*j; sum += x; } } How many steps? Nested loops, each loop will be executed n times: n 2

Increase of Cost w.r.t. n Example 1: n Example 2: n/2 Example 3: 2n 2 What if n is 100? What if n is 3 times larger? Example 1 and 2 are linear to the input size Example 3 is quadratic to the input size

Mathematical notations for algorithm analysis The cost function can be complicated and lengthy mathematical expressions E.g. 3n 3 + 20n 2 + 5 We careabout how thecost increasesw.r.t. theproblem size,rather than the absolute cost Use simplified mathematical notions Tilde notation Big O notation

Tilde Notation Tilde notation: ignore insignificant terms Definition: we write f(n) ~ g(n) if f(n)/g(n) approaches 1 as n grows 2n+10 ~ 2n 3n 3 + 20n 2 + 5 ~ 3n 3

Big-Oh Notation Given functions f(n) and 10,000 g(n), we say that f(n) is O(g(n))if there are 1,000 positive constants cand n 0 such that 100 0 3n 2n+10 n f(n) cg(n) for n n 0 Example: 2n + 10is O(n) pick c = 3 and n 0 = 10 10 1 1 10 100 1,000 n

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

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

Important Functions in Big-Oh Analysis Constant: 1 Logarithmic: log n Linear: n N-Log-N: n log n Quadratic: n 2 Cubic: n 3 Polynomial: n d Exponential: 2 n Factorial: n!

Growth Rate Interms oftheorder: exponentials > polynomials > logarithms> constant. Increasing order n O(3 ) n O(2 ) 3 O(n ) 2 O(n ) O(nlogn) O(n) O(logn) O(1) Exponential Polynomial Log-linear Linear Log Constant

Big-Oh Analysis Write down cost function f(n) 1. Look for highest-order term (tilde notation) 2. Drop constant factors Examples 3n 3 + 20n 2 + 5 n log n + 10

Useful Approximations Harmonic sum 1 + 1/2 + 1/3 + + 1/N ~ lnn Triangular sum 1 + 2 + 3 + + N = N(N+1)/2 ~ N 2 /2 Geometric sum 1 + 2 + 4 + + N = 2N -1 ~ 2N when N = 2 n Stirling sapproximation lgn! = lg1 + lg2 + lg3 + + lgn~ NlgN