§3 Compare the Algorithms 〖 Example 〗 Given (possibly negative) integers A 1, A 2, …, A N, find the maximum value of Max sum is 0 if all the integers.

Slides:



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

EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Counting the bits Analysis of Algorithms Will it run on a larger problem? When will it fail?
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Chapter 2: Algorithm Analysis
Chapter 2: Algorithm Analysis What is an algorithm? What to analyse? What we want to know? How accurate is required?
Analysis of Algorithms Review COMP171 Fall 2005 Adapted from Notes of S. Sarkar of UPenn, Skiena of Stony Brook, etc.
Algorithm Analysis. Analysis of Algorithms / Slide 2 Introduction * Data structures n Methods of organizing data * What is Algorithm? n a clearly specified.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Algorithm Analysis. Math Review – 1.2 Exponents –X A X B = X A+B –X A /X B =X A-B –(X A ) B = X AB –X N +X N = 2X N ≠ X 2N –2 N+ 2 N = 2 N+1 Logarithms.
Data Structures Review Session 1
Lec 5 Feb 10 Goals: analysis of algorithms (continued) O notation summation formulas maximum subsequence sum problem (Chapter 2) three algorithms image.
J. Michael Moore Searching & Sorting CSCE 110. J. Michael Moore Searching with Linear Search Many times, it is necessary to search an array to find a.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
CS 201 Data Structures and Algorithms Chapter 2: Algorithm Analysis - II Text: Read Weiss, §2.4.3 – Izmir University of Economics.
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
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.
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
CSS342: Algorithm Analysis1 Professor: Munehiro Fukuda.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Algorithm Analysis O Ω.
Recap Introduction to Algorithm Analysis Different Functions Function’s Growth Rate Three Problems Related to Algorithm Running Time Find Minimum in an.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Asymptotic Behavior Algorithm : Design & Analysis [2]
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
CISC 235: Topic 1 Complexity of Iterative Algorithms.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
Chapter 2 Algorithm Analysis Mathematical Background Figure 2.1 Typical growth rates.
Pei Zheng, Michigan State University 1 Chapter 8 Recursion.
Vishnu Kotrajaras, PhD.1 Data Structures
ALGORITHM ANALYSIS Analysis of Resource consumption by processor –Time –Memory –Number of processors –Power Proof that the algorithm works correctly Debasis.
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.
Lecture 15.
Chapter 2 Algorithm Analysis
Pseudo-code 1 Running time of algorithm is O(n)
Growth of Functions & Algorithms
Recitation 13 Searching and Sorting.
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Searching CSCE 121 J. Michael Moore.
CS 3343: Analysis of Algorithms
CSE 143 Lecture 5 Binary search; complexity reading:
CS 3343: Analysis of Algorithms
Data Structures Review Session
Programming and Data Structure
CE 221 Data Structures and Algorithms
Programming and Data Structure
CE 221 Data Structures and Algorithms
slides created by Ethan Apter
Design and Analysis of Algorithms. Presentation Topic Divide & Conquer Paradigm to design/develop algorithms.
1 // Cubic maximum contiguous subsequence sum algorithm.
Divide-and-Conquer Divide: the problem into subproblems
Presentation transcript:

§3 Compare the Algorithms 〖 Example 〗 Given (possibly negative) integers A 1, A 2, …, A N, find the maximum value of Max sum is 0 if all the integers are negative. Algorithm 1 int MaxSubsequenceSum ( const int A[ ], int N ) { int ThisSum, MaxSum, i, j, k; /* 1*/ MaxSum = 0; /* initialize the maximum sum */ /* 2*/ for( i = 0; i < N; i++ ) /* start from A[ i ] */ /* 3*/ for( j = i; j < N; j++ ) { /* end at A[ j ] */ /* 4*/ ThisSum = 0; /* 5*/ for( k = i; k <= j; k++ ) /* 6*/ ThisSum += A[ k ]; /* sum from A[ i ] to A[ j ] */ /* 7*/ if ( ThisSum > MaxSum ) /* 8*/ MaxSum = ThisSum; /* update max sum */ } /* end for-j and for-i */ /* 9*/ return MaxSum; } T( N ) = O( N 3 ) Detailed analysis is given on p /9

