This program solves the minimum dominating set but it takes an insanely long time to run. In case you do not know, in graph theory, a dominating set for a graph G is a subset D of its vertices, such that any vertex of G is either in D, or has a neighbor in D. Can someone help me make my code more efficient.
import java.io.FileNotFoundException;
import java.util.*;
import java.io.File;
public class FireStationPlacement {
public static void main(String[] args) {
Graph graph = new Graph();
try {
Scanner Layout2 = new Scanner( new File("text/LayoutSecond.txt") );
for (int x=0;x<5;x++) {
graph.addEdge(Layout2.nextInt(), 1, Layout2);
}
for (int x=0;x<20;x++) {
graph.addEdge(Layout2.nextInt(), 2, Layout2);
}
for (int x=0;x<6;x++) {
graph.addEdge(Layout2.nextInt(), 3,Layout2);
}
Layout2.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.err.println( "File not found" );
}
// Print the adjacency list representation of the graph
System.out.print(graph.minFireStationsBruteForce(graph.adjacencyList));
}
}
class Graph {
Map<Integer, List<Integer>> adjacencyList;
Graph() {
adjacencyList = new HashMap<>();
}
void addEdge(int vertex, int num, Scanner layout2) {
if (num==1) {
adjacencyList.put(vertex, Arrays.asList(layout2.nextInt()));
}
else if (num==2) {
adjacencyList.put(vertex, Arrays.asList(layout2.nextInt(),layout2.nextInt()));
}
else if (num==3) {
adjacencyList.put(vertex, Arrays.asList(layout2.nextInt(),layout2.nextInt(),layout2.nextInt() ));
}
}
List<Integer> getNeighbors(int vertex) {
return adjacencyList.getOrDefault(vertex, Collections.emptyList());
}
private void dfs(int currentVertex, Set<Integer> visited) {
visited.add(currentVertex);
for (int neighbor : adjacencyList.get(currentVertex)) {
if (!visited.contains(neighbor)) {
dfs(neighbor, visited);
}
}
}
public void dfsHelper(int startVertex) {
Set<Integer> visited = new HashSet<>();
dfs(startVertex, visited);
}
static boolean isCovered(Set<Integer> subset, Map<Integer, List<Integer>> graph) {
Set<Integer> covered = new HashSet<>(subset);
for (int neighborhood : subset) {
covered.addAll(graph.get(neighborhood));
}
return covered.equals(graph.keySet());
}
public static int minFireStationsBruteForce( Map<Integer, List<Integer>> graph) {
int n = graph.size();
System.out.println("Graph size is " + n);
List<Integer> neighborhoods = new ArrayList<>(graph.keySet());
for (int k = 1; k <= 31; k++) {
for (Set<Integer> subset : combinations(neighborhoods, k)) {
if (isCovered(subset, graph)) {
System.out.println(subset.toString());
return k;
}
}
}
return n;
}
private static Set<Set<Integer>> combinations(List<Integer> elements, int k) {
Set<Set<Integer>> allCombinations = new HashSet<>();
combinations(allCombinations, new HashSet<>(), elements, k, 0);
return allCombinations;
}
private static void combinations(Set<Set<Integer>> allCombinations, Set<Integer> current, List<Integer> elements, int k, int start) {
if (current.size() == k) {
allCombinations.add(new HashSet<>(current));
return;
}
for (int i = start; i < elements.size(); i++) {
current.add(elements.get(i));
combinations(allCombinations, current, elements, k, i + 1);
current.remove(elements.get(i));
}
}
}