Algorithm Cost Algorithm Complexity. Algorithm Cost.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
Analysys & Complexity of Algorithms Big Oh Notation.
CS 3610/5610N data structures Lecture: complexity analysis Data Structures Using C++ 2E1.
the fourth iteration of this loop is shown here
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 3 Growth of Functions
Big-O and Friends. Formal definition of Big-O A function f(n) is O(g(n)) if there exist positive numbers c and N such that: f(n) = N Example: Let f(n)
Analysis of Algorithms intro.  What is “goodness”?  How to measure efficiency? ◦ Profiling, Big-Oh  Big-Oh: ◦ Motivation ◦ Informal examples ◦ Informal.
Not all algorithms are created equally Insertion of words from a dictionary into a sorted list takes a very long time. Insertion of the same words into.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Cmpt-225 Algorithm Efficiency.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Elementary Data Structures and Algorithms
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
Algorithm Analysis (Big O)
Program Performance & Asymptotic Notations CSE, POSTECH.
Week 2 CS 361: Advanced Data Structures and Algorithms
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
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.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Mathematics Review and Asymptotic Notation
Analysis of Algorithms
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
 DATA STRUCTURE DATA STRUCTURE  DATA STRUCTURE OPERATIONS DATA STRUCTURE OPERATIONS  BIG-O NOTATION BIG-O NOTATION  TYPES OF DATA STRUCTURE TYPES.
Computation, Complexity, and Algorithm IST 501 Fall 2014 Dongwon Lee, Ph.D.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Data Structure Introduction.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Introduction to Algorithms: Verification, Complexity, and Searching (2) Andy Wang Data Structures, Algorithms, and Generic Programming.
Introduction to Analysis of Algorithms CS342 S2004.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Algorithm Analysis Part of slides are borrowed from UST.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Computation, Complexity, and Algorithm IST 512 Spring 2012 Dongwon Lee, Ph.D.
Foundations of Algorithms, Fourth Edition
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Algorithm Analysis (Big O)
Scalability for Search Scaling means how a system must grow if resources or work grows –Scalability is the ability of a system, network, or process, to.
Searching Topics Sequential Search Binary Search.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Analysis of Algorithms
Last time More and more information born digital
Scalability for Search
David Kauchak CS52 – Spring 2015
Algorithm design and Analysis
Scalability for Search and Big O
CS 201 Fundamental Structures of Computer Science
Analysys & Complexity of Algorithms
Algorithm Efficiency and Sorting
Searching, Sorting, and Asymptotic Complexity
Searching, Sorting, and Asymptotic Complexity
Presentation transcript:

Algorithm Cost Algorithm Complexity

Algorithm Cost

Back to Bunnies Recall that we calculated Fibonacci Numbers using two different techniques –Recursion –Iteration LB

Back to Bunnies Recursive calculation of Fibonacci Numbers: Fib(1) = 1 Fib(2) = 1 Fib(N) = Fib(N-1) + Fib(N-2) So: Fib(3) = Fib(2) + Fib(1) = = 2 LB

Tree Recursion? f(n) f(n-1)f(n-2) f(n-3)f(n-4)f(n-3) f(n-4) f(n-5) f(n-4)f(n-5) f(n-6) LB

Tree Recursion Example f(6) f(5)f(4) f(3) f(2) f(3) f(2) f(1) f(2)f(1) f(2)f(1) LB

Recursively public static int fibR(int n) { if(n == 1 || n ==2) return 1; else return fibR(n-1) + fibR(n-2); } LB

Iteratively public static int fibI(int n) { int oldest = 1; int old = 1; int fib = 1; while(n-- > 2) { fib = old + oldest; oldest = old; old = fib; } return fib; } LB

Slight Modifications LB public static int fibR(int n) { if(n == 1 || n ==2) return 1; else return fibR(n-1) + fibR(n-2); } public static int fibR(int n) { if(n == 1 || n ==2) return 1; else return fibR(n-1) + fibR(n-2); } public static int fibI(int n) { int oldest = 1; int old = 1; int fib = 1; while(n-- > 2) { fib = old + oldest; oldest = old; old = fib; } return fib; } public static int fibI(int n) { int oldest = 1; int old = 1; int fib = 1; while(n-- > 2) { fib = old + oldest; oldest = old; old = fib; } return fib; } Add Counters

Demo LB

Conclusion Algorithm choice or design can make a big difference! LB

Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them to do so efficiently, making the best use of –Space –Time

Time and Space Time –Instructions take time. –How fast does the algorithm perform? –What affects its runtime? Space –Data structures take space. –What kind of data structures can be used? –How does the choice of data structure affect the runtime?

Time vs. Space Very often, we can trade space for time: For example: maintain a collection of students’ with SSN information. –Use an array of a billion elements and have immediate access (better time) –Use an array of 35 elements and have to search (better space)

