ALG0183 Algorithms & Data Structures Lecture 6 The maximum contiguous subsequence sum problem. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy.

Slides:



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

AE1APS Algorithmic Problem Solving John Drake
ALG0183 Algorithms & Data Structures Lecture 7 Big-Oh, Big-Omega, Big-Theta, Little-Oh 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks.
ALG0183 Algorithms & Data Structures Lecture 3 Algorithm Analysis 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Weiss Chapter 5 Sahni.
Factoring Polynomials
Sorted list matching & Experimental run-Time COP 3502.
ALG0183 Algorithms & Data Structures Lecture 17 Quicksort 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks comparison sort worse-case.
CSE 143 Lecture 4: testing and complexity reading:
Lecture 7: Greedy Algorithms II Shang-Hua Teng. Greedy algorithms A greedy algorithm always makes the choice that looks best at the moment –My everyday.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Introduction to Analysis of Algorithms
CS 206 Introduction to Computer Science II 09 / 12 / 2008 Instructor: Michael Eckmann.
Discrete Structures & Algorithms Summations EECE 320 — UBC.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
CSC 2300 Data Structures & Algorithms January 26, 2007 Chapter 2. Algorithm Analysis.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Data Structures CS 310. Abstract Data Types (ADTs) An ADT is a formal description of a set of data values and a set of operations that manipulate the.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
CS 201 Data Structures and Algorithms Chapter 2: Algorithm Analysis - II Text: Read Weiss, §2.4.3 – Izmir University of Economics.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Chapter 1 Equations, Inequalities, and Mathematical Models
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Reading and Writing Mathematical Proofs
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Array Dependence Analysis COMP 621 Special Topics By Nurudeen Lameed
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Recursion Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 7 © 2002 Addison Wesley.
Algorithm Analysis Data Structures and Algorithms (60-254)
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Recap Introduction to Algorithm Analysis Different Functions Function’s Growth Rate Three Problems Related to Algorithm Running Time Find Minimum in an.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
1, Mustafa Jarrar Lecture Notes on Sequences & Mathematical Induction. Birzeit University, Palestine, 2015 mjarrar©2015 Sequences & Mathematical Induction.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
CS 206 Introduction to Computer Science II 09 / 18 / 2009 Instructor: Michael Eckmann.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
CS 206 Introduction to Computer Science II 01 / 30 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 02 / 2009 Instructor: Michael Eckmann.
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Copyright ©2015 Pearson Education, Inc. All rights reserved.
1 Approximation algorithms Algorithms and Networks 2015/2016 Hans L. Bodlaender Johan M. M. van Rooij TexPoint fonts used in EMF. Read the TexPoint manual.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Building Java Programs Chapter 13 Lecture 13-1: binary search and complexity reading:
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
Recursion.
Applied Discrete Mathematics Week 2: Functions and Sequences
Mathematical Induction Recursion
CE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms
Presentation transcript:

ALG0183 Algorithms & Data Structures Lecture 6 The maximum contiguous subsequence sum problem. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Weiss Chapter 5.3 There are many algorithms to solve this problem and their performances vary dramatically.

The maximum contiguous subsequence sum problem. Given (possibly negative) integers A 1, A 2,...,A N, find (and identify the sequence corresponding to) the maximum value of: The maximum contiguous subsequence sum is zero if all the integers are negative. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 2

Examples If the input is {-2, 11, -4, 13, -5, 2} the maximum sum is 20. If the input is {1, -3, 4, -2, -1, 6} the maximum sum is 7. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 3 The case where all the numbers are positive is not interesting.

“Always consider emptiness” Weiss If all the integers were negative, the algorithm could return the smallest negative integer as the maximum contiguous subsequence sum. This would, however, be ignoring the empty subsequence of zero integers whose sum is 0. – Remember, the empty set is a subset of any set: ∀ A: ∅ ⊆ A 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 4 “Be aware that emptiness is always a possibility and that in many instances it is not a special case at all.” Weiss

The obvious O(N 3 ) algorithm. exhaustive search or brute-force An obvious solution is work out the sum of all possible subsequences, updating maximum sum as required. A loop is needed to iterate through all the possible starting subscripts of the subsequences. An inner loop is needed to iterate through all the possible ending subscripts of the subsequences. For each subsequence, a loop is needed to compute the sum of the elements. We have a loop inside a loop inside a loop. – Informally, Big-Oh is O(N 3 ). 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 5

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 6 A cubic maximum contiguous subsequence sum algorithm.

MaxSumTest.java the beginning 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 7 static means “any changes to a static member are made to all objects” private means “cannot be accessed by members of another class”

A more formal calculation of Big-Oh. The summation is carried out the following number of times: 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 8 How do we get the RHS from the LHS?

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 9 expand terms in brackets 1 is added to itself (j-i+1) times (n-i+1)

An improved O(N 2 ) algorithm. To improve Big-Oh from O(N 3 ) to O(N 2 ) requires removing a loop (if possible). For a value of i, the summation includes just one more element from the previous calculation. This previous calculation is thrown away by the O(N 3 ) algorithm. Making use of the previous calculation allows us to remove a loop and make the algorithm O(N 2 ). 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 10

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 11 A quadratic maximum contiguous subsequence sum algorithm.

Theorem 5.2 Weiss Let A i,j be any sequence with sum S i,j j, then A i,q is not the maximum subsequence. – A larger subsequence can be obtained by excluding the negative subsequence A i,j. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 12 Figure 5.6 © Addison-Wesley A maximum subsequence cannot begin with a negative number.

Observation 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 13 “All contiguous subsequences that border the maximum contiguous subsequence must have negative (or 0) sums (otherwise, we would include them).” Weiss

Observation 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 14 At some i, at the first value of j for which the sum of the subsequence becomes negative, we know that: a) The maximum occured between i and j-1, or b) The maximum started before i, or c) The maximum starts after j. Any new maximum will occur after index j, so i can be updated to j+1. For cases a) and b) we have already met and computed the maximum. maximum - maximum subsequence

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 15 At some i, we begin the usual process of summing subsequences. At some j, here when -7 is read, the sum is found to be negative. There is no point in considering further sequences starting at 1, 2, or 3. a negative subsequence Any subsequence that begins with a negative subsequence can be improved by eliminating that negative subsequence.

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 16 A linear maximum contiguous subsequence sum algorithm.

Lecture activity: trace maxSubSum3 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 17 thisSummaxSumseqStartseqEnd j= j= j= j= j= j= j= j=

From O(N 3 ) to O(N 2 ) to O(N): be amazed! Of course, it is usually not possible to improve an algorithm´s performance quite so much. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 18 Figure 5.1 ©Addison Wesley