Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Maximum-sum contiguous subarray Prudence Wong.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Searching Prudence Wong
5/15/ Algorithms1 Algorithms – Ch4 - Divide & Conquer Recurrences: as we saw in another lecture, the Divide and Conquer approach leads to Recurrence.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 1 Class 15 - Recursive sorting methods r Processing arrays by recursion r Divide-and-conquer.
Design and Analysis of Algorithms Maximum-subarray problem and matrix multiplication Haidong Xue Summer 2012, at GSU.
Advanced Topics in Algorithms and Data Structures Lecture 7.1, page 1 An overview of lecture 7 An optimal parallel algorithm for the 2D convex hull problem,
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
CSE 143 Lecture 4: testing and complexity reading:
Chapter 2: Algorithm Analysis
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Design and Analysis of Algorithms - Chapter 1
Instruction count for statements Methods Examples
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.
CS 206 Introduction to Computer Science II 10 / 13 / 2008 Instructor: Michael Eckmann.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Insight Through Computing 18. Two-Dimensional Arrays Set-Up Rows and Columns Subscripting Operations Examples.
CSC 2300 Data Structures & Algorithms January 26, 2007 Chapter 2. Algorithm Analysis.
Multiple Sequence alignment Chitta Baral Arizona State University.
Sorting and Heaps Eugene Weinstein New York University Computer Science Department Data Structures Fall 2008.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Proving correctness. Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant.
CSE 143 Lecture 5 Binary search; complexity reading: slides created by Marty Stepp and Hélène Martin
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
Algorithms Instructor: Ming Li David R. Cheriton School of Computer Science University of Waterloo CS341, Winter, 2011.
Reading and Writing Mathematical Proofs
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
Data Structure & Algorithm Lecture 1 – Selection & Insertion Sort JJCAO Steal some from Prof. Yoram Moses.
1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.
1 Chapter 4 Divide-and-Conquer. 2 About this lecture Recall the divide-and-conquer paradigm, which we used for merge sort: – Divide the problem into a.
1 Programming for Engineers in Python Autumn Lecture 12: Dynamic Programming.
Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself.
Dynamic Programming.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
Data Structure Introduction.
1 Chapter 6 Dynamic Programming. 2 Algorithmic Paradigms Greedy. Build up a solution incrementally, optimizing some local criterion. Divide-and-conquer.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
1 Algorithms CSCI 235, Fall 2015 Lecture 11 Elementary Sorts.
Design and Analysis of Algorithms Maximum-subarray problem and matrix multiplication (with more examples) Haidong Xue Summer 2012, at GSU.
Algorithm Design Techniques An Example The Problem Algorithm 1: Cubic Time Algorithm 2: Quadratic Time Algorithm 3: O(n log n) Time Algorithm 4: Linear.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Quick Sort Modifications By Mr. Dave Clausen Updated for Python.
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
QUICKSORT Quicksort is a sort-in-place algorithm that uses “divide-and-conquer”. First, partition the array into two subarrays A and B such that all in.
Building Java Programs Chapter 13 Lecture 13-1: binary search and complexity reading:
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8a. Sorting(1): Elementary Algorithms.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Lecture 2: More Examples CS 341: Algorithms Thursday, May 5 th
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Fundamentals of Algorithms MCS - 2 Lecture # 11
Applied Discrete Mathematics Week 2: Functions and Sequences
COMP108 Algorithmic Foundations Algorithm efficiency
Data Structures and Algorithms
Advanced Algorithms Analysis and Design
CSE 143 Lecture 5 Binary search; complexity reading:
Lecture 5: complexity reading:
Building Java Programs
2019/4/10 chapter25.
COMP108 Algorithmic Foundations Dynamic Programming
Building Java Programs
COMP108 Algorithmic Foundations Searching
At the end of this session, learner will be able to:
1 // Cubic maximum contiguous subsequence sum algorithm.
COMP108 Algorithmic Foundations Searching
Presentation transcript:

Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Maximum-sum contiguous subarray Prudence Wong

Algorithmic Foundations COMP108 2 (Contiguous Subarray) Given an array A[ ] of n numbers, find the maximum sum found in any contiguous subarray A zero length subarray has maximum 0 Example:  if A[ ] contains -2, 5, -3, 6, -1, then the maximum sum will be = 8  if A[ ] contains 10, 15, -3, -4, -2, -1, 8, 5, then the maximum sum will be 28 (all numbers added together) O(n 2 ), O(n log n), O(n) time algorithms Maximum-sum contiguous subarray

Algorithmic Foundations COMP108 Brute-force algorithm

Algorithmic Foundations COMP108 Brute-force algorithm  All possible contiguous subarrays  A[1..1], A[1..2], A[1..3],..., A[1..(n-1)], A[1..n]  A[2..2], A[2..3],..., A[2..(n-1)], A[2..n] ...  A[(n-1)..(n-1)], A[(n-1)..n]  A[n..n]  How many of them in total?  Algorithm: For each subarray, compute the sum. Find the subarray that has the maximum sum. 4 (Contiguous Subarray) O(n 2 )

