Question

In: Computer Science

Create a greedy solution to the MaxSumSubArray problem, describe it and it's runtime, and prove it's...

Create a greedy solution to the MaxSumSubArray problem, describe it and it's runtime, and prove it's optimal

Code it in python

Solutions

Expert Solution

please check out the photos for better understanding.... And please do a comment for any doubt, thank you...

The algorithm for the MaxSumSubArray is known as Kadane's Algorithm which follows Greedy and Dynamic approach to find the optimal solution... It's a standard Algorithm...

algorithm...(picture)

At first,Max,Max_all is assigned to 0

then for all element in the array, we have to add the current elements with the Max_all and then store it again in Max_all until Max_all becomes <0 i.e. negetive

tnen compare the Max with Max_all and find if Max < Max_all. if so,then assign Max=Max_all

This is the approach...

Runtime:

the for loop is running from 0 to n-1 where n is the size of the array...

So, the Running Complexity of the algorithm is O(n)

optimal?

the algorithm is optimal except one condition... if all the elements in the array are negative, the algorithm gives wrong answer...

There is a solution for that...at first we have to check if all the elements are negative or nor, if so, then max sum sub array will be the largest element of the array...

now this will be the optimal answer...

code: (optimal code)

def MaxSumSubArray(arr,n) :
        Max=max(arr)   #finding the maximum element in the array
        if Max<0:   
                return Max    #if maximum is negative, then all the elements in the array is negative
        Max=Max_all=0    #else assigning Max=0 and Max_all=0
        for i in range(0,n) :    #for all element in arr
                Max_all+=arr[i]
                if Max < Max_all :
                        Max = Max_all
                if Max_all < 0 :   #if Max_all is negative
                        Max_all = 0
        return Max;   #returning max
print("Input the array Elements :")
arr = [int (x) for x in input().split()]   #array input separated by space
n=len(arr)   #calculating the length of the array
print("Maximum Sum of SubArray is : ", MaxSumSubArray(arr,n))  #calling function

#end of program

output...

screen sort of code:


Related Solutions

Maximum-weight independent set. (a) Prove that the two greedy algorithms(i.e., Greedy and Greedy 2) are equivalent....
Maximum-weight independent set. (a) Prove that the two greedy algorithms(i.e., Greedy and Greedy 2) are equivalent. That is, they return the same output for a given input. (b) Prove that Greedy 2 returns an optimal solution to the maximum weight independent set problem by application of lemmata.
Describe the problem with reporting bias and it's threat to meta analysis.
Describe the problem with reporting bias and it's threat to meta analysis.
Prove that the solution of the discrete least squares problem is given by the orthogonal projection.
Prove that the solution of the discrete least squares problem is given by the orthogonal projection.
Maximum-weight independent set. (a) Prove that the two greedy algorithms, optimal substructure and activity selection are...
Maximum-weight independent set. (a) Prove that the two greedy algorithms, optimal substructure and activity selection are equivalent. That is, they return the same output for a given input. (b) Prove that Greedy 2 returns an optimal solution to the maximum weight independent set problem by application of lemmata.
Describe a real world business problem and propose a solution or solutions to the problem by...
Describe a real world business problem and propose a solution or solutions to the problem by applying managerial economics concepts. Focus on a problem and its solutions. What will be the pros and cons of the solution or solutions.
Give one problem of 7 eleven and it's solution which 7 eleven faces in past few...
Give one problem of 7 eleven and it's solution which 7 eleven faces in past few years?
Create a transportation problem Select an initial feasible solution ( any method). Solve the transportation problem...
Create a transportation problem Select an initial feasible solution ( any method). Solve the transportation problem using the method of multipliers.
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT