CSE100: Design and Analysis of Algorithms - Maximum Subarray & Matrix Multiplication PDF

Document Details

BrighterPine7696

Uploaded by BrighterPine7696

University of California, Merced

2025

Alberto E. Cerpa

Tags

algorithm analysis matrix multiplication maximum subarray computer science

Summary

This document is a set of lecture notes for the CSE100 course, focusing on the design and analysis of algorithms. The content covers two key problems: the maximum subarray problem and matrix multiplication. The notes also delve into divide-and-conquer strategies, including example pseudocode, and time complexity analysis, including a discussion of Strassen's algorithm.

Full Transcript

CSE100: Design and Analysis of Algorithms Lecture #05 – Maximum Subarray & Matrix Multiplication 2025-02-06 Professor Alberto E. Cerpa more divide a...

CSE100: Design and Analysis of Algorithms Lecture #05 – Maximum Subarray & Matrix Multiplication 2025-02-06 Professor Alberto E. Cerpa more divide and conquer, Strassen’s algorithm CSE 100 L05 Maximum-subarray and Matrix Multiplication 1 Cerpa, Fall 2019 © UCM Last Time We saw a (pretty clever) algorithm to do SELECT in time O(n). We proved that it worked using the Substitution Method. The Master Theorem wouldn’t have worked for this. In practice for this algorithm, it’s often better to just choose the pivot randomly. You’ll see an analysis of that if you take EECS278 (randomized algorithms). We’ll also see some randomized algorithms… …in a few lectures CSE 100 L05 Maximum-subarray and Matrix Multiplication 2 Cerpa, Fall 2019 © UCM Today: more divide and conquer algorithms. The Maximum Subarray problem: find the contiguous subarray within an array whose values have the largest sum. The Matrix Multiplication problem: how do we efficiently multiply 2 large matrices? CSE 100 L05 Maximum-subarray and Matrix Multiplication 3 Cerpa, Fall 2019 © UCM Maximum-subarray problem background If you know the price of certain stock from day i to day j; You can only buy and sell one share once How to maximize your profit? 120 110 100 Price 90 80 70 60 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 day # CSE 100 L05 Maximum-subarray and Matrix Multiplication 4 Cerpa, Fall 2019 © UCM Brute Force What is the brute-force solution? max = -infinity; for each day pair p { if(p.priceDifference > max) max=p.priceDifference; } 𝑛𝑛 Time complexity? 2 pairs, so O(𝑛𝑛2 ) CSE 100 L05 Maximum-subarray and Matrix Multiplication 5 Cerpa, Fall 2019 © UCM Maximum-subarray problem If we know the price difference of each 2 contiguous days The solution can be found from the maximum- subarray Maximum-subarray of array A is: A subarray of A Nonempty Contiguous Whose values have the largest sum among all other possible subarrays CSE 100 L05 Maximum-subarray and Matrix Multiplication 6 Cerpa, Fall 2019 © UCM Example Day 0 1 2 3 4 Price 10 11 7 10 6 Difference 1 -4 3 -4 What is the solution? Buy on day 2, sell on day 3 Can be solved it by the maximum-subarray of difference array? Sub-array 0-1 0-2 0-3 0-4 1-2 1-3 1-4 2-3 2-4 3-4 Sum 1 -3 0 -4 -4 -1 -5 3 -1 -4 CSE 100 L05 Maximum-subarray and Matrix Multiplication 7 Cerpa, Fall 2019 © UCM Divide-and-conquer algorithm How to divide? Divide into 2 arrays What is the base case? How to combine the sub problem solutions to the current solution? The process: Divide array A[i, …, j] into A[i, …, mid] and A[mid+1, …, j] A sub array must be in one of them (or across them!) A[i, …, mid] // the left array A[mid+1, …, j] // the right array A[ …, mid, mid+1,…] // the array across the midpoint The maximum subarray is the largest sub-array among maximum subarrays of those 3 CSE 100 L05 Maximum-subarray and Matrix Multiplication 8 Cerpa, Fall 2019 © UCM Basic Pseudocode Input: array A[i, …, j] Ouput: sum of maximum-subarray, start point of maximum-subarray, end point of maximum-subarray FindMaxSubarray: 1. if(j