Problem Description
proving program correctness
Consider the following program specification:
Input: An integer n > 0 and an array A[0..(n - 1)] of n integers.
Output: The smallest index s such that A[s] is the largest value in A[0..(n - 1)].
For example, if n = 9 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 1, since the largest value in A is 8 and the smallest index at which 8 appears is index 1.
Consider the following implementation:
| indexOfMax(n, A) | ||||
| (1) | i ← n - 2 | |||
| (2) | m ← A[n - 1] | |||
| (3) | s ← n - 1 | |||
| (4) | while i ≥ 0 | |||
| (5) | if A[i] ≥ m then | |||
| (6) | m ← A[i] | |||
| (7) | s ← i | |||
| (8) | end | |||
| (9) | i ← i - 1 | |||
| (10) | end | |||
| (11) | return s | |||
In the implementation above, line numbers are given so you can refer to specific lines in your answers and ← is used to indicate assignment.
Part A
Use induction to establish the following loop invariant right before the while test in line (4) is executed:
i. -1 ≤ i < n - 1
ii. m = A[s]
iii. m = max A[(i + 1) .. (n - 1)] (i.e., m is the maximum value in that appears in the array A between indices i + 1 and n - 1, inclusive)
iv. s is the smallest index at which max A[(i + 1) .. (n - 1)] appears
Hints and tips:
Use only one induction proof and prove each of the four parts of the invariant in your base case and inductive step.
You may assume that i and s will always be integers (i.e., you don't have to prove this).
Part B
Prove the correctness of the implementation by arguing partial correctness and termination.
Problem Description proving program correctness Consider the following program specification: Input: An integer n > 0...
Argue the correctness of Heapsort using the following loop invariant for the second loop in the algorithm (after the array has been heapified): "At the start of each iteration of the for loop, the subarray A[0 ... i-1] is a max-heap containing the i smallest elements of the original array, and the subarray A[i ... n-1] contains the n-i largest elements of the original array in sorted order
10. (10 points) Computational problem solving: Proving correctness: Function g (n: nonnegative integer) if n si then return(n) else return(5*g(n-1) - 6*g(n-2)) Prove by induction that algorithm g is correct, if it is intended to compute the function 3"-2" for all n 20. Base Case Proof: Inductive Hypothesis: Inductive Step:
An array A of n integers is called semi-sorted if it is increasing until some index and then decreasing afterwards. In other words, there is some index 1 ≤ p ≤ n such that: • A[i] < A[i + 1] for 1 ≤ i < p, and • A[i] > A[i + 1] for p ≤ i < n. Note that in this case, A[p] is the maximum element of A. Give an algorithm with running time O(logn) that finds...
Java question
Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...
Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A if A[0] + A[1] + ... + A[i-1] = A[i+1] + A[i+2] + ... + A[n-1]; where 0 < i < n-1. Similarly, 0 is an stability index if (A[1] + A[2] + ... + A[n-1]) = 0 and...
Prove procedure to compute Fibinocci(n) where F0 = 0, F1 = 1, Fn = Fn-2 + Fn-1. Prove by establishing and proving loop invariant then using induction to prove soundness and termination. 1: Procedure Fib(n) 2: i←0,j←1,k←1,m←n 3: while m ≥ 3 do 4: m←m−3 5: i←j+k 6: j←i+k 7: k←i+j 8: if m = 0 then 9: return i 10: else if m = 1 then 11: return j 12: else 13. return k
Prove procedure to compute Fibinocci(n) where F0 = 0, F1 = 1, Fn = Fn-2 + Fn-1. Prove by establishing and proving loop invariant then using induction to prove soundness and termination. 1: Procedure Fib(n) 2: i←0,j←1,k←1,m←n 3: while m ≥ 3 do 4: m←m−3 5: i←j+k 6: j←i+k 7: k←i+j 8: if m = 0 then 9: return i 10: else if m = 1 then 11: return j 12: else 13. return k
(15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an earray A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A i=j-1 while i > 0 and A[i] > key do Ali + 1] = Ai i=i-1 7: A[i+1] = key (a) Follow this algorithm for A[1..4) =< 7,9,6,8 >. Specifically, please indicate the contents of the array after each iteration of the outer...
PLEASE USE INDUCTION, NO PROGRAMS ALLOWED
Consider the following program, where a and n are positive integers. Input: a, n x = a; m = n; y = 1; while (m > 1) {if m is even x = x*x; m = m/2; if m is odd y = x*y; x = x*x; m = (m - 1)/2;} Output x*y Show by induction that the above loop has the following invariant: a^n = x^m times y. What does the above...
An array A[1,2,... ,n is unimodal if its consists of an increasing sequence followed by sequence a decreasing sequence. More precisely, there exists an index k є {1,2,… ,n} such that there exists an indes . AlE]< Ali1 for all 1 i< k, and Ai]Ali 1 for all k< i< n A1,2,..,n] in O(logn) time the loop invariant (s) that your algorithm maintains and show why they lead to the correctness Give an algorithm to compute the maximum element of...