Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class Main {
  4.     final static int INFINITY = 9999;
  5.     final static String PATH = "D:\\My_content\\My_Desktop\\BellmanFord\\src\\";
  6.  
  7.     public static void main(String[] args) throws IOException {
  8.         PrintWriter pw = new PrintWriter(new File(PATH + "output.txt"));
  9.  
  10.         int countOfFiles = (int) (50 + Math.random() *50);
  11.         for (int i = 0; i < countOfFiles; i++) {
  12.             int vert = makeInput(PATH + "inputs\\input" + i + ".txt");
  13.             int[][] edges = readInput(PATH + "inputs\\input" + i + ".txt", vert);
  14.  
  15.             long timeNow = System.nanoTime() / 1000;
  16.             int iteration = bellmanFord(edges, 1);
  17.             long timeAfter = System.nanoTime()/1000 - timeNow;
  18.             pw.println((new File(PATH + "inputs\\input" + i + ".txt").length()/1024 ) + "\t" +
  19.                     iteration + "\t" +
  20.                     timeAfter);
  21.         }
  22.         pw.close();
  23.     }
  24.  
  25.     public static int bellmanFord(int[][] edges, int source) {
  26.         int n = edges.length;
  27.         int dist[] = new int[n];
  28.         int iter = 0;
  29.  
  30.         for (int i = 0; i < n; i++) {
  31.             dist[i] = INFINITY;
  32.         }
  33.         dist[source] = 0;
  34.         for (int k = 0; k < n - 1; k++) {
  35.             boolean any = false;
  36.             for (int i = 0; i < n; i++) {
  37.                 for (int j = 0; j < n; j++) {
  38.                     iter++;
  39.                     if (edges[i][j] != INFINITY) {
  40.                         if ((dist[j] > dist[i] + edges[i][j]) && edges[i][j] != 0) {
  41.                             dist[j] = dist[i] + edges[i][j];
  42.                             any = true;
  43.                         }
  44.                     }
  45.                 }
  46.             }
  47.             if (!any) break;
  48.         }
  49.         return iter;
  50.     }
  51.  
  52.     public static int makeInput(String path) throws FileNotFoundException {
  53.         int countOfEdges = (int) (100 + Math.random() * 1000);
  54.         int countOfVertex = (int) (20 + Math.random() * 100);
  55.         PrintWriter pw = new PrintWriter(new File(path));
  56.         for (int i = 0; i < countOfEdges; i++) {
  57.             pw.print((int) (Math.random() * countOfVertex));
  58.             pw.print(" ");
  59.             pw.print((int) (Math.random() * countOfVertex));
  60.             pw.print(" ");
  61.             pw.print((int) (1 + Math.random() * 99));
  62.             pw.println();
  63.         }
  64.  
  65.         pw.close();
  66.         return countOfVertex;
  67.     }
  68.  
  69.     public static int[][] readInput(String path, int countOfVertex) throws IOException {
  70.         FileReader fileReader = new FileReader(new File(path));
  71.         BufferedReader br = new BufferedReader(fileReader);
  72.         String line = null;
  73.         int[][] edges = new int[countOfVertex][countOfVertex];
  74.         while ((line = br.readLine()) != null) {
  75.             String[] lines = line.split(" ");
  76.             if (lines.length == 3) {
  77.                 edges[Integer.parseInt(lines[0])][Integer.parseInt(lines[1])] = Integer.parseInt(lines[2]);
  78.             }
  79.         }
  80.         return edges;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement