The Wayback Machine - https://web.archive.org/web/20240907022723/https://www.geeksforgeeks.org/analysis-of-algorithms-big-theta-notation/
Open In App

Analysis of Algorithms | Big – Θ (Big Theta) Notation

Last Updated : 02 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Basic
38.65%
4.9K

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 are constants c1, c2 > 0 and a natural number n0 such that c1* g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0

Mathematical Representation:

Θ (g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that 0 ≤ c1 * g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0}
Note: Θ(g) is a set

The above definition means, if f(n) is theta of g(n), then the value f(n) is always between c1 * g(n) and c2 * g(n) for large values of n (n ≥ n0). The definition of theta also requires that f(n) must be non-negative for values of n greater than n0. 

Graphical Representation:

Graphical Representation

In simple language, Big – Theta(Θ) notation specifies asymptotic bounds (both upper and lower) for a function f(n) and provides the average time complexity of an algorithm. 

Follow the steps below to find the average time complexity of any program:

  1. Break the program into smaller segments.
  2. Find all types and number of inputs and calculate the number of operations they take to be executed. Make sure that the input cases are equally distributed.
  3. Find the sum of all the calculated values and divide the sum by the total number of inputs let say the function of n obtained is g(n) after removing all the constants, then in Θ notation its represented as Θ(g(n))

Example: Consider an example to find whether a key exists in an array or not using linear search. The idea is to traverse the array and check every element if it is equal to the key or not.

The pseudo-code is as follows:

bool linearSearch(int a[], int n, int key)
{
for (int i = 0; i < n; i++) {
if (a[i] == key)
return true;
}

return false;
}

Below is the implementation of the above approach:

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find whether a key exists in an
// array or not using linear search
bool linearSearch(int a[], int n, int key)
{
    // Traverse the given array, a[]
    for (int i = 0; i < n; i++) {
 
        // Check if a[i] is equal to key
        if (a[i] == key)
            return true;
    }
 
    return false;
}
 
// Driver Code
int main()
{
    // Given Input
    int arr[] = { 2, 3, 4, 10, 40 };
    int x = 10;
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
   
    if (linearSearch(arr, n, x))
        cout << "Element is present in array";
    else
        cout << "Element is not present in array";
 
    return 0;
}

                    

Java

// Java program for the above approach
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find whether a key exists in an
// array or not using linear search
static boolean linearSearch(int a[], int n,
                            int key)
{
     
    // Traverse the given array, a[]
    for(int i = 0; i < n; i++)
    {
         
        // Check if a[i] is equal to key
        if (a[i] == key)
            return true;
    }
    return false;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given Input
    int arr[] = { 2, 3, 4, 10, 40 };
    int x = 10;
    int n = arr.length;
 
    // Function Call
    if (linearSearch(arr, n, x))
        System.out.println("Element is present in array");
    else
        System.out.println("Element is not present in array");
}
}
 
// This code is contributed by avijitmondal1998

                    

Python3

# Python3 program for the above approach
 
# Function to find whether a key exists in an
# array or not using linear search
def linearSearch(a, n, key):
 
    # Traverse the given array, a[]
    for i in range(0, n):
 
        # Check if a[i] is equal to key
        if (a[i] == key):
            return True
     
    return False
 
# Driver Code
 
# Given Input
arr =  2, 3, 4, 10, 40
x = 10
n = len(arr)
 
# Function Call
if (linearSearch(arr, n, x)):
    print("Element is present in array")
else:
    print("Element is not present in array")
     
# This code is contributed by shivanisinghss2110

                    

C#

// C# program for above approach
using System;
 
class GFG{
 
// Function to find whether a key exists in an
// array or not using linear search
static bool linearSearch(int[] a, int n,
                            int key)
{
     
    // Traverse the given array, a[]
    for(int i = 0; i < n; i++)
    {
         
        // Check if a[i] is equal to key
        if (a[i] == key)
            return true;
    }
    return false;
}
 
 
// Driver Code
static void Main()
{
    // Given Input
    int[] arr = { 2, 3, 4, 10, 40 };
    int x = 10;
    int n = arr.Length;
 
    // Function Call
    if (linearSearch(arr, n, x))
        Console.Write("Element is present in array");
    else
        Console.Write("Element is not present in array");
}
}
 
// This code is contributed by sanjoy_62.

                    

Javascript

<script>
// JavaScript program for the above approach
// Function to find whether a key exists in an
// array or not using linear search
function linearSearch(a, n, key)
{
     
    // Traverse the given array, a[]
    for(var i = 0; i < n; i++)
    {
         
        // Check if a[i] is equal to key
        if (a[i] == key)
            return true;
    }
    return false;
}
 
