Ive been assigned a project where by i must find an MST via Kruskals algorithm.
The problem I'm having is that I've been instructed to make a forest:
"The forest can be implemented as a 2-D array of Boolean values (or as an array of linked list if
you prefer). This is a static structure but will make the initial implementation easier. The ith
row contains the component starting with vertex i.
If an element in a row is marked as `1` then that vertex id is in that component.
Operations on the Forest are:
1. InitialiseForest : initialise component i with vertex i.
2. FindVertex: search the Forest looking for vertex – return the component number
where vertex is found. Otherwise return -1 as error case.
3. MergeComponents: merge the components c1 and c2 of the forest"
I guess this is due to detecting cycles which may appear during the MST construction via Kruskals algorithm and hence cycle detection can be done by:
- checking for connected components:
- Maintain a set of connected components – initially each vertex in its own component
- When a new edge (u,v) is selected
- –If u and v are in the same component then adding (u,v) creates a cycle
- –If not a cycle then merge the connected components containing u and v
The following is the code that I already have set up. Any help would be really appreciated, thanks so much
FOR CLASS "Edge":
public class Edge {
public static int totalV = 1; //keeps record and helps set the total vertexes
public static int totalEdges = 0; //keeps record of how many vertexes there are
public int v; int w; //v and w are the vertexes assosiated with the edge
public int weight; //this is the cost/weight of the edge
public Edge(int v, int w, int weight)
{
this.v = v;
this.w = w;
this.weight = weight;
totalEdges++;
}
public int either()
{
return v;
}
public int other(int vertex)
{
if(vertex == v)
{
return w;
}
else
{
return v;
}
}
//////////////////////////////GETTERS
public int getWeight()
{
return weight;
}
public int getV()
{
return v;
}
public int getW()
{
return w;
}
public void setWeight(int weightArg) {
weight = weightArg;
}
}
FOR CLASS "WeightedGraph":
import java.util.ArrayList;
public class WeightedGraph {
private ArrayList<Edge> weightedGraph = new ArrayList<Edge>();
private boolean adjacencyMatrix[][];
private int vertexCount;
public WeightedGraph(int vertexCount)
{
this.vertexCount = vertexCount;
adjacencyMatrix = new boolean[vertexCount][vertexCount];
}
public ArrayList<Edge> getEdges()
{
return weightedGraph;
}
public ArrayList<Edge> sortByWeight()
{
for(int i = 0; i < weightedGraph.size(); i++)
{
for(int j = 1; j < weightedGraph.size(); j++)
{
int a = weightedGraph.get(i).getWeight();
int b = weightedGraph.get(j).getWeight();
if(b < a)
{
weightedGraph.get(i).setWeight(b);
weightedGraph.get(j).setWeight(a);
}
}
}
return weightedGraph;
}
public void addEdge(Edge e)
{
int i = e.getV();
int j = e.getW();
if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) {//checks wether edge vertices are within range
adjacencyMatrix[i][j] = true;
adjacencyMatrix[j][i] = true;
weightedGraph.add(e);
System.out.print("Edge added.\n");
}
else if(i > vertexCount)
{
System.out.print("Sorry, the first vertex is not within range (Vertex value too large)");
}
else if(i < 0)
{
System.out.print("Sorry, the first vertex is not within range (No negative vertexes allowed)");
}
else if(j > vertexCount)
{
System.out.print("Sorry, the second vertex is not within range (Vertex value too large)");
}
else if(i < 0)
{
System.out.print("Sorry, the second vertex is not within range (No negative vertexes allowed)");
}
}
public void removeEdge(Edge e)
{
int i = e.getV();
int j = e.getW();
if (i > 0 && i < vertexCount && j > 0 && j <= vertexCount) {
adjacencyMatrix[i][j] = false;
adjacencyMatrix[j][i] = false;
weightedGraph.remove(e);
System.out.print("Edge removed.\n");
}
else if(i > vertexCount)
{
System.out.print("Sorry, the first vertex is not within range (Vertex value too large)");
}
else if(i < 0)
{
System.out.print("Sorry, the first vertex is not within range (No negative vertexes allowed)");
}
else if(j > vertexCount)
{
System.out.print("Sorry, the second vertex is not within range (Vertex value too large)");
}
else if(i < 0)
{
System.out.print("Sorry, the second vertex is not within range (No negative vertexes allowed)");
}
}
public boolean isEdge(Edge e)
{
int i = e.getV();
int j = e.getW();
if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
return adjacencyMatrix[i][j];
else
return false;
}
}