Algorithms

CodeClub, SMVDU

What are Algorithms

A procedure to solve problems

Real Life Examples

Motivation

Why Algorithms?

Why ?

 

Because time and space are not infinite.

What kind of problem?

 

Suppose you have a list of random phone numbers and you want to check duplicates.

What can algorithms do?

How to design one when you need?

Revisiting the problem -

 

Suppose you have a list of random phone numbers and you want to check duplicates.​

Ideas?

 Greatest Common Divisor : GCD

Euclid's Algorithm

For finding Gcd

gcd(a,b) -> gcd(b%a,a)  //if(a!=0)

Ex. gcd(6,15)

Step 1 : gcd(6,15)=gcd(15%6,6)=gcd(3,6)

Step 2 : gcd(3,6)=gcd(6%3,3)=gcd(0,3)

Step 3 : gcd(0,3)=3                // gcd(0,p)= p

Step 4 : gcd(6,15)=3                

Ex. gcd(12,32)

gcd(a,b)
{
r=b%a;
while(r!=0)
{
b=a;
a=r;
r=b%a;
}
return b;
}

Code for Euclid's Algorithm

Time Complexity of Euclid's Algo : O(logn)

Lcm(a,b) = (a*b)/gcd(a,b)
Lcm(a,b)=(ab)/gcd(a,b)Lcm(a,b) = (a*b)/gcd(a,b)

Questions ?

Thank You !

Made with Slides.com