Design and Analysis of Algorithms

Design and Analysis of Algorithms is a fundamental aspect of computer science that involves creating efficient solutions to computational problems and evaluating their performance. DSA focuses on designing algorithms that effectively address specific challenges and analyzing their efficiency in terms of time and space complexity .

Complete Guide On Complexity Analysis

Table of Content

Basics on Analysis of Algorithms:

Asymptotic Notations:

Some Advance topics:

Complexity Proofs:

Like Article -->

Please Login to comment.

Similar Reads

Asymptotic Notation and Analysis (Based on input size) in Complexity Analysis of Algorithms

Asymptotic Analysis is defined as the big idea that handles the above issues in analyzing algorithms. In Asymptotic Analysis, we evaluate the performance of an algorithm in terms of input size (we don't measure the actual running time). We calculate, how the time (or space) taken by an algorithm increases with the input size. Asymptotic notation is

8 min read Algorithms | Analysis of Algorithms | Question 2

What is the time complexity of fun()? C/C++ Code int fun(int n) < int count = 0; for (int i = 0; i 0; j--) count = count + 1; return count; >(A) Theta (n) (B) Theta (n2) (C) Theta (n*log(n)) (D) Theta (n*(log(n*log(n)))) Answer: (B)Explanation: The time complexity can be calculated by counting the number of times the expression "count = count + 1;

1 min read Algorithms | Analysis of Algorithms | Question 3

The recurrence relation capturing the optimal time of the Tower of Hanoi problem with n discs is. (GATE CS 2012) (A) T(n) = 2T(n – 2) + 2 (B) T(n) = 2T(n – 1) + n (C) T(n) = 2T(n/2) + 1 (D) T(n) = 2T(n – 1) + 1 Answer: (D)Explanation: Following are the steps to follow to solve Tower of Hanoi problem recursively. Let the three pegs be A, B and C. Th

1 min read Algorithms | Analysis of Algorithms | Question 4

O( n2 ) is the worst case time complexity, so among the given options it can represent :- (A) O( n ) (B) O( 1 ) (C) O ( nlogn ) (D) All of the above Answer: (D) Explanation: O( n2 ) is the worst case time complexity, so, if the time complexity is O( n2 ), they all can be represented by it. Quiz of this Question Please comment below if you find anyt

1 min read Algorithms | Analysis of Algorithms | Question 5

Which of the following is not O(n2)? (A) (15) * n2 (B) n1.98 (C) n3/(sqrt(n)) (D) (20) * n2 Answer: (C) Explanation: The order of growth of option c is n2.5 which is higher than n2. Quiz of this Question Please comment below if you find anything wrong in the above post

1 min read Algorithms | Analysis of Algorithms | Question 19

Which of the given options provides the increasing order of asymptotic complexity of functions f1, f2, f3, and f4? f1(n) = 2n f2(n) = n(3/2) f3(n) = n*log(n) f4(n) = nlog(n) (A) f3, f2, f4, f1 (B) f3, f2, f1, f4 (C) f2, f3, f1, f4 (D) f2, f3, f4, f1 Answer: (A)Explanation: f1(n) = 2^n f2(n) = n^(3/2) f3(n) = n*log(n) f4(n) = n^log(n) Except for f3,

1 min read Algorithms | Analysis of Algorithms | Question 19

Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let n = D1D2…Dm int n, rev; rev = 0; while (n > 0) < rev = rev*10 + n%10; n = n/10; >The loop invariant condition at the end of the ith iteration is: (GATE CS 2004) (A) n = D1D2….Dm-i and rev = DmDm-1…Dm-i+1 (B) n = Dm-i+1…Dm-1Dm and rev

1 min read Algorithms | Analysis of Algorithms | Question 8

What is the time complexity of the below function? C/C++ Code ) < int i = 0, j = 0; for (; i (A) O(n) (B) O(n2) (C) O(n*log(n)) (D) O(n*log(n)2) Answer: (A)Explanation: At first look, the time complexity seems to be O(n2) due to two loops. But, please note that the variable j is not initialized for each value of variable i. So, the inner loop runs

1 min read Algorithms | Analysis of Algorithms | Question 9

In a competition, four different functions are observed. All the functions use a single for loop and within the for loop, same set of statements are executed. Consider the following for loops: C/C++ Code A) for(i = 0; i If n is the size of input(positive), which function is most efficient(if the task to be performed is not an issue)? (A) A (B) B (C

1 min read Algorithms | Analysis of Algorithms | Question 10

The following statement is valid. log(n!) = [Tex]\\theta [/Tex](n log n). (A) True (B) False Answer: (A)Explanation: Order of growth of \\log n! and n\\log n is the same for large values of , i.e., \\theta (\\log n!) = \\theta (n\\log n) . So time complexity of fun() is \\theta (n\\log n) . The expression \\theta (\\log n!) = \\theta (n\\log n) can

1 min read Algorithms | Analysis of Algorithms | Question 11

What does it mean when we say that an algorithm X is asymptotically more efficient than Y? (A) X will be a better choice for all inputs (B) X will be a better choice for all inputs except possibly small inputs (C) X will be a better choice for all inputs except possibly large inputs (D) Y will be a better choice for small inputs Answer: (B)Explanat

2 min read Algorithms | Analysis of Algorithms | Question 12

What is the time complexity of Floyd–Warshall algorithm to calculate all pair shortest path in a graph with n vertices? (A) O(n2log(n)) (B) Theta(n2log(n)) (C) Theta(n4) (D) Theta(n3) Answer: (D)Explanation: Floyd–Warshall algorithm uses three nested loops to calculate all pairs shortest path. So, the time complexity is Theta(n3).Please read here f

1 min read Algorithms | Analysis of Algorithms | Question 13

Consider the following functions: f(n) = 2n g(n) = n! h(n) = nlog(n) 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) = [Tex]\\Omega [/Tex](g(n)); g(n) = O(h(n))(C) g(n) = O(f(n)); h(n) = O(f(n))(D) h(n) = O(f(n)); g(n) = [Tex]\\Omega [/Tex](f(n)) (A) A (B) B

1 min read Algorithms | Analysis of Algorithms | Question 15

Consider the following functions f(n) = 3n^>g(n) = 2^<\log_<2>n>>h(n) = n! Which of the following is true? (GATE CS 2000)(A) h(n) is 0(f(n))(B) h(n) is 0(g(n))(C) g(n) is not 0(f(n))(D) f(n) is 0(g(n)) (A) A (B) B (C) C (D) D Answer: (D)Explanation: g(n) = 2^[Tex](\\sqrt \\log ) [/Tex]= n^[Tex](\\sqrt) [/Tex]f(n) and g(n) 1 min read Algorithms | Analysis of Algorithms | Question 17

Let s be a sorted array of n integers. Let t(n) denote the time taken for the most efficient algorithm to determined if there are two elements with sum less than 1000 in s. which of the following statements is true? (GATE CS 2000)a) t (n) is 0 (1)b) n < t (n) < n[Tex] [/Tex]c) n log 2 n < t (n) < [Tex] [/Tex]d) t

