Computer Science Homework Solutions
Problem
#6555

A problem about Verification in Java

A problem about Verification in Java. Please see attachment. The original course website where the problem comes from is here, I think it will be helpful if you take a look at it first: www.student.math.uwaterloo.ca/~cs134

Attached file(s):
Attachments
Question.doc  View File

Attachment Content Summary (Note: view attachment at the above link before purchasing. Actual attachment content may vary slightly from that shown below.)

Question.doc
Question: Part (A)

Consider the maximum subsequence sum problem (and Bentley's solution
(*)) discussed on slide 80 of the course notes

: given an integer array A, find the values i and j, which maximizes


j

∑A[k]

k=i

A solution to the problem can be solved in O(n) time as stated on slide
81. The algorithm is as follows (assuming that it is contained within a
method of an object that implements the ADT List):

public int maxSubsequenceSum() {

// pre: this is a list of integers

// post: returns the maximum sum(A[i..j]) over all indices i and j

// where 1 <= i <= j <= this.size(). If the sum is negative,

// then returns zero instead.

int nMaxSoFar = 0;

int nMaxEndingHere = 0;

for (int i = 1;

// inv:

i <= this.size(); i++) {

nMaxEndingHere = Math.max(0, nMaxEndingHere +

((Integer)(this.get(i)).intValue());

nMaxSoFar = Math.max(nMaxSoFar, nMaxEndingHere);

}

// term:

return nMaxSoFar;

}

The key to understanding this algorithm is the variable nMaxEndingHere.
Before the first assignment statement in the loop, nMaxEndingHere
contains the value of the maximum subsequence ending in position i-1;
the assignment statement modifies it to contain the value of the maximum
subsequence ending in position i. The statement increases nMaxEndingHere
by the value this[i] so long as doing so keeps it positive; when
nMaxEndingHere becomes negative, it is reset to zero (that is, the
maximum subsequence ending at i is the empty sequence).

Provide inv, an invariant for the for-loop.

Provide term, a termination condition for the for-loop.

Carefully prove that inv + exit-loop Ю term

You may assume that the code for Math.max is correct.

(*) Jon Bentley, "Programming Pearls: Algorithm Design Techniques",
Comm. ACM 27, 9 (Sept. 1984) pp. 865-871

Part (B)

Prove that the following recursive method is correct. All the assertions
you will need are given to you. You may assume that the left() and
right() methods return, respectively, the left and right subtrees of the
binary tree on which the size method is called.

Note: Since this is a recursive method, you need to use induction.

public int size () {

// pre: this is a binary tree

// post:
retu湲⁳畮扭牥漠⁦汥浥湥獴椠桴獩ഠ晩⠠獩浅瑰⡹
笠‍†⼯搠湯㩥琠楨⁳獩攠灭祴愠摮琠楨❳⁳楳敺椠
ര††敲畴湲〠※ഠ†素‍†⼯渠瑯潄敮›桴獩椠⁳
⁴浥瑰⁹湡⁤桴獩猧猠穩⁥獩愠⁴敬獡⁴റ††‍†
畴湲ㄠ⬠氠晥⡴⸩楳敺⤨⬠爠杩瑨⤨献穩⡥㨩

Њ



љ

љ

Ь

&

'

I

J

Ѓ

І

љ



%

A



м

т

у

਀&䘋

⑛尀$摥ᬬ䴝฀ // sizeFound: this's size is 1 plus sum of sizes
of

// its left and right subtrees

}
Solution
What is this?
By OTA - Overall OTA Rating
Tyler Xie, MSc (IP) - 4.5/5
Purchase Cost Now
$2.19 CAD (was ~$15.96)
Included in Download
  • Plain text response
  • Attached file(s):
    • Answer_6555.doc
$2.19 Instant Download
Add to Cart
Why you can trust BrainMass.com
  • Your Information is Secure
  • Best Online Academic Help Service
  • Students find real academic Success
Related Solutions
  • A problem about data structure in Java - Question: You have a computer where multiplication is an enormously expensive operation compared to addition. Consider an algorithm for computing cubes using only addition (FYI: the first 5 cubes are ...
  • How do you envision the ways program verification and performance tuning will be accomplished in the future? - How do you envision the ways program verification and performance tuning will be accomplished in the future? Will it still be a work of art? Will it always be the result of one's experience? Or do yo ...
  • HTML project - Validate the credit card expiration year Verify in a comparison with the current date that the credit card expiration date is valid Add a mortgage calculator to the page as a value added aspect fo ...
  • A problem about Sorting in Java - Part (A) Sort the array 15 80 35 25 60 30 into descending order using a) the selection sort. b) the buble sort. Part (B) A first year student attempted to write mergesort in pseudoco ...
  • A problem about binary trees in java - A problem about binary trees in java. Please see attachment. The original course website where the problem comes from is here, I think it will be helpful if you take a look at it first: www.student.ma ...
Browse