Question: did i do the two classes correctly? 1 . inexactVC. This algorithm will find non - optimal vertex covers, but must run in polynomial time.
did i do the two classes correctly?
inexactVC. This algorithm will find nonoptimal vertex covers, but must run in polynomial time. Your solution cannot be trivial eg the set of all vertices is a trivial VC
inexactIS. This algorithm will find nonoptimal independent sets, but must run in
polynomial time. Your solution cannot be trivial eg the set of no vertices is a trivial IS
public class Graph
return in polynomial time an array containing the vertex numbers of a VC
public static int inexactVCGraph inputGraph
int graph inputGraph.getGraph;
boolean coveredEdges new booleangraphlength;
List vertexCover new ArrayList;
Loop over all vertices
for int i ; i graph.length; i
for int neighbor : graphi
if coveredEdgesi && coveredEdgesneighbor
vertexCover.addi;
vertexCover.addneighbor;
coveredEdgesi true;
coveredEdgesneighbor true;
break; Move to the next vertex once an edge is covered
Return the vertex cover as an array
return vertexCover.streammapToIntInteger::intValuetoArray;
public static int inexactISGraph inputGraph
int adjacencyList inputGraph.getGraph;
int n adjacencyList.length;
Initialize the independent set array means selected, means not selected
int independentSet new intn;
Arrays.fillindependentSet; Start with an empty independent set
To keep track of selected vertices and their neighbors
boolean inSet new booleann; Whether a vertex is in the independent set
Iterate over all vertices and add to the independent set if possible
for int vertex ; vertex n; vertex
if inSetvertex If vertex is not already in the independent set
Add the vertex to the independent set
independentSetvertex;
inSetvertex true;
Mark all neighbors of the vertex as excluded
for int neighbor : adjacencyListvertex
inSetneighbor true;
Collect the vertices that are in the independent set
int setSize ;
for int i ; i n; i
if independentSeti
setSize;
Create a result array to store the independent set vertices
int result new intsetSize;
int index ;
for int i ; i n; i
if independentSeti
resultindex i;
System.out.printlnInexact Independent Set found: Arrays.toStringresult;
return result;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
