 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CSE Lecture 3 – Algorithms I
Analysis of Algorithms
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Chapter 1 – Basic Concepts
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
Analysis of Algorithms Review COMP171 Fall 2005 Adapted from Notes of S. Sarkar of UPenn, Skiena of Stony Brook, etc.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithm.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
Asymptotic Notations Iterative Algorithms and their analysis
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
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.
Vishnu Kotrajaras, PhD.1 Data Structures. Vishnu Kotrajaras, PhD.2 Introduction Why study data structure?  Can understand more code.  Can choose a correct.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Iterative Algorithm Analysis & Asymptotic Notations
Analysis of Algorithms
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CSC 211 Data Structures Lecture 13
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.
Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Introduction to Analysis of Algorithms CS342 S2004.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Algorithm Analysis Lakshmish Ramaswamy. Formal Definitions Big-Oh: T(N) is O(F(N)) if there exists positive constants N 0 and c such that T(N) N 0 Big-Omega:
CMSC 341 Asymptotic Anaylsis1 CMSC 341 Asymptotic Analysis.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
Vishnu Kotrajaras, PhD.1 Data Structures
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.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Analysis of Algorithms
Analysis of Algorithms
Introduction to Algorithms
Algorithm Analysis (not included in any exams!)
Algorithm design and Analysis
CS 201 Fundamental Structures of Computer Science
Analysis of Algorithms
At the end of this session, learner will be able to:
Estimating Algorithm Performance
Analysis of Algorithms
Presentation transcript:

 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm

Complexity Theory  Three different symbols will be discussed in class:  O (called the Big Oh)   (upper case Greek letter OMEGA)   (upper case Greek letter THETA)  Weiss, chapter 5

O (Big Oh)  The Big Oh is the upper bound of a function.  In the case of algorithm analysis, we use it to bound the worst- case running time, or the longest running time possible for any input of size n.  We can say that the maximum running time of the algorithm is in the order of Big Oh.  T(n) = O(f(n)) if there are positive constants c and n 0 such that T(n)  c*f(n) for all n  n 0

   is is the lower bound of a function.  We can say that the minimum running time of the algorithm is in the order of   T(n) =  (f(n)) if there are positive constants c and n 0 such that T(n)  c*f(n) for all n  n 0

   is composed of both O and .  T(n) =  (f(n)) if and only if T(n) is O(f(n)) and T(n) is  (f(n)) n T upper bound lower bound

A Few Remarks  We only care what happens for large n (n  n 0 )  O, ,  "hide" the constant c from us.  Find simple and small functions  E.g. (n 2 + 3n + 4) is O(n 3 ), since (n 2 + 3n + 4) 3, but it is also O(n 2 ), since (n 2 + 3n + 4) 10

The Logarithm  For any b, n>0: log b n=k if b k =n  We use log instead of log 2 (as in Weiss)  Be careful, often  lg instead of log 2  log instead of log 10  Properties of logarithms  log b (x*y) = log b x + log b y  log b (x/y) = log b x - log b y  log b x a = a*log b x  log b x= (log a x)/(log a b)  For complexity analysis the base does not matter  For any constant b>1: log b n = O(log n)

Repeated Doubling and Halving  Repeated doubling  We can repeatedly double only logarithmically many times until we reach n  Repeated halving  Starting at n, we can halve only logarithmically many times before we reach 1

Static Searching Problem  Given an integer num and an array arr, return the position of num in arr or an indication that it is not present. If num occurs more than once, return any occurrence. The array is never altered (is static).  Weiss, chapter “7”“7”“3”“3”“12”“42”“56”“75”“77”

