SlideShare a Scribd company logo
|| JAI SRI GURUDEV ||
SJB INSTITUTE OF TECHNOLOGY
#67, BGS HEALTH & EDUCATION CITY, DR. VISHNUVARDHAN ROAD, KENGERI,
BENGALURU – 560060, KARNATAKA, INDIA
Module 1: Introduction to Algorithms
IV Semester
Design and Analysis of algorithms
(18CS56)
By
L Kala Chandrashekhar
Asst. Prof.
Department of Computer Science and Engineering
COURSE OBJECTIVE:
UNDERSTANDTHE BASIC CONCEPT OF ALGORITHM
DESCRIBEVARIOUS METHODS OF ALGORITHM ANALYSIS
COURSE OUTCOME:
“UNDERSTANDTHE BASICS OF ALGORITHM, METHODS FOR
ANALYZING ALGORITHM AND ALSO EXPRESSINGTHE BOUNDARIES
OF EFFICIENCIES USING ASYMPTOTIC NOTATIONS”,
TABLE OF CONTENTS
• Algorithm
• What Is An Algorithm
• Euclid’s Algorithm
• Two forms of Euclid’s algorithm
• Other methods for computing gcd
• Other methods for finding gcd continued…
• Sieve of Eratosthenes
ALGORITHM
• An algorithm is an exact specification of how to solve a computational problem
• An algorithm must specify every step completely, so a computer can implement it without
any further “understanding”
• An algorithm must work for all possible inputs of the problem.
• 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
• There can be many different algorithms for each computational problem.
WHAT IS AN ALGORITHM?
“Computer”
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 Output
8
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 FORMS 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
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
OTHER METHODS FOR FINDING GCD CONTINUED…
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)
SIEVE OF ERATOSTHENES
Input: Integer n ≥ 2
Output: List of primes less than or equal to n
for p ← 2 to n do A[p] ← p
for p ← 2 to sqrt(n) do
if A[p]  0 //p hasn’t been eliminated on previous passes
j ← p*p
while j ≤ n do
A[j] ← 0 //mark element as eliminated
j ← j + p
• Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
• Output: 2 3 5 7 11 13 17 19
This Photo by Unknown Author is licensed under CC BY-SA

More Related Content

digital design and algorithm module 1 ppt

  • 1. || JAI SRI GURUDEV || SJB INSTITUTE OF TECHNOLOGY #67, BGS HEALTH & EDUCATION CITY, DR. VISHNUVARDHAN ROAD, KENGERI, BENGALURU – 560060, KARNATAKA, INDIA Module 1: Introduction to Algorithms IV Semester Design and Analysis of algorithms (18CS56) By L Kala Chandrashekhar Asst. Prof. Department of Computer Science and Engineering
  • 2. COURSE OBJECTIVE: UNDERSTANDTHE BASIC CONCEPT OF ALGORITHM DESCRIBEVARIOUS METHODS OF ALGORITHM ANALYSIS COURSE OUTCOME: “UNDERSTANDTHE BASICS OF ALGORITHM, METHODS FOR ANALYZING ALGORITHM AND ALSO EXPRESSINGTHE BOUNDARIES OF EFFICIENCIES USING ASYMPTOTIC NOTATIONS”,
  • 3. TABLE OF CONTENTS • Algorithm • What Is An Algorithm • Euclid’s Algorithm • Two forms of Euclid’s algorithm • Other methods for computing gcd • Other methods for finding gcd continued… • Sieve of Eratosthenes
  • 4. ALGORITHM • An algorithm is an exact specification of how to solve a computational problem • An algorithm must specify every step completely, so a computer can implement it without any further “understanding” • An algorithm must work for all possible inputs of the problem. • 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 • There can be many different algorithms for each computational problem.
  • 5. WHAT IS AN ALGORITHM? “Computer” 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 Output 8
  • 6. 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
  • 7. TWO FORMS 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
  • 8. OTHER METHODS FOR COMPUTING GCD 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
  • 9. OTHER METHODS FOR FINDING GCD CONTINUED… 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)
  • 10. SIEVE OF ERATOSTHENES Input: Integer n ≥ 2 Output: List of primes less than or equal to n for p ← 2 to n do A[p] ← p for p ← 2 to sqrt(n) do if A[p]  0 //p hasn’t been eliminated on previous passes j ← p*p while j ≤ n do A[j] ← 0 //mark element as eliminated j ← j + p • Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 • Output: 2 3 5 7 11 13 17 19
  • 11. This Photo by Unknown Author is licensed under CC BY-SA