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

Complexity of Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Complexity of Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

2CP02 : Discrete Mathematics

Complexity of
Algorithms

Faculty : Mr. Mayur M. Vegad


WHY DO WE NEED TO LEARN COMPLEXITY OF ALGORITHMS?

WHAT IS THE USE OF THE DISCRETE MATHEMATICS IN THE


TIME COMPLEXITY ? -> OPTIMIZATION USING THE LOGIC

WHY WE NEED TO REDUCE THE TIME COMPLEXITY?


Algorithms:-
• An algorithm is a finite set of instructions, those if followed,
accomplishes a particular task.
• An algorithm is a finite set of precise instructions for performing a
computation or for solving a problem
• It is not language specific, we can use any language and symbols to
represent instructions.
• For example:
Sorting algorithms: Merge sort, Quick sort, Heap sort
Searching algorithms: Linear search, Binary search
Characteristics of Algorithms:-
Example : Linear Search
But we can show it like this too.
Better Approach :- Binary Search
• Example- Given the sorted array and the number of elements are 100.
Find the Time complexity of the binary search Algorithm.

100->50->25->12->6->3->1
Time Complexity : O(log n)
The Growth of Functions :-
• Given functions f : N ->R or f : R ->R.
• Analyzing how fast a function grows.
• Comparing two functions.
• Comparing the efficiency of different
algorithms that solve the same problem.
How to measure growth of
functions??
• To measure the growth of functions, particularly
in the context of algorithm analysis and
mathematical comparisons, we typically use
asymptotic notation, primarily Big O notation.
• This notation helps to classify functions according
to their growth rates as the input size approaches
infinity.

*Asymptotic Notations are languages that allow us to analyze an algorithm's running time by
identifying its behavior as the input size for the algorithm increases
Big-O Notation
• Let f, g : R-> R . We say that f is O(g) if there are constants C and k
such that
• e.g. f (x) = x^2 +2x +1, g(x) = x^2. f is O(g) witness k = 1 and C = 4.
Growth of Common Functions:-
The growth rates of these common functions, from slowest to fastest, are:
Big Omega notation:-
Let f, g : R -> R. We say that f is Ω(g) if there are constants C and k such that

Big Theta notation:-


Let f ,g : R -> R. We say that f is Θ(g) if f is O(g) and f is Ω(g).
1. Best Case (Big Omega - Ω)
• In the best-case scenario, the array is already sorted. The algorithm only needs to
make one comparison for each element, resulting in:
• Time Complexity: Ω(n)

• This indicates that the minimum time required to grow linearly with the number
of elements.
2. Worst Case (Big O - O)
• In the worst-case scenario, the array is sorted in reverse order.
• The algorithm will need to compare each element with all the other elements in the
sorted section, leading to:
• Time Complexity: O(n^2)
• This shows that the maximum time required to grow quadratically with the number
of elements.
3. Average Case (Big Theta - Θ)
• On average, the algorithm will perform a number of comparisons and shifts that
is between the best and worst cases.
• The average-case time complexity is:
• Time Complexity: Θ(n^2)
• This indicates that the expected time required to grow quadratically with the
number of elements.
For Time Complexity,
Example : Insertion Sort
Algorithm: Insertion-Sort(A)
for j = 2 to A.length
key = A[j]
i = j – 1
while i > 0 and A[i] > key
A[i + 1] = A[i]
i = i -1
A[i + 1] = key

Can you guess Time-Complexity (Big-O Notation) for this one ???
Answer:-
• Big O (Worst Case): O(n^2) - The maximum number of operations
required when the input is in the worst possible order.
• Big Omega (Best Case): Ω(n) - The minimum number of operations
required when the input is already sorted.
• Big Theta (Average Case): Θ(n^2) - The expected number of
operations for a typical input scenario.

But why ??
Complexity of Algorithms:-
• The name itself suggest that it refers to analyse how efficient is this algorithm for
solving a problem given input of a particular size?
• How much time does this algorithm use to solve a problem?(time
complexity)
• How much computer memory does this algorithm use to solve a
problem?(space complexity)
• We focus on the worst-case time complexity of an algorithm. Derive an upper
bound on the number of operations an algorithm uses to solve a problem with
input of a particular size. (As opposed to the best-case complexity.)
Common time
complexities and
their names:
Runtime and Time Complexity are same
Space Complexity:-
• Space complexity is a measure of the total amount of memory that an algorithm
requires to execute as a function of the input size.
• It encompasses all the memory space needed by the algorithm during its
execution, including both the space for input data and any additional space
required for processing.
Key components of Space complexity:-
• Input Space: This is the memory required to store the input values. For example, if an
algorithm processes an array of size n, it will require O(n) space for the input.
• Auxiliary Space: This refers to the extra space or temporary space used by an
algorithm during its execution, excluding the input space. For example, if an
algorithm uses additional data structures (like arrays or linked lists) to perform its
operations, the space required for these structures is counted as auxiliary space.
• Total Space Complexity: The total space complexity of an algorithm can be expressed
as : Space Complexity=Auxiliary Space + Input Space
For Space Complexity,
Example :
• function sum_array(arr):
• total = 0 # O(1) for the variable 'total'
• for num in arr: # O(n) for the input array
• total += num # O(1) for each addition
• return total
Answer:

• Input Space: Requires O(n) for the input array.


• Auxiliary Space: Requires O(1) for the variable total.
• Total Space Complexity: O(n)+O(1)=O(n).
References:-
• GeeksforGeeks:
https://www.geeksforgeeks.org/what-is-
algorithm-and-why-analysis-of-it-is-important
• tutorials-point:
https://www.tutorialspoint.com/algorithms-
and-complexities

• Discrete Mathematics and Its Applications,


Book by Kenneth H. Rosen
Thank You

You might also like