4 Linear (Sequential) Search for (int i = 0; i <= last; i++) { if (arr[i] = num) { return i; }  Complexity  E.g. search for “56”  O(n) (linear) “7”“7”“3”“3”“12”“42”“56”“75”“77”

Linear (Sequential) Search for (int i = 0; i <= last; i++) { if (arr[i] = num) { return i; }  Complexity  E.g. search for “98”  O(n) (linear) “7”“7”“3”“3”“12”“42”“56”“75”“77”

Binary Search (1)  Array must be sorted  Routine to return index where element is equal to argument  Look in middle of occupied part of array  If that is the element we are looking for, we found it  Otherwise, decide whether the element must be in front half or back half (do recursive call to look there)  E. g. search for “56”  This is a divide-and-conquer algorithm “7”“7”“3”“3”“12”“42”“56”“75”“77”

Binary Search (1)  Array must be sorted  Routine to return index where element is equal to argument  Look in middle of occupied part of array  If that is the element we are looking for, we found it  Otherwise, decide whether the element must be in front half or back half (do recursive call to look there)  E. g. search for “56”  This is a divide-and-conquer algorithm “7”“7”“3”“3”“12”“42”“56”“75”“77”

Binary Search (1)  Array must be sorted  Routine to return index where element is equal to argument  Look in middle of occupied part of array  If that is the element we are looking for, we found it  Otherwise, decide whether the element must be in front half or back half (do recursive call to look there)  E. g. search for “56”  This is a divide-and-conquer algorithm “7”“7”“3”“3”“12”“42”“56”“75”“77”

Binary Search (1)  Array must be sorted  Routine to return index where element is equal to argument  Look in middle of occupied part of array  If that is the element we are looking for, we found it  Otherwise, decide whether the element must be in front half or back half (do recursive call to look there)  E. g. search for “56”  This is a divide-and-conquer algorithm “7”“7”“3”“3”“12”“42”“56”“75”“77”

Binary Search (1)  Array must be sorted  Routine to return index where element is equal to argument  Look in middle of occupied part of array  If that is the element we are looking for, we found it  Otherwise, decide whether the element must be in front half or back half (do recursive call to look there)  E. g. search for “56”  This is a divide-and-conquer algorithm “7”“7”“3”“3”“12”“42”“56”“75”“77”

Binary Search (2) “7”“7” “3”“3”“12” “42” “56” “75” “77”

Binary Search (3)  It can also be written as a loop (non recursive)  Warning: the idea (binary search) is simple, but the code is tricky to get right  Avoid infinite recursion or infinite loop  Treat boundary cases properly  Avoid looking at elements which are not in the sequence  Each time we deal with half as much of the array as the previous call (or iteration)  So we make log n calls or iterations  Each time we do const work besides the recursion  So the total cost is O(log n) (much faster than simple searching or searching in an unsorted array)

Code public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 }

Code public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } lowhigh find 56

Code public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } lowhigh find 56

Code public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } lowhighmid find 56

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code low highmid find 56

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code low highmid find 56

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code low highmid find 56

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code low high mid find 56

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code low high mid find 56

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code low high mid find 56

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code low high mid find 56

Code public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } lowhigh find 98

Code public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } lowhigh find 98

Code public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } lowhighmid find 98

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code low highmid find 98

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code low highmid find 98

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code low highmid find 98

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code low mid find 98 high

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code find 98 midhigh low

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code find 98 high low mid

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code find 98 high low mid

public static int binarySearch(Comparable [ ] a, Comparable x) { int low = 0; int high = a.length - 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid].compareTo(x) < 0) low = mid + 1; else if(a[mid].compareTo(x) > 0) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 } Code find 98 high low mid

Simplifying Rules  If f(n) is in O(g(n)) and g(n) is in O(h(n)), then f(n) is in O(h(n)).  If f(n) is in O(k*g(n)) for any constant k > 0, then f(n) is in O(g(n)).  If f 1 (n) is in O(g 1 (n)) and f 2 (n) is in O(g 2 (n)), then f 1 (n)+f 2 (n) is in max{O(g 1 (n)), O(g 2 (n))}.  If f 1 (n) is in O(g 1 (n)) and f 2 (n) is in O(g 2 (n)), then f 1 (n)*f 2 (n) is in O(g 1 (n)*g 2 (n)).

Analysing Control Statements  While loop  Analyse like a for loop  If statement  Take greater complexity of then/else clauses  Switch statement  Take complexity of most expensive case  Subroutine call  Complexity of the subroutine

Limitations of Big Oh  Not for small amount of input  Is designed for theoretical analysis  E.g. memory access is O(1), does not differentiate between main memory and disc  Constants may be to large for practical use  Worst case is sometimes uncommon an can be ignored

Recursion  This is a review of the main ideas  We expect some experience from first year  Read Weiss Sections

Key Ideas  A method includes one or more calls to the same method  The result of the nested calls is used to compute the result of the outer call  There is a base case  The call may be indirectly  Understand the method by assuming the nested calls work correctly  First, let us look at a non-programming example

Recursive Definition  Define a list of number separated by commas  Example: 23, 27, 31, 1  Like this  LIST is number  or LIST isnumber, LIST

Example  Try to see whether “23, 27, 31, 1” is a list  Apply definition LIST: number or LIST: number, LIST 23, 27, 31, 1 23, LIST 27, LIST 31, LIST 1

Erroneous Examples  4. 54, 23  D, 34, 2 LIST: number or LIST: number, LIST

Base Case  Notice that we had one case (LIST: number) that does no recursion  That is called the base case  Otherwise, we would have infinite recursion LIST: number or LIST: number, LIST 31, LIST 1 …

What About Programs?  We can solve a lot of mathematical problems recursively  E.g.  i=0 i = … + n public static int sum (int n) { if (n < 1) return 0; else return(sum(n – 1) + n); } n

Example public static int sum(int n) { if(n < 1) return 0; return(sum(n – 1) + n); } sum(4) sum(3) sum(2) sum(1) sum(0)

Progress  Notice that we must make progress toward base case  Sum(0)  There is steady reduction in size of problem  Otherwise, trouble

Iterative Solution  For this problem a recursive solution does not make sense  Elegant, but slower than the following solution public static int sum(int n) { int result = 0; for (int i = 1; i <= n; i++) result += i; return(n); }  The best solution public static int sum(int n) { return (n/2)*(n+1); }