際際滷

際際滷Share a Scribd company logo
The Maximum Subarray Problem
Defining problem , its brute force
solution, divide and conquer
solution
Presented by: Kamran Ashraf
Formal Problem Definition
 Given a sequence of numbers <a1,a2,..an> we
work to find a subsequence of A that is
contiguous and whose values have the maximum
sum.
 Maximum Subarray problem is only challenging
when having both positive and negative
numbers.
掘恰温馨沿鉛艶
Here, the subarray A[811] is the Maximum
Subarray, with sum 43, has the greatest sum of
any contiguous subarray of array A.
Brute Force Solution
To solve the Maximum Subarray Problem we can use
brute force solution however in brute force we will
have to compare all possible continuous subarray
which will increase running time. Brute force will
result in 儔(n2), and the best we can hope for is to
evaluate each pair in constant time which will result in
畚(n2).
Divide and Conquer is a more efficient method for
solving large number of problems resulting in
儔(nlogn).
Divide and Conquer Solution
 First we Divide the array A [low  high] into two
subarrays A [low  mid] (left) and A [mid +1 
high] (right).
 We recursively find the maximum subarray of A
[low  mid] (left), and the maximum of A [mid
+1  high] (right).
 We also find maximum crossing subarray that
crosses the midpoint.
 Finally we take a subarray with the largest sum
out of three.
掘恰温馨沿鉛艶
Time Analysis
 Find-Max-Cross-Subarray takes: 儔 (n) time
 Two recursive calls on input size n/2 takes:
2T(n/2) time
 Hence:
T(n) = 2T(n/2) + 儔 (n)
T(n) = 儔 (n log n)
Pseudo Code
Max-subarray(A, Left, Right)
if (Right == Left)
return (left, right, A[left])
else mid= [(left+right)/2]
L1=Find-Maximum-Subarray(A,left,mid)
R1=Find-Maximum-Subarray(A,mid+1,right)
M1=Find-Max-Crossing-Subarray(A,left,mid,right)
If sum(L1) > sum(R1) and sum(L1) > sum(M1)
Return L1
elseif sum(R1) > sum(L1) and sum(R1) > sum(M1)
Return R1
Else return M1

More Related Content

What's hot (20)

Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
Mohamed Loey
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Y脹ld脹r脹m Tam
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
Amin Omi
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
Rezwan Siam
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
sandeep54552
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
Respa Peter
Assignment problem branch and bound.pptx
Assignment problem branch and bound.pptxAssignment problem branch and bound.pptx
Assignment problem branch and bound.pptx
KrishnaVardhan50
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
Kumar
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
Branch and bound
Branch and boundBranch and bound
Branch and bound
Nv Thejaswini
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
padmeshagrekar
Job sequencing with Deadlines
Job sequencing with DeadlinesJob sequencing with Deadlines
Job sequencing with Deadlines
YashiUpadhyay3
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
Arafat Hossan
Introduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of OptimalityIntroduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of Optimality
Bhavin Darji
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
Jenny Galino
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
Mohamed Loey
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
Amin Omi
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
Rezwan Siam
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
sandeep54552
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
Respa Peter
Assignment problem branch and bound.pptx
Assignment problem branch and bound.pptxAssignment problem branch and bound.pptx
Assignment problem branch and bound.pptx
KrishnaVardhan50
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
Kumar
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
padmeshagrekar
Job sequencing with Deadlines
Job sequencing with DeadlinesJob sequencing with Deadlines
Job sequencing with Deadlines
YashiUpadhyay3
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
Arafat Hossan
Introduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of OptimalityIntroduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of Optimality
Bhavin Darji
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
Jenny Galino

Viewers also liked (9)

Ubiquitous Computing
Ubiquitous ComputingUbiquitous Computing
Ubiquitous Computing
Kamran Ashraf
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
Swati Swati
Longest common subsequence lcs
Longest common subsequence  lcsLongest common subsequence  lcs
Longest common subsequence lcs
Shahariar Rabby
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
Amrinder Arora
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
Amrinder Arora
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Sahil Kumar
The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017
LinkedIn
State of the Word 2011
State of the Word 2011State of the Word 2011
State of the Word 2011
photomatt
Ubiquitous Computing
Ubiquitous ComputingUbiquitous Computing
Ubiquitous Computing
Kamran Ashraf
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
Swati Swati
Longest common subsequence lcs
Longest common subsequence  lcsLongest common subsequence  lcs
Longest common subsequence lcs
Shahariar Rabby
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
Amrinder Arora
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
Amrinder Arora
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Sahil Kumar
The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017
LinkedIn
State of the Word 2011
State of the Word 2011State of the Word 2011
State of the Word 2011
photomatt

Similar to The Maximum Subarray Problem (20)

