Similar documents
CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

Complexity of Algorithms

Complexity of Algorithms. Andreas Klappenecker

Recall from Last Time: Big-Oh Notation

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Analysis of Algorithms. CSE Data Structures April 10, 2002

Computer Algorithms. Introduction to Algorithm

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

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

Chapter 2: Complexity Analysis

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

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

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

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

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

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

CSI33 Data Structures

Sorting Pearson Education, Inc. All rights reserved.

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

CS:3330 (22c:31) Algorithms

The Running Time of Programs

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

Complexity of Algorithms

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

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

Algorithm Analysis. CENG 707 Data Structures and Algorithms

Measuring Efficiency

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

Measuring algorithm efficiency

You should know the first sum above. The rest will be given if you ever need them. However, you should remember that,, and.

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Choice of C++ as Language

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. Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity

Algorithms A Look At Efficiency

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

COT 5407: Introduction to Algorithms. Giri Narasimhan. ECS 254A; Phone: x3748

Sorting race.

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

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

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

CSE 332: Data Structures & Parallelism Lecture 5: Algorithm Analysis II. Ruth Anderson Winter 2018

Arrays.

UNIT 5B Binary Search

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

Analysis of Algorithm. Chapter 2

Data Structures and Algorithms. Part 2

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

Introduction to Analysis of Algorithms

CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

Chapter Contents. An Introduction to Sorting. Selection Sort. Selection Sort. Selection Sort. Iterative Selection Sort. Chapter 9

Algorithm Analysis. Performance Factors

Introduction to Computers and Programming. Today

Data Structures and Algorithms Chapter 2

UNIT 1 ANALYSIS OF ALGORITHMS

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

Elementary maths for GMT. Algorithm analysis Part II

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

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

CSC 273 Data Structures

The Knapsack Problem an Introduction to Dynamic Programming. Slides based on Kevin Wayne / Pearson-Addison Wesley

csci 210: Data Structures Program Analysis

Comparison of x with an entry in the array

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

Homework 1. Problem 1. The following code fragment processes an array and produces two values in registers $v0 and $v1:

Basic Data Structures (Version 7) Name:

CS 173, Running Time Analysis, Counting, and Dynamic Programming. Tandy Warnow

Computational Complexity: Measuring the Efficiency of Algorithms

4.4 Algorithm Design Technique: Randomization

Algorithmic Analysis. Go go Big O(h)!

HOMEWORK FILE SOLUTIONS

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

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

CS240 Fall Mike Lam, Professor. Algorithm Analysis

CSE 373: Data Structures and Algorithms

The power of logarithmic computations. Recursive x y. Calculate x y. Can we reduce the number of multiplications?

Algorithm Analysis and Design

Announcement. Submit assignment 3 on CourSys Do not hand in hard copy Due Friday, 15:20:00. Caution: Assignment 4 will be due next Wednesday

Spring 2002: Fundamental Structures of Computer Science

Agenda. We ve done Growth of functions Asymptotic Notations (O, o,,!, ) Now Binary search as an example Recurrence relations, solving them

Sorting Algorithms. + Analysis of the Sorting Algorithms

CS S-02 Algorithm Analysis 1

CS 206 Introduction to Computer Science II

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

ENGI 4892: Data Structures Assignment 2 SOLUTIONS

EECS 477: Introduction to algorithms. Lecture 6

Department of Computer Science Admission Test for PhD Program. Part I Time : 30 min Max Marks: 15

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

Analysis of Algorithms - Introduction -

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

Sorting & Growth of Functions

Sorting. Bubble Sort. Selection Sort

Last time s big ideas

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Outline. Simple Recursive Examples Analyzing Recursion Sorting The Tower of Hanoi Divide-and-conquer Approach In-Class Work. 1 Chapter 6: Recursion

CITS3001. Algorithms, Agents and Artificial Intelligence. Semester 2, 2016

Transcription:

https://cs.uiowa.edu/resources/events

Searching an array Let R(N) be the running time to search for an integer in an unsorted array. Can we find an f(n) such that R N O(f N )?

Searching an array Let R(N) be the worst-case running time to search for an integer in an unsorted array. Algorithm: look at elements one-by-one until we find the integer or get to the end Find an f(n) such that R N O(f N ) f N = N or, in other words R N O(N)

