Daa L1
Daa L1
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
• Uniqueness – results of each step are uniquely defined and only depend on the
input and the result of the preceding steps.
4
Properties of Algorithm
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
Design an algorithm
Prove correctness
PROBABILISTIC ANALYSIS
8
ANALYZING AN ALGORITHM
AMORTIZED ANALYSIS
9
Asymptotic analysis
10
ANALYZING AN ALGORITHM
11
ANALYZING AN ALGORITHM
12
ANALYZING AN ALGORITHM
13
Algorithm design techniques
14
Example
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