1 min read Algorithms | Analysis of Algorithms | Question 18

Consider the following function, int unknown(int n) < int i, j, k = 0; for (i = n/2; i <= n; i++) for (j = 2; j <= n; j = j * 2) k = k + n/2; return k; >What is the time complexity of the function? (GATE CS 2013) (A) n^2 (B) n logn (C) n^3 (D) n^3 logn Answer: (B)Explanation: In the below explanation, \'^\' is used to represent exponent: The

1 min read Algorithms | Analysis of Algorithms | Question 19

Consider the following two functions. What are time complexities of the functions? C/C++ Code int fun1(int n) < if (n C/C++ Code int fun2(int n) < if (n (A) O(2^n) for both fun1() and fun2() (B) O(n) for fun1() and O(2^n) for fun2() (C) O(2^n) for fun1() and O(n) for fun2() (D) O(n) for both fun1() and fun2() Answer: (B)Explanation: Time complexity

1 min read Algorithms | Analysis of Algorithms | Question 1

What is time complexity of fun()? C/C++ Code int fun(int n) < int count = 0; for (int i = n; i >0; i /= 2) for (int j = 0; j (A) O(n2) (B) O(n*log(n)) (C) O(n) (D) O(n*log(n*Log(n))) Answer: (B) Explanation: For an input integer n, the outermost loop of fun() is executed log(n) times the innermost statement of fun() is executed following times. n

1 min read Algorithms | Analysis of Algorithms | Question 14

In the following C function, let n >= m. C/C++ Code int gcd(n, m) < if (n % m == 0) return m; n = n % m; return gcd(m, n); >How many recursive calls are made by this function?(A) [Tex]\\theta [/Tex](log(n))(B) [Tex]\\Omega [/Tex](n)(C) [Tex]\\theta [/Tex](log(log(n)))(D) [Tex]\\theta [/Tex](sqrt(n)) (A) A (B) B (C) C (D) D Answer: (A) Explanati

1 min read Algorithms | Analysis of Algorithms | Question 16

Consider the following three claims: I [Tex](n + k)^m = \theta(n^m)[/Tex] where k and m are constants II [Tex]2^ = O(2^n) [/Tex] III 2^ = O(2^n) Which of these claims is correct? (GATE CS 2003) (A) I and II (B) I and III (C) II and III (D) I, II and III Answer: (A) Explanation:(I) (n+m)k = nk + c1*n(k-1) + . km = [Tex]\\theta[/Tex](nk)

1 min read RFM Analysis Analysis Using Python

In this article, we are going to see Recency, Frequency, Monetary value analysis using Python. But first, let us understand the RFM analysis briefly. What is RFM analysis? RFM stands for recency, frequency, monetary value. In business analytics, we often use this concept to divide customers into different segments, like high-value customers, medium

3 min read Factor Analysis | Data Analysis

Factor analysis is a statistical method used to analyze the relationships among a set of observed variables by explaining the correlations or covariances between them in terms of a smaller number of unobserved variables called factors. Table of Content What is Factor Analysis?What does Factor mean in Factor Analysis?How to do Factor Analysis (Facto

13 min read Time and Space Complexity Analysis of Tree Traversal Algorithms

Let us discuss the Time and Space complexity of different Tree Traversal techniques, such as Inorder Traversal, Preorder Traversal, Postorder Traversal, etc. Time Complexity of Tree Traversal AlgorithmsLet us see different corner cases: Complexity function T(n) — for all problems where tree traversal is involved — can be defined as: T(n) = T(k) + T

2 min read Randomized Algorithms | Set 1 (Introduction and Analysis)

What is a Randomized Algorithm? An algorithm that uses random numbers to decide what to do next anywhere in its logic is called a Randomized Algorithm. For example, in Randomized Quick Sort, we use a random number to pick the next pivot (or we randomly shuffle the array). And in Karger's algorithm, we randomly pick an edge. How to analyse Randomize

6 min read Analysis of algorithms | little o and little omega notations

The main idea of asymptotic analysis is to have a measure of the efficiency of algorithms that don’t depend on machine-specific constants, mainly because this analysis doesn’t require algorithms to be implemented and time taken by programs to be compared. We have already discussed Three main asymptotic notations. The following 2 more asymptotic not

4 min read Asymptotic Analysis and comparison of sorting algorithms

It is a well established fact that merge sort runs faster than insertion sort. Using asymptotic analysis. we can prove that merge sort runs in O(nlogn) time and insertion sort takes O(n^2). It is obvious because merge sort uses a divide-and-conquer approach by recursively solving the problems where as insertion sort follows an incremental approach.

15+ min read Worst, Average and Best Case Analysis of Algorithms

In the previous post, we discussed how Asymptotic analysis overcomes the problems of the naive way of analyzing algorithms. But let's take an overview of the asymptotic notation and learn about What is Worst, Average, and Best cases of an algorithm: Popular Notations in Complexity Analysis of Algorithms1. Big-O NotationWe define an algorithm’s wors

14 min read Algorithms Sample Questions | Set 3 | Time Order Analysis

Question 1: What is the asymptotic boundary of T(n)? [Tex] T(n) = \sum_^ log_n = log_n + log_n + \ldots + log_n [/Tex] θ( n*log(n) ) θ( n2 ) θ( n ) θ( n*log2(n) ) θ( n2*log2(n) ) Answer: 3 Explanation: To find appropriate upper and lower boundaries, an approach which first comes to mind is to expand

10 min read Analysis of Algorithms | Big - Θ (Big Theta) Notation

In the analysis of algorithms, asymptotic notations are used to evaluate the performance of an algorithm, in its best cases and worst cases. This article will discuss Big - Theta notations represented by a Greek letter (Θ). Definition: Let g and f be the function from the set of natural numbers to itself. The function f is said to be Θ(g), if there

6 min read How to Analyse Loops for Complexity Analysis of Algorithms

We have discussed Asymptotic Analysis, Worst, Average and Best Cases and Asymptotic Notations in previous posts. In this post, an analysis of iterative programs with simple examples is discussed. The analysis of loops for the complexity analysis of algorithms involves finding the number of operations performed by a loop as a function of the input s

15+ min read Article Tags :