Aaaaa988

RGZfix

Oct 14th, 2020 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.07 KB | None | 0 0
  1. package ru.favarish;
  2.  
  3. import java.io.*;
  4. import java.math.BigInteger;
  5. import java.util.ArrayList;
  6.  
  7. public class RGZ {
  8.     private static int n;
  9.     private static int m;
  10.     private static ArrayList<Edge> edges = new ArrayList<>();
  11.  
  12.     private static final boolean DEBUG_MODE = true;
  13.     private static final int ACCURACY = 5;
  14.  
  15.     public static final String fileName = "testGraphIncorrect.txt";
  16.  
  17.     public static void graphColoringTask() throws IOException {
  18.         Edge edgeForBob;
  19.         BigInteger z1Bobs;
  20.         BigInteger z2Bobs;
  21.         BigInteger z1;
  22.         BigInteger z2;
  23.  
  24.         readGraphFromFile();
  25.         for (Edge e : edges) {
  26.             e.genrateRSA();
  27.         }
  28.  
  29.         for (int i = 0; i < ACCURACY*m; i++) {
  30.             ArrayList<Integer> colorsConteiner = randColors();
  31.             permutationOfColors(colorsConteiner.get(0), colorsConteiner.get(1), colorsConteiner.get(2));
  32.  
  33.             if(DEBUG_MODE)
  34.                 System.out.println("Текущая конфигурация цветов (" +colorsConteiner.get(0)+","+ colorsConteiner.get(1)+","+ colorsConteiner.get(2)+")");
  35.  
  36.             int indexEdge = random(0, edges.size() - 1);
  37.             edgeForBob = edges.get(indexEdge);
  38.             z1Bobs = edgeForBob.getZ1();
  39.             z2Bobs = edgeForBob.getZ2();
  40.  
  41.             z1 = Lab1.fastModuloExponentiation(z1Bobs, edgeForBob.getC1(), edgeForBob.getN1());
  42.             z2 = Lab1.fastModuloExponentiation(z2Bobs, edgeForBob.getC2(), edgeForBob.getN2());
  43.  
  44.             System.out.print("Проверка ребра №"+indexEdge);
  45.             if (!equalsYoungBit(z1, z2)) {
  46.                 System.out.println("Ребро прошло проверку!\n");
  47.  
  48.             } else {
  49.                 System.out.println("Программа прервала своё выполнение, т.к. было обнаруженно ребро с одинакого окрашенными вершинами!");
  50.  
  51.                 if(DEBUG_MODE)
  52.                     System.out.println((indexEdge+2)+" строка в файле имеет равные цвета "+ edgeForBob.getColor1() +" & "+ edgeForBob.getColor2());
  53.  
  54.                 break;
  55.             }
  56.             edgeForBob.genrateRSA();
  57.         }
  58.     }
  59.  
  60.     public static ArrayList<Integer> randColors() {
  61.         ArrayList<Integer> colors = new ArrayList<Integer>();
  62.         colors.add(random(0, 2));
  63.         int temp;
  64.         do {
  65.             temp = random(0, 2);
  66.         } while (colors.contains(temp));
  67.         colors.add(temp);
  68.         do {
  69.             temp = random(0, 2);
  70.         } while (colors.contains(temp));
  71.         colors.add(temp);
  72.        
  73.         return colors;
  74.     }
  75.  
  76.     public static void permutationOfColors(int cFirst, int cSecond, int cThird) {
  77.         for (Edge edge: edges) {
  78.             edge.generateRandZ(cFirst, cSecond, cThird);
  79.         }
  80.     }
  81.  
  82.  
  83.     private static void readGraphFromFile() throws IOException {
  84.         String tmp;
  85.         String[] tmpMas;
  86.  
  87.         File file = new File(fileName);
  88.         BufferedReader reader = new BufferedReader(new FileReader(file));
  89.  
  90.         tmpMas = reader.readLine().split(",");
  91.         n = Integer.parseInt(tmpMas[0]);
  92.         m = Integer.parseInt(tmpMas[1]);
  93.        
  94.         while ((tmp = reader.readLine()) != null) {
  95.             tmpMas = tmp.split(",");
  96.             edges.add(new Edge(Integer.valueOf(tmpMas[0]), Integer.valueOf(tmpMas[1]), Integer.valueOf(tmpMas[2]),
  97.                     Integer.valueOf(tmpMas[3])));
  98.  
  99.         }
  100.     }
  101.    
  102.     private static int random(int min, int max) {
  103.         max -= min;
  104.         return (int) (Math.random() * ++max) + min;
  105.     }
  106.  
  107.     private static boolean equalsYoungBit(BigInteger a, BigInteger b) {
  108.         String first = a.toString(2).substring(a.toString(2).length()-2);
  109.         String second = b.toString(2).substring(b.toString(2).length()-2);
  110.  
  111.         if(DEBUG_MODE)
  112.             System.out.println(", ребро имеет цвета "+Integer.parseInt(first, 2)+" & " + Integer.parseInt(second, 2));
  113.  
  114.         return first.equals(second);
  115.     }
  116. }
  117.  
Add Comment
Please, Sign In to add comment