● It is also known as time memory tradeoff.
● It is a way of solving problems in less time by using more storage space.
● It can be used with the problem of data storage.
● For example: time complexity of merge sort is O(n log n)in all cases. But requires an auxiliary array.
● In quick sort complexity is O(n log n) for best and average case but it doesn’t require auxiliary space.
● Two Space-for-Time Algorithm Varieties:● Input enhancement - To store some info, preprocess the input (or its portion) to be used in solving the issue later● counting sorts ● string searching algorithms ● Pre-structuring-preprocessing the input to access its components
T (n) = θ (1) if n=1 2T + θ (n) if n>1
|
T (n) = T + n
|
For T (n) = O (log n)
We must show that for some constant c, for some constant c, T (n) ≤c logn.
Place this in the Recurrence Equation given. T (n) ≤c log+ 1 ≤c log+ 1 = c logn-clog2 2+1 ≤c logn for c≥1 Thus T (n) =O logn.
|
T (n) = 4T +n
|
|
T (n) = a T+ f (n) with a≥1 and b≥1 be constant & f(n) be a function and can be interpreted as
On non-negative integers, let T(n) be determined by the recurrence.
T (n) = a T+ f (n)
|
Case1: If f (n) = for some constant ε >0, then it follows that:
T (n) = Θ Case2 : If it is real, this is: for any constant k ≥ 0 F (n) = Θ then it follows that: T (n) = Θ
Case3: If it is true f(n) = Ω for some constant ε >0 and it also true that : a f for some constant c<1 for large value of n ,then : T (n) = Θ((f (n))
|
T (n) = 2
|
Compare the given issue with the problem, T (n) = a T a= 2, b =2, f (n) = n2, logba = log22 =1 Put in all values f (n) = Ω ..... (Eq. 1)
If we enter all of the value in (Eq.1), we will obtain
n2 = Ω(n1+ε) put ε =1, Then equality can be maintained.
n2 = Ω(n1+1) = Ω(n2)
Now we'll verify the second condition as well: 2
If we choose c = 1/2, the truth is: ∀ n ≥1
So it follows: T (n) = Θ ((f (n)) T (n) = Θ(n2)
|
// Return position of largest value in "A" static int largest (int [] A) { int curlarge = 0; // Holds largest element position for (int i=1; i<A.length; i++) // For each element if (A[curlarge] < A[i]) // if A[i] is larger curlarge = i; // remember its position return curlarge; // Return largest position }
|