1 - 1 - Fundamentals - Algorithm - Problem - Sloving
1 - 1 - Fundamentals - Algorithm - Problem - Sloving
Objective
⮚What is an Algorithm
⮚Steps in Designing and Implementing an
Algorithm
⮚Important Problem Types
⮚Fundamental Data Structures
2
ALGORITHM
Important points
• Non-ambiguity
• Range of inputs
• The same algorithm can be represented in
different ways
5
Example : gcd(m,n) m,n : non negative ,not both Zero integers
6
Example : gcd(m,n) m,n : non negative ,not both Zero integers
7
sieve of Eratosthenes (prime No)
The algorithm starts by initializing a list of prime candidates with consecutive
integers from 2 to n
8
ALGORITHM Sieve(n)
//Implements the sieve of Eratosthenes
//Input: A positive integer n > 1
//Output: Array L of all prime numbers less than or equal to n
L= ( 2,3,5,7,11)
6<=11 , A[6] =0,j=6+2=8
8<=11,A[8]=0,j=8+2=10
10<=11,A[10]=0,j=10+2=12
P=3,A[3] !=0 ,j=3*3=9 9<=11,A[9]=0,j=9+3=12 9
Play Time
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?
10
A locker is toggled once for every divisor it has.
For example, locker #6 is toggled on passes 1, 2, 3, and 6. The only lockers
toggled an odd number of times will end up open.
Now, the number of divisors of a number is odd if and only if the number is a
perfect square (4,16) So, after the n passes, only the lockers with numbers
that are perfect squares will be open.
To determine how many lockers are open, you need to find how many perfect
squares are less than or equal to n. This corresponds to finding the floor of the
square root of n, and since the lockers are numbered from 1 to n, the number
of open lockers is the floor of the square root of n.
In summary, after n passes, the open lockers are those with numbers that are
perfect squares less than or equal to n, and the number of open lockers is the
floor of the square root of n.
11
Try Out
Design an algorithm
Prove correctness
Analyze the algorithm
Read the problem’s description carefully and ask questions if you have
any doubts about the problem, do a few small examples by hand, think
about special cases, and ask questions again if needed.
If you fail to do this, your algorithm may work correctly for a majority of
inputs but crash on some “boundary” value. Remember that a correct
algorithm is not one that works most of the time, but one that works
correctly for all legitimate inputs.
14
Ascertaining the Capabilities of the Computational Device
15
Choosing between Exact and Approximate Problem Solving Choose
between solving the problem exactly or solving it approximately.
Prove correctness
Correct output for every legitimate input in finite time . Based on mathematical
induction
16
⮚ Sorting
⮚ Searching
⮚ String processing
⮚ Graph problems
⮚ Combinatorial problems Pigeonhole Principle
⮚ Geometric problems Area and Perimeter
Problems
⮚ Numerical problems
18
21
• Algorithm classification
• By types of problems
• By design technique
• Design techniques
• a general approach to solving problems
22
gcd(31415, 14142) = gcd(14142, 3131) = gcd(3131, 1618) =
gcd(1618, 1513) = gcd(1513, 105) = gcd(1513, 105) = gcd(105, 43)
= gcd(43, 19) = gcd(19, 5) = gcd(5, 4) = gcd(4, 1) = gcd(1, 0) = 1
divisions Speedup≈28284/11≈2571.27
Therefore, Euclid's algorithm is estimated to be between 1300 (lower bound) and 2600
(upper bound) times faster than the consecutive integer checking algorithm.
23