0% found this document useful (0 votes)
6 views

Notes 10 Graphs

Uploaded by

imsulimansultan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Notes 10 Graphs

Uploaded by

imsulimansultan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Ford & Fulkerson Algorithm

Page 745 (of acrobat reader) of Cormen’s book.


There should be no loops on the graph.
If there is a cycle, then introduce a new node to remove cycle like this:

Example:
v1 has one out flow of 10 going to v2 and at the same time v2 has outflow of 4 going to v1. So we
introduced a new node v’ accepting flow from v1 and giving outflow to v2. This way, we removed cycle
from the graph.

Flow of a network is sum all outgoing flows from the source s, or sum of all incoming flows in the sink.
Source and Sink
There will be no inward edges at source and outward edges from sink.

In min-cut we only count edges going out (means they are contributing to the path towards sink)
The capacity of minimum cut value, is going to be the maximum flow.
The given edge weights in a graph tells the capacity (of e.g., water) on the edge.
By default the capacities are given on the edges of the graph. As shown in first graph of below figure.
As first step we set the flow of graph equal to zero, e.g., an edge will capacity 13 will be written as 0/13
which is flow/capacity.
Then, we pick a random path, and find the edge e with minimum capacity Ce(p) on that path. This is
called bottleneck edge.
For example, in below figure, we pick the path as s -> v1 -> v3 -> t, highlighted as yellow.
We add the Ce(p) to the flows of edges on the same path, mark the edge as blocked where we have
flow==capacity, e.g., v1 to v3 in the below graph, we have now 12/12 and marked as blocked.
Example:

In picking the next path, if we already have a none zero flow on edge, then we will subtract the flow from
capacity, and find the edge whose flow after subtraction is minimum to identify the bottleneck edge.
The process is repeated until all the paths are blocked (i.e., when an edge of the path has label a/b on a
path where a==b, and we marked that edge as reverse edge)
Whatever total flow leaves the source should be equal to the one entering the sink in max flow.
Min-Cut
We want to perform a cut on edges in original graph so that the edges through which the cut is going has
minimum flow. Moreover, we only count edges that entering the cut.
Min-cut travels from top to bottom

In below example, the edge with cross will not be counted because if we think of it as an edge lying
horizontal from left to right, the red arc will be passing through under it, not above it and so it will not be
counted.
NOTE:
If an edge is in down to up direction, and the minimum cut line is crossing it from the left side, then it will
be counted, otherwise, not.
If an edge is in up to down direction, and the minimum cut line is crossing it from right, then it will be
counted, otherwise not.
If an edge is in horizontal direction, from left to right, and the minimum cut line is cross it from top to
down it will be counted, otherwise not

Example: (CLRS book, 3rd Ed – Page 748 of pdf)


Here min-cut on original graph is also 23.
Example:
Example MaxFlow
Example:
Min-cut
Example
package javatestprog;

//Java program for implementation of Ford Fulkerson algorithm


import java.util.LinkedList;

class Main
{
static final int V = 6; //Number of vertices in graph

/* Returns true if there is a path from source 's' to sink


't' in residual graph. Also fills parent[] to store the
path */
boolean bfs(int rGraph[][], int s, int t, int parent[])
{
// Create a visited array and mark all vertices as not
// visited
boolean visited[] = new boolean[V];
for(int i=0; i<V; ++i)
visited[i]=false;

// Create a queue, enqueue source vertex and mark


// source vertex as visited
LinkedList<Integer> queue = new LinkedList<Integer>();
queue.add(s);
visited[s] = true;
parent[s]=-1;

// Standard BFS Loop


while (queue.size()!=0)
{
int u = queue.poll();

for (int v=0; v<V; v++)


{
if (visited[v]==false && rGraph[u][v] > 0)
{
queue.add(v);
parent[v] = u;
visited[v] = true;
}
}
}

// If we reached sink in BFS starting from source, then


// return true, else false
return (visited[t] == true);
}
// Returns tne maximum flow from s to t in the given graph
int fordFulkerson(int graph[][], int s, int t)
{
int u, v;

// Create a residual graph and fill the residual graph


// with given capacities in the original graph as
// residual capacities in residual graph

// Residual graph where rGraph[i][j] indicates


// residual capacity of edge from i to j (if there
// is an edge. If rGraph[i][j] is 0, then there is
// not)
int rGraph[][] = new int[V][V];

for (u = 0; u < V; u++)


for (v = 0; v < V; v++)
rGraph[u][v] = graph[u][v];

// This array is filled by BFS and to store path


int parent[] = new int[V];

int max_flow = 0; // There is no flow initially

// Augment the flow while tere is path from source


// to sink
while (bfs(rGraph, s, t, parent))
{
// Find minimum residual capacity of the edhes
// along the path filled by BFS. Or we can say
// find the maximum flow through the path found.
int path_flow = Integer.MAX_VALUE;
for (v=t; v!=s; v=parent[v])
{
u = parent[v];
path_flow = Math.min(path_flow, rGraph[u][v]);
}

// update residual capacities of the edges and


// reverse edges along the path
for (v=t; v != s; v=parent[v])
{
u = parent[v];
rGraph[u][v] -= path_flow;
rGraph[v][u] += path_flow;
}

// Add path flow to overall flow


max_flow += path_flow;
}
// Return the overall flow
return max_flow;
}

// Driver program to test above functions


public static void main (String[] args) throws java.lang.Exception
{
// Let us create a graph shown in the above example
int graph[][] =new int[][] { {0, 16, 13, 0, 0, 0},
{0, 0, 10, 12, 0, 0},
{0, 4, 0, 0, 14, 0},
{0, 0, 9, 0, 0, 20},
{0, 0, 0, 7, 0, 4},
{0, 0, 0, 0, 0, 0}
};
Main m = new Main();

System.out.println("The maximum possible flow is " +


m.fordFulkerson(graph, 0, 5));

}
}

Output: The maximum possible flow is 23

You might also like