The Right Balance The best solution uses a reasonable mix of space and time. –Select effective data structures to represent your data model. –Utilize efficient methods on these data structures.

Questions?

Algorithm Complexity

Scenarios I’ve got two algorithms that accomplish the same task –Which is better? Given an algorithm, can I determine how long it will take to run? –Input is unknown –Don’t want to trace all possible paths of execution For different input, can I determine how an algorithm’s runtime changes?

Measuring the Growth of Work While it is possible to measure the work done by an algorithm for a given set of input, we need a way to: –Measure the rate of growth of an algorithm based upon the size of the input –Compare algorithms to determine which is better for the situation

Introducing Big O Will allow us to evaluate algorithms. Has precise mathematical definition We will use simplified version in CS 1311 Caution for the real world: Only tells part of the story! Used in a sense to put algorithms into families LB

Why Use Big-O Notation Used when we only know the asymptotic upper bound. If you are not guaranteed certain input, then it is a valid upper bound that even the worst- case input will be below. May often be determined by inspection of an algorithm. Thus we don’t have to do a proof!

Size of Input In analyzing rate of growth based upon size of input, we’ll use a variable –For each factor in the size, use a new variable –N is most common… Examples: –A linked list of N elements –A 2D array of N x M elements –A Binary Search Tree of P elements

Formal Definition For a given function g(n), O(g(n)) is defined to be the set of functions O(g(n)) = {f(n) : there exist positive constants c and n 0 such that 0  f(n)  cg(n) for all n  n 0 }

Visual O() Meaning f(n) cg(n) n0n0 f(n) = O(g(n)) Size of input Work done Our Algorithm Upper Bound

Simplifying O() Answers (Throw-Away Math!) We say 3n = O(n 2 )  drop constants! because we can show that there is a n 0 and a c such that: 0  3n  cn 2 for n  n 0 i.e. c = 4 and n 0 = 2 yields: 0  3n  4n 2 for n  2

Correct but Meaningless You could say 3n = O(n 6 ) or 3n = O(n 7 ) But this is like answering: What’s the world record for the mile? –Less than 3 days. How long does it take to drive to Chicago? –Less than 11 years.

Comparing Algorithms Now that we know the formal definition of O() notation (and what it means)… If we can determine the O() of algorithms… This establishes the worst they perform. Thus now we can compare them and see which has the “better” performance.

Comparing Factors N log N N2N2 1 Size of input Work done

Correctly Interpreting O() O(1) or “Order One” –Does not mean that it takes only one operation –Does mean that the work doesn’t change as N changes –Is notation for “constant work” O(N) or “Order N” –Does not mean that it takes N operations –Does mean that the work changes in a way that is proportional to N –Is a notation for “work grows at a linear rate”

Complex/Combined Factors Algorithms typically consist of a sequence of logical steps/sections We need a way to analyze these more complex algorithms… It’s easy – analyze the sections and then combine them!

Example: Insert in a Sorted Linked List Insert an element into an ordered list… –Find the right location –Do the steps to create the node and add it to the list head // Inserting 75 Step 1: find the location = O(N)

Example: Insert in a Sorted Linked List Insert an element into an ordered list… –Find the right location –Do the steps to create the node and add it to the list head // Step 2: Do the node insertion = O(1) 75

Combine the Analysis Find the right location = O(N) Insert Node = O(1) Sequential, so add: –O(N) + O(1) = O(N + 1) = Only keep dominant factor O(N)

Example: Search a 2D Array Search an unsorted 2D array (row, then column) –Traverse all rows –For each row, examine all the cells (changing columns) Row Column O(N)

Example: Search a 2D Array Search an unsorted 2D array (row, then column) –Traverse all rows –For each row, examine all the cells (changing columns) Row Column O(M)

Combine the Analysis Traverse rows = O(N) –Examine all cells in row = O(M) Embedded, so multiply: –O(N) x O(M) = O(N*M)

Sequential Steps If steps appear sequentially (one after another), then add their respective O(). loop... endloop loop... endloop N M O(N + M)

Embedded Steps If steps appear embedded (one inside another), then multiply their respective O(). loop... endloop MN O(N*M)

Correctly Determining O() Can have multiple factors: –O(N*M) –O(logP + N 2 ) But keep only the dominant factors: –O(N + NlogN)  –O(N*M + P)  –O(V 2 + VlogV)  Drop constants: –O(2N + 3N 2 )  O(NlogN) remains the same O(V 2 )  O(N 2 ) O(N + N 2 )

Summary We use O() notation to discuss the rate at which the work of an algorithm grows with respect to the size of the input. O() is an upper bound, so only keep dominant terms and drop constants

Questions?