top of page
ishitajuneja01

Is Kadanes Algorithm Greedy or Optimised DP?


One might think of Kadane’s algorithm as both greedy and DP. As we can see, we maintain a running total of integers, and when it falls below zero, we set it back to zero (greedy part).

This is so that continuing with a negative sum is significantly worse than beginning over with a new range.


Now it may also be seen as a DP, with two options available at each stage: either take the current element and carry on with the previous total OR start a new range. The implementation will consider both of these options.


A greedy algorithm always chooses something that looks ideal at the moment. In other words, it chooses a locally optimal course in the hopes that it would result in a globally optimal outcome.

By setting current_sum to max(0, current_sum + x), Kadane’s algorithm does seek a locally optimal solution; however, this might also be considered as a dynamic programming solution that is also space-optimized since dp[i] only depends on dp[i-1].

Therefore, developers utilize an integer variable to preserve space. As a result, the DP transition function has a greedy behavior, making it appear both DP-like and greedy.


Overview of Kadanes Algorithm


What is a Greedy Algorithm?

When solving a problem, a greedy algorithm selects the best option available. It is unconcerned with whether the most excellent result at the moment will produce the final best result.

Even if the option was erroneous, the algorithm never changed its mind. It function....



0 views0 comments

Recent Posts

See All

DBMS Mastery: An Online Learning Adventure

Introduction In the dynamic landscape of technology and data-driven industries, the mastery of Database Management Systems (DBMS) is...

Kommentare


bottom of page