0% found this document useful (0 votes)
164 views5 pages

Ford

This document discusses algorithms for solving maximum flow problems and maximum bipartite matching problems. It begins by introducing maximum flow problems and defining residual graphs. It then summarizes the Ford-Fulkerson algorithm for finding maximum flow in a single-source, single-sink flow network. Finally, it describes how the maximum bipartite matching problem can be modeled as a maximum flow problem and solved using Ford-Fulkerson by building a flow network from the bipartite graph.

Uploaded by

sharadacg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
164 views5 pages

Ford

This document discusses algorithms for solving maximum flow problems and maximum bipartite matching problems. It begins by introducing maximum flow problems and defining residual graphs. It then summarizes the Ford-Fulkerson algorithm for finding maximum flow in a single-source, single-sink flow network. Finally, it describes how the maximum bipartite matching problem can be modeled as a maximum flow problem and solved using Ford-Fulkerson by building a flow network from the bipartite graph.

Uploaded by

sharadacg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Max Flow Problem Introduction

Maximum flow problems involve finding a feasible flow through a single-source, single-sink flow network that
is maximum.

Lets take an image to explain how above definition wants to say.

Each edge is labeled with a capacity, the maximum amount of stuff that it can carry. The goal is to figure out
how much stuff can be pushed from the vertex s(source) to the vertex t(sink).

.
Maximum flow possible is : 23
Following are different approaches to solve the problem :

1. Naive Greedy Algorithm Approach (May not produce optimal or correct result)

Greedy approach to the maximum flow problem is to start with the all-zero flow and greedily produce flows
with ever-higher value. The natural way to proceed from one to the next is to send more flow on some path from
s to t
Note that the path search just needs to determine whether or not there is an s-t path in the subgraph of edges e
with f(e) < C(e). This is easily done in linear time using BFS or DFS.

There is a path from source (s) to sink(t) [ s -> 1 -> 2 -> t] with maximum flow 3 unit ( path show in blue color )
After removing all useless edge from graph its look like

For above graph there is no path from source to sink so maximum flow : 3 unit But maximum flow is 5 unit. to
over come form this issue we use residual Graph.

2. Residual Graphs

The idea is to extend the naive greedy algorithm by allowing undo operations. For example, from the point
where this algorithm gets stuck in above image, wed like to route two more units of flow along the edge (s, 2),
then backward along the edge (1, 2), undoing 2 of the 3 units we routed the previous iteration, and finally along
the edge (1,t)

backward edge : ( f(e) ) and forward edge : ( C(e) f(e) )


We need a way of formally specifying the allowable undo operations. This motivates the following simple but
important defnition, of a residual network. The idea is that, given a graph G and a flow f in it, we form a new
flow network Gf that has the same vertex set of G and that has two edges for each edge of G. An edge e = (1,2)
of G that carries ?ow f(e) and has capacity C(e) (for above image ) spawns a forward edge of G f with capacity
C(e)-f(e) (the room remaining) and a backward edge (2,1) of G f with capacity f(e) (the amount of previously
routed ?ow that can be undone). source(s)- sink(t) paths with f(e) < C(e) for all edges, as searched for by the
naive greedy algorithm, correspond to the special case of s-t paths of Gf that comprise only forward edges.
The idea of residual graph is used The Ford-Fulkerson and Dinics algorithms

Ford-Fulkerson Algorithm for Maximum Flow


Problem
Given a graph which represents a flow network where every edge has a capacity. Also given two
vertices source s and sink t in the graph, find the maximum possible flow from s to t with following
constraints:
a) Flow on an edge doesnt exceed the given capacity of the edge.
b) Incoming flow is equal to outgoing flow for every vertex except s and t.
For example, consider the following graph from CLRS book.

The maximum possible flow in the above graph is 23.

Prerequisite : Max Flow Problem Introduction


Ford-Fulkerson Algorithm
The following is simple idea of Ford-Fulkerson algorithm:
1) Start with initial flow as 0.
2) While there is a augmenting path from source to sink.
Add this path-flow to flow.
3) Return flow.

