1- Ch 1- Intro to Algo (3) (4)
1- Ch 1- Intro to Algo (3) (4)
Introduction to Algorithm
• Applications
• Computational problems
• Writing Algorithms
• Analyzing Algorithms
Design Analysis
Methods/ ideas/ tricks Abstract/ mathematical computation
for developing (fast !) of algorithms
algorithms. (without actually implementing them)
• The statement of the problem specifies in general terms the desired input/output
relationship.
• The algorithm describes a specific computational procedure for achieving that
input/output relationship.
• An instance of a problem consists of the input (satisfying whatever constraints are
imposed in the problem statement) needed to compute a solution to the problem.
• There might be different ways (algorithms) to solve a given problem!
• Usually, we use “pseudo-code” to describe algorithms
• Algorithms can be implemented in any programming language
• …
• ..
• Example 2:
• Name: ListSort
• Input: L (list of names of people)
• Output: L (same list sorted alphabetically). .
• Algorithm:
• Steps …
• Example 3:
• Name: SumOfNumbers
• Input: n (positive integer)
• Output: sum( sum of all positive integers from 1 to n)
• Algorithm:
initially sum is zero
Loop from 1 to n
Add current loop counter to the sum
output sum
• Example 3:
• Name: SumOfNumbers
• Input: n (positive integer)
• Output: sum( sum of all positive integers from 1 to n)
• Algorithm:
sum = 0
for count =1 to n
{
sum = sum + count
}
output sum
• Example 3:
• Name: SumOfNumbers
• Input: n (positive integer)
• Output: sum( sum of all positive integers from 1 to n)
• Algorithm:
count = 1
sum = 0
while (count <= n)
{
sum = sum + count
count = count + 1
}
output sum
Faculty of Information Technology - Computer Science Department 18
Algorithm Example
• Example 3:
• Name: SumOfNumbers
• Input: n (positive integer)
• Output: sum( sum of all positive integers from 1 to n)
• Algorithm:
count = 1
sum = 0
while (count <= n)
{
sum = sum + count
count = count + 1
}
output sum
Faculty of Information Technology - Computer Science Department 19
Algorithms correctness and efficiency
• An algorithm is said to be correct if, for every input instance, it halts with the proper
output
• An incorrect algorithm might not halt at all on some input instances, or it might halt
with an answer other than the desired one.
• Algorithms must be:
• Correct: For each input produce an appropriate output
• Efficient: run as quickly as possible, and use as little memory as possible – more
about this later
• Name: ConvertPoundToKg
• Input: weight-in-pounds (weight in pound)
• Output: weight-in-Kg (weight in Kg )
• Algorithm:
1. Read weight-in-pounds
2. Calculate weight-in-Kg = weight-in-pounds * 0.454
3. Print weight-in-Kg
Faculty of Information Technology - Computer Science Department 22
Algorithm Example (cont.)
OR
• Name: ConvertPoundToKg
• Input: x (weight in pound)
• Output: y (weight in Kg)
• Algorithm:
1. Read x
2. Calculate y = x * 0.454
3. Print y
• Name: ConvertPoundToKg
• Input: weight-in-pounds (weight in pound)
• Output: weight-in-Kg (weight in Kg )
• Algorithm:
1. Read weight-in-pounds
2. Calculate weight-in-Kg = weight-in-pounds / 0.454
3. Print weight-in-Kg
• Example 1 again
Name: Prime
Input: n (an integer number)
Output: Prime or not prime
Algorithm:
• Examples:
GCD(8,12) = 4
GCD(12,8) = 4
GCD(10, 25) = 5
GCD(23, 19) = 1
OR
Name: GreatestCommonDivisor2 (or GCD2)
Input: x, y (two natural numbers)
Output: x (Greatest Common Divisor of x and y)
Algorithm:
GCD(x, y)
{
while (y != 0)
{
t = mod(x, y)
x=y
y=t
}
Output x
Faculty of Information Technology - Computer Science Department 31
}
Euclid’s GCD Algorithm
OR
Name: GreatestCommonDivisor2 (or GCD2)
Input: x, y (two natural numbers)
Output: x (Greatest Common Divisor of x and y)
Algorithm:
GCD(x, y)
{
if y = 0 return x
else return GCD( y, x mod y)
}