You are on page 1of 7

ADVANCED ALGORITHM LAB

BY
PROMIT KUMAR MOHANTA
PAPER CODE:- PGSE-292
ROLL NO : 10011321010
REG NO : 211000411310010
Department of Software Engineering
Maulana Abul Kalam Azad University of Technology,
Westbengal
Haringhata, Nadia 741249
Date of Experiment: -4th may, 2022
Date of Submission: -9th may, 2022
Session- 2021 to 2023
Year: - 1st year Sem: - 2nd sem
Program – M.Tech
Assignment No. - 7

Signature of the teacher Signature of the student


With Date With Date
1) Write a python program to implement Ford-Fulkerson
Algorithm.

Ford-Fulkerson Algorithm:
Ford-Fulkerson algorithm is a greedy approach for calculating
the maximum possible flow in a network or a graph.
A term, flow network, is used to describe a network of vertices
and edges with a source (0) and a sink (5). Each vertex,
except S and T, can receive and send an equal amount of stuff
through it. (0) can only send and (5) can only receive stuff.
We can visualize the understanding of the algorithm using a
flow of liquid inside a network of pipes of different capacities.
Each pipe has a certain capacity of liquid it can transfer at an
instance. For this algorithm, we are going to find how much
liquid can be flowed from the source to the sink at an instance
using the network.

Terminologies Used
Augmenting Path
It is the path available in a flow network.

Residual Graph
It represents the flow network that has additional possible flow.

Residual Capacity
It is the capacity of the edge after subtracting the flow from the
maximum capacity.

How Ford-Fulkerson Algorithm works?

The algorithm follows:

1.Initialize the flow in all the edges to 0.


2.While there is an augmenting path between the source and
the sink, add this path to the flow.
3.Update the residual graph.

Ford-Fulkerson Example:
consider the following graph-

The maximum possible flow in the above graph is 23.

Code:

# Ford-Fulkerson algorith in Python

from collections import defaultdict


class Graph:

def __init__(self, graph):


self.graph = graph
self. ROW = len(graph)

# Using BFS as a searching algorithm


def searching_algo_BFS(self, s, t, parent):

visited = [False] * (self.ROW)


queue = []

queue.append(s)
visited[s] = True

while queue:

u = queue.pop(0)

for ind, val in enumerate(self.graph[u]):


if visited[ind] == False and val > 0:
queue.append(ind)
visited[ind] = True
parent[ind] = u

return True if visited[t] else False

# Applying fordfulkerson algorithm


def ford_fulkerson(self, source, sink):
parent = [-1] * (self.ROW)
max_flow = 0

while self.searching_algo_BFS(source, sink, parent):


path_flow = float("Inf")
s = sink
while(s != source):
path_flow = min(path_flow, self.graph[parent[s]][s])
s = parent[s]

# Adding the path flows


max_flow += path_flow

# Updating the residual values of edges


v = sink
while(v != source):
u = parent[v]
self.graph[u][v] -= path_flow
self.graph[v][u] += path_flow
v = parent[v]

return max_flow

graph = [[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]]
g = Graph(graph)

source = 0
sink = 5

print("Max Flow: %d " % g.ford_fulkerson(source, sink))


Output:

Max flow: 23

Screenshot:
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).

Ford-Fulkerson Applications:

1.Water distribution pipeline


2.Bipartite matching problem
3.Circulation with demands

You might also like