0% found this document useful (0 votes)
3 views16 pages

3-Introduction To Algorithms, Asymptotic Notations-27!04!2023

The document provides an introduction to algorithms, defining them as sequences of unambiguous instructions for problem-solving. It outlines the steps in designing and implementing algorithms, including understanding the problem, choosing techniques, and analyzing correctness and efficiency. Additionally, it discusses specific algorithms like Euclid's for finding the greatest common divisor and insertion sort, highlighting their characteristics and stages of development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

3-Introduction To Algorithms, Asymptotic Notations-27!04!2023

The document provides an introduction to algorithms, defining them as sequences of unambiguous instructions for problem-solving. It outlines the steps in designing and implementing algorithms, including understanding the problem, choosing techniques, and analyzing correctness and efficiency. Additionally, it discusses specific algorithms like Euclid's for finding the greatest common divisor and insertion sort, highlighting their characteristics and stages of development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Introduction to Algorithms

Introduction to Algorithms
• What is an Algorithm
• Steps in Designing and Implementing an
Algorithm
• Important Problem Types
ALGORITHM
• A sequence of unambiguous
instructions for solving a
problem, i.e. for obtaining the
required output for any
legitimate input in a finite
amount of time
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

input “computer” output


Algorithm
• Characteristics of good algorithm

Can be represented various forms


• Unambiguity/clearness
•Precise
•Uniqueness
• Effectiveness
• Finiteness/termination
• Correctness
Understand the problem

Decide on computational means


Exact vs approximate solution
Data structures
Algorithm design technique

Design an algorithm

Prove the correctness

Analyze the algorithm

Code the algorithm


Euclid’s Algorithm
Problem: Find gcd(m,n), the greatest common divisor of
two nonnegative, not both zero integers m and n

Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ?

Euclid’s algorithm is based on repeated application of


equality
gcd(m,n) = gcd(n, m mod n)
until the second number becomes 0, which makes the
problem
trivial.

Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12


Two descriptions of Euclid’s algorithm
Step 1 If n = 0, return m and stop; otherwise go to Step 2
Step 2 Divide m by n and assign the value of the remainder
to r
Step 3 Assign the value of n to m and the value of r to n.
Go to
Step 1.

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 4 Decrease t by 1 and go to Step 2
Is this slower than Euclid’s algorithm? How much
slower? O(n), if n <= m , vs O(log n)
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?

How efficient is it?


Time complexity: O(sqrt(n))
Stages of Algorithm development
• Describing the Problem
• Identifying Suitable technique
• Design of an algorithm
• Proof of Correctness of an algorithm
• Computing the time complexity
Describing the Problem

• Define the problem clearly


Ex. Sorting Problem
Matrix manipulations
Shortest path problem etc
Identifying Suitable technique
• Find the efficient technique to solve the problem.
• For every problem there is always more than one technique
to solve it.
• Need to decide the algorithm paradigm to be used to solve a
problem
Ex: To perform matrix multiplication whether to perform
divide and conquer or dynamic programming technique.
For sorting problems : Brute force or divide and conquer
techniques
• Need to analyse and identify the best algorithm to solve the
given problem which takes less time and occupies least
memory space.
Design of an Algorithm
• Algorithm must be free from ambiguity
• Must focus on problem rather than syntactical
considerations
• Must be efficient
Algorithm for Insertion sort
• INSERTION-SORT A[1…….n]
1. for j = 2 to length(A)
2. key ← A [j]
3. // Insert A[j] into the sorted sequence A[1..j-1]
4. j←i–1
5. while i > 0 and A[i] > key
6. A[i+1] ← A[i]
7. i←i–1
8. A[j+1] ← key
• Let us take one example and see the Operations of Insertion
Sort on the given Array. A= { 5 , 2, 4, 6, 1, 3}

You might also like