Algorithms Lecture 13: Maximum Sub-array Problem using Divide-and-Conquer

Sdílet
Vložit
  • čas přidán 4. 01. 2019
  • California State University, Sacramento
    Spring 2018
    Algorithms
    by Ghassan Shobaki
    Text book: Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein, 3rd Edition, MIT Press, Cambridge (2009)
    Note: There is a dynamic programming algorithm (Kadane's algorithm) that solves this problem in linear time, but it is not discussed here. This lecture is limited to the divide-and-conquer algorithm.
    Correction:
    Line 3 in the pseudo-code (written at Minute 13) should be
    L = MaxSubarray(A, p, q)
    The third parameter should be q not q-1
    MaxCrossingSubarray(A, p, q, r)
    leftSum = -∞
    sum = 0
    for i = q down to p
    sum = sum + A[i]
    if sum Greater than leftSum
    leftSum = sum
    rightSum = -∞
    sum = 0
    for j = q+1 to r
    sum = sum + A[j]
    if sum Greater than rightSum
    rightSum = sum
    return leftSum + rightSum

Komentáře • 77

  • @heejune
    @heejune Před 5 lety +84

    Finally found the video clearly explains maximum Sub-array problem using Divide-and-Conquer! Really appreciate!

  • @adarshagrawal5858
    @adarshagrawal5858 Před 4 lety +39

    I understood the Kadane's Algorithm but was struggling in divide and conquer solution. This video cleared all my doubts. Thanks, Professor!!

  • @shubhamshrivastava1649
    @shubhamshrivastava1649 Před 4 lety +10

    Thanks professor for this valuable lecture.
    And Guys here is the full solution----First try out with yourself and do let me knw if u have any problem in solving programming problems
    class Solution {
    public int maxSubArray(int[] a) {
    return max_sum_array_recursion(a, 0, a.length-1);
    }

    private static int max_sum_array_recursion(int[] a, int i, int j) {
    if(i==j)return a[i];
    int middle= (i+j)/2;
    int L= max_sum_array_recursion(a,i,middle);
    int R= max_sum_array_recursion(a,middle+1,j);
    int C = findMaxCrossing(a,i,j,middle);

    return Math.max(Math.max(L, R),C);
    }

    static int findMaxCrossing(int[] arr, int startIndex, int endIndex, int mid) {
    int maxLeftSum = arr[mid];
    int currentSum = maxLeftSum;
    for (int i = mid - 1; i >= startIndex; i--) {
    currentSum += arr[i];
    maxLeftSum = Math.max(maxLeftSum, currentSum);
    }
    int maxRightSum = arr[mid + 1];
    currentSum = maxRightSum;
    for (int i = mid + 2; i

  • @debaratighatak2211
    @debaratighatak2211 Před 3 lety

    This is the best video for understanding maximum subarray sum using divide and conquer. Thank you sir :)

  • @lordbaggot
    @lordbaggot Před 4 lety +1

    Thanks Ghassan Shobaki, great stuff!

  • @ankit9953817851
    @ankit9953817851 Před 5 lety +13

    Have seen many videos on the same problem. Though, it doesn't introduced the Kadane's algo, but divide and conquer explanation is just superb. Much appreciated! Thank you :)

  • @kashif-ghafoor
    @kashif-ghafoor Před 3 lety +1

    helped me a lot. your teaching method is excellent

  • @lequocthinh8992
    @lequocthinh8992 Před 3 lety

    Thank Professor, now i understand the Divide and Conquer solution.

  • @InfiniteRabbitHole
    @InfiniteRabbitHole Před 3 lety

    This is actually so good. Thanks, man.

  • @priyankad6988
    @priyankad6988 Před 4 lety

    Awesome
    Awesome explanation... Please have the bright light so that we can read everything clearly and it looks good.

  • @striking_village
    @striking_village Před 2 lety

    finally sir, thank you so much I had understood this problem so perfectly

  • @silviudinca6501
    @silviudinca6501 Před rokem

    best explanation on this topic. seriously. thanks

  • @aaronk9740
    @aaronk9740 Před 4 lety +7

    Much better than Cormen's explanation. Thank you, professor Ghassan :)

  • @senthilkumar5
    @senthilkumar5 Před 4 lety

    Thank you so much'. Best video on divide and conquer

  • @andywei5518
    @andywei5518 Před 3 lety +1

    Thanks for clarifying this question :)

  • @_hackslang_9990
    @_hackslang_9990 Před 4 lety +11

    The explanation is good but i think if you had given an example while making the recurrence tree for a small problem it would have been really amazing.

  • @joshguevara9570
    @joshguevara9570 Před 4 lety

    Awesome explanation.

  • @user-in5fo6jw3f
    @user-in5fo6jw3f Před 10 měsíci

    Excellent teacher!

  • @skrolikowski
    @skrolikowski Před 4 lety

    Great explanation!

  • @Yaddu143
    @Yaddu143 Před 4 měsíci

    wow, thank you Professor

  • @dheerajmishra8742
    @dheerajmishra8742 Před 2 lety

    Explanation was Priceless !
    The Psuedocode needs to modified a bit
    private static int maxSubArrayInternal(int[] nums, int start, int end) {
    if (start == end) {
    return nums[start];
    }
    int mid = (start+(end-start)/2);
    int left = maxSubArrayInternal(nums, start, mid);
    int right = maxSubArrayInternal(nums, mid+1, end);
    int crossingSum = maxSubArrayCrossing(nums, start, mid, end);
    return Math.max(left, Math.max(right, crossingSum));
    }

  • @MrTrollFaceInc
    @MrTrollFaceInc Před 2 lety

    fantastic lecture. thank you.

  • @harischandrachaursiya9037

    Finally understood... Thank you

  • @seal0118
    @seal0118 Před 3 lety

    why do i get different results if i start to sum my left subarray in my max_cross_sum function from start then incrementing instead of mid then decrementing?
    for example: if i sum left subarray starting from middle then decrementing, i get with the example from the lecture {-1, 3, 4, -5, 9, -2}->11 which is correct, but if i left sum from start then increment with the same example i get a 10,
    my question is how is my own implementation wrong? its the same implementation i just decided to operate on the array from the other end.

  • @osmankhalid2005
    @osmankhalid2005 Před 3 lety +1

    I was able to understand this easily than how it was explained in the book.

  • @ekaami27
    @ekaami27 Před 3 lety

    Awesome explanation !! Cleared all the prior confusions . Thanks, Professor !

  • @pranjalnama2420
    @pranjalnama2420 Před 2 lety

    awesome explanation sir, and this how we should be taught in our college instead of where no classes are taken

    • @movocode
      @movocode Před rokem

      This is Univ. of California - how can you expect your tier 3 clg in India turn into this 🤣

  • @rbdtrades9790
    @rbdtrades9790 Před rokem

    thank you sir, extremely helpful

  • @maganaluis92
    @maganaluis92 Před 3 lety

    Interesting, so the best way to use Divide and Conquer is to find the least amount of sub-problems to solve. In this case is 2, however in the quick sort algorithm we find the smallest sub-problem.

  • @camrenwoodstock2510
    @camrenwoodstock2510 Před 2 lety +1

    Thanks for not writing the maximumCrossingSubarray very helpful

  • @user-ln6hz2nb7o
    @user-ln6hz2nb7o Před rokem

    thank you professor

  • @zahabmetra4848
    @zahabmetra4848 Před 2 lety

    Thank you : )

  • @tjek6rj5ejt66
    @tjek6rj5ejt66 Před 4 lety

    Thanks🙏🏻

  • @manikantabandla3923
    @manikantabandla3923 Před 5 lety +1

    Finally I've get to know the crossing sum in divide and conquer approach.

  • @praveenchouhan6388
    @praveenchouhan6388 Před 3 lety

    I think when we go left it will be T(logn) instead of T(n/2) and for every left and right we do T(n) for centre so the overall time would be T(nlogn) which is merge sort, please correct here if wrong.

    • @procoderssquad4165
      @procoderssquad4165 Před 3 lety +1

      No, it will be T(n/2) because we have divided the space into half, the problem is still the same. The overall time complexity will surely come as nlogn.

  • @Story.Blocks
    @Story.Blocks Před rokem

    please add more lights in room

  • @ecuchetti
    @ecuchetti Před 2 lety

    Once I know the MaxLeft and the MathRight, I can add both values to get the MaxCrossing value. So in the end I just need to return the max value of those 3
    Like this:
    public static int maxSubArrayDivideAndConquer(int A[]) {
    int n = A.length;
    if (n == 0)
    return 0;
    return maxSubArrayHelperFunction(A, 0, n - 1);
    }
    public static int maxSubArrayHelperFunction(int A[], int left, int right) {
    if (right == left)
    return A[left];
    int middle = (left + right) / 2;
    int leftans = maxSubArrayHelperFunction(A, left, middle);
    int rightans = maxSubArrayHelperFunction(A, middle + 1, right);
    int leftmax = A[middle];
    int rightmax = A[middle + 1];
    int temp = 0;
    for (int i = middle; i >= left; i--) {
    temp += A[i];
    if (temp > leftmax)
    leftmax = temp;
    }
    temp = 0;
    for (int i = middle + 1; i rightmax)
    rightmax = temp;
    }
    return Math.max(Math.max(leftans, rightans), leftmax + rightmax);
    }

  • @eggwarrior5630
    @eggwarrior5630 Před 2 lety +1

    May I know what does p and r indicate as? Sorry for asking dumb questions

    • @ghassanshobakicomputerscie9478
      @ghassanshobakicomputerscie9478  Před 2 lety

      As defined in the earlier lectures on divide-and-conquer, p is the start index and r is the end index of the input array.

  • @MohamedAshraf-uc8zo
    @MohamedAshraf-uc8zo Před rokem

    greetings to our egyption professor🎓🎓

  • @freebird6954
    @freebird6954 Před 4 lety

    MaxCrossingSubarray function, does it need at most (q-p+1) sum and comparations plus (r-q+1) sum and comparations?

    • @freebird6954
      @freebird6954 Před 4 lety

      it seems the computation cost is high

    • @ghassanshobakicomputerscie9478
      @ghassanshobakicomputerscie9478  Před 4 lety

      The computation cost for the MaxCrossingSubarray is clearly linear

    • @scootaloo118
      @scootaloo118 Před 3 lety

      @@ghassanshobakicomputerscie9478
      I thought you were speak on gematria. I must have clicked on the wrong video.
      Nice explanation though. Thank you.

  • @nisarali1954
    @nisarali1954 Před 5 lety

    thanks

  • @abhishekchakravorty234
    @abhishekchakravorty234 Před 4 lety +1

    Is it "L = MaxSubarray(A, p, q - 1)" OR "L = MaxSubarray(A, p, q)"? Isn't L from 0 to q (not q-1), and R from q+1 to r?

    • @ghassanshobakicomputerscie9478
      @ghassanshobakicomputerscie9478  Před 4 lety +1

      You are right. L is from p to q and R is from q+1 to r. No element is skipped. Please note that there is a correction in the Description section.

  • @naematullahbharwana3383
    @naematullahbharwana3383 Před 4 lety +1

    algorithm has mistake
    the correction is
    R=maxsubarray(A,q,r)

  • @siobhanahbois
    @siobhanahbois Před 4 lety +7

    4:54

  • @manuelguerrero9917
    @manuelguerrero9917 Před 3 lety

    Finally Yeahhhh

  • @klakshma
    @klakshma Před 2 lety

    It is a very good lecture. Still I get some doubts. At 3.23-3.27, he tells the solution is the index range which gives the max. sub array value. But while writing algorithm, the output is the value of the max. subarray (max{L,C,R}). Also in algo, q-1 should be q; It should be a typo. Otherwise, every time, you cut the middle element from the array. To calculate C one time, I agree the time complexity is O(n) obviously. But is not we find C recursively everytime we divide and as the height is log n, is not that n*logn time we calculate C value?

    • @ghassanshobakicomputerscie9478
      @ghassanshobakicomputerscie9478  Před 2 lety +1

      You are right about both points.
      A complete and useful implementation should return the indices not just the value. In the pseudo-code given here, we return the value for simplicity. Returning the indices requires tracking indices and that would make the pseudo-code unnecessarily messy. I make a note about this at 15:00.
      The index on Line 3 should be q not q-1 and this correction is noted in the description.

    • @klakshma
      @klakshma Před 2 lety

      @@ghassanshobakicomputerscie9478 many thanks for your kind reply. This max_subarray was a nice video explanation of yours sir If you have other videos on algo., I would be happy to watch and learn.

    • @ghassanshobakicomputerscie9478
      @ghassanshobakicomputerscie9478  Před 2 lety

      Here is the link to the Algorithms playlist:
      czcams.com/play/PL6KMWPQP_DM8t5pQmuLlarpmVc47DVXWd.html

  • @Hubert9509
    @Hubert9509 Před 4 lety +3

    what is p and r ? about 12:20

  • @arpitsrivastava9097
    @arpitsrivastava9097 Před 3 lety

    This can be solved by sliding window

  • @tudoriacob9632
    @tudoriacob9632 Před 3 lety +3

    U could’ve just use O(n) with a queue

  • @DanT-iu6oc
    @DanT-iu6oc Před 4 lety

    that code at the end is wrong. How does it sum up the sub-array values? no addition at all.

    • @ghassanshobakicomputerscie9478
      @ghassanshobakicomputerscie9478  Před 4 lety

      The summation is inside the MaxCrossingSubarray function, for which the code is not shown

    • @shreevatsalamborghin
      @shreevatsalamborghin Před 4 lety

      @@ghassanshobakicomputerscie9478 can you please show it now ! Because I want to learn and was not able to find.

    • @suryavaliveti8355
      @suryavaliveti8355 Před 4 lety +3

      @@shreevatsalamborghin here is a java implementation:
      static int findMaxCrossing(int[] arr, int startIndex, int endIndex, int mid) {
      int maxLeftSum = arr[mid];
      int currentSum = maxLeftSum;
      for (int i = mid - 1; i >= startIndex; i--) {
      currentSum += arr[i];
      maxLeftSum = Math.max(maxLeftSum, currentSum);
      }
      int maxRightSum = arr[mid + 1];
      currentSum = maxRightSum;
      for (int i = mid + 2; i

    • @shreevatsalamborghin
      @shreevatsalamborghin Před 4 lety +1

      Surya Valiveti Dhanyavad

  • @martininorway4113
    @martininorway4113 Před 4 lety

    There's a solution faster than this with divide and conquer.

  • @MrDivad006
    @MrDivad006 Před 5 lety

    Why bother talking about an O(nlogn) solution when a simpler O(n) solution exists?

    • @ghassanshobakicomputerscie9478
      @ghassanshobakicomputerscie9478  Před 5 lety +25

      Because this algorithm is a very good example of the divide-and-conquer method

    • @nirajgusain1452
      @nirajgusain1452 Před 4 lety

      Just to clear basics

    • @siobhanahbois
      @siobhanahbois Před 4 lety +6

      Why bother looking at video titled "... Divide-and-Conquer" if you don't want to learn about it?

    • @seal0118
      @seal0118 Před 3 lety +1

      this is a very good example of divide and conquer approach, you can solve alot of array related problems and achieve a not bad time complexity O(nlogn) instead of something like o(n^2), if you debug the code and understand how recursion/this algorithm works, you can later use the same concept on a different problem