Question: did i do the two classes correctly? 1 . exactVC. This algorithm will find optimal vertex covers. 2 . exactIS. This algorithm will find optimal

did i do the two classes correctly?
1. exactVC. This algorithm will find optimal vertex covers.
2. exactIS. This algorithm will find optimal independent sets.
public class Graph {
// return an array containing the vertex numbers of an optimal VC.
public static int[] exactVC(Graph inputGraph){
int[][] graph = inputGraph.getGraph();
boolean[] coveredEdges = new boolean[graph.length]; // Track if edges are covered
List vertexCover = new ArrayList<>();
// Loop until all edges are covered
while (!allEdgesCovered(graph, coveredEdges)){
int bestVertex =-1;
int maxCovered =-1;
// Find the vertex that covers the most uncovered edges
for (int i =0; i < graph.length; i++){
int uncoveredEdgeCount =0;
// Loop through the neighbors of vertex 'i'
for (int neighbor : graph[i]){
if (!coveredEdges[i]||!coveredEdges[neighbor]){
uncoveredEdgeCount++;
}
}
//System.out.println("Vertex "+ i +" covers "+ uncoveredEdgeCount +" uncovered edges.");
if (uncoveredEdgeCount > maxCovered){
maxCovered = uncoveredEdgeCount;
bestVertex = i;
}
}
// If no best vertex is found, there's an issue
if (bestVertex ==-1){
System.out.println("Error: No valid vertex found!");
break;
}
// Add the best vertex to the vertex cover
vertexCover.add(bestVertex);
// Mark edges incident to the best vertex as covered
for (int neighbor : graph[bestVertex]){
coveredEdges[bestVertex]= true;
coveredEdges[neighbor]= true;
}
}
// Return the vertex cover as an array
return vertexCover.stream().mapToInt(Integer::intValue).toArray();
}
// Helper function to check if all edges are covered
private static boolean allEdgesCovered(int[][] graph, boolean[] coveredEdges){
for (int i =0; i < graph.length; i++){
for (int neighbor : graph[i]){
if (!coveredEdges[i] && !coveredEdges[neighbor]){
return false; // An uncovered edge exists
}
}
}
return true; // All edges are covered
}
public static int[] optimalIS(Graph inputGraph){
int[][] adjacencyList = inputGraph.getGraph();
int n = adjacencyList.length;
boolean[] selected = new boolean[n];
List result = new ArrayList<>();
// Helper function for backtracking
findMaxIndependentSet(adjacencyList, selected, 0, new ArrayList<>(), result);
// Convert result list to array
int[] optimalSet = result.stream().mapToInt(Integer::intValue).toArray();
System.out.println("Optimal Independent Set found: "+ Arrays.toString(optimalSet));
return optimalSet;
}
private static void findMaxIndependentSet(
int[][] adjList, boolean[] selected, int index, List currentSet, List bestSet){
if (index == adjList.length){
if (currentSet.size()> bestSet.size()){
bestSet.clear();
bestSet.addAll(currentSet);
}
return;
}
// Exclude the current vertex
findMaxIndependentSet(adjList, selected, index +1, currentSet, bestSet);
// Include the current vertex if possible
boolean canInclude = true;
for (int neighbor : adjList[index]){
if (selected[neighbor]){
canInclude = false;
break;
}
}
if (canInclude){
selected[index]= true;
currentSet.add(index);
findMaxIndependentSet(adjList, selected, index +1, currentSet, bestSet);
currentSet.remove(currentSet.size()-1);
selected[index]= false;
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!