C++ :: Algorithm Has Asymptotic Complexity Recursive?
May 22, 2013
would like to know that my algorithm has asymptotic complexity recursive?
int solve(pair<int,int> actual)
{
if(actual.first == PuntoInicio.first && actual.second == PuntoInicio.second)
return 1;
[Code]....
View 1 Replies
ADVERTISEMENT
Jan 23, 2013
this function has to check if an array is not descending it has to be recursive and it's space complexity has to be O(log(n))
Code:
int is_sorted(int a[ ], int n) {
int check=n-1;
if(n==1 || n==0) return 1;
if(a[check]<a[check-1])return 0;
else return(is_sorted(a,n-1));
}
this is what i came up with. i know it's space complexity is O(n) how do i make it O(log(n))?
View 2 Replies
View Related
Jan 25, 2015
define the time and space complexity of an algorithm and about what can i do when the time and space complexity are given and i should write the code corresponding to the restrictions , which i find very difficult.
View 1 Replies
View Related
Dec 27, 2014
I've been working on a matrix class and I ran into a problem in writing my matrix class. I keep getting 0 as a determinant and with some debugging, I found that I was losing allocated data or something similar. This algorithm I'm pretty sure works because I used this same algorithm in a function I made in python. [URL] .... That's where I found the algorithm.
/*template <class T>
double Matrix<T>::det(T* array, size_t dim, bool recursion)*/
double det(T* array = NULL, size_t dim = 0, bool recursion = false) {
if (recursion == false) {
if (m != n) {
return 0;
[Code] .....
View 2 Replies
View Related
Apr 23, 2015
So I am working on a dual pivot quicksort, I can correctly sort the array the first time around however in the main it is called again on the sorted array and in the function I get a seg fault at line 17 in sort.cpp, for the life of me I cant figure it out as the values are the same when I pass it in the first time. Here is the code:
main1.cpp
#include <iostream>
#include "movement.h"
#include "sort.h"
using namespace std;
int main() {
const int size = 10;
T array[size] = {6, 5, 1, 8, 4, 7, 2, 9, 6, 3};
[Code] ....
View 4 Replies
View Related
May 19, 2013
I have a problem to implement a recursive version of an algorithm that I made, I get different values. Here is the code so Iterative (OK) and Recursive code form that is not OK.
The data sets do not give equal:
The algorithm is given two source and target positions on a board, find the number of paths between them...
Input Example: 5
2 3
4 4
Output Example: 5
Iterative Algorithm ( OK )
Code:
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
int n , dp [1000][1000], x, y, xx, yy;
[Code] ....
View 2 Replies
View Related
Jun 1, 2013
My maze algorithm must be able to count total steps. He is not allowed to "jump" from a deadend back to the cross-way he originally came from.
Code:
int R2D2Turbo::findIt(Labyrinth* incLab, int x, int y){
if ((x == incLab->getExit().x) && (y == incLab->getExit().y))
{
return 1;
[Code] .....
Due to the nature of recursive algoirthms, he jumps instead of moving the way back from the deadend one by one... The only solutions I could think of are way overloaded...
View 3 Replies
View Related
Feb 13, 2013
I have written this code to arrange user input array in an order.
//Recursive Quick Sort
#include<stdio.h>
#define ARRAY_SIZE 10
void quick_sort(int array[], int len)
}
[code].....
View 6 Replies
View Related
Feb 1, 2013
What would the worst, average and best case space complexity be for a data structure of type map<string, vector<int> > in big O notation? I'm parsing through a document and storing each word as a key and im attaching an associated int (in a vector) to it as the value.
View 4 Replies
View Related
May 5, 2012
For the following code :
s = 0 ;
for(i=m ; i<=(2*n-1) ; i+=m) {
if(i<=n+1)
{ s+=(i-1)/2 ; }
else
{ s+=(2*n-i+1)/2 ; }
}
I want to change the complexity of the code from O(n) to O(1) . So I wanted to eliminate the for loop . But as the sum "s" stores values like (i-1)/2 or (2*n-i+1)/2 so eliminating the loop involves tedious calculation of floor value of each (i-1)/2 or (2*n-i+1)/2 . It became very difficult for me to do so as I might have derived the wrong formula in sums of floors . Need Changing complexity from O(n) to O(1). Is there any other way to reduce the complexity ? If yes ... then how ?
View 3 Replies
View Related
Mar 24, 2013
Assuming that we have :
Code:
int arr2d[rows][columns] ; // Not valid syntax of course ... let be arr2d rows * columns size
for(int i=0; i<rows; i++)
for(int j=0; j<columns; j++)
arr2d[rows][columns] = some_value;
What is the complexity? I believe O(n) and not O(n^2) on this case because if you have 3*3 size you would put 9 elements (from 9 elements input of course)... for each input you have one insertion and that is the meaning. Same as 4*4 size 16 input times 16 insertions .. or 5*5 and so forth...
View 8 Replies
View Related
Jul 21, 2014
I have a very simple program the time complexity of the function that I used in this program is O(mn)because it has a nested loop, I really need to reduce the time complexity to O(n)
[code=c++]
#include <iostream.h>
#include<stdlib.h>
int *char_count( const char* DNA, const int *starts, const int *ends, char letter);
int main()
[Code].....
View 5 Replies
View Related
Feb 9, 2015
Giving a dynamic array, we want to pass the array elements from a file. The first number in the file N gives us the array length. They follow N numbers, the actual elements of the array.
Three brothers are going to work in the shop of their father for a time. The shop is doing well and every day generates profit which the three brothers may provide. The brothers agreed that they would divide the total time in three successive parts, not necessarily equal duration, and that each one will be working in the shop during one of these parts and collects the corresponding profit on his behalf. But they want to make a deal fairly, so that one received from three more profit someone else. Specifically, they want to minimize the profit the most favored of the three will receive.
I first created a program that WORKS! with complexity O(n3).
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int sum_array(int* array, int cnt){
int res = 0;
int i;
for ( i = 0; i < cnt ; ++i)
[Code] .....
Let's assume that we have the following input:
10
1 1 8 1 1 3 4 9 5 2
The ouptut should be 15 -> A = 1 + 1 + 8 + 1 + 1 + 3 = 15 , B = 4 + 9 = 13 , C = 5 + 2 = 7.
But the ouptut is 16!
How can I reduce the complexity of my first working! C code?
View 9 Replies
View Related
Sep 2, 2013
I was just going through Radix Sort algorithm. Though I got the logic and the concept used in it, I am still pretty much confused about the space complexity being O(k+n) for this algorithm. (where, k is the max no of digits in a number, and n is the no. of inputs to be sorted).
View 1 Replies
View Related
Feb 14, 2014
Suppose you have a switch statement defined like:
switch (parameter) {
case ONE:
case TWO:
// ... other n-2 cases
case N:
doSomething();
break;
default:
somethingElse();
break;
}
What is the complexity of the doSomething()? Is it still equal to the complexity of doSomething() , or the complexity of doSomething() plus the number of case statements? In other words, does the number of case statements a conditional piece of code has count towards the complexity?
View 10 Replies
View Related
Oct 29, 2014
We were discussing how to find average time complexity of different algorithms. Suppose I've a the following nested loop
for(int i=0;i<n;i++)
{
min = i;
[Code].....
Now the outer loop will iterate N times. the inner loop will always iterate 3 times no matter what the value of N is. so the worst case time complexity should be O(n^2) but what about the average case and the best case? I mean in terms of searching we can figure out and distinguish between the worst,best and average as it depends upon the number of comparisons. But here how can the average and best case be different then the worst case.
View 13 Replies
View Related
Jun 23, 2014
I am having trouble with recursively modifying a <string, int> map (NCPB) -- when I call for what should be existing values in the map, I get "junk" values back (integers, but sometimes negative, large numbers, etc.). I've posted only the problematic function here:
int Count_Noncrossing(string RNA, map<string, int> &NCPB) {
map <string, int>::iterator it;
if (RNA.length() <= 2)//perfect short interval can only have 1 match; return 1 {
return 1;
[Code] ....
The problem is that when I ask for existing map values in a subsequent recursive call, they don't seem to be there. I imagine I'm missing something straightforward but can't seem to find it. I've tried declaring the map globally, passing it (as shown above), nothing seems to work.
View 3 Replies
View Related
Nov 16, 2013
The recursive function is bolded, i got feedback and was told that the static variable made the function seem a lot like a iterative function but he did not say why.
Code:
#define MAX 100
#include <string.h>
#include <stdio.h>
int checkPalindrome(char string[MAX]);
int checkRecPalindrome(char string[MAX]);
[Code] .....
View 7 Replies
View Related
May 13, 2014
cause I cant find why it crashes. It compiles without any error,but crushes when i run it and I can't find where is wrong the code.
Code:
#include <stdio.h>
#include <time.h>
#include <math.h>
[Code].....
View 7 Replies
View Related
Apr 27, 2013
This is simple recursive solution of Fibonacci number:
Code:
int fibo(int n)
{
if(n<=1)
return 1;
else
return fibo(n-1)+fibo(n-2);
}
Now the recursion will generate a large recursion tree, like if n=5, 5 will call (5-1), (5-2) or 4,3 . What I want to know is, will fibo(n-1) will be called 1st go all the way to the base case 1, then do the summation or fibo(n-2) will be called right after fibo(n-1) ?
View 6 Replies
View Related
May 5, 2014
I'm writing a program that starts at a given number n and adds all the way to 2:
n + (n-2) + (n-4) + (n-6) + .....
The following is my code, it compiles but after I enter an integer the program crashes.
Code:
#include<stdio.h>
#include<stdlib.h>
int sum_even(int n);
int sum_even(int n){
if(n==1){
[Code] ....
View 11 Replies
View Related
Nov 9, 2013
the functions checks if the word is a palindrome like"level" "madam" etc. but with input "dfdfdfdffdfd" my recursive function fails.
Code:
/* main.c - Created on: Nov 9, 2013 - Author: Kaj P. Madsen*/
#define MAX 100
#include <string.h>
#include <stdio.h>
int checkPalindrome(char checkString[MAX]);
int checkRecPalindrome(char checkString[MAX], int strLgt, int a);
}
[code]....
results from "dfdfdfdffdfd" added some print to see that its the variables a and strLgt not functioning properly
Code:
dfdfdfdffdfd.
The word is not a palindrome(iterative)
strLgt: 11 a: 0
a: d strLgt: dstrLgt: 10 a: 1
a: f strLgt: fstrLgt: 9 a: 2
a: d strLgt: dstrLgt: 8 a: 3
a: f strLgt: fstrLgt: 7 a: 4
The word is palindrome (recursive)
View 4 Replies
View Related
Jul 10, 2014
map< int, int > cache;
int count( int n ){
if( cache[n] != 0 ){
return cache[n];
[Code] ....
I don't know how to turn this recursive function into an iterative...
View 7 Replies
View Related
Jan 5, 2015
I am trying to make from f_rec (recursive function) to f_iter (iterative function) but I can't.
(My logic was to create a loop to calculate the results of f_rec(n-1), another loop for 2*f_rec(n-2) and one loop for f_rec(n-3);
But I'm wrong)
int f_rec(int n) {
if(n>=3)
return f_rec(n-1)+2*f_rec(n-2)+f_rec(n-3);
[Code] .....
I also think that my run time for the f_rec is 3^n ...
View 2 Replies
View Related
Mar 22, 2014
I am implementing a recursive descent parser that recognizes strings in the language below.
The grammar:
A -> I = E | E
E -> T + E | T - E | T
T -> F * T | F / T | F
F -> P ^ F | P
P -> I | L | UI | UL | (A)
U -> + | - | !
I -> C | CI
C -> a | b | ... | y | z
L -> D | DL
D -> 0 | 1 | ... | 8 | 9
My input file has the following two strings:
a=a+b-c*d
a=a**b++c
The desired output:
String read from file: a=a+b-c*d
The string "a=a+b-c*d" is in the language.
String read from file: a=a**b++c
The string "a=a**b++c" is not in the language.
[Code].....
When I test the code without reading the text file and just write a string in the source code, it appears to parse fine. I believe my main problem is in the int main function and how i am reading the text file and outputting it. I was able to write the same program fine in Java.
View 3 Replies
View Related
Feb 10, 2014
how to recursively modify my program. The problem I'm hacing is the the program is not looping correctly and also not printing the correct number. I've calculated the payoff correctly, also I've only been able to print the first section of R3. I can't figure out how to loop it to get R2 to stay at 2 then go to 3 after all possiblities of R2 at 2. Enventually, R1 will change to 2 then 3; 3 being the highest number earned. To be mentioned that will be three recursive function loopR1, loopR2, and loopR3 for each column.
The result of the program should look like:
R1 R2 R3
1 1 1 payoff is 1
1 1 2 .......... 1
1 1 3 .......... 1
1 2 1 .......... 1
1 2 2 .......... 1
...
...
...
3 3 2 .............. 5
this is what I have so far:
#include <stdio.h>
#include <stdlib.h>
int payOff(int r1, int r2, int r3);
void loopR3(int R3, int upto);
void loopR2(int R2, int upto);
[Code].....
View 12 Replies
View Related