1.introduction To ADA
1.introduction To ADA
Semester :IV
Outline of chapter 1
1.1 What Is an Algorithm?
Solving
1.1 Introduction
What is an Algorithm?
Characteristics of an algorithm.
1.1 What is an algorithm?
Recipe, process, method, technique, procedure,
routine,… with the following requirements:
1. Finiteness
terminates after a finite number of steps
2. Definiteness
rigorously and unambiguously specified
3. Clearly specified input
valid inputs are clearly specified
4. Clearly specified/expected output
can be proved to produce the correct output given a
valid input
5. Effectiveness
steps are sufficiently simple and basic
1.1 What is an algorithm?
An algorithm is a sequence of unambiguous
instructions for solving a problem, i.e., for
obtaining a required output for any
legitimate input in a finite amount of time.
problem
algorithm
1. Euclid’s Algorithm
2. Consecutive Integer Checking
3. High School algorithm
Euclid’s Algorithm
Problem: Find gcd(m,n), the greatest common divisor of
two nonnegative, not both zero integers m and n
while n ≠ 0 do
r ← m mod n
m← n
n←r
return m
Other methods for computing gcd(m,n)
Consecutive integer checking algorithm
Step 1 Assign the value of min{m,n} to t
Step 2 Divide m by t. If the remainder is 0, go
to Step 3;
otherwise, go to Step 4
Step 3 Divide n by t. If the remainder is 0,
return t and stop;
otherwise, go to Step 4
Step
Is this 4 Decrease
slower t by
than Euclid’s 1 and How
algorithm? go to Step 2
much slower?
1-12
Other methods for gcd(m,n) [cont.]
Middle-school procedure
Step 1 Find the prime factorization of m
Step 2 Find the prime factorization of n
Step 3 Find all the common prime factors
Step 4 Compute the product of all the common
prime factors
and return it as gcd(m,n)
Is this an algorithm?
15
Design an algorithm
• Build a computational model of
the solving process
Prove correctness
• Correct output for every
legitimate input in finite time
• Based on correct math formula
• By induction
16
Analyze the algorithm
Efficiency: time and space
Simplicity
Generality: range of inputs, special
cases
Optimality:
no other algorithm can do
better
Coding
How the objects and operations in the
17
Important problem
types
Sorting
Searching
String processing
Graph problems
Combinatorial problems
Geometric problems
Numerical problems
18
Two main issues related to algorithms
How to design algorithms
Practical importance
Backtracking
space efficiency