Give an O(n2 ) algorithm (Pseudocode) that, given a sequence S, finds the longest subsequence that first increases then decreases.
For instance, in the sequence S = [10, 4, 5, 11, 2, 7, 4, 3, 9] the longest such subsequence is [4,5,11,7,4,3]. The subsequence does not have to be consecutive. (Hint: Use two arrays, one for increasing subsequences and the other for decreasing subsequences.)
function longestBitonicSequence( Array arr[], Integer n as size )
{
Integer i, j;
/* Allocate memory for LIS[] and initialize LIS values as 1 for
all indexes */
Array lis[n]
for i = 0 to n do
lis[i] = 1
end loop
/* Compute LIS values from left to right */
for i 1 to n-1 do
for j 0 to i-1 do
if (arr[i] > arr[j] && lis[i] < lis[j] + 1) then
lis[i] = lis[j] + 1
end if
end loop
end loop
/* Allocate memory for lds and initialize LDS values for
all indexes */
array lds[n];
for i = 0 to n do
lds[i] = 1
end loop
/* Compute LDS values from right to left */
for i = n-2 to 0 do
for j = n-1 to i+1 do
if (arr[i] > arr[j] && lds[i] < lds[j] + 1) then
lds[i] = lds[j] + 1
end if
end loop
end loop
/* Return the maximum value of lis[i] + lds[i] - 1*/
max = lis[0] + lds[0] - 1;
for i = 1 to n do
if (lis[i] + lds[i] - 1 > max) then
max = lis[i] + lds[i] - 1
end if
end loop
return max;
}
Give an O(n2 ) algorithm (Pseudocode) that, given a sequence S, finds the longest subsequence that...
Longest common subsequence (LCS) algorithms give a way to decide how similar two given strings are. However, sometimes, we have to filter away some common subsequences that are in some pattern. Here is a problem for you to solve. Given two strings alpha and beta, let gamma to be the longest word satisfying all of the following conditions: gamma is a subsequence of alpha. gamma is a subsequence of beta. gamma does not contain abb. Design an algorithm that finds...
You are given a sequence of integer values. Describe an algorithm requiring no worse than O(n2) time for finding the length of a longest rising subsequence in that array. A subsequence is rising when each element is less than or equal to the one following it. For example, in the sequence [23, -15, 10, 25, 7, 32], the length of a longest rising sequence is 4, found in the subsequence [-15, 10, 25, 32].
Please help with #6
'rove: Given a sequence of n2 +1 distinct integers, either there is an increasing subsequence of n+1 terms or a decreasing subsequence of n +1 terms.
'rove: Given a sequence of n2 +1 distinct integers, either there is an increasing subsequence of n+1 terms or a decreasing subsequence of n +1 terms.
Let's say you are given a sequence of distinct positive numbers. We want to find a subsequence with the maximum possible sum, with the restriction that we are not allowed to take three consecutive elements from the original sequence. For example, for input 1, 6, 5, 2, 7, 9, 3, 4, the subsequence with the maximum possible sum is 6, 5, 7, 9, 4 (we have two pairs of consecutive elements 6, 5 and 7, 9 but not three consecutive...
Create a C++ Header Function with DYNAMIC programing with the following details: longest common subsequence input: a string a of length m and a string b of length n output: the longest string ssuch that s is a subsequence of both a and b; in the case of ties, use the substring that comes first alphabetically The dynamic programming algorithm for subsequences is similar to the one for substrings. Both involve a 2D array of strings, base cases, and a...
2. (25) [Rising trend] Textbook Exercise 17 in Chapter 6. The problem to solve is, in other words, to select from a sequence of n numbers a subset, including the first number, such that the selected numbers make a longest monotonously increasing sequence. In the exercise b, (i) write an optimal substructure (with an explanation),ii) write the iterative dynamic programming algorithm (pseudocode), and (iii) show the running-time analysis. Hints: the algorithm in the exercise a is a greedy algorithm; the...
Question 2: Consider an array of numbers. Give an algorithm that finds the maximum contiguous subarray that is increasing, Example: A = [1, 7, 2, 5, 4, 6, 8, 9, 3, 14, 16, 11, 30]. The maximum contiguous increasing sequence is 4, 6, 8, 9. Please write an explanation of the algorithm in words and state the run time as well.
Question 3: Give an algorithm that finds the maximum size sub-array that is increasing (the entries may not be contiguous. It may be any set but you need to keep the order). In the array, A = [1, 7, 2, 5, 4, 6, 8, 9, 3, 14, 16, 11, 30], the maximum non contiguous increasing subset is 1, 2, 4, 6, 8, 9, 14, 16, 30 that forms an increasing sequence. Please explain the algorithm in words and state the...
Give an algorithm that finds the maximum size subarray that is increas- ing (the entries may not be contiguous. It may be any set but you need to keep the order.). In the above array the maximum non contiguous increasing subset is 1, 2, 4, 6, 8, 9, 14, 16, 30 that forms an increasing sequence. There are no other array provided in this question, thank you
Subject: Algorithm
need this urgent please thank you.
4. Give pseudocode for an algorithm that will solve the following problem. Given an array A[1..n) that contains every number between 1 and n +1 in order, except that one of the numbers is missing. Find the miss sorted ing mber. Your algorithm should run in time (log n). (Hint: Modify Binary search). A pseudocode means an algorithm with if statements and loops, etc. Don't just write a paragraph. Also, if your...