Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2020
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. import java.util.*;
  2. import java.time.LocalTime;
  3.  
  4. public class solveClique {
  5.   //Current max clique
  6.   static List<Integer> max = new ArrayList<>();
  7.  
  8.   public static void main(String args[]) {
  9.     String graphFilename = "graphs2020.txt";
  10.     GraphParser gr = new GraphParser(graphFilename);
  11.     ArrayList<Graph> graphs = gr.getGraphs();
  12.  
  13.     System.out.print(LocalTime.now());
  14.     for (int i = 0; i < graphs.size(); i++) {
  15.  
  16.       System.out.print("(" + i + ")");
  17.       findMaxClique(graphs.get(i));
  18.     }
  19.     System.out.print(LocalTime.now());
  20.   }
  21.  
  22.   public static void findMaxClique(Graph graph) {
  23.     max = new ArrayList<>();
  24.  
  25.     //don't check nodes with degree of 1
  26.     for (int i = 0; i < graph.size; i++) {
  27.       List<Integer> clique = new ArrayList<>();
  28.       clique.add(i);
  29.       maxClique(graph, clique);
  30.     }
  31.  
  32.     System.out.println(graph.size + ": " + max.size() + " " + Arrays.toString(max.toArray()));
  33.   }
  34.  
  35.   // MEAT HERE
  36.   public static void maxClique(Graph graph, List<Integer> clique) {
  37.     //Get the newest node added to the clique
  38.     int currentValue = clique.get(clique.size() - 1);
  39.  
  40.     if (clique.size() > max.size()) {
  41.       max = clique;
  42.     }
  43.  
  44.     // Check every node
  45.     for (int i = 0; i < graph.size; i++) {
  46.  
  47.       // If the node being checked is connected to the current node, and isn't the current node
  48.       if (graph.matrix[currentValue][i] == 1 && !clique.contains(i)) {
  49.         //Check if the new clique is bigger than the current max.
  50.  
  51.  
  52.         //Make a new clique and add all the nodes from the current clique to it
  53.         ArrayList<Integer> newClique = new ArrayList<>();
  54.         newClique.addAll(clique);
  55.         //Add the new node
  56.         newClique.add(i);
  57.  
  58.         //Repeat
  59.         if (makesNewClique(graph, clique, i)) {
  60.           maxClique(graph, newClique);
  61.         }
  62.       }
  63.     }
  64.   }
  65.  
  66.  
  67.   public static boolean makesNewClique(Graph graph, List<Integer> clique, int newNode) {
  68.     for (int node : clique) {
  69.       if (graph.matrix[node][newNode] == 0) {
  70.         return false;
  71.       }
  72.     }
  73.     return true;
  74.   }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement