Algo Assignment 6
Algo Assignment 6
ASSIGNMENT 6
Reinforcements:
ANS R 4.1 In order to plot these functions on a logarithmic scale for the x and y
axes, we will use the coordinates (log(n), log(f(n)) to create markers. The
functions and corresponding transformations are as follows:
1. f(n)=8n
2. f(n)=4nlogn
3. f(n)=2n2
4. f(n)=n3
5. f(n)=2n
For each function, we'll calculate y=logf(n) and x=log n and then plot the points
on the graph.
Let's start:
f(n)=8n
y=log(8n)=log8+logn
nf(n)=4nlogn
y=log(4nlogn)=log4+logn+log(logn)
(n)=2n2
y=log(2n2)=2logn+log2
f(n)=n3
y=log(n3)=3logn
f(n)=2n
y=log(2n) = n
ANS R 4.3 We must locate the point at which the function representing
algorithm A, 40n2, is less than the function representing algorithm B, 2n3, to
ascertain the value of n0 such that algorithm A is superior to algorithm B for
n≥n0.
40n2<2n3
Since n2 is always positive for n>0, we can first simplify this inequality by
dividing both sides by 2n2:
20 < n
This transformation shows that a straight line with slope cc will appear on a
log-log scale when f(n)=nc is shown.
Now think of any situation besides the worst. In this circumstance, algorithm
A's running time may be less than or equal to its worst-case duration. Thus, for
any sufficiently big n, the running time of algorithm A in this case is bounded
above by c′⋅f(n), satisfying the definition of O(f(n)).
We can conclude that statement (a) and statement (b) are equivalent since
both directions have been demonstrated.
Example 1
This function iterates through each element in the array once to determine
the sum of all of its elements. O(n), where n is the array's length, is the
temporal complexity.
The example1 method iterates through each element once to determine the
total of all the integers in the provided array. As a result, the method's time
complexity directly correlates with the input array's size.
The temporal complexity of this approach is expressed in Big O notation as
O(n), where n is the length of the input array. This indicates that the example 1
method's running time increases linearly with the input array's size.
The sum of the array's prefix sums is determined by this function. With a
nested loop structure, the inner loop iterates from 0 to j, where j is the outer
loop index, and the outer loop iterates from 0 to n−1. O(n2) is the resulting
temporal complexity.
The outer loop iterates through each element of the array once, while the
inner loop performs a constant amount of work proportionate to the index j
for each iteration. As a result, the sum of the first n positive numbers
determines how many operations this technique will complete in total.
The formula n(n+1)/2 gives the sum of the first n positive integers.
Creativity
ANS C 4.37 We need to find a function that does not increase asymptotically at
the same rate as n, nor does it grow asymptotically faster or slower than n in
order to find a function f(n) that is neither O(n) nor Ω(n).
Problem
import java.util.Random;
startTime = System.nanoTime();
example1(arr);
endTime = System.nanoTime();
startTime = System.nanoTime();
example2(arr);
endTime = System.nanoTime();
startTime = System.nanoTime();
example3(arr);
endTime = System.nanoTime();
startTime = System.nanoTime();
example4(arr);
endTime = System.nanoTime();
startTime = System.nanoTime();
example5(arr, arr2);
endTime = System.nanoTime();
arr[i] = random.nextInt(100);
return arr;