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

ADA Exercise - 1 (Recursion Trees For GCD, Max and Power)

The document contains 3 questions about analyzing recursive algorithms: 1. It asks to draw the recursion tree and return values for a recursive algorithm to find the maximum element in an array containing 10 elements. 2. It asks to draw and compare the recursion trees for two recursive algorithms to calculate the greatest common divisor (GCD) of two numbers for 4 different number pairs. 3. It asks how many times a recursive power function is called to calculate 2^7, and to improve the algorithm to reduce calls.

Uploaded by

md.zainul haque
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

ADA Exercise - 1 (Recursion Trees For GCD, Max and Power)

The document contains 3 questions about analyzing recursive algorithms: 1. It asks to draw the recursion tree and return values for a recursive algorithm to find the maximum element in an array containing 10 elements. 2. It asks to draw and compare the recursion trees for two recursive algorithms to calculate the greatest common divisor (GCD) of two numbers for 4 different number pairs. 3. It asks how many times a recursive power function is called to calculate 2^7, and to improve the algorithm to reduce calls.

Uploaded by

md.zainul haque
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Analysis and Design of Algorithms Assignment -1

Q1 Consider the following recursive algorithm to pick up the largest element


from an array of N elements. Max_of_3(int , int , int ) is function that returns
the largest of 3 integers.

int max(int arr[] , int left , int right )


{ if( left < right)
{
int x,y,mid;
int mid = (left + right) / 2 ;
x = max( arr, left , mid-1 ); // the max element from the left partition
m = arr[mid]; // the mid element of the current partition
y = max( arr, mid+1, right); // the max element from the right partition
r = max_of_3( x,m,y);
return r;
}
return arr[left];
}//end of function max
Draw the recursion tree for the above recursive function and show the return
values for the function call on the array [5, 10, 9, 2, 10, 4, 8, 16, 3, 7] .
Q2 Consider the following two recursive algorithms to calculate the GCD of
two integers .
int gcd(int a, int b) / * Algorithm – 1 (Euclidean Algorithm for GCD) */
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int gcd(int a, int b) /* Algorithm – 2 */
{
if (a == 0) return b;
if (b == 0) return a;
if (a == b) return a;
if (a > b) return gcd(a-b, b);
return gcd(a, b-a);
}
Draw the Recursion Tree for the following function calls for both the
algorithms and compare them in terms of their depth.
a) gcd( 98,56)
b) gcd(98,98)
c) gcd(98,1)
d) gcd(98,49)
Q3 Consider the following recursive method for calculating the nth power of x
, where both n is a non-negative integer.
int power(int x, int n)
{
if( n == 0)
return 1;
return x * power(x,n-1);
}
a) How many times is the above recursive method called for calculating
the value power(2,7) ?

b) Improvise the algorithm to reduce the number of recursive function


calls.

You might also like