Question

In: Computer Science

a) Write a trial run for the algorithm using the input 5,3,1,4 b) Use induction to...

a) Write a trial run for the algorithm using the input 5,3,1,4

b) Use induction to prove that your algorithm returns the correct value

The recursive algorithm in pseudocode is as follows (The input: a1,a2, ..., an a sequence of numbers where n>=1, the output: m, the minimum value of the sequence)

minvalue (a1,a2,...,an)

If(n=1) return a1

m: = minvalue(a2,a3,...,an)

If (a1

return m

Solutions

Expert Solution

In case of any queries, please revert back.

The Algorithm is as follows =>

minvalue (a1,a2,...,an) {

If(n=1) return a1

m: = minvalue(a2,a3,...,an)

If (a1 < m) return a1

return m}

Now, this is a recusrive function to find minimum value in a sequence of elements a1,a2,a3,a4....,an.

A.) Trial Run For the Algorithm

5,3,1,4

ITER 1 :

5,3,1,4

Now, n!=1 , so, we branch and call ITER 2 :

3,1,4

Now, n!=1, So, We branch and call ITER 3:

1,4

Now, n!=1, So, We branch and call ITER 4:

4

Now, n=1, So, ITER 4 returns 4 to ITER 3.

ITER 3 Compares return of ITER 4 that is 4 and 1.

Now, ITER 3 returns 1 to ITER 2.

ITER 2 Compares return of ITER 3 that is 1 and 3.

Now, ITER 2 returns 1 to ITER 1.

ITER 1 returns smaller of 1 and 5 which is 1

So, The final Function minvalue(5,3,1,4) returns 1.

B.) USE OF INDUCTION FOR PROVING CORRECTNESS.

Let us check the Base case First.

For, n=2 , We always get the correct value. This is becuase recursive call returns minimum of 2 values. So, the algortihm Satisfies the base case.

Now, Let us assume that algorithm gives correct result a1,a2....,am. We have to prove that algorithm will give correct result for a1,a2,a3.....,am,am+1.(In this way we can generalize algorithm over all Natural numbers N).

If we look at function and we add a value before a1 in a1,a2....,am ie. a0.

Now, we have :-

minvalue(a0,a1,a2......,am){

if(n=1) return a0

m = minvalue(a1,a2,a3.....,am) ////// We are sure that this statement gives correct value.

if(a0<m) return a0 //// So, the next 2 statements just compare m and a0.

return m //// They always return minimum of a0 and m.

}

Hence, our algorithm works for a1,a2.....,am+1 and so, we can generalize this for all natural numbers.

This is how inductive hypothesis proves correctness of the algorithm.


Related Solutions

Write a program in javascript to encrypt and decrypt the user input using the caesar algorithm...
Write a program in javascript to encrypt and decrypt the user input using the caesar algorithm with the substitution algorithm. Specify the min and max of the message user can enter to encrypt. Specify the length of the key user can enter to encrypt and decrypt the message. document your design by words or diagrams.
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
how to write program in java for encrypt and decrypt input text using DH algorithm
how to write program in java for encrypt and decrypt input text using DH algorithm
Using Python Question 1 Write an input function. Then use it to supply the input to...
Using Python Question 1 Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even. Question 2 Write function that asks for input from a user. If the user types 'end', the program exits, otherwise it just keeps going.
Pythons code using idle Write an input function. Then use it to supply the input to...
Pythons code using idle Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even.
Write a program to implement the Romberg Algorithm. Input: f(x), an interval [a,b], N (the number...
Write a program to implement the Romberg Algorithm. Input: f(x), an interval [a,b], N (the number of subintervals on the finest grid on level 0 is 2^N, therefore, N is usualy a small integer) Output: the triangle generated by Romberg Algorithm.
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Write an algorithm to input a number n, then calculate 13 +2 3 + 33 +...
Write an algorithm to input a number n, then calculate 13 +2 3 + 33 + ... + n3, the sum of the first n cubic numbers, and output the result. 2-Construct a trace table of the algorithm in question 1 with input n=4.
Write a MIPS program to implement the Bubble Sort algorithm, that sorts an input list of...
Write a MIPS program to implement the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 8 as...
Use a mathematical induction for Prove a^(2n-1) + b^(2n-1) is divisible by a + b, for...
Use a mathematical induction for Prove a^(2n-1) + b^(2n-1) is divisible by a + b, for n is a positive integer
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT