Advertisement
Guest User

Untitled

a guest
May 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.32 KB | None | 0 0
  1.  
  2. class Graph {
  3.     private static int[][] matrix;
  4.     static int[][] pred;
  5.  
  6.     private Graph(int n) {
  7.         if (n < 0) {
  8.             System.err.println("Error! No negative graph size!");
  9.         } else {
  10.             matrix = new int[n + 1][n + 1];
  11.             pred = new int[n + 1][n + 1];
  12.             for (int i = 1; i < matrix.length; i++) {
  13.                 for (int j = 1; j < matrix.length; j++) {
  14.                     if (i == j) {
  15.                         matrix[i][j] = 0;
  16.                         pred[i][j] = Integer.MAX_VALUE;
  17.                     } else {
  18.                         matrix[i][j] = -1;
  19.                         pred[i][j] = Integer.MAX_VALUE;
  20.                     }
  21.                 }
  22.             }
  23.         }
  24.     }
  25.  
  26.     public static void newMat(int n) {
  27.         new Graph(n);
  28.     }
  29.  
  30.     public static void debug() {
  31.         System.out.println();
  32.         if (matrix == null) {
  33.             System.err.println("Error! Create a new graph first!");
  34.         } else {
  35.             for (int i = 1; i < matrix.length; i++) {
  36.                 for (int j = 1; j < matrix.length; j++) {
  37.                     System.out.print(matrix[i][j] + "  ");
  38.                 }
  39.                 System.out.println();
  40.             }
  41.             System.out.println();
  42.         }
  43.     }
  44.  
  45.     public static void add(int source, int target, int dist) {
  46.         if (matrix == null) {
  47.             System.err.println("Error! Create new graph first");
  48.         } else if (dist < 0) {
  49.             System.err.println("Error! Cannot add negative distances!");
  50.         } else if ((source <= 0) || (target <= 0)) {
  51.             System.err.println("Error! Node has to be between" + " 1 and "
  52.                     + (matrix.length - 1));
  53.         } else if (source == target) {
  54.             System.err.println("Error! Distance"
  55.                     + " between same nodes is always zero.");
  56.         } else if (source > matrix.length || target > matrix.length) {
  57.             System.err.println("Error! Nodes too  big for Graph!");
  58.         } else if (hasEdge(source, target)) {
  59.             System.err.println("Error! There is already a connection.");
  60.         } else {
  61.             matrix[source][target] = dist;
  62.         }
  63.     }
  64.  
  65.     public static void delete(int source, int target) {
  66.         if (matrix == null) {
  67.             System.err.println("Error! Create a new graph first");
  68.         } else if (source == target) {
  69.             System.err.println("Error! Cannot remove if source = target");
  70.         } else if (matrix[source][target] == -1) {
  71.             System.err.println("Error! Nothing to delete");
  72.         } else {
  73.             matrix[source][target] = -1;
  74.         }
  75.     }
  76.  
  77.     public static void help() {
  78.         System.out.println("' 'q'  terminates the program.");
  79.         System.out.println();
  80.         System.out.println("''n x' creates a new graph with the size x. "
  81.                 + "The size has to be bigger than 0.");
  82.         System.out.println();
  83.         System.out.println("'d' displays the graph.");
  84.         System.out.println();
  85.         System.out.println("''a x y z' adds a connection from source x, "
  86.                 + "to target y, with length z.");
  87.         System.out.println("    If there is already an connection,"
  88.                 + " the program displays an error message.");
  89.         System.out.println();
  90.         System.out.println("'Remove x y' | 'r x y' "
  91.                 + "removes a connection between x and y.");
  92.         System.out.println("If there is no connection, "
  93.                 + " the program displays an error message.");
  94.     }
  95.  
  96.     public static void matPrint(int[][] mat) {
  97.         for (int i = 1; i < mat.length; i++) {
  98.             for (int j = 1; j < mat.length; j++) {
  99.                 System.out.print(mat[i][j] + "  ");
  100.             }
  101.             System.out.println();
  102.         }
  103.         System.out.println();
  104.     }
  105.  
  106.     public static int[] nearNodes(int[][] mat, int source) {
  107.         int count = 0;
  108.         for (int i = 0; i < mat[source].length; i++) {
  109.             if (mat[source][i] > 0 && mat[source][i] != Integer.MAX_VALUE) {
  110.                 count++;
  111.             }
  112.         }
  113.         final int[] result = new int[count];
  114.         count = 0;
  115.         for (int i = 0; i < mat[source].length; i++) {
  116.             if (mat[source][i] > 0 && mat[source][i] != Integer.MAX_VALUE) {
  117.                 result[count++] = i;
  118.             }
  119.         }
  120.         return result;
  121.     }
  122.  
  123.     public static boolean hasEdge(int source, int target) {
  124.         return matrix[source][target] > 0;
  125.     }
  126.  
  127.     public static boolean isValidEdge(int source, int target, int length) {
  128.         return matrix[source][target] == length;
  129.     }
  130.  
  131.     public static int getDis(int[][] mat, int source, int target) {
  132.         return mat[source][target];
  133.     }
  134.  
  135.     public static int[][] dojktrsa(int source, int target) {
  136.  
  137.        
  138.        
  139.         int[] pred = new int[matrix.length];
  140.         boolean[] checked = new boolean[matrix.length];
  141.         int[] length = new int[matrix.length];
  142.         int test1 = 0;
  143.         int[] test = nearNodes(matrix, 1);
  144.         for (int a = 0; a<test.length; a++){
  145.             test1  = test[a];
  146.             length[test1] = getDis(matrix, 1, test1);
  147.             pred[test1] = 1;
  148.         }
  149.         for (int i = 1; i < matrix.length; i++) {      
  150.             int x = getShortestNode(i);
  151.             checked[i] = true;
  152.            
  153.             int[] i1 = nearNodes(matrix, x);
  154.             pred[x] = i;
  155.            
  156.            
  157.            
  158.             for (int z = 0; z < i1.length; z++) {
  159.                 int t = i1[z];
  160.                 int newlength = getDis(matrix, x, i1[z]);
  161.                 System.out.println(newlength + "neue länge");
  162.                 int bitebite = getDis(matrix, i, x);
  163.                 System.out.println(bitebite + " wie groß ist die");
  164.                 if (length[t] != 0) {
  165.                     if ( newlength > length[t]) {
  166.                         System.out.println("mal gucken");
  167.                     } else {
  168.                         length[t] = newlength;
  169.                     }
  170.                 } else {
  171.                     length[t] = newlength +bitebite ;
  172.  
  173.                 }
  174.             }
  175.                 for (int s = 0; s < length.length; s++) {
  176.                     System.out.print(length[s] + " ");
  177.                     System.out.println(pred[s] + "");
  178.  
  179.                 }
  180.                 System.out.println();
  181.             }
  182.  
  183.         for (int s = 0; s < length.length; s++) {
  184.             System.out.println(length[s]);
  185.  
  186.         }
  187.  
  188.         return null;
  189.  
  190.     }
  191.  
  192.     public static int getShortestNode(int source) {
  193.         int[] mat = new int[matrix.length];
  194.         for (int i = 1; i < matrix.length; i++) {
  195.             if (i == source) {
  196.                 for (int j = 1; j < matrix.length; j++) {
  197.                     if (matrix[i][j] != -1) {
  198.                         mat[j] = matrix[i][j];
  199.                     }
  200.                 }
  201.             }
  202.         }
  203.         for (int i = 0; i < mat.length; i++) {
  204.             if (mat[i] == 0) {
  205.                 mat[i] = Integer.MAX_VALUE;
  206.             }
  207.         }
  208.         int node = 0;
  209.         int small = mat[0];
  210.         for (int i = 0; i < mat.length; i++) {
  211.             if (mat[i] < small) {
  212.                 small = mat[i];
  213.                 node = i;
  214.             }
  215.         }
  216.         return node;
  217.     }
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement