0% found this document useful (0 votes)
3 views

Algo Assignment 6

The document discusses various algorithms and their time complexities, including logarithmic transformations and comparisons between different algorithms. It provides examples of calculating running times and demonstrates the equivalence of certain statements regarding algorithm performance. Additionally, it includes a Java program for experimental analysis of execution times for different methods with varying input sizes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Algo Assignment 6

The document discusses various algorithms and their time complexities, including logarithmic transformations and comparisons between different algorithms. It provides examples of calculating running times and demonstrates the equivalence of certain statements regarding algorithm performance. Additionally, it includes a Java program for experimental analysis of execution times for different methods with varying input sizes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

ALGORITHMS AND COMPUTING THEORY

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.

So, our goal is to address the inequality:

40n2<2n3

Since n2 is always positive for n>0, we can first simplify this inequality by
dividing both sides by 2n2:

20 < n

Thus, when n≥20, algorithm A will perform better than algorithm B.

Consequently, n0=20. Algorithm A will do fewer operations than algorithm B


for n≥20
ANS R 4.5 The x- and y-axes are logarithmic when you plot the function f(n)=nc
on a log-log scale. This indicates that a logarithmic transformation is applied to
both the input n and the output (f(n).

Now let's examine the change:

For the x-axis, x=log(n)

For the y-axis, y=log(f(n))=log(nc)=c⋅log(n)

This transformation shows that a straight line with slope cc will appear on a
log-log scale when f(n)=nc is shown.

This is the reason why:

 Logarithmic Transformation of x and y: The input and output are


converted into logarithmic scales by taking the logarithm of both n and
f(n).
 Logarithmic properties: Logarithmic characteristics state that when you
have log(nc), this simplifies to c⋅log(n).
 Linear Relationship: The transformation shows that log(n) and log(f(n)),
with a slope of c, has a linear relationship.

Put otherwise, f(n)=nc increases exponentially as n increases, but the


connection becomes linear with a slope of c when n and f(n) are translated
logarithmically. For this reason, on a log-log scale, the function n c plot looks
like a straight line with a slope of c.

ANS R 4.7 We must demonstrate both directions in order to demonstrate that


the two statements are equivalent:

1. Statement (b) is true if and only if statement (a) is accurate.


2. Statement (a) is true if and only if statement (b) is accurate.

First, let's discuss the first directive:

1. The following holds true if statement (a) is accurate:


According to statement (a), algorithm A's running time is always O(f(n)). This
indicates that there is a constant cc such that the execution time of algorithm
A is bounded above by c⋅f(n) for all sufficiently big n.

Now imagine the algorithm A worst-case situation. In the worst situation,


algorithm A's running time won't be longer than it is in any other situation.
Thus, for all sufficiently big n, the worst-case running time of algorithm A is
likewise bounded above by c⋅f(n), satisfying the definition of O(f(n)).

Statement (b) is therefore accurate.

Let us now demonstrate the second direction:

1. If statement (b) is true, then statement (a) is true:

According to statement (b), algorithm A's worst-case running time is O(f(n)).


This indicates that for every sufficiently big n, there is a constant c ′ such that c
′.f(n) is the upper bound for the worst-case running time of algorithm A.

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)).

Statement (a) is therefore accurate.

We can conclude that statement (a) and statement (b) are equivalent since
both directions have been demonstrated.

ANS R 4.9 Let's examine each of the instances given:

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.

ANS R 4.11 Example 3:

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.

The temporal complexity of this method is expressed as O(n2) in Big O notation


because the sum of the first n positive integers is quadratic in n. This indicates
that the example3 method's running time increases quadratically with the
input array's size.

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).

A function like this is f(n)=sin⁡(n).

Here's why the criteria are satisfied by f(n)=sin(n):

 As n changes, f(n)=sin(n) oscillates between -1 and 1. Its growth with n is


not a linear one.

 There is no linear function that bounded from below f(n)=sin(n). Sin(n)


can have values above and below k for any positive constant k and
suitably large n.
Hence, neither O(n) nor Ω(n) can be found for f(n)=sin(n).

Problem

ANS P 4.61 To undertake an experimental analysis comparing the relative


running times of the methods offered, we can construct a Java program that
measures the execution time of each method for varied sizes of input arrays.
We'll construct random input arrays for each size and measure the time it
takes for each method to execute.

import java.util.Random;

public class ExperimentalAnalysis {

public static void main(String[] args) {

int[] sizes = {10, 100, 1000, 10000};

for (int size : sizes) {

int[] arr = generateRandomArray(size);

int[] arr2 = generateRandomArray(size);

long startTime, endTime, elapsedTime;

startTime = System.nanoTime();

example1(arr);

endTime = System.nanoTime();

elapsedTime = endTime - startTime;

System.out.println("example1 with size " + size + ": " + elapsedTime + "


nanoseconds")

startTime = System.nanoTime();

example2(arr);

endTime = System.nanoTime();

elapsedTime = endTime - startTime;


System.out.println("example2 with size " + size + ": " + elapsedTime + "
nanoseconds");

startTime = System.nanoTime();

example3(arr);

endTime = System.nanoTime();

elapsedTime = endTime - startTime;

System.out.println("example3 with size " + size + ": " + elapsedTime + "


nanoseconds");

startTime = System.nanoTime();

example4(arr);

endTime = System.nanoTime();

elapsedTime = endTime - startTime;

System.out.println("example4 with size " + size + ": " + elapsedTime + "


nanoseconds");

startTime = System.nanoTime();

example5(arr, arr2);

endTime = System.nanoTime();

elapsedTime = endTime - startTime;

System.out.println("example5 with size " + size + ": " + elapsedTime + "


nanoseconds");

public static int[] generateRandomArray(int size) {

Random random = new Random();

int[] arr = new int[size];


for (int i = 0; i < size; i++) {

arr[i] = random.nextInt(100);

return arr;

You might also like