Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.favarish;
- import java.io.*;
- import java.math.BigInteger;
- import java.util.ArrayList;
- public class RGZ {
- private static int n;
- private static int m;
- private static ArrayList<Edge> edges = new ArrayList<>();
- private static final boolean DEBUG_MODE = true;
- private static final int ACCURACY = 5;
- public static final String fileName = "testGraphIncorrect.txt";
- public static void graphColoringTask() throws IOException {
- Edge edgeForBob;
- BigInteger z1Bobs;
- BigInteger z2Bobs;
- BigInteger z1;
- BigInteger z2;
- readGraphFromFile();
- for (Edge e : edges) {
- e.genrateRSA();
- }
- for (int i = 0; i < ACCURACY*m; i++) {
- ArrayList<Integer> colorsConteiner = randColors();
- permutationOfColors(colorsConteiner.get(0), colorsConteiner.get(1), colorsConteiner.get(2));
- if(DEBUG_MODE)
- System.out.println("Текущая конфигурация цветов (" +colorsConteiner.get(0)+","+ colorsConteiner.get(1)+","+ colorsConteiner.get(2)+")");
- int indexEdge = random(0, edges.size() - 1);
- edgeForBob = edges.get(indexEdge);
- z1Bobs = edgeForBob.getZ1();
- z2Bobs = edgeForBob.getZ2();
- z1 = Lab1.fastModuloExponentiation(z1Bobs, edgeForBob.getC1(), edgeForBob.getN1());
- z2 = Lab1.fastModuloExponentiation(z2Bobs, edgeForBob.getC2(), edgeForBob.getN2());
- System.out.print("Проверка ребра №"+indexEdge);
- if (!equalsYoungBit(z1, z2)) {
- System.out.println("Ребро прошло проверку!\n");
- } else {
- System.out.println("Программа прервала своё выполнение, т.к. было обнаруженно ребро с одинакого окрашенными вершинами!");
- if(DEBUG_MODE)
- System.out.println((indexEdge+2)+" строка в файле имеет равные цвета "+ edgeForBob.getColor1() +" & "+ edgeForBob.getColor2());
- break;
- }
- edgeForBob.genrateRSA();
- }
- }
- public static ArrayList<Integer> randColors() {
- ArrayList<Integer> colors = new ArrayList<Integer>();
- colors.add(random(0, 2));
- int temp;
- do {
- temp = random(0, 2);
- } while (colors.contains(temp));
- colors.add(temp);
- do {
- temp = random(0, 2);
- } while (colors.contains(temp));
- colors.add(temp);
- return colors;
- }
- public static void permutationOfColors(int cFirst, int cSecond, int cThird) {
- for (Edge edge: edges) {
- edge.generateRandZ(cFirst, cSecond, cThird);
- }
- }
- private static void readGraphFromFile() throws IOException {
- String tmp;
- String[] tmpMas;
- File file = new File(fileName);
- BufferedReader reader = new BufferedReader(new FileReader(file));
- tmpMas = reader.readLine().split(",");
- n = Integer.parseInt(tmpMas[0]);
- m = Integer.parseInt(tmpMas[1]);
- while ((tmp = reader.readLine()) != null) {
- tmpMas = tmp.split(",");
- edges.add(new Edge(Integer.valueOf(tmpMas[0]), Integer.valueOf(tmpMas[1]), Integer.valueOf(tmpMas[2]),
- Integer.valueOf(tmpMas[3])));
- }
- }
- private static int random(int min, int max) {
- max -= min;
- return (int) (Math.random() * ++max) + min;
- }
- private static boolean equalsYoungBit(BigInteger a, BigInteger b) {
- String first = a.toString(2).substring(a.toString(2).length()-2);
- String second = b.toString(2).substring(b.toString(2).length()-2);
- if(DEBUG_MODE)
- System.out.println(", ребро имеет цвета "+Integer.parseInt(first, 2)+" & " + Integer.parseInt(second, 2));
- return first.equals(second);
- }
- }
Add Comment
Please, Sign In to add comment