Peer instruction for (int i=0; i<array.length; i += 2) { if (array[i] > 10) break; else array[i] = i; Let R(N) be the worst-case running time of this code, where N is the length of the array. Find an f(n) such that R(N) O(f n ). a) 1 b) 2 c) 10 d) N e) N 2 https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

Searching an array Let R(N) be the worst-case running time to search for an integer in an unsorted array. Find an f(n) such that R N O(f N ) f N = N or, in other words R N O(N) What if the array is sorted? Can we do better than O(N)?

Binary search for key 22

Binary search for key 22

Binary search for key 22

Binary search for key 22

Calculating R(N) let c=time to compare two integers R(N) = c + R(N/2) how to solve the recurrence?

Calculating R(N) let c=time to compare two integers c c c each stage does c work...but how many stages are there? c

how many times can you halve* N until you get to 1? N 2 34567 = 1 2 34567 = N times = log A N times needs to be integer so times = log A N *by halve we really mean N/2

Calculating R(N) let c=time to compare two integers c c c there are log A N stages c R(N) =c log A N O(log A N)

Peer instruction int foo(int[] arr, int N) { if (count==0) return 0; for (int i=0; i<n; i++) { arr[i]++; return foo(arr, N/2); Let R(N) be the worst-case running time of a call to foo. Find an f(n) such that R(N) O(f n ). a) log M 2 b) log A N c) N O P d) N e) N R P f) N log A N g) N A

Nested loops Algorithm to find if an integer appears at least twice in an array. int N = a.length; iterations are shaded 3 2 i 1 0 when i=0, the inner loop runs N-1 times when i=1, the inner loop runs N-2 times... when i=n-2 the inner loop runs 1 time when i=n-1 the inner loop runs 0 times 3 2 1 0 k for (int i=0; i < N; i++) { for (int k=i+1; k< N; k++) { if (a[i]==a[k]) return true; return false; N 1 + N 2 + + 2 + 1 = N(N 1)/2 1 2 NA 1 2 N R N O N A

Peer instruction for (int i=0; i<array.length; i++) { for (int j=1; j<array.length; j++) { for (int k=0; k<array.length; k+=j) { array[i] = array[j]+array[k]; Let R(N) be the worst-case running time of this code, where N is the length of the array. Find an f(n) such that R(N) O(f n ). a) N 2 b) N 3 c) N 4 d) N ln N e) N 2 ln N f) N 3 ln N https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

Fib n = Fib n-1 + Fib n-2 We ll look at two ways to compute the Nth Fibonacci number and analyze their running times https://en.wikipedia.org/wiki/fibonacci_number

Binary recursion to compute Fibonacci numbers public int fibbad(int n) { if (n==0) return 0; else if (n==1) return 1; else return fibbad(n-1) + fibbad(n-2); R(N) = time to compute fibbad(n-1) + time to compute fibbad(n-2) + time to do everything else R(N) = R(N-1) + R(N-2) + c R(N) will be exponential in N

Linear recursion to compute Fibonacci numbers public static long[] fibgood(int n) { // computes fib(n) and fib(n-1) together if (n <= 1) { long[] ans = {n,0; return ans; else { long[] tmp = fibgood(n-1); long[] ans = {tmp[0]+tmp[1], tmp[0]; return ans; R(N) = time to compute fibgood(n-1) + time to do everything else R(N) = R(N-1) + c R N O(N)

Peer instruction int weird(int[] arr, int N) { if (N== 0) return 0; if (N== 1) return arr[0]; return weird(arr, N/3) + weird(arr, N-1); Let R(N) be the worst-case running time of a call to weird. Find an f(n) such that R(N) O(f n ). a) log V N b) log V N + log W N c) N O R d) N log V N e) N f) N A log V N g) N V h) 3 M

Today s big ideas big-oh O(g(n)), big-omega Ω g n, and bigtheta Θ(g n ) are used to describe the growth rate of functions can ask individually about worst, best, or average running time of an algorithm Try using this notation to describe the running time of some real algorithms!

Acknowledgements these slides borrow ideas from http://homepage.divms.uiowa.edu/~ghosh/2116.html http://datastructur.es/sp16/