Brute Force and Divide & Conquer Technique
Brute Force and Divide & Conquer TechniqueBrute Force and Divide & Conquer Technique
Brute Force and Divide & Conquer Technique
ssusered62011
Calculus Assignment Help
Calculus Assignment HelpCalculus Assignment Help
Calculus Assignment Help
Math Homework Solver
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
B.Kirron Reddi
Calculus Assignment Help
Calculus Assignment HelpCalculus Assignment Help
Calculus Assignment Help
Maths Assignment Help
Brute force-algorithm
Brute force-algorithmBrute force-algorithm
Brute force-algorithm
9854098540
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
chidabdu
Maximum Subarray Sum in Python
Maximum Subarray Sum in PythonMaximum Subarray Sum in Python
Maximum Subarray Sum in Python
Kal Bartal
Calculus Homework Help
Calculus Homework HelpCalculus Homework Help
Calculus Homework Help
Math Homework Solver
Computer Science Exam Help
Computer Science Exam Help Computer Science Exam Help
Computer Science Exam Help
Programming Exam Help
Statistical computing with r estatistica - maria l. rizzo
Statistical computing with r   estatistica - maria l. rizzoStatistical computing with r   estatistica - maria l. rizzo
Statistical computing with r estatistica - maria l. rizzo
Andr辿 Oliveira Souza
Algorithms Exam Help
Algorithms Exam HelpAlgorithms Exam Help
Algorithms Exam Help
Programming Exam Help
Daa chapter6
Daa chapter6Daa chapter6
Daa chapter6
B.Kirron Reddi
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfUnit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
yashodamb
Solution 3.
Solution 3.Solution 3.
Solution 3.
sansaristic
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
Nirmalavenkatachalam
Programming Exam Help
Programming Exam Help Programming Exam Help
Programming Exam Help
Programming Exam Help
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
Programming Homework Help
Learn Dynamic Programming Roadmap at Tutort Academy
Learn Dynamic Programming Roadmap at Tutort AcademyLearn Dynamic Programming Roadmap at Tutort Academy
Learn Dynamic Programming Roadmap at Tutort Academy
Tutort Academy
Undecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsUndecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation Algorithms
Muthu Vinayagam
test pre
test pretest pre
test pre
farazch
Brute Force and Divide & Conquer Technique
Brute Force and Divide & Conquer TechniqueBrute Force and Divide & Conquer Technique
Brute Force and Divide & Conquer Technique
ssusered62011
Brute force-algorithm
Brute force-algorithmBrute force-algorithm
Brute force-algorithm
9854098540
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
chidabdu
Maximum Subarray Sum in Python
Maximum Subarray Sum in PythonMaximum Subarray Sum in Python
Maximum Subarray Sum in Python
Kal Bartal
Statistical computing with r estatistica - maria l. rizzo
Statistical computing with r   estatistica - maria l. rizzoStatistical computing with r   estatistica - maria l. rizzo
Statistical computing with r estatistica - maria l. rizzo
Andr辿 Oliveira Souza
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfUnit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
yashodamb
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
Nirmalavenkatachalam
Learn Dynamic Programming Roadmap at Tutort Academy
Learn Dynamic Programming Roadmap at Tutort AcademyLearn Dynamic Programming Roadmap at Tutort Academy
Learn Dynamic Programming Roadmap at Tutort Academy
Tutort Academy
Undecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsUndecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation Algorithms
Muthu Vinayagam
test pre
test pretest pre
test pre
farazch

The Maximum Subarray Problem

  • 1. The Maximum Subarray Problem Defining problem , its brute force solution, divide and conquer solution Presented by: Kamran Ashraf
  • 2. Formal Problem Definition Given a sequence of numbers <a1,a2,..an> we work to find a subsequence of A that is contiguous and whose values have the maximum sum. Maximum Subarray problem is only challenging when having both positive and negative numbers.
  • 3. 掘恰温馨沿鉛艶 Here, the subarray A[811] is the Maximum Subarray, with sum 43, has the greatest sum of any contiguous subarray of array A.
  • 4. Brute Force Solution To solve the Maximum Subarray Problem we can use brute force solution however in brute force we will have to compare all possible continuous subarray which will increase running time. Brute force will result in 儔(n2), and the best we can hope for is to evaluate each pair in constant time which will result in 畚(n2). Divide and Conquer is a more efficient method for solving large number of problems resulting in 儔(nlogn).
  • 5. Divide and Conquer Solution First we Divide the array A [low high] into two subarrays A [low mid] (left) and A [mid +1 high] (right). We recursively find the maximum subarray of A [low mid] (left), and the maximum of A [mid +1 high] (right). We also find maximum crossing subarray that crosses the midpoint. Finally we take a subarray with the largest sum out of three.
  • 7. Time Analysis Find-Max-Cross-Subarray takes: 儔 (n) time Two recursive calls on input size n/2 takes: 2T(n/2) time Hence: T(n) = 2T(n/2) + 儔 (n) T(n) = 儔 (n log n)
  • 8. Pseudo Code Max-subarray(A, Left, Right) if (Right == Left) return (left, right, A[left]) else mid= [(left+right)/2] L1=Find-Maximum-Subarray(A,left,mid) R1=Find-Maximum-Subarray(A,mid+1,right) M1=Find-Max-Crossing-Subarray(A,left,mid,right) If sum(L1) > sum(R1) and sum(L1) > sum(M1) Return L1 elseif sum(R1) > sum(L1) and sum(R1) > sum(M1) Return R1 Else return M1