Algorithm 2 §3 Compare the Algorithms int MaxSubsequenceSum ( const int A[ ], int N ) { int ThisSum, MaxSum, i, j; /* 1*/ MaxSum = 0; /* initialize the maximum sum */ /* 2*/ for( i = 0; i < N; i++ ) { /* start from A[ i ] */ /* 3*/ ThisSum = 0; /* 4*/ for( j = i; j < N; j++ ) { /* end at A[ j ] */ /* 5*/ ThisSum += A[ j ]; /* sum from A[ i ] to A[ j ] */ /* 6*/ if ( ThisSum > MaxSum ) /* 7*/ MaxSum = ThisSum; /* update max sum */ } /* end for-j */ } /* end for-i */ /* 8*/ return MaxSum; } T( N ) = O( N 2 ) 2/9

§3 Compare the Algorithms Algorithm 3 Divide and Conquer 4 33 5 22 11 26 22 conquer divide T ( N/2 ) O( N ) T ( N ) = 2 T( N/2 ) + c N, T(1) = O(1) = 2 [2 T( N/2 2 ) + c N/2] + c N = 2 k O(1) + c k N where N/2 k = 1 = O( N log N ) Also true for N  2 k The program can be found on p.21. 3/9

§3 Compare the Algorithms Algorithm 4 On-line Algorithm int MaxSubsequenceSum( const int A[ ], int N ) { int ThisSum, MaxSum, j; /* 1*/ ThisSum = MaxSum = 0; /* 2*/ for ( j = 0; j < N; j++ ) { /* 3*/ ThisSum += A[ j ]; /* 4*/ if ( ThisSum > MaxSum ) /* 5*/ MaxSum = ThisSum; /* 6*/ else if ( ThisSum < 0 ) /* 7*/ ThisSum = 0; } /* end for-j */ /* 8*/ return MaxSum; } T( N ) = O( N ) A[ ] is scanned once only. 11 3 22 4 66 16 11 113  2 4  6 At any point in time, the algorithm can correctly give an answer to the subsequence problem for the data it has already read. 4/9

§3 Compare the Algorithms NA NA O( N )O(N log N)O( N 2 )O( N 3 ) Time 4321 Algorithm N =10 N =100 N =1,000 N =10,000 N =100,000 Input Size Running times of several algorithms for maximum subsequence sum (in seconds) Note: The time required to read the input is not included. 5/9

§4 Logarithms in the Running Time 〖 Example 〗 Binary Search : Given: A [0]  A [1]  ……  A [N  1] ; X Task: Find X Output: i if X = = A [ i ]  1 if X is not found lowhighmid X ~ A [ mid ] low high = mid  1 highlow= mid + 1 <>== mid 6/9

int BinarySearch ( const ElementType A[ ], ElementType X, int N ) { int Low, Mid, High; /* 1*/ Low = 0; High = N - 1; /* 2*/ while ( Low <= High ) { /* 3*/ Mid = ( Low + High ) / 2; /* 4*/ if ( A[ Mid ] < X ) /* 5*/ Low = Mid + 1; else /* 6*/ if ( A[ Mid ] > X ) /* 7*/ High = Mid - 1; else /* 8*/ return Mid; /* Found */ } /* end while */ /* 9*/ return NotFound; /* NotFound is defined as -1 */ } §4 Logarithms in the Running Time T(N) = ? T worst ( N ) = O( log N ) Very useful in case the data are static and is in sorted order (e.g. find words from a dictionary). Home work: Self-study Euclid’s Algorithm and Exponentiation on p /9

§5 Checking Your Analysis Method 1 When T(N) = O(N), check if T(2N)/T(N)  2 When T(N) = O(N 2 ), check if T(2N)/T(N)  4 When T(N) = O(N 3 ), check if T(2N)/T(N)  8 … Method 2 When T(N) = O( f (N) ), check if Read the example given on p.28 (Figures 2.12 & 2.13). 8/9

Laboratory Project 1 Performance Measurement Detailed requirements can be downloaded from Real Programmers don't comment their code. If it was hard to write, it should be hard to understand and harder to modify. I will not read and grade any program which has less than 30% lines commented. Don’t forget to sign you names and duties at the end of your report. Due: Friday, September 25th, 2009 at 10:00pm 9/9