Advertisement
Guest User

Untitled

a guest
May 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Random;
  6. import java.util.concurrent.ThreadLocalRandom;
  7.  
  8. public class Main {
  9.  
  10.  
  11.     /**Рыбаков
  12.     Исходные данные:
  13.     В1 = 3, А = 2, B = 21, C = 15, D=4
  14.     K = a mod 5 = 2 mod 5 = 2
  15.     Если К = 2 то N = 14*/
  16.     public static void main(String[] args) {
  17.  
  18.         int a = 2;
  19.         int b = 21;
  20.         int c = 15;
  21.         int d = 4;
  22.         int n = 14;
  23.         Graph g = generateGraph(n,a,b,c,d);
  24.         for (Vertex vertice : g.vertices) {
  25.             System.out.println("Ver "+ vertice.position);
  26.         }
  27.         for (Edge edge : g.edges) {
  28.             System.out.println("Edge "+edge.vertices[0].position+"->"+ edge.vertices[1].position+" weight "+edge.weight);
  29.         }
  30.     }
  31.  
  32.     public static boolean edgeCondition(int i,int j,int a,int b,int c,int d){
  33.         return ((i*a+b*j)/c)%d <=1;
  34.     }
  35.  
  36.     public static int randomWeight(int a, int c){
  37.         return ThreadLocalRandom.current().nextInt(a, a + c + 1);
  38.     }
  39.  
  40.     public static Graph generateGraph(int n,int a,int b,int c,int d){
  41.         List<Vertex> vertices = new LinkedList<>();
  42.         List<Edge> edges = new LinkedList<>();
  43.         for (int i = 0; i < 14; i++) {
  44.             vertices.add(new Vertex(i));
  45.         }
  46.         for (int i = 0; i < n; i++) {
  47.             for (int j = 0; j < n; j++) {
  48.                 if (edgeCondition(i,j,a,b,c,d)){
  49.                     edges.add(new Edge(
  50.                             randomWeight(a,c),
  51.                             new Vertex[]{vertices.get(i),vertices.get(j)}));
  52.                 }
  53.             }
  54.         }
  55.         return new Graph(vertices,edges);
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement