FAQ Database Discussion Community

## How would I go about analyzing the formal complexity of this algorithm under Big Theta Θ notation?

java,algorithm,complexity-theory
I’m needing some help understanding algorithm analysis using Big Theta Θ notation: Big O is the most commonly used asymptotic notation for comparing functions, although in many cases Big O may be replaced with Big Theta Θ for asymptotically tighter bounds. How would I analyze this algorithm step by step?...

## Polynomial time reduction from A to B - B is at least as hard as A

complexity-theory,time-complexity
Many times I've heard that if we can reduce problem A to problem B in polynomial time, then the problem B is at least as hard as problem A. How precise is this statement? I believe we should understand it in this way: if A can be poly-time reduced to...

## How can I reason about big O for various functions?

algorithm,time-complexity,complexity-theory
Consider the following functions: f(n) = 2^n g(n) = n! h(n) = n^logn Which of the following statements about the asymptotic behavior of f(n), g(n), and h(n) is true? (A) f(n) = O(g(n)); g(n) = O(h(n)) (B) f(n) = \Omega(g(n)); g(n) = O(h(n)) (C) g(n) = O(f(n)); h(n) = O(f(n))...

## Big O notation for the complexity function of the fourth root of n

algorithm,math,big-o,time-complexity,complexity-theory
I am expected to find the Big O notation for the following complexity function: f(n) = n^(1/4). I have come up with a few possible answers. The more accurate answer would seem to be O(n^1/4). However, since it contains a root, it isn't a polynomial, and I've never seen this...

## complexity of simple algorithm

java,algorithm,complexity-theory,code-complexity
I have the following algorithm but I dont know its' complexity. Could someone help me? Input size is n. int x = n; while (x > 0) { System.out.println("Value is" + x); x = x/5; } Thanks a lot!...

## How can I tell how many times these nested statements will execute?

algorithm,sorting,big-o,time-complexity,complexity-theory
I have the following pseudocode: SelectionSort(A) n = A.length for j=1 to n-1 smallest = j for i=(j+1) to n if A[i] < A[smallest] smallest = i exchange A[j] with A[smallest] I think that the first for-loop test will execute n times, and the nested for-loop will execute 1 +...

## Determination of computational complexity of sample code

complexity-theory
I give you three short codes: First code: procedure Proc (n:integer) begin if n>0 then begin writeln('x') Proc(n-2) writeln('*'); Proc(n-2) end end Second code: procedure Proc (n:integer) begin if n>0 then begin writeln('*'); Proc(n-1) end end Third code: procedure Proc (n:integer) begin if n>0 then begin writeln('x') Proc(n/2) writeln('*'); Proc(n/2)...

## What is the time complexity of the given algorthm?

algorithm,time-complexity,complexity-theory,asymptotic-complexity,big-theta
x=0 for i=1 to ceiling(log(n)) for j=1 to i for k=1 to 10 x=x+1 I've included the answer I've come up with here: I think the time complexity is θ(n^2 log(n)), but I am not sure my logic is correct. I would really appreciate any help understanding how to do...

## Would this algorithm run in O(n)?

