Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Kadane's Algorithm Greedy or Optimised DP?

I feel like Kadane's algorithm is a modified version of the true dynamic programming solution of maximum subarray problem.Why do I feel so? I feel because the way to calculate the maximum subarray can be taken by:

for(i=0;i<N;i++)
    {
        DP[i][A[i]]=true;
        for(j= -ve maximum ;j<= +ve maximum ;j++)
        if(DP[i-1][j])
            DP[i][j+A[i]]=true;
    }

The recurrence being if it is possible to form j with a subarray ending at i-1 elements i can form j+A[i] using the i th element and also form A[i] alone by starting a subarray at i th position And at last we can search this DP array for maximum j that is marked true!

Note : DP[i][j] represents if it is possible to make j using a sub array ending at i! Here I assume j can be negative too.! Now one can easily derive that sum+ a negative number < sum . That implies adding any negative indices wont help getting a better sum thats why we can drop them! Morover we care about the maximum j till i-1 th position and connect it with i th element which makes me feel it is kind of making a greedy choice ( Just because maximum + element gives me a maximum).

NOTE: I haven't studied Greedy algorithms by now but I have an idea what a greedy choice is!

EDIT: SOmeone said my algorithm doesn't makes any sense so I am trying to post my code to make myself clear. I haven't taken j as -ve as they aren't fruitful. I repeat my state is defined as is it possible to make j using a subarray ending at i.

#include<bits/stdc++.h>
using namespace std;
int DP[101][101];
int main()
{
    int i,j,ans=INT_MIN;
    int A[]={3,-1,2,-1,5,-3};
    int N=sizeof(A)/sizeof(int);
    for(i=1;i<=N;i++)
    {
        if(A[i-1]>=0)
            DP[i][A[i-1]]++;
        for(j=0;j<=100;j++)
        {
            if(DP[i-1][j])
            {
                if(j+A[i-1]>=0)
                    DP[i][j+A[i-1]]++;
            }
            if(DP[i][j])
                ans=max(ans,j);
        }
    }
    cout<<ans<<"\n";
    return 0;
}

Output 8

like image 609
Shubham Sharma Avatar asked Jul 01 '15 07:07

Shubham Sharma


2 Answers

Kadane's is an iterative dynamic programming algorithm.

It is very common to optimize iterative DP algorithms to remove one dimension of the DP matrix along the major axis of the algorithm's progression.

The usual 'longest common subsequence' algorithm, for example, is usually described with a 2D matrix, but if the algorithm progresses from left to right, then you really only need space for 2 columns.

Kadane's algorithm is a similar optimization applied to a 1D problem, so the whole DP array disappears. The DP code in your question has a 2D matrix for some reason. I don't know why -- it doesn't really make sense.

This site does a pretty good job of explaining the derivation: https://hackernoon.com/kadanes-algorithm-explained-50316f4fd8a6

like image 54
Matt Timmermans Avatar answered Sep 18 '22 01:09

Matt Timmermans


I think that it is a greedy algorithm because kadanes algorithm finds the maximum sum at each step and then finds the overall solution .

like image 21
Radhika S Avatar answered Sep 22 '22 01:09

Radhika S