|
1 | 1 | package ds.graph.vertexcover; |
2 | 2 |
|
| 3 | +import ds.graph.Graph; |
| 4 | +import ds.graph.UndirectedGraph; |
| 5 | +import ds.graph.Vertex; |
| 6 | + |
| 7 | +import java.util.*; |
| 8 | + |
3 | 9 | /** |
4 | 10 | * Created by sherxon on 3/21/17. |
5 | 11 | */ |
6 | 12 | public class PolynomialSolutionForVertexCover { |
| 13 | + public static void main(String[] args) { |
| 14 | + |
| 15 | + int ran = (int) (Math.random() * 100); |
| 16 | + System.out.println("number of graphs" + ran); |
| 17 | + for (int i = 0; i < ran; i++) { |
| 18 | + Graph<Integer> graph = new UndirectedGraph<>(); |
| 19 | + Graph<Integer> graphForBrute = new UndirectedGraph<>(); |
| 20 | + int randomVertexNum = (int) (Math.random() * 100); |
| 21 | + System.out.println("number of vertices" + randomVertexNum); |
| 22 | + for (int j = 0; j < randomVertexNum; j++) { |
| 23 | + graph.addVertex(j); |
| 24 | + graphForBrute.addVertex(j); |
| 25 | + } |
| 26 | + |
| 27 | + int numOfEdge = (int) (Math.random() * randomVertexNum); |
| 28 | + System.out.println("number of edges" + numOfEdge); |
| 29 | + for (int j = 0; j < numOfEdge; j++) { |
| 30 | + int from = (int) (Math.random() * 100); |
| 31 | + int to = (int) (Math.random() * 100); |
| 32 | + graph.addEdge(from, to); |
| 33 | + graphForBrute.addEdge(from, to); |
| 34 | + } |
| 35 | + Set<Vertex<Integer>> mySolution = getSolution(graph); |
| 36 | + Set<Vertex<Integer>> bruteSolution = getSolutionBrute(graphForBrute); |
| 37 | + // check solutions |
| 38 | + if (mySolution.size() != bruteSolution.size()) { |
| 39 | + System.out.println("found a bug"); |
| 40 | + System.out.println(mySolution); |
| 41 | + System.out.println(bruteSolution); |
| 42 | + } else { |
| 43 | + System.out.println("Size the same!!!!------------------------"); |
| 44 | + for (Vertex<Integer> integerVertex : mySolution) { |
| 45 | + if (!bruteSolution.contains(integerVertex)) { |
| 46 | + System.out.println("found a bug here"); |
| 47 | + System.out.println(integerVertex); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private static Set<Vertex<Integer>> getSolutionBrute(Graph<Integer> graph) { |
| 56 | + Set<Vertex<Integer>> set = new HashSet<>(); |
| 57 | + List<List<Vertex<Integer>>> powerSet = getPowerSet(graph.getVertices()); |
| 58 | + Collections.sort(powerSet, (a, b) -> a.size() - b.size()); |
| 59 | + for (List<Vertex<Integer>> vertexes : powerSet) { |
| 60 | + if (isMinVCover(vertexes, graph)) { |
| 61 | + return new HashSet<>(vertexes); |
| 62 | + } |
| 63 | + } |
| 64 | + return set; |
| 65 | + } |
| 66 | + |
| 67 | + private static List<List<Vertex<Integer>>> getPowerSet(Collection<Vertex<Integer>> vertices) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + private static boolean isMinVCover(List<Vertex<Integer>> vertexes, Graph<Integer> graph) { |
| 72 | + Set<Vertex<Integer>> set = new HashSet<>(vertexes); |
| 73 | + |
| 74 | + return false; |
| 75 | + } |
7 | 76 |
|
| 77 | + private static Set<Vertex<Integer>> getSolution(Graph<Integer> graph) { |
| 78 | + Set<Vertex<Integer>> set = new HashSet<>(); |
| 79 | + return set; |
| 80 | + } |
8 | 81 | } |
0 commit comments