CPSC 212-301 Test 3A: MyDisjointSet Class Implementation, Exams of Algorithms and Programming

The instructions and requirements for completing a programming assignment in the cpsc 212-301 course. Students are tasked with implementing a mydisjointset class with methods find, unionbyheight, tostring, and treestructure for a disjoint set represented by an array x[]. The document also includes an honor pledge and submission instructions.

Typology: Exams

Pre 2010

Uploaded on 07/28/2009

koofers-user-tb1
koofers-user-tb1 🇺🇸

8 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPSC 212-301 Name: _____________________________
Test #3A December 4, 2006
Honor Pledge:
I pledge that I have neither given nor received unauthorized assistance on this test.
Signed: _______________________ Date: _____________
20 points. 50 minutes. Open Java API. You may not visit any other web page. You are to develop a class
called MyDisjointSet with one constructor:
public MyDisjointSet (int x[]) {
// ... your code ...
} // MyDisjointSet
where x[] represents a disjoint set as discussed in class and in your text. Index i is the root of a tree iff the
contents of x[i] is negative. The array x[] has been initialized. You may assume that there are no errors.
In class, we discussed the disjoint set operations of find and union by height. Complete the following
public methods (5 points each):
public int find(int u) { public void unionByHeight(int u, int v) {
// ... your code ... // ... your code ...
} // find } // unionByHeight
Recall that find(int u)returns the set (index) to which node u belongs. Note that this method need not
perform a smart find operation. The method unionbyHeight() takes the union of the two sets to which u
and v belong. You may discover that your find()method will be useful in your unionByHeight() method.
Write a toString() method (5 points) that returns a String containing the contents of the array.
public String toString() {
// ... your code ...
} // toString
The returned String should be neatly formatted so that the output looks something like:
i: 0 1 2 3 4 5 6 7
x[]: 6 2 4 4 -4 -2 -2 5
You may assume that the contents of the array are at most 2-digit numbers.
Finally, write a method (5 points) called treeStructure():
public String treeStructure () {
// ... your code ...
} // treeStructure
which returns a String that shows the dependences of the disjoint set.
For example, consider the disjoint set on the right
represented by the array x[] below:
i: 0 1 2 3 4 5 6 7
x[]: 6 2 4 4 -2 -1 -1 5
pf2

Partial preview of the text

Download CPSC 212-301 Test 3A: MyDisjointSet Class Implementation and more Exams Algorithms and Programming in PDF only on Docsity!

CPSC 212-301 Name: _____________________________

Test #3A December 4, 2006

Honor Pledge:

I pledge that I have neither given nor received unauthorized assistance on this test.

Signed: _______________________ Date: _____________

20 points. 50 minutes. Open Java API. You may not visit any other web page. You are to develop a class

called MyDisjointSet with one constructor:

public MyDisjointSet (int x[]) { // ... your code ... } // MyDisjointSet

where x[] represents a disjoint set as discussed in class and in your text. Index i is the root of a tree iff the

contents of x[i] is negative. The array x[] has been initialized. You may assume that there are no errors.

In class, we discussed the disjoint set operations of find and union by height. Complete the following

public methods (5 points each):

public int find(int u) { public void unionByHeight(int u, int v) { // ... your code ... // ... your code ... } // find } // unionByHeight

Recall that find(int u)returns the set (index) to which node u belongs. Note that this method need not

perform a smart find operation. The method unionbyHeight() takes the union of the two sets to which u

and v belong. You may discover that your find()method will be useful in your unionByHeight() method.

Write a toString() method (5 points) that returns a String containing the contents of the array.

public String toString() { // ... your code ... } // toString

The returned String should be neatly formatted so that the output looks something like:

i: 0 1 2 3 4 5 6 7 x[]: 6 2 4 4 -4 -2 -2 5

You may assume that the contents of the array are at most 2-digit numbers.

Finally, write a method (5 points) called treeStructure():

public String treeStructure () { // ... your code ... } // treeStructure

which returns a String that shows the dependences of the disjoint set.

For example, consider the disjoint set on the right

represented by the array x[] below:

i: 0 1 2 3 4 5 6 7 x[]: 6 2 4 4 -2 -1 -1 5

The set has four trees rooted at nodes 4, 5, and 6. Your method should return a String that describes the

trees in a manner similar to the following:

roots: 4 5 6 children of 4: 2 3 children of 2: 1 children of 5: 7 children of 6: 0

You may download the skeleton now. (Go to the CPSC 212 Schedule page, December 4, 2006.)

Submission:

Submit your work using: handin.212.301 3 MyDisjointSet.java

Also submit any helper methods and/or classes you developed during this test and required by your class.

Important:

As always, if you cannot complete one or more of the methods, be sure to include at least a skeleton of the

method which prints the message:

“Method ___________ not completed”

so that my test driver will be able to compile your program.

Evaluation:

Your program will be evaluated on the correctness of algorithm:

find() 5 points

unionByHeight() 5 points

toString() 5 points

treeStructure() 5 points