Java. hello Please I just need to complete 2 simple methods but Im getting lost
ID: 3702278 • Letter: J
Question
Java. hello Please I just need to complete 2 simple methods but Im getting lost and frusatrted. its due in 2hours so please help if you can. I will poast the code and the question. Just answer Question 2 please. Its a Kruskal’s algorithm problem. Please just 2 methods. In question 2. I will just put link to the drive for the template if you need it, thats where the classes is, but I will just post the code below. Its commented "TODO" thats the one to complete. https://drive.google.com/file/d/1f4G53Dtj1lmGlRz6bFNlPAVFN9Aj6ps5/view?usp=sharing
import lib280.graph.Edge280;
import lib280.graph.GraphAdjListRep280;
import lib280.graph.Vertex280;
public class UnionFind280 {
GraphAdjListRep280<Vertex280, Edge280<Vertex280>> G;
/**
* Create a new union-find structure.
*
* @param numElements Number of elements (numbered 1 through numElements, inclusive) in the set.
* @postcond The structure is initialized such that each element is in its own subset.
*/
public UnionFind280(int numElements) {
G = new GraphAdjListRep280<Vertex280, Edge280<Vertex280>>(numElements, true);
G.ensureVertices(numElements);
}
/**
* Return the representative element (equivalence class) of a given element.
* @param id The elements whose equivalence class we wish to find.
* @return The representative element (equivalence class) of the element 'id'.
*/
public int find(int id) {
// TODO - Write this method
return 0; // Remove this when you are ready. It is just a placeholder to prevent a compiler error.
}
/**
* Merge the subsets containing two items, id1 and id2, making them, and all of the other elemnets in both sets, "equivalent".
* @param id1 First element.
* @param id2 Second element.
*/
public void union(int id1, int id2) {
// TODO - Write this method.
}
}
1. Import the project Kruskal-Template (provided) module into Intellij workspace. You may need to add the lib280-asn8 project (also provided) as a module depdnency of the Kruskal-Template module (this process is covered in the self-guided tutorials on Moodle) 2. In the UnionFind280 class in the Kruskal-Template project, complete the implementation of the methods union ) and find). Do not modify anything else. You may add a main method to the UnionFind class for testing purposesExplanation / Answer
UnionFind280.java
package lib280.algo;
import lib280.graph.Edge280;
import lib280.graph.GraphAdjListRep280;
import lib280.graph.Vertex280;
public class UnionFind280
{
GraphAdjListRep280<Vertex280, Edge280<Vertex280>> G;
/**
* Create a new union-find structure.
*
* @param numElements Number of elements (numbered 1 through numElements, inclusive) in the set.
* @postcond The structure is initialized such that each element is in its own subset.
*/
public UnionFind280(int numElements) {
G = new GraphAdjListRep280<Vertex280, Edge280<Vertex280>>(numElements, true);
G.ensureVertices(numElements);
}
/**
* Return the representative element (equivalence class) of a given element.
* @param id The elements whose equivalence class we wish to find.
* @return The representative element (equivalence class) of the element 'id'.
*/
public int find(int id) {
Vertex280 x = G.vertex(id);
G.goVertex(x);
G.eGoFirst(x);
// Follow the chain of directed edges starting from x
while (G.eItemExists()) {
G.goIndex(G.eItem().secondItem().index());
G.eGoFirst(G.item());
}
return G.item().index();
}
/**
* Merge the subsets containing two items, id1 and id2, making them,
* and all of the other elements in both sets, "equivalent".
* @param id1 First element.
* @param id2 Second element.
*/
public void union(int id1, int id2) {
int rep1 = find(id1);
int rep2 = find(id2);
if (rep1 != rep2)
G.addEdge(rep1, rep2);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.