keymasterviriya1150

Untitled

Apr 5th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package javaapplication1;
  7.  
  8. import java.io.File;
  9. import java.util.ArrayList;
  10. import java.util.LinkedList;
  11. import java.util.Scanner;
  12.  
  13. /**
  14.  *
  15.  * @author 5730213093
  16.  */
  17. public class NewClass1 {
  18.  
  19.     public static void main(String[] args) throws Exception {
  20.         if (args.length != 1) {
  21.             System.out.println("Usage: java EdmondsKarp <data-file>");
  22.             return;
  23.         }
  24.  
  25.         Scanner sc = new Scanner(new File(args[0]));
  26.         int numVs = sc.nextInt();
  27.         int source = sc.nextInt();
  28.         int sink = sc.nextInt();
  29.  
  30.         // build residual graph without creating an ordinary graph first
  31.         int[][] rGraph = new int[numVs][numVs];
  32.         for (int u = 0; u < numVs; u++) {
  33.             int numNeighbors = sc.nextInt();
  34.             for (int j = 0; j < numNeighbors; j++) {
  35.                 rGraph[u][sc.nextInt()] = sc.nextInt();   // (u, v) = weight
  36.             }
  37.         }
  38.  
  39.         System.out.println("The maximum possible flow is "
  40.                 + edmondsKarp(rGraph, source, sink));
  41.     }  // end of main()
  42.  
  43.     public static int edmondsKarp(int[][] rGraph, int s, int t) {
  44.         int maxFlow = 0;
  45.         // Augment the flow while there is a path from source to sink
  46.         ArrayList<IPair> path = null;
  47.         while ((path = findShortestPath(rGraph, s, t)) != null) {
  48.             System.out.print("Found: ");
  49.             for (IPair p : path) {
  50.                 System.out.print(p.getX() + " -> ");
  51.             }
  52.             System.out.println(t);
  53.  
  54.             // Find minimum residual capacity of the edges along the path
  55.             int bottleneck = Integer.MAX_VALUE;
  56.             for (IPair p : path) {
  57.                 bottleneck = Math.min(bottleneck, rGraph[p.getX()][p.getY()]);
  58.             }
  59.             System.out.println("  Bottlneck: " + bottleneck);
  60.  
  61.             // update residual capacities along the path
  62.             for (IPair p : path) {
  63.                 int u = p.getX();
  64.                 int v = p.getY();
  65.                 rGraph[u][v] -= bottleneck;   // forward edge
  66.                 rGraph[v][u] += bottleneck;   // backward edge
  67.             }
  68.             maxFlow += bottleneck;
  69.         }
  70.         return maxFlow;
  71.     }  // end of edmondsKarp()
  72.  
  73.     public static ArrayList<IPair> findShortestPath(int[][] rGraph,
  74.             int s, int t) /* Returns the shortest augmented path from source s to sink t
  75.      in the residual graph. */ {
  76.         // set all of visited array as not visited
  77.         int numVs = rGraph.length;
  78.         boolean visited[] = new boolean[numVs];  // false by default
  79.         int parentOf[] = new int[numVs];
  80.  
  81.         // enqueue source and mark as visited
  82.         LinkedList<Integer> queue = new LinkedList<Integer>();
  83.         queue.add(s);
  84.         visited[s] = true;
  85.         parentOf[s] = -1;
  86.         // BFS implemented using a queue and a loop
  87.         while (queue.size() != 0) {
  88.             int u = queue.poll();
  89.             for (int v = 0; v < numVs; v++) {
  90.                 if ((!visited[v]) && (rGraph[u][v] > 0)) {
  91.                     /* there's some residual capacity available for
  92.                      visiting the unvisited v */
  93.                     queue.add(v);
  94.                     parentOf[v] = u;
  95.                     visited[v] = true;
  96.                 }
  97.             }
  98.         }
  99.         if (visited[t]) {   // have we got from source to sink?
  100.             ArrayList<IPair> path = new ArrayList<IPair>();
  101.             for (int v = t; v != s; v = parentOf[v]) // from t to s
  102.             {
  103.                 path.add(0, new IPair(parentOf[v], v));   // add (u,v) so in rev order
  104.             }
  105.             return path;
  106.         } else {
  107.             return null;
  108.         }
  109.     }  // end of findShortestPath()
  110.  
  111. }
  112. class IPair
  113. {
  114.     private int x;
  115.     private int y;
  116.     public IPair(int x,int y) {
  117.         this.x=x;
  118.         this.y=y;
  119.     }
  120.     int getX()
  121.     {
  122.         return x;
  123.     }
  124.     int getY()
  125.     {
  126.         return y;
  127.     }
  128.    
  129. }
Add Comment
Please, Sign In to add comment