Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kz.logic;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Main {
- private static Scanner scanner = new Scanner(System.in);
- private static Plot plot;
- private static ArrayList<Vertex> graph;
- private static ArrayList<Vertex> clique;
- public static Plot getPlot() {
- return plot;
- }
- public static void main(String[] args) {
- graph = new ArrayList<Vertex>();
- clique = new ArrayList<Vertex>();
- displayMainMenu();
- }
- private static void displayMainMenu() {
- System.out.println("========");
- System.out.println("LABA 7 1");
- System.out.println("========");
- System.out.println("Choose option by picking a number.");
- System.out.println("1) Show graph");
- System.out.println("2) Add vertices");
- System.out.println("3) Add vertices (file)");
- System.out.println("4) Exit");
- int picked = 0;
- try {
- picked = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- displayMainMenu();
- return;
- }
- if(picked < 1 || picked > 4) {
- displayMainMenu();
- return;
- }
- switch(picked) {
- case 1:
- showGraph();
- return;
- case 2:
- addVertices();
- return;
- case 3:
- addVerticesFile();
- return;
- case 4:
- System.out.println("The program has now terminated.");
- return;
- }
- }
- private static void showGraph() {
- extendClique();
- plot = new Plot(graph, clique);
- System.out.println("Loading...");
- Renderer.init();
- }
- private static void extendClique() {
- ArrayList<Vertex> candidates = new ArrayList<Vertex>(graph);
- ArrayList<Vertex> not = new ArrayList<Vertex>();
- ArrayList<Vertex> compsub = new ArrayList<Vertex>();
- extendClique(candidates, not, compsub);
- }
- // алгоритм Брона-Кербоша O(3^(n/3))
- private static void extendClique(ArrayList<Vertex> candidates, ArrayList<Vertex> not, ArrayList<Vertex> compsub) {
- while(!candidates.isEmpty() && !adjacentToAll(not, candidates)) {
- Vertex v = candidates.get(0);
- compsub.add(v);
- ArrayList<Vertex> newCandidates = removeNotAdjacent(candidates, v);
- ArrayList<Vertex> newNot = removeNotAdjacent(not, v);
- if(!newCandidates.isEmpty() || !newNot.isEmpty())
- extendClique(newCandidates, newNot, compsub);
- else {
- if(compsub.size() > clique.size())
- clique = new ArrayList<Vertex>(compsub);
- }
- compsub.remove(v);
- candidates.remove(v);
- not.add(v);
- }
- }
- private static ArrayList<Vertex> removeNotAdjacent(ArrayList<Vertex> verteces, Vertex v) {
- ArrayList<Vertex> newVerteces = new ArrayList<Vertex>();
- for(int i = 0; i < verteces.size(); i++) {
- int[] adjacent = verteces.get(i).getVertices();
- boolean connected = false;
- for(int j = 0; j < adjacent.length; j++) {
- if(adjacent[j] == v.getIndex()) {
- connected = true;
- break;
- }
- }
- if(connected)
- newVerteces.add(verteces.get(i));
- }
- return newVerteces;
- }
- private static boolean adjacentToAll(ArrayList<Vertex> first, ArrayList<Vertex> second) {
- if(first.isEmpty() || second.isEmpty())
- return false;
- for(int i = 0; i < first.size(); i++) {
- int[] vertices = first.get(i).getVertices();
- if(vertices.length != second.size())
- continue;
- boolean connected = true;
- for(int j = 0; j < second.size(); j++) {
- if(!contains(vertices, second.get(j).getIndex())) {
- connected = false;
- break;
- }
- }
- if(connected)
- return true;
- }
- return false;
- }
- private static boolean contains(int[] arr, int value) {
- for(int i = 0; i < arr.length; i++) {
- if(arr[i] == value)
- return true;
- }
- return false;
- }
- private static void addVertices() {
- graph.clear();
- int amount = inputFunc("amount of vertices", 2, 11);
- for(int i = 0; i < amount; i++) {
- ArrayList<Integer> connections = new ArrayList<Integer>();
- for(int j = 0; j < amount; j++) {
- int adjacent = inputFunc("adjacent value of vertices " + i + "-" + j, 0, 2);
- if(adjacent == 1 && i != j)
- connections.add(j);
- else if(adjacent == 1) {
- System.out.println("Vertex cannot be loopped! Adjacent value changed to zero");
- adjacent = 0;
- }
- }
- graph.add(new Vertex(i, connections));
- }
- System.out.println("Vertices successfully added.");
- displayIncidentMatrix();
- displayMainMenu();
- }
- private static void displayIncidentMatrix() {
- System.out.println("\n======MATRIX======");
- for(int i = 0; i < graph.size(); i++) {
- System.out.print(i + ": ");
- int[] v = graph.get(i).getVertices();
- for(int j = 0; j < v.length; j++) {
- System.out.print(v[j] + " ");
- }
- System.out.println("");
- }
- System.out.println("==================\n");
- }
- private static int inputFunc(String name, int min, int max) {
- boolean incorrect;
- String line;
- int element = 0;
- do {
- incorrect = false;
- System.out.println("Input " + name + ":");
- line = scanner.nextLine();
- try {
- element = Integer.parseInt(line);
- } catch (Exception e) {
- incorrect = true;
- System.out.println("Element should be a numerical value");
- }
- if(element < min || element >= max) {
- incorrect = true;
- System.out.printf("Element should be between %d and %d\n", min, max - 1);
- }
- } while(incorrect);
- return element;
- }
- private static void addVerticesFile() {
- graph.clear();
- System.out.println("Input location of the file:");
- String line;
- try (BufferedReader br = new BufferedReader(new FileReader(new File(scanner.nextLine())))) {
- line = br.readLine();
- int amount = Integer.parseInt(line);
- for(int i = 0; i < amount; i++) {
- line = br.readLine();
- ArrayList<Integer> connections = new ArrayList<Integer>();
- for(int j = 0; j < amount; j++) {
- int adjacent = line.charAt(j) - '0';
- if(adjacent == 1 && i != j)
- connections.add(j);
- else if(adjacent == 1) {
- System.out.println("Vertex cannot be loopped! Adjacent value changed to zero");
- adjacent = 0;
- }
- }
- graph.add(new Vertex(i, connections));
- }
- } catch(Exception e) {
- e.printStackTrace();
- System.out.println("An error has occured.");
- displayMainMenu();
- return;
- }
- System.out.println("Vertices successfully added.");
- displayIncidentMatrix();
- displayMainMenu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement