Introduction To Algorithm: Agenda

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

16 January 2024

L-02
AGENDA:
❑Introduction to Algorithm
What is
Algorithm?

❑An algorithm is any well-defined computational procedure that takes


some value, or set of values, as input and produces some value, or set of
values, as output.
What is Algorithm?

INPUT Algorithm OUTPUT

Catch: An algorithm is thus a sequence of computational steps-that transform


the input into the output.
Algorithm to add two numbers

Problem − Design an algorithm to add two numbers and display the result.

Step 1: Start ADD


Step 2: get the values of a and b
Step 3: c  a + b
Step 4: display c
Step 5: Stop
Problem: Find the maximum key value from a list of n number
Algorithm:
Find_Maximum(a, n)
//Input: List a containing n data items
//Output: maximum from list a
1. max a[0]
2. for i1 to n-1 do
if a[i]>max
Max  a[i]
3. Return max
Characteristics of an Algorithm

• An algorithm requires 0 or more input


Input values.

• At the end of an algorithm, you will have


Output one or more outcomes.

• its instructions should be clear and


Unambiguity straightforward.
Characteristics of an Algorithm

• The algorithm must terminate


Finiteness after finite number of iterations

Language • An algorithm must be language-


independence independent.
A
L
G
O
Computational R Solution in
I
Problem T terms of Input
H
M
Computational Problem
Instance of Sorting Problem
• Input: A sequence of n=6 numbers <10, 8, 1, 15, 9, 6>
• Output: <1, 6, 8, 9, 10, 15>

Quick Sort

Heap Sort

10, 8, 1, 15, 9, 6 1, 6, 8, 9, 10, 15


Merge Sort

Insertion Sort

Note : Notion of goodness and badness of an algorithm come into the


existence
Performance of Algorithms
• Parameter to judge the performance of Algorithms are

TIME SPACE
Time Vs Space

P P

A1 A2 A3 A1 A2 A3

2 µsec 3 µsec 5 µsec 12 KB 3 KB 35 KB

A1 is BEST A2 is BEST
Computing Time Complexity

Traditional Approach
1. Convert the Algorithm to its equivalent program using a programming
language like C, C++, Java, Python, etc
2. Run the program on a platform
3. Compute the execution time by using the following formulae
Texec = Tend - Tstart
P

A1 A2 Algorithms

P1 in C P2 in C++ Programs

Window Mac Platform

Texec =2 ns Texec = 5 ns Time of execution

Is this a Correct way to compare the performance of


two Algorithms ?
Pitfalls in Traditional Approach
❑Converting Algorithm to program is itself a time consuming
process

❑The process is platform dependent


Computing Time Complexity
1. Write an algorithm using pen and paper

2. Count the total number of key operations

3. Texec = approximately (TNkey)


Computing Time Complexity:
Example
Problem: Find the maximum key value among a list of n number
Algorithm:
Find_Maximum(a, n)
//input: List a containing n data items
//Output: maximum from list a 1 n-1
1. max a[0] ----------------------------------------- 1
2. for i1 to n-1 do -------------------------------------3n for (i=1; i≤ n-1; i++)
if a[i]>max ---------------------------------------------------n - 1
Max  a[i] -------------------------------------------------------n - 1 n
n
3. Return max -------------------------------------------1
==========================================
Total Number of key operations = 5n
Computing Time Complexity
• Total number of key operation= 5n

5n1
Linear
• Total number of key operation = Approx. time of execution
Linear Time Algorithm
• Texec = 5n Linear Time
Algorithm
• Texec ∝ n

• T1= 5n +10
Linear Time
• T2 = 100n +20
Algorithm
• T3 = 19n
Quadratic Time Algorithm
• T4 = n2 + 5n + 3
• T5 = 6n2 + 7n +10
• T6 = 5n2
• T7 = 2n2 + 8
Logarithmic Time Algorithm
• T1 = logn
• T2 = 2logn + 5
Exponential Time Algorithm
• T1 = 2n +3
• T 2 = 3n
• T 3 = 5n + 6 Kn Where K is Constant
• T4 = 22n
Constant Time Algorithm
• T1 = 5
• T2 = 100
• T3 = 1000
• T4 = 100000000000
• T5 = 100000000000000000000000000000
Classification of Algorithm

You might also like