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

Algorithms Lec 3

Uploaded by

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

Algorithms Lec 3

Uploaded by

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

CS 478: Design and Analysis of Algorithms

Algorithm Analysis

Fall 2024
Mr. Ahsan Shah

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


CS 478: Design and Analysis of Algorithms

Algorithm Analysis

• Algorithm analysis is an important part of computational


complexity theory, which provides theoretical estimation for the
required resources of an algorithm to solve a specific
computational problem. Analysis of algorithms is the
determination of the amount of time and space resources
required to execute it.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


CS 478: Design and Analysis of Algorithms

Why Analysis of Algorithms is important?

• To predict the behavior of an algorithm without


implementing it on a specific computer.
• It is much more convenient to have simple measures
for the efficiency of an algorithm than to implement the
algorithm and test the efficiency every time a certain parameter
in the underlying computer system changes.
• It is impossible to predict the exact behavior of an algorithm.
There are too many influencing factors.
• The analysis is thus only an approximation; it is not perfect.
• More importantly, by analyzing different algorithms, we can
compare them to determine the best one for our purpose.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


CS 478: Design and Analysis of Algorithms

Algorithm Analysis

Algorithm is a combination or sequence of finite-state to solve a given problem. If the


problem is having more than one solution or algorithm then the best one is decided by
the analysis based on two factors.
• CPU Time (Time Complexity)
• Main memory space (Space Complexity)

Time complexity of an algorithm can be calculated by using two methods:


• Posteriori Analysis
• Priori Analysis

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


CS 478: Design and Analysis of Algorithms

Difference between Posterior analysis and Priori analysis

Posteriori analysis Priori analysis


Posteriori analysis is a relative analysis. Priori analysis is an absolute analysis.

It is dependent on language of compiler and type It is independent of language of compiler and


of hardware. types of hardware.

It will give exact answer. It will give approximate answer.

It uses the asymptotic notations to represent how


It doesn’t use asymptotic notations to represent
much time the algorithm will take in order to
the time complexity of an algorithm.
complete its execution.

The time complexity of an algorithm using a The time complexity of an algorithm using a
posteriori analysis differ from system to system. priori analysis is same for every system.

If the time taken by the program is less, then the If the algorithm running faster, credit goes to the
credit will go to compiler and hardware. programmer.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


CS 478: Design and Analysis of Algorithms

Example

• let us consider the search problem (searching a given item) in a sorted array.

The solution to above search problem includes:


• Linear Search (order of growth is linear)
• Binary Search (order of growth is logarithmic).
• let us say:
– we run the Linear Search on a fast computer A and
– Binary Search on a slow computer B and
– pick the constant values for the two computers so that it tells us exactly how long it takes for the
given machine to perform the search in seconds.
• Let’s say the constant for A is 0.2 and the constant for B is 1000 which means that A
is 5000 times more powerful than B.
• For small values of input array size n, the fast computer may take less time.
• But, after a certain value of input array size, the Binary Search will definitely
start taking less time compared to the Linear Search even though the Binary
Search is being run on a slow machine.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


CS 478: Design and Analysis of Algorithms

Why performance analysis?

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


CS 478: Design and Analysis of Algorithms

Types of Algorithm Analysis

• Best case
• Worst case
• Average case
• Best case: Define the input for which algorithm takes less time or minimum time. In
the best case calculate the lower bound of an algorithm. Example: In the linear search
when search data is present at the first location of large data then the best case
occurs.
• Worst Case: Define the input for which algorithm takes a long time or maximum time.
In the worst calculate the upper bound of an algorithm. Example: In the linear search
when search data is not present at all then the worst case occurs.
• Average case: In the average case take all random inputs and calculate the
computation time for all inputs. And then we divide it by the total number of inputs.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


CS 478: Design and Analysis of Algorithms

Algorithmic Common Runtimes

• The common algorithmic runtimes from fastest to slowest are:


• constant: Θ(1)
• logarithmic: Θ(log N)
• linear: Θ(N)
• polynomial: Θ(N^2)
• exponential: Θ(2^N)
• factorial: Θ(N!)

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


CS 478: Design and Analysis of Algorithms

Big O Notation

• Worst Case
• Upper Bound
O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ f(n) ≤ cg(n) for all n
≥ n0 }

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


CS 478: Design and Analysis of Algorithms

Big Omega Notation (Ω-notation)

• Best Case
• Lower Bound
Ω(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ cg(n) ≤ f(n) for all n
≥ n0 }

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


CS 478: Design and Analysis of Algorithms

Theta Notation (Θ-notation)

• Average Case
• Exact Time
Θ(g(n)) = { f(n): there exist positive constants c1, c2 and n0 such that 0 ≤ c1g(n) ≤ f(n) ≤
c2g(n) for all n ≥ n0 }

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

You might also like