0% found this document useful (0 votes)
6 views23 pages

DAA Lecture 1

The document provides an overview of algorithms, defining them as step-by-step procedures to solve specific problems, and contrasts them with programs, which are concrete implementations. It discusses characteristics of algorithms, such as finiteness, definiteness, and effectiveness, and highlights the significance of good versus bad algorithms in terms of execution time. Additionally, it introduces decidable and undecidable algorithms, along with methods for analyzing algorithm performance based on time and memory consumption.

Uploaded by

arkbofficial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views23 pages

DAA Lecture 1

The document provides an overview of algorithms, defining them as step-by-step procedures to solve specific problems, and contrasts them with programs, which are concrete implementations. It discusses characteristics of algorithms, such as finiteness, definiteness, and effectiveness, and highlights the significance of good versus bad algorithms in terms of execution time. Additionally, it introduces decidable and undecidable algorithms, along with methods for analyzing algorithm performance based on time and memory consumption.

Uploaded by

arkbofficial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Design and Analysis of

Algorithm
By: Hafsa Zaman

Department of Computer Science, Engineering & IT


Thal University Bhakkar
What is an Algorithm?
• “Step-By-Step Procedure to Solve a Specific Problem.”

• Problem Statement: Write an algorithm to find the largest number in


a list of numbers.
• Solution:
1. Input the list of numbers
2. Set the first number in the list as the largest.
3. Compare the current largest number with the next number in the list. ●If the
next number is greater, update the largest number.
4. Repeat step 4 until all numbers in the list are checked.
5. Output the largest number.
What is a Program?
• “Step-By-Step Procedure to Solve a Specific Problem.”
• int main()
• { int n, num, largest;
• cout << "Enter the number of elements: ";
• cin >> n;
• cout << "Enter the first number: ";
• cin >> largest; // Assume the first number is the largest
• for (int i = 1; i < n; ++i)
• { cout << "Enter the next number: ";
• cin >> num;
• if (num > largest)
• { largest = num; // Update largest if a bigger number is found } }
Quick Walkthrough
Input
• Enter the number of elements: 5
• Enter the first number: 3
• Enter the next number: 7
• Enter the next number: 2
• Enter the next number: 9
• Enter the next number: 5
Output
• The largest number is: 9
Algorithm vs Program
• An Algorithm is an abstract concept while a Program is a concrete
implementation of an algorithm.
• An Algorithm can be written in any language, while a Program must
be written in programming language only.
• An Algorithm is developed during the design phase, while a Program
is developed during the development phase.
• An algorithm does not depend on Hardware and Operating system,
while a Program depends upon them.
• An Algorithm is analyzed, while a Program is tested.
Characteristics of an Algorithm
1. Inputs: An Algorithm can take 0 or more inputs.
Algorithm with 0 input Algorithm with two inputs

1. Start 1. Start
2. Generate a random number. 2. Input the first number. (num1)
3. Output a random number. 3. Input the second number.
4. Stop. (num2)
4. Add num1 & num2.
5. Output the result of addition.
6. Stop.
Characteristics of an Algorithm
1. Outputs: An Algorithm must produce at least one output.
Algorithm with one output Algorithm with two outputs

1. Start 1. Input the first number. (num1)


2. Generate a random number. 2. Input the second number.
3. Output a random number. (num2)
4. Stop. 3. Add num1 & num2.
4. Multiply num1 & num2.
5. Output the result of addition.
6. Output the result of
Multiplication.
Characteristics of an Algorithm
1. Finiteness: An Algorithm must terminate in finite time.
Algorithm that runs forever Terminates in finite time

1. Initialize count to 0. 1. Initialize count to 0.


2. Enter the loop. 2. Set limit for num.of counts(10)
3. Within the loop, increment the 3. Enter loop for specified limit.
count by 1. 4. Within loop, increment by 1.
4. Repeat steps 3 & 4. 5. Repeat steps 4 & 5 until
specified limit is reached.
Characteristics of an Algorithm
1. Definiteness: An Algorithm must be clear and unambiguous.
Algorithm with ambiguity Without ambiguity

1. Start. 1. Start.
2. Input two numbers a & b. 2. Input two numbers a & b.
3. Perform some operations on a 3. Calculate the sum of a & b and
& b. store it n a variable sum.
4. Output the result. 4. Output the value of sum.
5. Stop. 5. Stop.
Characteristics of an Algorithm
1. Effectiveness: An Algorithm should take less time and space to
execute.
Less Effective Algorithm More Effective Algorithm

1. Input a positive integer n. 1. Start.


2. Initialize the variable sum to 0. 2. Input a positive integer n.
3. Enter the loop that iterates 3. Calculate the sum using the
from 1 to n. formula sum=n(n+1)/2.
4. Add the current iteration value 4. Output the value of sum.
to sum. 5. Stop.
5. Repeat steps 4 & 5.
6. Output the value of sum.
Homework
• Determine what characteristic(s) is/are missing in the following
algorithm:
1. Start.
2. Ask the user to input two numbers.
3. Check if both inputs are valid numeric values.
4. If valid, perform some operations of your choice on two numbers.
5. Stop.
Significance of Algorithm(Bad Algorithm)
• A bad algorithm can lead to longer execution time.

Bad Algorithm Equivalent Algorithm


1. Start Long int calculate_sum( ) {
2. Initialize the variable sum to 0. long int sum = 0, i;
3. Enter the loop that iterates from 1 to 10^12. for( i=1; i<=1000000000000; i++) {
4. Add the current iteration value to sum. sum+ = i;
5. Repeat Steps 4 and 5. }
6. Return the value of sum. return sum;
7. Stop. }

Number of Instructions = 2 + 1 + 1000000000000 * (1 + 1 +1) + 1 = 3000000000004


Significance of Algorithm(Bad Algorithm)
• Assume that a computer takes 1 second to execute y instructions, &
y = 100000000
time to run y instructions = 1 sec
Time to run 1 instruction = 1/y sec
Time to run x instruction = x/y sec
time to run 3000000000004 instructions = 3000000000004/100000000
= 30,000 sec = 500 minutes
Total Time = 8 hours
Significance of Algorithm(Good Algorithm)
• A bad algorithm can lead to shorter execution time.
Bad Algorithm Equivalent Algorithm
1. Start Long int calculate_sum( ) {
2. Initialize n to 10^12. long int n = 100000000;
3. Calculate the sum using the formula long int sum;
sum = n(n +1) / 2.
4. Return the value of sum. sum = ( n *( n + 1 ) / 2
5. Stop. return sum;
}

Number of Instructions = 2 + 4 + 1 = 7
Significance of Algorithm(Good Algorithm)
• Assume that a computer takes 1 second to execute y instructions, &
y = 100000000
time to run y instructions = 1 sec
Time to run 1 instruction = 1/y sec
Time to run x instruction = x/y sec
Time to run 7 instructions = 7/100000000
Total Time = 0.00000007 sec
Decidable Algorithms
• The Problem for which an efficient algorithms exist.
• Takes Polynomial Time or Less time to execute.

Terminates in
Decidable
Finite Input Polynomial
Algorithm
Time or Less
Undecidable Algorithms
• The Problem for which no efficient algorithms exist.
• Takes Exponential Time to execute.

Terminates in
Undecidable
Finite Input Exponential
Algorithm
Time
Polynomial Time vs Exponential Time
Polynomial Growth:
• Increases at a slower rate than exponential growth. Polynomial-
time algorithms are considered efficient and can perform
mathematical operations like addition, subtraction, and
multiplication.
Exponential Growth:
• Increases rapidly as input size increases. Exponential-time
algorithms are considered inefficient and can become infeasible
as the input size increases.
Example:
• An algorithm with polynomial time complexity might run in
O(n^2), while an algorithm with exponential complexity could
run in O(2^n).
Nature of Undecidable Problems
• Consider the world’s fastest computer is capable of executing 2^20
instructions per second.

• In a year, computer an execute


2^20 * 60 * 60 * 24 * 365
= 2^20 * 2^6 * 2^6 * 2^5 * 2^8
= 2^45 instructions
Nature of Undecidable Problems
• Let’s say that the input size is 100.

Input size(n=100) Algorithm (2^n) ?

2^45 = 1 year
2^100 = 2^100 / 2^45 = 2^55 years
Introduction to Analysis of Algorithm
• Study of an algorithm’s performance based on time and memory
space consumption.
• The algorithm taking least time and space is preferred.

Two ways to analyze an algorithm.


1. Priori Analysis
2. Posteriori Analysis
Priori vs Posteriori Algorithm

Priori Algorithm Posteriori Algorithm

• Calculation of Time and Memory


• Estimation of Time and Memory Space
Space

• Independent of programming
• Dependent of programming language
language

• Independent of Hardware • Dependent of Hardware


Thanks!

You might also like