0% found this document useful (0 votes)
184 views

Introduction To Algorithms

The document introduces algorithms and discusses their key characteristics and design process. It provides examples of algorithms like Euclid's algorithm for finding the greatest common divisor (GCD) and insertion sort. It also explains the stages of algorithm development including describing the problem, identifying a suitable technique, designing the algorithm, proving its correctness, and analyzing its time complexity.

Uploaded by

Kritika Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
184 views

Introduction To Algorithms

The document introduces algorithms and discusses their key characteristics and design process. It provides examples of algorithms like Euclid's algorithm for finding the greatest common divisor (GCD) and insertion sort. It also explains the stages of algorithm development including describing the problem, identifying a suitable technique, designing the algorithm, proving its correctness, and analyzing its time complexity.

Uploaded by

Kritika Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

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
Notion of algorithm and problem
problem

algorithm

input “computer” output


(or instance)

algorithmic solution
(different from a conventional solution)
Understand the problem

Decide on computational means


Exact vs approximate solution
Data structures
Algorithm design technique

Design an algorithm

Prove 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}

Proof of Correctness of an algorithm
• Algorithm should be concise and compact.
• Must facilitate verification of their correctness
• Verification involves observing the
performance of algorithm with selected test
cases.
Loop invariant Conditions
• Initialization
• Maintenance
• Termination
Loop Invariant for Insertion Sort
Alg.: INSERTION-SORT(A)
for j ← 2 to n
do key ← A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1]

i←j-1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1
A[i + 1] ← key
Invariant: at the start of the for loop the elements in A[1 . . j-1] are in
sorted order
19
Proving Loop Invariants
• Proving loop invariants works like induction
• Initialization (base case):
– It is true prior to the first iteration of the loop
• Maintenance (inductive step):
– If it is true before an iteration of the loop, it remains true before the
next iteration
• Termination:
– When the loop terminates, the invariant gives us a useful property
that helps show that the algorithm is correct
– Stop the induction when the loop terminates

20
Loop Invariant for Insertion Sort
• Initialization:
– Just before the first iteration, j =
2:
the subarray A[1 . . j-1] = A[1],
(the element originally in A[1]) –
is sorted

21
Loop Invariant for Insertion Sort
• Maintenance:
– the while inner loop moves A[j -1], A[j -2], A[j
-3], and so on, by one position to the right until
the proper position for key (which has the value
that started out in A[j]) is found
– At that point, the value of key is placed into this
position.

22
Loop Invariant for Insertion Sort
• Termination:
– The outer for loop ends when j = n + 1  j-1 = n
– Replace n with j-1 in the loop invariant:
• the subarray A[1 . . n] consists of the elements
originally in A[1 . . n], but in sorted orderj - 1 j

• The entire array is sorted!


Invariant: at the start of the for loop the elements in A[1 . . j-1] are in
sorted order
23

You might also like