Time Complexity: Time complexity of the above algorithm is O(max_flow * E). We run a loop while there is
an augmenting path. In worst case, we may add 1 unit flow in every iteration. Therefore the time complexity
becomes O(max_flow * E).

How to implement the above simple algorithm?


Let us first define the concept of Residual Graph which is needed for understanding the implementation.
Residual Graph of a flow network is a graph which indicates additional possible flow. If there is a path from
source to sink in residual graph, then it is possible to add flow. Every edge of a residual graph has a value
called residual capacity which is equal to original capacity of the edge minus current flow. Residual capacity is
basically the current capacity of the edge.

Let us now talk about implementation details. Residual capacity is 0 if there is no edge between two vertices of
residual graph. We can initialize the residual graph as original graph as there is no initial flow and initially
residual capacity is equal to original capacity. To find an augmenting path, we can either do a BFS or DFS of the
residual graph. We have used BFS in below implementation. Using BFS, we can find out if there is a path from
source to sink. BFS also builds parent[] array. Using the parent[] array, we traverse through the found path and
find possible flow through this path by finding minimum residual capacity along the path. We later add the
found path flow to overall flow.

The important thing is, we need to update residual capacities in the residual graph. We subtract path flow from
all edges along the path and we add path flow along the reverse edges We need to add path flow along reverse
edges because may later need to send flow in reverse direction

Maximum Bipartite Matching


A matching in a Bipartite Graph is a set of the edges chosen in such a way that no two edges share an endpoint.
A maximum matching is a matching of maximum size (maximum number of edges). In a maximum matching, if
any edge is added to it, it is no longer a matching. There can be more than one maximum matchings for a given
Bipartite Graph.
Why do we care?
There are many real world problems that can be formed as Bipartite Matching. For example, consider the
following problem:
There are M job applicants and N jobs. Each applicant has a subset of jobs that he/she is interested in. Each job
opening can only accept one applicant and a job applicant can be appointed for only one job. Find an
assignment of jobs to applicants in such that as many applicants as possible get jobs.

We strongly recommend to read the following post first.


Ford-Fulkerson Algorithm for Maximum Flow Problem
Maximum Bipartite Matching and Max Flow Problem
Maximum Bipartite Matching (MBP) problem can be solved by converting it into a flow network
(See this video to know how did we arrive this conclusion). Following are the steps.
1) Build a Flow Network
There must be a source and sink in a flow network. So we add a source and add edges from source to all
applicants. Similarly, add edges from all jobs to sink. The capacity of every edge is marked as 1 unit.

2) Find the maximum flow.


We use Ford-Fulkerson algorithm to find the maximum flow in the flow network built in step 1. The maximum
flow is actually the MBP we are looking for.

How to implement the above approach?


Let us first define input and output forms. Input is in the form of Edmonds matrix which is a 2D array
bpGraph[M][N] with M rows (for M job applicants) and N columns (for N jobs). The value bpGraph[i][j] is 1
if ith applicant is interested in jth job, otherwise 0.
Output is number maximum number of people that can get jobs.
A simple way to implement this is to create a matrix that represents adjacency matrix representation of a
directed graph with M+N+2 vertices. Call the fordFulkerson() for the matrix. This implementation requires
O((M+N)*(M+N)) extra space.
Extra space can be be reduced and code can be simplified using the fact that the graph is bipartite and capacity
of every edge is either 0 or 1. The idea is to use DFS traversal to find a job for an applicant (similar to
augmenting path in Ford-Fulkerson). We call bpm() for every applicant, bpm() is the DFS based function that
tries all possibilities to assign a job to the applicant.
In bpm(), we one by one try all jobs that an applicant u is interested in until we find a job, or all jobs are tried
without luck. For every job we try, we do following.
If a job is not assigned to anybody, we simply assign it to the applicant and return true. If a job is assigned to
somebody else say x, then we recursively check whether x can be assigned some other job. To make sure that x
doesnt get the same job again, we mark the job v as seen before we make recursive call for x. If x can get
other job, we change the applicant for job v and return true. We use an array maxR[0..N-1] that stores the
applicants assigned to different jobs.
If bmp() returns true, then it means that there is an augmenting path in flow network and 1 unit of flow is added
to the result in maxBPM().

You might also like