// Driver code
    // Given Input
    var arr = [ 2, 3, 4, 10, 40 ];
    var x = 10;
    var n = arr.length;
 
    // Function Call
    if (linearSearch(arr, n, x))
        document.write("Element is present in array");
    else
        document.write("Element is not present in array");
 
 
// This code is contributed by shivanisinghss2110
</script>

                    

Output
Element is present in array

Time Complexity: O(n)
Auxiliary Space: O(1)

In a linear search problem, let’s assume that all the cases are uniformly distributed (including the case when the key is absent in the array). So, sum all the cases (when the key is present at position 1, 2, 3, ……, n and not present, and divide the sum by n + 1. 

Average case time complexity = \frac{\sum_{i=1}^{n+1}\theta(i)}{n + 1}

⇒ \frac{\theta((n+1)*(n+2)/2)}{n+1}

⇒ \theta(1 + n/2)

⇒ \theta(n)              (constants are removed)

When to use Big – Θ notation: Big – Θ analyzes an algorithm with most precise accuracy since while calculating Big – Θ, a uniform distribution of different type and length of inputs are considered, it provides the average time complexity of an algorithm, which is most precise while analyzing, however in practice sometimes it becomes difficult to find the uniformly distributed set of inputs for an algorithm, in that case, Big-O notation is used which represent the asymptotic upper bound of a function f.

For more details, please refer: Design and Analysis of Algorithms.



Previous Article
Next Article

Similar Reads

Difference between Big O vs Big Theta Θ vs Big Omega Ω Notations
Prerequisite - Asymptotic Notations, Properties of Asymptotic Notations, Analysis of Algorithms1. Big O notation (O): It is defined as upper bound and upper bound on an algorithm is the most amount of time required ( the worst case performance).Big O notation is used to describe the asymptotic upper bound. Mathematically, if f(n) describes the runn
4 min read
Analysis of Algorithms | Big-Omega Ω 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-Omega Notation represented by a Greek letter (Ω). Table of Content What is Big-Omega Ω Notation?Definition of Big-Omega Ω Notation?How to Determine Big-Omega Ω Notation?Example of
9 min read
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
Top 30 Big-O Notation Interview Questions & Answers 2023
Big O notation plays a role, in computer science and software engineering as it helps us analyze the efficiency and performance of algorithms. Whether you're someone preparing for an interview or an employer evaluating a candidates knowledge having an understanding of Big O notation is vital. In this article, we'll explore the 30 interview question
12 min read
Program to Find the value of cos(n&Theta;)
Given a value of cos(?) and a variable [Tex]n [/Tex]. The task is to find the value of cos(n?) using property of trigonometric functions.Note: n &lt;= 15.Examples: Input : cos(?) = 0.5, n = 10 Output : -0.5 Input :cos(?) = 0.5, n = 3 Output : -0.995523 The problem can be solved using De moivre's theorem and Binomial theorem as described below:Using
6 min read
Find a time for which angle between hour and minute hands is given theta
Given angle theta, find a possible time (in hh:mm format) when angle between hour hand and clock hand is theta. If no such time exists then print -1. Examples : Input : theta = 90.0 Output : 3:0 Input : theta = 60.0 Output : 2:0 We have discussed how to find angle for given time in below post.Calculate the angle between hour hand and minute hand In
6 min read
Maximize big when both big and small can be exchanged
Given N Big Candies and M Small Candies. One Big Candy can be bought by paying X small candies. Alternatively, one big candy can be sold for Y small candies. The task is to find the maximum number of big candies that can be bought.Examples: Input: N = 3, M = 10, X = 4, Y = 2 Output: 5 8 small candies are exchanged for 2 big candies.Input: N = 3, M
5 min read
Exponential notation of a decimal number
Given a positive decimal number, find the simple exponential notation (x = a·10^b) of the given number. Examples: Input : 100.0 Output : 1E2 Explanation: The exponential notation of 100.0 is 1E2. Input :19 Output :1.9E1 Explanation: The exponential notation of 16 is 1.6E1. Approach: The simplest way is to find the position of the first non zero dig
5 min read
Convert Infix To Prefix Notation
Given an infix expression, the task is to convert it to a prefix expression. Infix Expression: The expression of type a 'operator' b (a+b, where + is an operator) i.e., when the operator is between two operands. Prefix Expression: The expression of type 'operator' a b (+ab where + is an operator) i.e., when the operator is placed before the operand
12 min read
Program to convert Infix notation to Expression Tree
Given a string representing infix notation. The task is to convert it to an expression tree.Expression Tree is a binary tree where the operands are represented by leaf nodes and operators are represented by intermediate nodes. No node can have a single child. Construction of Expression tree The algorithm follows a combination of shunting yard along
12 min read
Knuth's Up-Arrow Notation For Exponentiation
Knuth's up-arrow notation, also known as Knuth's arrow notation, is a mathematical notation for exponentiation that was introduced by Donald Knuth in his book "Concrete Mathematics". It uses a sequence of up-arrows (↑) to represent exponentiation with various bases and exponents. Approach: Exponentiation depends on the number of arrows used in the
4 min read
Print all words matching a pattern in CamelCase Notation Dictionary
Given a dictionary of words where each word follows CamelCase notation, The task is to print all words in the dictionary that match with a given pattern consisting of uppercase characters only. Note: CamelCase is the practice of writing compound words or phrases such that each word or abbreviation begins with a capital letter. Common examples inclu
15+ min read
A shorthand array notation in C for repeated values
In C, when there are many repeated values, we can use a shorthand array notation to define array. Below program demonstrates same. // C program to demonstrate working of shorthand // array rotation. #include &lt;stdio.h&gt; int main() { // This line is same as // int array[10] = {1, 1, 1, 1, 0, 0, 2, 2, 2, 2}; int array[10] = {[0 ... 3]1, [6 ... 9]
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
Algorithms Sample Questions | Set 3 | Time Order Analysis
Question 1: What is the asymptotic boundary of T(n)? [Tex] T(n) = \sum_{i=2}^{n} log_{i}n = log_{2}n + log_{3}n + \ldots + log_{n}n [/Tex] &theta;( n*log(n) ) &theta;( n2 ) &theta;( n ) &theta;( n*log2(n) ) &theta;( 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
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
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. Table of Content W
2 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
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
Sample Practice Problems on Complexity Analysis of Algorithms
Prerequisite: Asymptotic Analysis, Worst, Average and Best Cases, Asymptotic Notations, Analysis of loops. Problem 1: Find the complexity of the below recurrence: { 3T(n-1), if n&gt;0,T(n) = { 1, otherwise Solution: Let us solve using substitution. T(n) = 3T(n-1) = 3(3T(n-2)) = 32T(n-2) = 33T(n-3) ... ... = 3nT(n-n) = 3nT(0) = 3n This clearly shows
15 min read
Types of Asymptotic Notations in Complexity Analysis of Algorithms
We have discussed Asymptotic Analysis, and Worst, Average, and Best Cases of Algorithms. The main idea of asymptotic analysis is to have a measure of the efficiency of algorithms that don't depend on machine-specific constants and don't require algorithms to be implemented and time taken by programs to be compared. Asymptotic notations are mathemat
8 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
Divide a big number into two parts that differ by k
Given a big positive number N. The task is divide N into two number 'A' and 'B' such that difference between them is K (1 &lt;= K &lt;= 10100) i.e A - B = K. Examples: Input : N = 10, K = 2 Output : A = 6 B = 4 Input : N = 20, K = 4 Output : A = 12 B = 8 Let the two required number be 'A' and 'B'. So, we know sum of 'A' and 'B' will end upto N. So,
15+ min read
The World of Big Data
“No power on earth can stop an idea whose time has come.” – Victor Hugo Big data is one such remarkable idea. In today’s socially active world, data is growing at a tremendous pace of 2.5 quintillion bytes a day roughly that is only set to increase over the coming years. Business giants like Facebook, Google, LinkedIn, Twitter etc. have been amongs
7 min read
Expected Properties of a Big Data System
Prerequisite - Introduction to Big Data, Benefits of Big Data There are various properties which mostly relies on complexity as per their scalability in the big data. As per these properties, Big data system should perform well, efficient, and reasonable as well. Let’s explore these properties step by step. Robustness and error tolerance – As per t
3 min read
6V's of Big Data
In recent years, Big Data was defined by the “3Vs” but now there is “6Vs” of Big Data which are also termed as the characteristics of Big Data as follows: 1. Volume: The name ‘Big Data’ itself is related to a size which is enormous.Volume is a huge amount of data.To determine the value of data, size of data plays a very crucial role. If the volume
3 min read
Proof of Concept on News Aggregator using Big Data Technologies
Big Data is a huge dataset that can have a high volume of data, velocity, and variety of data. For example, billions of users searching on Google at the same time and that will be a very large dataset. In this, we will discuss Proof of concept(POC) on news aggregator using Big Data (Hadoop, hive, pig). And will perform operations based on MapReduce
3 min read
Practice Tags :