Advertisement
Guest User

Graph.java

a guest
Nov 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.25 KB | None | 0 0
  1. package edgeconnectivity;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. public class Graph {
  7.     int n;
  8.     int[][] m, g, gf;
  9.     ArrayList<Integer> x, y, v;
  10.    
  11.     public Graph(File file) throws NoInputFileException,
  12.             InputFileIsEmptyException, IncorrectGraphEntryException {
  13.         try {
  14.             Scanner sc = new Scanner(file);
  15.             if (!sc.hasNextLine())
  16.                 throw new InputFileIsEmptyException();
  17.             String str = sc.nextLine();
  18.             String[] substr = null;
  19.             if (!str.contains("x")) {
  20.                 throw new IncorrectGraphEntryException();
  21.             } else {
  22.                 substr = str.split("x");
  23.             }
  24.             if (substr.length != 2)
  25.                 throw new IncorrectGraphEntryException();
  26.             try {
  27.                 n = Integer.parseInt(substr[0]);
  28.             } catch (NumberFormatException ex) {
  29.                 throw new IncorrectGraphEntryException();
  30.             }
  31.             int m;
  32.             try {
  33.                 m = Integer.parseInt(substr[1]);
  34.             } catch (NumberFormatException ex) {
  35.                 throw new IncorrectGraphEntryException();
  36.             }
  37.             this.m = new int[n][m];
  38.             for (int i = 0; i < n; ++i) {
  39.                 if (!sc.hasNextLine())
  40.                     throw new IncorrectGraphEntryException();
  41.                 str = sc.nextLine();
  42.                 int j = 0;
  43.                 String temp = "";
  44.                 for (char c : str.toCharArray()) {
  45.                     if (j > m - 1)
  46.                         if (Character.isDigit(c)) {
  47.                             --j;
  48.                             temp += c;
  49.                             this.m[i][j] = Integer.parseInt(temp);
  50.                             ++j;
  51.                         } else
  52.                             temp = "";
  53.                     if (j == m - 1) {
  54.                         if (c == '*')
  55.                             this.m[i][j] = 0;
  56.                         else if (Character.isDigit(c)) {
  57.                             temp += c;
  58.                             this.m[i][j] = Integer.parseInt(temp);
  59.                             ++j;
  60.                             temp = "";
  61.                         }
  62.                     }
  63.                     if (c == '*') {
  64.                         if (temp.length() > 0) {
  65.                             this.m[i][j] = Integer.parseInt(temp);
  66.                             ++j;
  67.                             if (j > m)
  68.                                 throw new IncorrectGraphEntryException();
  69.                             temp = "";
  70.                         }
  71.                         this.m[i][j] = 0;
  72.                         ++j;
  73.                         if (j > m)
  74.                             throw new IncorrectGraphEntryException();
  75.                     } else if (Character.isDigit(c))
  76.                         temp += c;
  77.                     else
  78.                         throw new IncorrectGraphEntryException();
  79.                 }
  80.             }
  81.             x = new ArrayList<Integer>();
  82.             y = new ArrayList<Integer>();
  83.             v = new ArrayList<Integer>();
  84.             for (int i = 0; i < n; ++i)
  85.                 for (int j = 0; j < m; ++j)
  86.                     if (this.m[i][j] > 0) {
  87.                         x.add(j);
  88.                         y.add(i);
  89.                         v.add(this.m[i][j]);
  90.                     }
  91.             n = v.size();
  92.             g = new int[n][n];
  93.             for (int i = 0; i < n; ++i)
  94.                 for (int j = 0; j < n; ++j)
  95.                     g[i][j] = 0;
  96.             while (sc.hasNextLine()) {
  97.                 if (!sc.hasNextInt())
  98.                     throw new IncorrectGraphEntryException();
  99.                 int u = sc.nextInt();
  100.                 if (!v.contains(u))
  101.                     throw new IncorrectGraphEntryException();
  102.                 int i = v.indexOf(u);
  103.                 if (!sc.hasNextInt())
  104.                     throw new IncorrectGraphEntryException();
  105.                 u = sc.nextInt();
  106.                 if (!v.contains(u))
  107.                     throw new IncorrectGraphEntryException();
  108.                 int j = v.indexOf(u);
  109.                 g[i][j] = g[j][i] = 1;
  110.             }
  111.         } catch (FileNotFoundException ex) {
  112.             throw new NoInputFileException();
  113.         }
  114.     }
  115.    
  116.     public ArrayList<Integer> neighbours(int u) {
  117.         var neighbours = new ArrayList<Integer>();
  118.         for (int v = 0; v < n; ++v)
  119.             if (g[u][v] != 0)
  120.                 neighbours.add(v);
  121.         return neighbours;
  122.     }
  123.    
  124.     public void printGraph() {
  125.         for (int u = 0; u < n; ++u) {
  126.             System.out.print(u + 1 + ": ");
  127.             for (int v : neighbours(u))
  128.                 System.out.print(v + 1 + " ");
  129.             System.out.println();
  130.         }
  131.     }
  132.    
  133.     private boolean bfs(int s, int t, int[] p) {
  134.         boolean[] used = new boolean[n];
  135.         for (int u = 0; u < n; ++u)
  136.             used[u] = false;
  137.         var q = new LinkedList<Integer>();
  138.         q.add(s);
  139.         used[s] = true;
  140.         p[s] = -1;
  141.         while (!q.isEmpty()) {
  142.             int v = q.poll();
  143.             for (int u = 0; u < n; ++u) {
  144.                 if (!used[u] && gf[v][u] > 0) {
  145.                     q.add(u);
  146.                     used[u] = true;
  147.                     p[u] = v;
  148.                 }
  149.             }
  150.         }
  151.         return used[t];
  152.     }
  153.    
  154.     private void dfs(int u, boolean[] used) {
  155.         used[u] = true;
  156.         for (int v = 0; v < n; ++v)
  157.             if (gf[u][v] > 0 && !used[v])
  158.                 dfs(v, used);
  159.     }
  160.    
  161.     public int FordFulkerson(int s, int t) {
  162.         gf = new int[n][n];
  163.         for (int u = 0; u < n; ++u)
  164.             for (int v = 0; v < n; ++v)
  165.                 gf[u][v] = g[u][v];
  166.         int[] p = new int[n];
  167.         int f = 0;
  168.         while (bfs(s, t, p)) {
  169.             int cf = Integer.MAX_VALUE;
  170.             for (int u = t; u != s; u = p[u]) {
  171.                 int v = p[u];
  172.                 cf = Math.min(cf, gf[v][u]);
  173.             }
  174.             for (int u = t; u != s; u = p[u]) {
  175.                 int v = p[u];
  176.                 gf[v][u] -= cf;
  177.                 gf[u][v] += cf;
  178.             }
  179.             f += cf;
  180.         }
  181.         return f;
  182.     }
  183.    
  184.     public boolean[][] minCut(int s) {
  185.         var mincut = new boolean[n][n];
  186.         for (int u = 0; u < n; ++u)
  187.             for (int v = 0; v < n; ++v)
  188.                 mincut[u][v] = false;
  189.         boolean[] used = new boolean[n];
  190.         dfs(s, used);
  191.         for (int u = 0; u < n; ++u)
  192.             for (int v : neighbours(u))
  193.                 if (g[u][v] > 0 && used[u] && !used[v])
  194.                     mincut[u][v] = mincut[v][u] = true;
  195.         return mincut;
  196.     }
  197.    
  198.     public boolean[][] edgeConnectivity() {
  199.         int ans = Integer.MAX_VALUE;
  200.         for (int s = 0; s < n; ++s)
  201.             for (int t = s + 1; t < n; ++t) {
  202.                 int flow = FordFulkerson(s, t);
  203.                 ans = Math.min(ans, flow);
  204.             }
  205.         for (int s = 0; s < n; ++s)
  206.             for (int t = s + 1; t < n; ++t) {
  207.                 int flow = FordFulkerson(s, t);
  208.                 if (flow == ans)
  209.                     return minCut(s);
  210.             }
  211.         return null;
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement