O'Reilly logo

Design and analysis of Algorithms, 2nd Edition by Himanshu B Dave

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix E

EXAMPLES OF ALGORITHMS

E.1  Existence of a Triangle

Develop an algorithm which determines whether or nor there exists a triangle having sides corresponding to the given set of three numbers (all positive).

Algorithm E.1 | Triangle (a, b, c)

 

1

if(a + b) > c AND(a + c) > bAND(b + c) > a then

2

return 1;

3

else

4

return 0;

5

end

E.2  GCD—Recursive Implementation

Develop a recursive algorithm which finds the GCD of the two given numbers a and b.

Algorithm E.2 | gcdr (a, b)

 

1

if a = b then return a;

2

if a > b then return gcdr(a – b, b);

3

if b > a then return gcdr(a, b – a);

E.3  Knight’s Tour

Knight’s Tour: In the game of Chess, a Knight can move to any of the eight different positions from a given ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required