Advertisement
Aaaaa988

RGZ

Oct 14th, 2020
1,952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.30 KB | None | 0 0
  1. package ru.favarish;
  2.  
  3. import javax.imageio.metadata.IIOMetadataFormatImpl;
  4. import java.io.*;
  5. import java.math.BigInteger;
  6. import java.util.ArrayList;
  7. import java.util.Random;
  8.  
  9. public class RGZ {
  10.     private static int n;
  11.     private static int m;
  12.     private static ArrayList<Edge> edges = new ArrayList<>();
  13.  
  14.     private static final boolean DEBUG_MODE = false;
  15.  
  16.     public static final String fileName = "testGraphIncorrect.txt";
  17.  
  18.  
  19.     public static void graphColoringTask() throws IOException {
  20.         Edge edgeForBob;
  21.         BigInteger z1Bobs;
  22.         BigInteger z2Bobs;
  23.         BigInteger _z1;
  24.         BigInteger _z2;
  25.  
  26.         readFileGraph();
  27.  
  28.         // TODO: как быть с RSA?
  29.         for (Edge e : edges) {
  30.             e.genrateDataRSA();
  31.         }
  32.  
  33.         //Тут должен быть цикл (пробегаемся по большому количеству вершин)
  34.         //в нем нужно добавить перетасовку вершин
  35.         for (int i = 0; i < 1000000000; i++) {
  36.             ArrayList<Integer> colorsRand = randomColors();
  37.             reColoringGraph(colorsRand.get(0), colorsRand.get(1), colorsRand.get(2));
  38.  
  39.             if(DEBUG_MODE)
  40.                 System.out.println("цвета " +colorsRand.get(0)+ colorsRand.get(1) + colorsRand.get(2));
  41.  
  42.             int indexEdge = random(0, edges.size() - 1);
  43.             edgeForBob = edges.get(indexEdge);
  44.             z1Bobs = edgeForBob.getZ1();
  45.             z2Bobs = edgeForBob.getZ2();
  46.  
  47.             _z1 = Lab1.fastModuloExponentiation(z1Bobs, edgeForBob.getC1(), edgeForBob.getN1());
  48.             _z2 = Lab1.fastModuloExponentiation(z2Bobs, edgeForBob.getC2(), edgeForBob.getN2());
  49.             if (equalsYoungBit(_z1, _z2)) {
  50.                 System.out.println("НЕПРАВИЛЬНЫЕ ЦВЕТА!");
  51.  
  52.                 if(DEBUG_MODE)
  53.                     System.out.println((indexEdge+2)+" ребро имеет цвета "+ edgeForBob.getColor1() +" ## "+ edgeForBob.getColor2());
  54.  
  55.                 break; //для выхода из цикла
  56.             } else {
  57.                 System.out.println("Вершины разных цветов");
  58.             }
  59.         }
  60.  
  61.  
  62.  
  63.  
  64.     }
  65.  
  66.     public static ArrayList<Integer> randomColors() {
  67.         ArrayList<Integer> colors = new ArrayList<Integer>();
  68.  
  69.         colors.add(random(0, 2));
  70.  
  71.         int temp;
  72.         do {
  73.             temp = random(0, 2);
  74.         } while (colors.contains(temp));
  75.         colors.add(temp);
  76.  
  77.         do {
  78.             temp = random(0, 2);
  79.         } while (colors.contains(temp));
  80.         colors.add(temp);
  81.  
  82. //        int color;
  83. //
  84. //        for (int i = 0; i < 3; i++) {
  85. //            do {
  86. //                color = random(0,2);
  87. //                System.out.println("blyat;");
  88. //            } while (!colors.contains(color));
  89. //            colors.add(color);
  90. //        }
  91.  
  92.         return colors;
  93.     }
  94.  
  95.     public static void reColoringGraph(int colorFirst, int colorSecond, int colorThird) {
  96.         for (Edge edge: edges) {
  97.             edge.generateRandZ(colorFirst, colorSecond, colorThird);
  98.         }
  99.     }
  100.  
  101.  
  102.     private static void readFileGraph() throws IOException {
  103.         String tmp;
  104.         String[] tmpMas;
  105.  
  106.         File file = new File(fileName);
  107.         BufferedReader reader = new BufferedReader(new FileReader(file));
  108.  
  109.         tmpMas = reader.readLine().split(",");
  110.         n = Integer.valueOf(tmpMas[0]);
  111.         m = Integer.parseInt(tmpMas[1]);
  112.  
  113.  
  114.         while ((tmp = reader.readLine()) != null) {
  115.             tmpMas = tmp.split(",");
  116.             edges.add(new Edge(Integer.valueOf(tmpMas[0]), Integer.valueOf(tmpMas[1]), Integer.valueOf(tmpMas[2]),
  117.                     Integer.valueOf(tmpMas[3])));
  118.  
  119.         }
  120.     }
  121.  
  122.     //метод Боба для выбора очередного ребра
  123.     private static int random(int min, int max) {
  124.         max -= min;
  125.         return (int) (Math.random() * ++max) + min;
  126.     }
  127.  
  128.     private static boolean equalsYoungBit(BigInteger a, BigInteger b) {
  129.         String first = a.toString(2).substring(a.toString(2).length()-2);
  130.         String second = b.toString(2).substring(b.toString(2).length()-2);
  131.  
  132.         if(DEBUG_MODE)
  133.             System.out.println(first+" && " + second);
  134.  
  135.         return first.equals(second);
  136.     }
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement