Dynamic programming and Divide-and-conquer:
Similarity:
1 Dynamic programming is an extension of Divide and Conquer.
2. Both work by breaking down the problem into a smaller subproblem,
3.Solution to the problem gets combine to find the original problem.
Difference:
Divide and Conquer
1. The subproblem is independent of each other.
2. Recursive
3. Consume more running time
4. Not very efficient
Dynamic program:
1. The subproblem is interrelated on each other.
2. Non-recursive
3. Consume less running time
4. Very efficient
package dp;
public class CoinRow {
public static int coinRow(int ar[]){
int n=ar.length + 1;
int temp[]= new int[n];
for (int i = 0; i < ar.length;
i++) {//initialize temp array with ar element but from index
1
temp[i + 1] =
ar[i];
}
int[] dp =
new int[n]; //carry result
dp[0] = 0;
//first element 0
dp[1] = temp[1];
//second element is first element of ar
for (int i = 2; i <n; i++) {
int s=temp[i] + dp[i - 2]; //take
sum of present element and two element behind, and prev
element
//of dp
dp[i] = Integer.max(s, dp[i - 1]); //which is greater
will be present result
}
//printing solution array
for (int i = 0; i < n; i++) {
System.out.print(dp[i]+" ");
}
return dp[ar.length]; //last element of
dp will be result
}
public static void main(String[] args) {
int ar[]={
10, 25, 1, 1, 5, 1, 25, 10, 10};
int
res=coinRow(ar);
System.out.println();
System.out.print("Res= ");
System.out.println(res);
}
}
output
0 10 25 25 26 30 30 55 55 65
Res= 65
What does dynamic programming have in common with divide-and-conquer? What is a principal difference between them?...
1) What is dynamic programming? How it is different than divide and conquer solutions? Which of the problems can be solved with dynamic programming?
need help in this algorithm question
Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...
I need this using C++.
In this project, you will implement the dynamic programming-based solution to find the longest common subsequence (LCS) of two sequences. Your inputs will be the two sequences (as Strings) and the outputs are the longest common subsequence (printed as a String) and the final matrix (printed as a two-dimensional array) depicting the length of the longest common subsequences (as shown in the slides) for all possible subsequences of the two input sequences. The two input...
1. Apply the dynamic programming algorithm discussed in class to solve the knapsack problem. (10 points) a. Show the completed table. b. Which items are included in the final configuration of the knapsack? c. What is the maximum value that can fit in the knapsack using a configuration of these items? item 1 2. 3 4 weight 3 2 value $25 $20 $15 1 capacity W = 6. 4 5 $40 $50 5
Suppose you are given an array of n integers ai, az, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for (5, 10, 9, 13, 12, 25, 19, 30) is 5 and one such sub-array is (5, 10, 13, 19, 303. Note you may have multiple solutions for this case. Use dynamic programming to...
Suppose you are given an array of n integers a1, a2, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for {5, 10, 9, 13, 12, 25, 19, 70} is 6 and one such sub-array is {5, 10, 13, 19, 70}. Note you may have multiple solutions for this case. Use dynamic programming to...
READ CAREFULLY AND CODE IN C++
Dynamic Programming: Matrix Chain Multiplication Description In
this assignment you are asked to implement a dynamic programming
algorithm: matrix chain multiplication (chapter 15.2), where the
goal is to find the most computationally efficient matrix order
when multiplying an arbitrary number of matrices in a row. You can
assume that the entire input will be given as integers that can be
stored using the standard C++ int type and that matrix sizes will
be at...
Need help with a few programming exercises. The language being used for all of them is JAVA. 1) exercises - branching (language is JAVA) • Write a method whatToWear(int temp) that takes a temperature and then outputs a string for what to wear in different weather conditions. There must be at least 3 categories. For example, whatToWear(90) might return “shorts” and whatToWear(30) might return “down coat” 2) Enum exercise • Define an enum Seasons, and rewrite the whatToWear method to...
Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...
-------------------------------------------------------------------- 1. How does Java support the concept of encapsulation? -------------------------------------------------------------------- 2. Describe the difference between an object and a class. -------------------------------------------------------------------- 3. What is the difference between the contents of a Java variable of a primitive type and a Java variable of a class type? -------------------------------------------------------------------- 4. (a) How is a 'static' class method different from a regular (non-static) class method? (b) How is a 'static' variable in a class different from a regular (instance) variable in a class?...