I need help with calculating T(n) and O(n) for this algorithm. It would be great if you can calculate it step by step. Thank you!!!
sequence longest_nonincreasing_powerset(const sequence& A) {
std::vector H(n, 0);
auto max = *std::max_element(H.begin(), H.end()) + 1;
std::vector<int> R(max);
const size_t n = A.size();
sequence best;
std::vector stack(n+1, 0);
size_t k = 0;
while (true) {
if (stack[k] < n) {
stack[k+1] = stack[k] + 1;
++k;
} else {
stack[k-1]++;
k--;
}
if (k == 0) {
break;
}
sequence candidate;
for (size_t i = 1; i <= k; ++i) {
candidate.push_back(A[stack[i]-1]);
}
if(is_nonincreasing(candidate) == true && candidate.size() > best.size()){
best = candidate;
}
}
return best;
}
//I'm showing you the complexity of the loops and functions in the code
//by which we'll compute the complexity of the function
sequence longest_nonincreasing_powerset(const sequence& A) {
std::vector H(n, 0);
//this will take a complexity of T1(n);
auto max = *std::max_element(H.begin(), H.end()) + 1;
std::vector<int> R(max);
const size_t n = A.size();
sequence best;
std::vector stack(n+1, 0);
size_t k = 0;
//it has a time complexity of T2(n^3)
//because the while loop will run n^2 times untill k again becomes
0.
//and for each iteration we have a loop of t(k) complexity
//considering for worst case and taking k = n
//we get the complexity as T2(n^3).
while (true) {
if (stack[k] < n) {
stack[k+1] = stack[k] + 1;
++k;
} else {
stack[k-1]++;
k--;
}
if (k == 0) {
break;
}
sequence candidate;
//this loops t(k) time complexity.
for (size_t i = 1; i <= k; ++i) {
candidate.push_back(A[stack[i]-1]);
}
if(is_nonincreasing(candidate) == true && candidate.size() > best.size()){
best = candidate;
}
}
return best;
}
--------------------------------------------------------------------------
so, T(n) for this function can be defined as T(n) =
T1(n) + T2(n^3).
and the worst case time complexity is O(n) = max(T(n))
= max(T(n) + T(n^3)) = T(n^3).
So, the O(n) is T(n^3).
I need help with calculating T(n) and O(n) for this algorithm. It would be great if...
I need help with calculating T(n) and O(n) for this algorithm. It would be great if you can calculate it step by step. Thank you!! sequence longest_nonincreasing_powerset(const sequence& A) { std::vector<size_t> H(n, 0); auto max = *std::max_element(H.begin(), H.end()) + 1; std::vector<int> R(max); const size_t n = A.size(); sequence best; std::vector<size_t> stack(n+1, 0); size_t k = 0; while (true) { if (stack[k] < n) { stack[k+1] = stack[k] + 1; ++k; } else { stack[k-1]++; k--; } if (k == 0)...
I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...
Hi guys! I need help for the Data Structure class i need to provide implementation of the following methods: Destructor Add Subtract Multiply Derive (extra credit ) Evaluate (extra credit ) ------------------------------------------------------- This is Main file cpp file #include "polynomial.h" #include <iostream> #include <sstream> using std::cout; using std::cin; using std::endl; using std::stringstream; int main(int argc, char* argv[]){ stringstream buffer1; buffer1.str( "3 -1 2 0 -2.5" ); Polynomial p(3); p.Read(buffer1); cout << p.ToString()...
vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector { public: Vector( int initsize = 0 ) : theSize( initsize ), theCapacity( initsize + SPARE_CAPACITY ) { objects = new T[ theCapacity ]; } Vector( const Vector & rhs ) : theSize( rhs.theSize), theCapacity( rhs.theCapacity ), objects( 0 ) { objects = new T[ theCapacity ]; for( int k = 0; k < theSize; ++k) objects[ k ] = rhs.objects[ k...
Below is my c++ code. For the first section how do I include multiple text files such as file8, file 25, file 50, file 125? Also, for algorithm 1 my clock function does not work. I need it to start the clock before the algorithm then run through it and record the end time. Then find the difference between end time and start time and convert it to milliseconds. The print out the max sum and the run time!!!! Im...
Sometimes when I sit in restaurants waiting on food, I request the children’s menu to play the games. One of my favorites is the word puzzle in which you search for words hidden in a scramble of letters. I began to work out a solution to this game programmatically. That seemed a little simple, so I decided to have you solve the problem of a scramble of integers, finding the sub-row or sub-column which sums to another integer. You will...
This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...
I need help fixing my code. My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...
#include using namespace std; vector split_string(string); // Complete the findMedian function below. int findMedian(vector arr) { } int main() { ofstream fout(getenv("OUTPUT_PATH")); int n; cin >> n; cin.ignore(numeric_limits::max(), '\n'); string arr_temp_temp; getline(cin, arr_temp_temp); vector arr_temp = split_string(arr_temp_temp); vector arr(n); for (int i = 0; i < n; i++) { int arr_item = stoi(arr_temp[i]); arr[i] = arr_item; } int result = findMedian(arr); fout << result << "\n"; fout.close(); return 0; } vector split_string(string input_string) { string::iterator new_end = unique(input_string.begin(), input_string.end(), []...
I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...