Let’s hope this doesn’t take too long. BIG O. WHY WE SHOULD CARE Computers are can perform billions of operations per second but they are still not infinite.

Slides:



Advertisements
Similar presentations
Algorithm Analysis Input size Time I1 T1 I2 T2 …
Advertisements

Chapter 20 Computational complexity. This chapter discusses n Algorithmic efficiency n A commonly used measure: computational complexity n The effects.
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Introduction to Algorithms Quicksort
College of Information Technology & Design
12-Apr-15 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
MATH 224 – Discrete Mathematics
Garfield AP Computer Science
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 2 Algorithm Analysis.
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Recursion CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Iteration.
Chapter 1 – Basic Concepts
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Quicksort.
Professor John Peterson
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Complexity (Running Time)
CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Lecture 16: Big-Oh Notation
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Java Unit 9: Arrays Declaring and Processing Arrays.
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
Lecture 2 Computational Complexity
Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,
Analysis of Algorithms
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Hopefully this lesson will give you an inception of what recursion is.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
 O(1) – constant time  The time is independent of n  O(log n) – logarithmic time  Usually the log is to the base 2  O(n) – linear time  O(n*logn)
Problem of the Day  I am thinking of a question and propose 3 possible answers. Exactly one of the following is the solution. Which is it? A. Answer 1.
3.3 Complexity of Algorithms
Algorithm Analysis (Big O)
Question of the Day  Move one matchstick to produce a square.
LECTURE 23: LOVE THE BIG-OH CSC 212 – Data Structures.
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
Problem of the Day  On the next slide I wrote today’s problem of the day. It has 3 possible answers. Can you guess which 1 of the following is the solution?
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
LECTURE 22: BIG-OH COMPLEXITY CSC 212 – Data Structures.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Algorithm Analysis 1.
Introduction to Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
Algorithmic complexity: Speed of algorithms
Efficiency (Chapter 2).
Big-Oh and Execution Time: A Review
Building Java Programs
CS 201 Fundamental Structures of Computer Science
Analysis of Algorithms
Building Java Programs
Revision of C++.
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Algorithmic complexity: Speed of algorithms
Analysis of Algorithms
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

Let’s hope this doesn’t take too long. BIG O

WHY WE SHOULD CARE Computers are can perform billions of operations per second but they are still not infinite in their power. Each operation, while seemingly instantaneous still takes time and resources If we are not careful in the way we design our algorithms, they can become inefficient Their run time could even outlast our own lifetimes If Usain Bolt and Mr. Mayewsky were in a race, Mr. Mayewsky would win if all he has to do is walk around the school while Usain has to run around the earth.

BIG O FOR A FOR LOOP public void algorithm(int [] arr){ for(int i1 = 0; i1 < arr.length; i1++){ //code } The method above would have a Big O of n where n is the size of what ever array gets passed in. We don’t know the size beforehand but we do know that it will directly correspond to the size of the array

BIG O FOR A NESTED FOR LOOP public void algorithm(int [] arr){ for(int i1 = 0; i1 < arr.length; i1++){ for(int i2 = 0; i2 < arr.length; i2++){ //code } The method above would have a Big O of n 2 where n is the size of what ever array gets passed in. The inside loop will have to go through all instances of n for every one time that the outside loop executes. Think of it like planting a row of crops and then being told you have multiple rows to plant. For each nested loop, Big O would be multiplied by a factor of n.

BIG O FOR A FOR LOOP WITH A STATIC END public void algorithm(int [] arr){ for(int i1 = 0; i1 < ; i1++){ //code } The method above would have a Big O of only 1. While the for loop is going to execute one billion times, it is no longer dependent on the size of the array. So it will always be limited at one billion

BIG O FOR A FOR LOOP WITH AN EXPONENTIAL DECREASE public void algorithm(int [] arr){ for(int i1 = 0; i1 < arr.length; i1 *= 2){ //code } The method above would have a Big O of only log(n). The loop is still dependent on the number of elements in the array, but the index is increasing exponentially since we have i1 *= 2 instead of i1++.

BIG O FOR A FOR LOOP WITH AN MULTIPLIER public void algorithm(int [] arr){ for(int i1 = 0; i1 < 2 * arr.length; i1++){ //code } The method above would have a Big O of n. Notice that the length of the array is being multiplied by a factor of two so you could describe the Big O as 2n. But in the world of Big O we don’t care about static multipliers.

BIG O FOR SEQUENTIAL FOR LOOPS public void algorithm(int [] arr){ for(int i1 = 0; i1 < arr.length; i1++){ for(int i2 = 0; i2 < arr.length; i2++){ //code } for(int i1 = 0; i1 < arr.length; i1 += 1){ //code } The method above would have a Big O of n 2. The first set of nested for loops have a Big O of n 2. The second has a Big O of n. We could describe the Big O as n 2 + n but we only pay attention to the most sever component. Think of it this way, you might be concerned if there was a scorpion crawling around you feet but not if there is Godzilla standing right behind you.

BIG O FOR A SINGLE CALL RECURSION public void algorithm(int [] arr, int i){ if(i >= arr.length) return; //code algorigthm(arr, i + 1); } The method above would have a Big O of n. When we only make one recursive call, recursion acts in the same way as a for loop.

BIG O FOR MULTIPLE CALL RECURSION public void algorithm(int [] arr, int i){ if(i >= arr.length) return; //code algorigthm(arr, i + 1); } The method above would have a Big O of 2 n. When we make multiple recursive calls we have a Big O of x n, we have branching take place, where n is the size of the array and the depth that each branch reached and x is the number of branching that take place at each level.

BEST, WORST, AND AVERAGE CASE The analysis of Big-O can be a complex science and we are only getting a basic look at it. What we have looked at are a lot of general cases. Depending on the algorithm, the Big-O can be difficult to analyze. Most also have some type of Best, Worst, and Average Case depending on the conditions of the input. Think of it like mowing a lawn. If you go to mow a lawn, it might have been 2 days ago, might have been mowed 2 weeks ago, or it might have been mowed 2 years ago.