Advertisement
sweet1cris

Untitled

Feb 22nd, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1. // Java program for implementation of Ford Fulkerson algorithm
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.io.*;
  5. import java.util.LinkedList;
  6.  
  7. class MaxFlow
  8. {
  9.     static final int V = 6; //Number of vertices in graph
  10.  
  11.     /* Returns true if there is a path from source 's' to sink
  12.       't' in residual graph. Also fills parent[] to store the
  13.       path */
  14.     boolean bfs(int rGraph[][], int s, int t, int parent[])
  15.     {
  16.         // Create a visited array and mark all vertices as not
  17.         // visited
  18.         boolean visited[] = new boolean[V];
  19.         for(int i=0; i<V; ++i)
  20.             visited[i]=false;
  21.  
  22.         // Create a queue, enqueue source vertex and mark
  23.         // source vertex as visited
  24.         LinkedList<Integer> queue = new LinkedList<Integer>();
  25.         queue.add(s);
  26.         visited[s] = true;
  27.         parent[s]=-1;
  28.  
  29.         // Standard BFS Loop
  30.         while (queue.size()!=0)
  31.         {
  32.             int u = queue.poll();
  33.  
  34.             for (int v=0; v<V; v++)
  35.             {
  36.                 if (visited[v]==false && rGraph[u][v] > 0)
  37.                 {
  38.                     queue.add(v);
  39.                     parent[v] = u;
  40.                     visited[v] = true;
  41.                 }
  42.             }
  43.         }
  44.  
  45.         // If we reached sink in BFS starting from source, then
  46.         // return true, else false
  47.         return (visited[t] == true);
  48.     }
  49.  
  50.     // Returns tne maximum flow from s to t in the given graph
  51.     int fordFulkerson(int graph[][], int s, int t)
  52.     {
  53.         int u, v;
  54.  
  55.         // Create a residual graph and fill the residual graph
  56.         // with given capacities in the original graph as
  57.         // residual capacities in residual graph
  58.  
  59.         // Residual graph where rGraph[i][j] indicates
  60.         // residual capacity of edge from i to j (if there
  61.         // is an edge. If rGraph[i][j] is 0, then there is
  62.         // not)
  63.         int rGraph[][] = new int[V][V];
  64.  
  65.         for (u = 0; u < V; u++)
  66.             for (v = 0; v < V; v++)
  67.                 rGraph[u][v] = graph[u][v];
  68.  
  69.         // This array is filled by BFS and to store path
  70.         int parent[] = new int[V];
  71.  
  72.         int max_flow = 0;  // There is no flow initially
  73.  
  74.         // Augment the flow while tere is path from source
  75.         // to sink
  76.         while (bfs(rGraph, s, t, parent))
  77.         {
  78.             // Find minimum residual capacity of the edhes
  79.             // along the path filled by BFS. Or we can say
  80.             // find the maximum flow through the path found.
  81.             int path_flow = Integer.MAX_VALUE;
  82.             for (v=t; v!=s; v=parent[v])
  83.             {
  84.                 u = parent[v];
  85.                 path_flow = Math.min(path_flow, rGraph[u][v]);
  86.             }
  87.  
  88.             // update residual capacities of the edges and
  89.             // reverse edges along the path
  90.             for (v=t; v != s; v=parent[v])
  91.             {
  92.                 u = parent[v];
  93.                 rGraph[u][v] -= path_flow;
  94.                 rGraph[v][u] += path_flow;
  95.             }
  96.  
  97.             // Add path flow to overall flow
  98.             max_flow += path_flow;
  99.         }
  100.  
  101.         // Return the overall flow
  102.         return max_flow;
  103.     }
  104.  
  105.     // Driver program to test above functions
  106.     public static void main (String[] args) throws java.lang.Exception
  107.     {
  108.         // Let us create a graph shown in the above example
  109.         int graph[][] =new int[][] { {0, 16, 13, 0, 0, 0},
  110.                                      {0, 0, 10, 12, 0, 0},
  111.                                      {0, 4, 0, 0, 14, 0},
  112.                                      {0, 0, 9, 0, 0, 20},
  113.                                      {0, 0, 0, 7, 0, 4},
  114.                                      {0, 0, 0, 0, 0, 0}
  115.                                    };
  116.         MaxFlow m = new MaxFlow();
  117.  
  118.         System.out.println("The maximum possible flow is " +
  119.                            m.fordFulkerson(graph, 0, 5));
  120.  
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement