0% found this document useful (0 votes)
27 views21 pages

Daa L1

Uploaded by

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

Daa L1

Uploaded by

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

Apex Institute of Technology

Department of Computer Science & Engineering


Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)

07/30/2024 DISCOVER . LEARN . EMPOWER


1
Content

Analysis Framework: Worst case analysis.

Analysis Framework: Average case analysis

Analysis Framework: Best case analysis

2
Definition
What is an Algorithm?
• An algorithm is a well-defined procedure that allows a computer to solve a problem.
• An algorithm is a sequence of unambiguous instructions.
 An algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem.
 The word derives from the mathematician Mohammad Ibn-Musa-al-Khwarizmi, who was part
of the Baghdad royal court and lived from about 780 to 850. Al Khwarizmi’s work is the likely
source for the word algebra as well.
• a sequence of instructions
• sequence is finite
• initial instructions acquire valid input
• intermediate instructions perform processing
• final instructions produce valid output for P
• algorithm terminates after a finite number of steps

3
Characteristics of Algorithm

The characteristics of a good algorithm are:


• Precision – the steps are precisely stated(defined).

• Uniqueness – results of each step are uniquely defined and only depend on the
input and the result of the preceding steps.

• Finiteness – the algorithm stops after a finite number of instructions are


executed.

4
Properties of Algorithm

• An algorithm must have five properties:


Input specified.
Output specified.
Definiteness.
Effectiveness.
Finiteness.

5
ANALYZING AN ALGORITHM

• Why is it important?
• Predict the feasibility requirement of your code (algorithm).
• Usual requirements
• Execution time
• Memory space
• The complexity of the algorithm is determined in terms of space and time.
• Space complexity ← memory space
• Time complexity ← execution time

6
ANALYZING AN ALGORITHM

Understand the problem

Decide on computational means


Exact vs approximate solution
Data structures
Algorithm design technique

Design an algorithm

Prove correctness

Analyze the algorithm

Code the algorithm


7
ANALYZING AN ALGORITHM

PROBABILISTIC ANALYSIS

Probabilistic analysis of algorithms is an approach to


estimating the computational complexity of an algorithm or
a computational problem. It starts from an assumption
about a probabilistic distribution of the set of all possible
inputs. This assumption is then used to design an efficient
algorithm or to derive the complexity of a known
algorithm.

8
ANALYZING AN ALGORITHM

AMORTIZED ANALYSIS

•Amortized analysis is a method of analyzing


algorithms that consider the entire sequence of operations
of the program. It allows for the establishment of a worst-
case bound for the performance of an algorithm irrespective
of the inputs by looking at all of the operations. This
analysis is most commonly discussed using big O notation.

9
Asymptotic analysis

 Asymptotic analysis of an algorithm refers to defining the mathematical


boundation/framing of its run-time performance.
 Using asymptotic analysis, we can very well conclude the best case, average
case, and worst case scenario of an algorithm.
 Asymptotic analysis is input bound i.e. if there's no input to the algorithm, it is
concluded to work in a constant time. Other than the "input" all other factors are
considered constant.
 Asymptotic analysis refers to computing the running time of any operation in
mathematical units of computation. For example, the running time of one
operation is computed as f(n), and maybe for another operation, it is computed
as g(n2). This means the first operation’s running time will increase linearly with
the increase in n and the running time of the second operation will increase
exponentially when n increases

10
ANALYZING AN ALGORITHM

Usually, the time required by an algorithm falls under three types −


· Best Case − Minimum time required for program execution.

11
ANALYZING AN ALGORITHM

Average Case − Average time required for program execution.

12
ANALYZING AN ALGORITHM

Worst Case − Maximum time required for program execution.

13
Algorithm design techniques

• Divide and conquer algorithms.


• Greedy algorithms.
• Dynamic programming algorithms.
• Backtracking algorithms.
• Branch and bound algorithm

14
Example

Write an algorithm to determine a student’s final grade and indicate whether it


is passing or failing. The final grade is calculated as the average of four marks.
Algorithm:
• Input a set of 4 marks.
• Calculate their average by summing and dividing by 4.
• if the average is below 50
• Print “FAIL”
• else
• Print “PASS”

15
Example

Psuedocode:
• Step 1: Input M1,M2,M3,M4 .
• Step 2: GRADE (M1+M2+M3+M4)/4 .
• Step 3: if (GRADE < 50)
• then
• Print “FAIL”
• else
• Print “PASS”
• End if
16
TASKS END OF LECTURE LEARNING (TELL):

TASK 1:
Locker doors There are n lockers in a hallway numbered sequentially from 1 to n.
Initially, all the locker doors are closed. You make n passes by the lockers, each time
starting with locker #1. On the ith pass, i = 1, 2, . . . , n, you toggle the door of every ith
locker: if the door is closed, you open it; if it is open, you close it. After the last pass,
which locker doors are open and which are closed? How many of them are open?

TASK 2:
What does Euclid’s algorithm do for a pair of integers in which the first is smaller than
the second? What is the maximum number of times this can happen during the
algorithm’s execution on such an input?

17
TASKS END OF LECTURE LEARNING (TELL):

TASK 3:
Design an algorithm to find all the common elements in two sorted lists of numbers.
For example, for the lists 2, 5, 5, 5, and 2, 2, 3, 5, 5, 7, the output should be 2, 5, 5.
What is the maximum number of comparisons your algorithm makes if the lengths
of the two given lists are m and n, respectively?

TASK 4:
Describe the algorithm used by your favorite ATM machine in dispensing cash. (You
may give your description in either English or pseudocode, whichever you find more
convenient.)

18
TASKS END OF LECTURE LEARNING (TELL):

TASK 5:
New World puzzle There are four people who want to cross a rickety bridge; they
all begin on the same side. You have 17 minutes to get them all across to the other
side. It is night, and they have one flashlight. A maximum of two people can cross
the bridge at one time. Any party that crosses, either one or two people, must have
the flashlight with them. The flashlight must be walked back and forth; it cannot
be thrown, for example. Person 1 takes 1 minute to cross the bridge, person 2 takes
2 minutes, person 3 takes 5 minutes, and person 4 takes 10 minutes. A pair must
walk together at the rate of the slower person’s pace. (Note: According to a rumor
on the Internet, interviewers at a well-known software company located near
Seattle have given this problem to interviewees.)

19
References
• Fundamentals of Computer Algorithms 2nd Edition (2008) by Horowitz, Sahni
and Rajasekaran
• Introduction to Algorithms 3rd Edition (2012) by Thomas H Cormen, Charles E
Lieserson, Ronald

20
THANK YOU

For queries
Email: [email protected]

07/30/2024 21

You might also like