algorithm,big-o,time-complexity,complexity-theory,asymptotic-complexity
Note: This is problem 4.3 from Cracking the Coding Interview 5th Edition Problem:Given a sorted(increasing order) array, write an algorithm to create a binary search tree with minimal height Here is my algorithm, written in Java to do this problem public static IntTreeNode createBST(int[] array) { return createBST(array, 0, array.length-1);...

## Determining the big-O runtimes of loop with inner loop repeat time is const

algorithm,math,big-o,time-complexity,complexity-theory
I have a function for(int i=0;i<n; i++) { b[i]=0; for(int j=0;j<5;j++) { b[i]=a[j+i] } } I need to calculate big-O of above function. My answers is: Inner loop run 5n time => O(n). So the total complexity is O(n). I think I have a mistake in my calculations, but I...

## Binary search tree intersection

algorithm,tree,binary-search-tree,complexity-theory
I have 2 binary search trees T1 and T2 with same number of nodes n >= 1. For each node P we have LEFT(P) and RIGHT(P) for links between nodes and KEY(P) for value off the node. The root of T1 is R1 and root of T2 is R2. I...

## Using Master theorem to calculate asymptotic time complexity of algorithm

algorithm,time,complexity-theory,master,theorem
Problem: You have an algorithm that divides n size problem to six subproblems with size of one quarter of the original. For dividing the algorithm makes 100 steps and for merging 75n. What's the time asymptotic complexity of an algorithm? So the formula for Master Theorem and for this problem...

## How to get Omega(n)

algorithm,big-o,complexity-theory,big-theta
I have the formula a(n) = n * a(n-1) +1 ; a(0) = 0 How can i get the Omega, Theta or O Notation from this without the Master Theorem or did anyone have a good site to understand the explanation...

## Calculating complexity of nested loops

big-o,complexity-theory,time-complexity
I know that every for loop is a O(log₂n), but I am not sure what 3 of them together would make? O(3⋅log₂n)? Thank you guys. for (int i = n; i > 0; i = i / 2) { for (int j = n; j > 0; j = j...

## What is the complexity of function f(n) with n=f(n).log(f(n))

algorithm,math,big-o,complexity-theory,logarithm
What is the complexity of function f(n),preferably the Big-O notation, and f(n) satisfies the condition n = f(n).log(f(n)) ,f(n) > 1 .Let assume that log in base 2. I tried to isolate f(n) from the condition but could not get it done. After using excel to get the graph of...

## Probability mass of summing two discrete random variables, in linearithmic time

arrays,algorithm,complexity-theory,probability-density
Given two discrete random variables, their (arbitrary) probability mass functions a and b and a natural-number N such that both of the variables have the domain [0..N] (therefore the functions can be represented as arrays), the probability that the functions' corresponding random variables have a given sum (i.e. P(A+B==target)) can...

## Asymptotic analysis of functions

algorithm,time-complexity,complexity-theory,asymptotic-complexity
I have the following function to prove that its time complexity is less or equal to O(xlogx) f(x) =xlogx+3logx2 I need some help to solve this....

## Why O(2^log(n)) = O(n) and it is not a exponential run time?

math,time-complexity,complexity-theory
Why is O(2^log(n)) equivalent to O(n)? Also why is this considered as an exponential run time and not a polynomial run time?

## Running time of a program on deterministic and non-deterministic Turing machine

complexity-theory,time-complexity,turing-machines
I've found the following statement: If a program P for Non Deterministic Turing Machine solves a decision problem in time limited by a polynomial p(S), where S-size of input, then it can be run on a Deterministic Turing Machine, and the solution will be found in time limited in time...

## P^N Combinaisons with Integers (Kernel), how to generate them?

java,math,complexity-theory,mathematical-optimization,algebra
My question is almost the same that the one I asked few months ago : 2^N Combinaisons with Integers (Kernel), how to generate them? Basically, I wanted to have the 2^N combinaisons inside a Kernel, but I generalized my version, and now it's even more complicated : I don't want...

## Simple Algorithm complexity

java,algorithm,complexity-theory,code-complexity
I have an algorithm and I need help finding the complexity of it (tightest possible upper bound) for(int i = 0; i < n/2; i++) for(int j = 0; j < n/4; j++) for(int k = 0; k < n; k++) x++; My analysis is that if n would not...

## Is finding a subset with exact cut with other given subsets NP-hard?

algorithm,complexity-theory,combinatorics,computation-theory
I am trying to figure out whether the following problem is NP-hard: Given G_1,..,G_n subsets of {1..m} c_1,..,c_n non-negative integers in {0..m} Find T subset of {1..m} S.T. for all i=1..n, T intersects G_i in exactly c_i elements I tried to find reductions to NP problems such as coloring, or...

## Time Complexity of Dependent for loop

algorithm,loops,time-complexity,complexity-theory,probability
string str = "abcdefghijklmnopqrstuvwxyz"; int sum = 0; for(int i=0; i<str.length; i++) { int val = str[i]; while(val > 0) { sum = val % 62; val = val / 62; } } I know that parent loop executes n+1 time and the child loop executes (val)^(1/62) times. For parent...

## Finding reachable vertices for every vertex in a directed graph

algorithm,complexity-theory,theory,graph-theory
I know that brute force approach to do this is perform DFS on all the vertices of the graph.So for this algorithm the complexity would be O(V|V+E|). But is there more efficient way to do this?

## A little help finding the complexity of time and and complexity of space

complexity-theory
int f2(int n) { int x, y, z = 0, i; for(x = n, i = 0; i < n; i ++, x *= n) { y = x; while (y > 1) { y /= 3; z += y; } } return z; } I get confused by the...

## Finding the complexity of a function

python,max,time-complexity,complexity-theory
I am trying to calculate the time complexity of the next function, max_list11, which finds a maximum of a list recursively: def max11(L,left,right): if left==right: return L[left] return max(L[left], max11(L,left+1,right)) def max_list11(L): return max11(L,0,len(L)-1) From what I found out, the time complexity should be O(n), since what the function does...

## Space complexity of printing all paths from root to leaf

java,algorithm,binary-tree,complexity-theory,space-complexity
What is the space complexity of storing a path ie. nodes of a binary tree from root to a particular leaf in an array? Basically, I am looking for space complexity of the following algorithm: public void printPath () { doPrint(root, new ArrayList<TreeNode>()); } private void doPrint(TreeNode node, List<TreeNode> path)...

## How can i find a running time of a recurrence relation?

recursion,big-o,complexity-theory
The running time for this recurrence relation is O(nlogn). Since I am new to algorithm how would I show that mathematically? T(n) = 2⋅T(n/2) + O(n) T(n) = 2 ( 2⋅T(n/4) + O(n) ) + O(n) // since T(n/2) = 2⋅T(n/4) + O(n) So far I can see that if...

## Best and worst case - Time compexity

algorithm,time,complexity-theory
I have to find the Best and worst case of an algorithm, but I don't understand the results: int chOne=1; for (int i=0; i<list.lenght; i++) if(list[i]<list[chOne]){ chOne=i; } return chOne; BC:2c+2c(n-1)=2cn WC:2c+3c(n-1)=3cn-c I don't know what is the "n-1"; and following other similar exercises, it would be (I think) BC:5c...

## What is the run-time complexity of this Code?

c++,algorithm,complexity-theory,analysis
What is the run-time complexity of this Code? #include <cstdio> #include <cstring> int main() { int n, i, l, j, c, ans = 25; scanf("%d", &n); char x, y; scanf("%s", &x); l = strlen(x); for(i=1; i<n; i++) { scanf("%s", &y); c = 0; for(j=0 ;j<l; j++) if(x[j] == y[j]) c++;...

## Find nearest object in 2D - can it be optimised below O(n)?

algorithm,complexity-theory
Imagine 2D plane with n objects. Pick any point on the plane and find which object is the nearest. The obvious solution is to calculate the distance from selected point to all n objects and choose the shortest distance but isn't there more optimal algorithm? Something like a binary tree,...

## Why do we ignore co-efficients in Big O notation?

algorithm,big-o,time-complexity,complexity-theory
While searching for an answer of Big O Notation I have googled a lot and have also seen many SO answers like link 1, link 2, link 3 and many more, but still I have not clearly understood some points. One of them is why do we ignore / remove...

## Calculating the complexity of an algorithm with 3 loops

algorithm,time-complexity,complexity-theory
I tried to solve the following exercise : What is the order of growth of the worst case running time of the following code fragment as a function of N? int sum = 0; for (int i = 1; i <= N; i++) for (int j = 1; j <=...

## O notation proof for exponents and power

algorithm,big-o,time-complexity,complexity-theory,exponential
I am trying to prove that 4^n is not in the order of O(2^n). Is this a valid method ? 4^n >= c*2^n => 4^n/2^n >= c => 2^n >= c I got lost here......

## Binary search - worst/avg case

search,big-o,time-complexity,complexity-theory,binary-search
I'm finding it difficult to understand why/how the worst and average case for searching for a key in an array/list using binary search is O(log(n)). log(1,000,000) is only 6. log(1,000,000,000) is only 9 - I get that, but I don't understand the explanation. If one did not test it, how...

## What is the time complexity of the following code?

java,time,complexity-theory
/* * Program to group anagrams from the string array input */ import java.util.*; public class StringArrayAnagrams { //function to group the anagrams together public static void groupAnagrams(String[] inputArray) { Hashtable<String, ArrayList<String>> store = new Hashtable<String, ArrayList<String>>(); //to store the sorted string as keys and words from input array that...

## time complexity of non-inplace binary search

big-o,time-complexity,complexity-theory,binary-search
Assuming that binary search is called upon a subarray of approximately length n/2 and that there are at most three comparions at a level I came up with T(n) = T(n/2) + 3 as a recurrence relation. But: When I call binarySearch recursively isn't the cost of the slice proportional...

## How to calculate the T(N) for this primes finder algorithm

algorithm,big-o,complexity-theory
This algorithm find all prime numbers below N var f = function(n){ var primes = ; //1 var flag; //1 for(var i=3; i<n; i+=2){ // ( from 3 to n-1 ) / 2 flag = true; //1 var startI = 0; // 1 for(var y=primes[startI]; y<=Math.sqrt(i); y=primes[++startI]){ // ??? if(i%y...

## Complexity of Lists in C# [closed]

c#,arrays,list,arraylist,complexity-theory
Is a List in C# equal to that of an ArrayList in terms of time complexity? As it is my understanding that they're both similar in how they function, being dynamic arrays?...

## Exponential growth doubling processor speed

algorithm,big-o,complexity-theory,exponential
According to Wikipedia article on Exponential growth E.g. if a slow processor can solve problems of size x in time t, then a processor twice as fast could only solve problems of size x+constant in the same time t. My question is, how do we calculate the constant value that...

## Complexity classes examples

time-complexity,complexity-theory
I wanted to know if my answers are indeed correct for the following statements: 3(n^3) + 5(n^2) + 25n + 10 = BigOmega(n^3) -> T ->Grows at a rate equals or slower 3(n^3) + 5(n^2) + 25n + 10 = Theta(n^3) -> T -Grows at a rate exactly equals 3(n^3)...

## Why does concatenation of lists take O(n)?

haskell,functional-programming,complexity-theory,algebraic-data-types
According to the theory of ADTs (Algebraic Data Types) the concatenation of two lists has to take O(n) where n is the length of the first list. You, basically, have to recursively iterate through the first list until you find the end. From a different point of view, one can...

## BIG O In absence of enough information

performance,algorithm,big-o,complexity-theory,growth-rate
So, lets say you have a function, X(N) that is a total black box. You don't know the growth rate of the function, you can't look it up, and you can't view the source (at the moment). Next lets examine it in the context of another function for(int i =...

## What is the complexity of this piece of code

c,complexity-theory
I had to determinate big O complexity of this piece of code. I thought the answer is nlogn but apparently its n. Can anyone help explain why that is so? void funct(int n) { for (int i = n; i > 0; i /= 2) for(int j = 0; j...

## Why is only one of the given statements about complexity classes correct? [closed]

algorithm,big-o,time-complexity,complexity-theory
Apparently the correct answer to the following question is (C), but why are the other options not correct until we know the value of n? If n=1, all of these seem correct except (B)! Where am I going wrong? Which of the following is not O(n2)? (A) 1510 * n...

## Why is the cylcomatic complexity of this function 12?

c#,complexity-theory,cyclomatic-complexity
I have a (C#) function that checks four sets of conditions and returns a bool. If any of them are true, it returns true. I'm sure I could simplify the logic but I want it to be fairly readable. The CodeMaid extension in Visual Studios and tells me the cylomatic...

## How many times is x=x+1 executed in theta notation in terms of n?

algorithm,language-agnostic,time-complexity,complexity-theory,analysis
I'm taking Data Analysis and Algorithms in the Summer. The question: Find a Θ-notation in terms of n for the number of times the statement x = x + 1 is executed. for i = 1 to 526 for j = 1 to n^2(lgn)^3 for k = 1 to n...

## Finding complexities of space and time using recursion

algorithm,complexity-theory
first function:- void strange (int n,int k) { int i; if(k > n) return; for(i=k; i<n; i++) printf("?"); strange(n, k+2); return; } second function:- void weird(int n, int k) { int i; if(n <= 0 || (k <= 0)) return; k *=2; for(i=0; i < k; i++){ printf("?"); weird(n/2, -n/2...

## Dropping non-constants in Algorithm Complexity

algorithm,graph,big-o,complexity-theory,bellman-ford
So, basically I'm implementing an algorithm to calculate distances from one source node to every other node in a weighted graph, and if a node is in a negative cycle, it detects and marks that node as such. My question regards the total time complexity of my algorithm. Assume V...

## What is an Approximation Factor?

time-complexity,complexity-theory
How is an approximation Factor different than time-complexity? I have heard, for example, of polynomial algorithms with exponential factors, what does that mean? Does that mean it is not technically in polynomial time?

## Searching for an element in log(n) time

algorithm,sorting,complexity-theory
I came across the following question: Suppose I modify a given sorted list of 4n distinct numbers as follows: Keep elements in even positions (positions 2, 4, 6, ... 4n) as they are. Form n disjoint pairs (i,j) on the odd numbered positions where i = 2k+1 for some k...

## Calculating the Recurrence Relation T(n)=T(n / log n) + Θ(1)

algorithm,recursion,big-o,complexity-theory,recurrence
The question comes from Introduction to Algorithms 3rd Edition, P63, Problem 3-6, where it's introduced as Iterated functions. I rewrite it as follows: int T(int n){ for(int count = 0; n > 2 ; ++count) { n = n/log₂(n); } return count; } Then give as tight a bound as...