Algorithmic Foundations COMP108 Idea of brute-force algorithm Example: sum from A[1]: sum from A[2]: sum from A[3]: sum from A[4]:3242 sum from A[5]:-11-1 sum from A[6]:20 sum from A[7]:-2 5 (Contiguous Subarray)

Algorithmic Foundations COMP108 Brute-force algorithm  Outer loop: index variable i to indicate start of subarray, for 1 ≤ i ≤ n, i.e., A[1], A[2],..., A[n]  for i = 1 to n do...  Inner loop: for each start index i, we need to go through A[i..i], A[i..(i+1)],..., A[i..n]  use an index j for i ≤ j ≤ n, i.e., consider A[i..j]  for j = i to n do... 6 (Contiguous Subarray)

Algorithmic Foundations COMP108 Pseudo code max = 0 for i = 1 to n do begin sum = 0 for j = i to n do begin sum = sum + A[j] if sum > max then max = sum end 7 (Contiguous Subarray) Time complexity? O(n 2 )

Algorithmic Foundations COMP108 Divide-and-conquer algorithm

Algorithmic Foundations COMP108 Divide-and-conquer algorithm  maximum contiguous subarray in A[1..n], let m=n/2 a. max contiguous subarray in A[1..m] b. max contiguous subarray in A[m+1..n] c. max contiguous subarray that spans across A[m]  (a) & (b) can be found recursively  (c) can be found in two steps  consider A[i..m] by fixing m but change i, i.e., 1 ≤ i ≤ m  consider A[(m+1)..j] by fixing (m+1) but change j, i.e., m+1 ≤ j ≤ n  sum these two maximum 9 (Contiguous Subarray)

Algorithmic Foundations COMP108 Idea of divide-and-conquer Example: max on L (recursion) 1015 max on R (recursion) 85 mid extend to L mid extend to R  Possible candidates:  25, 13, 28 (=18+10)  overall maximum 28, i.e., take all numbers 10 (Contiguous Subarray)

Algorithmic Foundations COMP108 Idea of divide-and-conquer Example: max on L (recursion) 5 max on R (recursion) 2-12 mid extend to L5-1 mid extend to R not take any  Possible candidates:  5, 3, 4 (=4+0)  overall maximum 5 11 (Contiguous Subarray)

Algorithmic Foundations COMP108 Pseudo code Algorithm MaxSum(p, q) if p == q then return max(A[p], 0) m =  (p+q)/2  maxL = MaxSum(p,m) maxR = MaxSum(m+1, q) compute maxMidL compute maxMidR maxMid = maxMidL + maxMidR return max(maxL, maxR, maxMid) 12 (Contiguous Subarray)

Algorithmic Foundations COMP108 Pseudo code Algorithm MaxSum(p, q) if p == q then return max(A[p], 0) m =  (p+q)/2  maxL = MaxSum(p,m) maxR = MaxSum(m+1, q) compute maxMidL compute maxMidR maxMid = maxMidL + maxMidR return max(maxL, maxR, maxMid) 13 (Contiguous Subarray) sum = 0, maxMidL = 0 for i = m down to p begin sum += A[i] maxMidL = max(maxMidL, sum) end

Algorithmic Foundations COMP108 Pseudo code Algorithm MaxSum(p, q) if p == q then return max(A[p], 0) m =  (p+q)/2  maxL = MaxSum(p,m) maxR = MaxSum(m+1, q) compute maxMidL compute maxMidR maxMid = maxMidL + maxMidR return max(maxL, maxR, maxMid) 14 (Contiguous Subarray) sum = 0, maxMidR = 0 for i = m+1 to q begin sum += A[i] maxMidR = max(maxMidR, sum) end

Algorithmic Foundations COMP108 Dynamic programming algorithm

Algorithmic Foundations COMP108 Dynamic programming algorithm Example 1: MaxHere = 66 MaxHere = 36-3 MaxHere = MaxHere = MaxHere = MaxHere = overall maximum is 6 16 (Contiguous Subarray)

Algorithmic Foundations COMP108 Dynamic programming algorithm Example 2: MaxHere = 22 MaxHere = 02-6 MaxHere = MaxHere = MaxHere = MaxHere = MaxHere = overall maximum is 4 17 (Contiguous Subarray)

Algorithmic Foundations COMP108 Dynamic programming algorithm Example 3: MaxHere = 0-2 MaxHere = 5-25 MaxHere = MaxHere = MaxHere = MaxHere = MaxHere = overall maximum is 5 18 (Contiguous Subarray)

Algorithmic Foundations COMP108 Pseudo Code – version 1 MaxHere[1] = max(A[1], 0) for i = 2 to n do MaxHere[i] = max(MaxHere[i-1]+A[i], 0) MaxSum = MaxHere[1] for i = 2 to n do MaxSum = max(MaxSum, MaxHere[i]) 19 (Contiguous Subarray)

Algorithmic Foundations COMP108 Pseudo Code – version 2 MaxHere = 0 MaxSoFar = 0 for i = 1 to n do begin MaxHere = max(MaxHere+A[i], 0) MaxSoFar = max(MaxSoFar, MaxHere) end MaxSum = MaxSoFar 20 (Contiguous Subarray)