Algorithms - 4
Algorithms - 4
CS 610
Assignment – 4
Name : Sreejan
Roll no : 24250093
Department : Mtech’24 – CSE
1|Page
Assignment – 4
Q1) You are given a directed unweighted graph, a source s, a sink t and a number k. You may remove
≤ k edges in the graph with the aim to decrease the size of minimum cut between and t. Design an
O(mn) time algorithm for this problem.
Solution :
Minimum Cut: The minimum cut between nodes s and t is the smallest set of edges that, if removed,
disconnects t from s. In an unweighted graph, its fewest edges needed to block all paths from s to t.
Using Max-Flow: By the Max-Flow Min-Cut Theorem, the minimum cut value equals the maximum
flow from s to t. We can find this in O(mn) time with the Edmonds-Karp algorithm, which uses BFS to
find augmenting paths.
Edge Removal Strategy: After determining the initial minimum cut, we attempt to reduce it by
removing up to k edges:
• Iterative Edge Removal: For each edge e in the initial min-cut, temporarily remove it,
recalculate the min-cut, and restore the edge.
Algorithm Summary:
o For each edge in the min-cut, temporarily remove it, recalculate, and restore the edge.
Pseudo Code :
int n = residualGraph.size();
queue<int> q;
q.push(s);
visited[s] = true;
parent[s] = -1;
while (!q.empty()) {
2|Page
int u = q.front();
q.pop();
q.push(v);
parent[v] = u;
visited[v] = true;
return false;
int n = graph.size();
vector<int> parent(n);
int maxFlow = 0;
int u = parent[v];
3|Page
for (int v = t; v != s; v = parent[v]) {
int u = parent[v];
residualGraph[u][v] -= pathFlow;
residualGraph[v][u] += pathFlow;
maxFlow += pathFlow;
return maxFlow;
4|Page
return minCutAfterRemoval;
1. Edmonds-Karp Complexity: Each max-flow calculation using the Edmonds-Karp algorithm has
a time complexity of O(mn).
2. Edge Removal Iterations: In the worst case, we might attempt to remove up to mmm edges,
performing an O(mn) operation each time.
Thus, the time complexity of the approach is O(k⋅m⋅n). Since k is a constant upper bound, this reduces
to O(mn), satisfying the problem's constraints.
o With k=0, no edges are removed, so the minimum cut is simply the initial cut, equal
to the max-flow between s and t (via the Max-Flow Min-Cut Theorem).
2. Inductive Hypothesis:
o Assume for k=n edges removed, we have minimized the cut optimally.
o Starting from the configuration after removing n edges, temporarily remove each edge
in the minimum cut set and calculate the new min-cut.
o Among all configurations where an additional edge is removed, select the one with
the smallest min-cut value.
o This guarantees that with n+1 edges removed, we have the optimal minimum cut.
By induction, this process achieves the smallest possible minimum cut after removing up to k edges.
**********************************************************************************
5|Page
Q2) An edge of a flow network is called critical if decreasing the capacity of this edge results in a
decrease in the maximum flow. Give an efficient algorithm that finds a critical edge in a network.
Solution :
Compute Initial Max Flow: Use the Edmonds-Karp algorithm (or any maximum flow algorithm) to find
the initial maximum flow from the source s to the sink t.
Identify Min-Cut Edges: After obtaining the max flow, use BFS/DFS from s to mark all reachable nodes
in the residual graph. An edge from a reachable node to an unreachable node (in the residual graph)
belongs to the minimum cut. These edges are candidates for being critical edges.
Check for Criticality: Any edge in the min-cut is critical. Reducing its capacity will reduce the flow across
the cut, thereby decreasing the maximum flow.
Pseudo Code :
// Function to mark reachable nodes from s in the residual graph after max flow
int n = residualGraph.size();
queue<int> q;
q.push(s);
reachable[s] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
reachable[v] = true;
q.push(v);
6|Page
vector<pair<int, int>> findCriticalEdges(vector<vector<int>>& graph, int s, int t) {
int n = graph.size();
// Mark all reachable nodes in residual graph from source after max flow
markReachable(residualGraph, s, reachable);
// Find edges that are part of the min-cut (reachable -> non-reachable)
criticalEdges.push_back({u, v});
return criticalEdges;
Time Complexity
• Reachable Marking: The markReachable function runs in O(V+E) time, as it performs a BFS
traversal of the residual graph.
• Critical Edge Identification: Checking each edge to ensure that it is part of the min-cut takes
O(V2) time.
Thus, the total complexity is dominated by the max-flow calculation, which is O(V⋅E2).
Induction Hypothesis
Assume for a flow network with k edges, our algorithm identifies all critical edges (those that, if
reduced, lower the max flow). We aim to show this holds for a network with k+1 edges.
Base Case: In a network with only two nodes s and t connected by one edge (s,t) of capacity C, the
edge is critical since reducing its capacity directly reduces the max flow, which equals C. The base case
is thus satisfied.
7|Page
Inductive Step
1. Compute Max Flow: Using Edmonds-Karp, find the max flow from s to t, which equals the
capacity of the min-cut.
2. Identify Min-Cut Edges: Mark nodes reachable from s in the residual graph. Any edge (u, v)
with u reachable and v non-reachable is a min-cut edge.
3. Determine Criticality:
o If (u,v) is a min-cut edge, reducing its capacity decreases the min-cut, thus reducing
the max flow, proving it’s critical.
o If (u,v) is not a min-cut edge, it does not impact max flow if reduced.
By the inductive hypothesis, our algorithm identifies all critical edges for any flow network with k+1
edges. Thus, the proof is complete.
**********************************************************************************
Q3) Suppose you are given a directed graph G = (V,E), with a positive integer capacity ce on each edge
e, a designated source s ∈ V , and a designated sink t ∈ V. You are also given a maximum s-t flow in G,
defined by a flow value f(e) on each edge e. The flow f(e) is acyclic: there is no cycle in G on which all
edges carry positive f low. (a) Suppose we pick a specific edge e∗ ∈ E and increase its capacity by 1
unit. Show how to find a maximum f low in the resulting capacitated graph in time O(m). (b) Suppose
we pick a specific edge e∗ ∈ E and reduce its capacity by 1 unit. Show how to find a maximum flow in
the resulting capacitated graph in time O(m), where m is the number of edges in G
Solution :
When we increase the capacity of a specific edge e∗ by 1 unit, we need to determine the new
maximum flow in the graph. The key observation is that increasing the capacity of a single edge can
potentially increase the flow by at most 1 unit, and we need to check if there is a new augmenting
path from the source s to the sink t.
Solution Approach:
1. Find Augmenting Path: When we increase the capacity of edge e∗, it may create an additional
augmenting path that wasn't possible before. We need to check if there exists a path from s
to t through e∗ or any other edges that were previously fully utilized.
2. Use BFS or DFS: We can use a breadth-first search (BFS) or depth-first search (DFS) from the
source s to the sink t to find an augmenting path in the residual graph.
3. Update Flow: If such a path exists, we push flow along this path and update the flow values.
Since we are only interested in augmenting the flow by a small amount (by 1 unit), we can perform
this operation efficiently in O(m) time.
8|Page
Pseudocode:
int n = residualGraph.size();
queue<int> q;
q.push(s);
visited[s] = true;
parent[s] = -1;
while (!q.empty()) {
int u = q.front();
q.pop();
q.push(v);
parent[v] = u;
visited[v] = true;
return false;
int n = graph.size();
vector<int> parent(n);
9|Page
// Find augmenting path using BFS
if (bfs(residualGraph, s, t, parent)) {
int u = parent[v];
int u = parent[v];
residualGraph[u][v] -= pathFlow;
residualGraph[v][u] += pathFlow;
return pathFlow;
Time Complexity:
• BFS Complexity: BFS runs in O(m), where m is the number of edges in the graph.
• Overall Complexity: Since we are only performing one BFS to find an augmenting path and
update the flow, the overall time complexity is O(m).
Inductive Proof:
Base Case: Initially, the flow is computed and the residual graph is updated. Increasing the capacity of
edge e∗ by 1 unit may create new augmenting paths.
10 | P a g e
1. Inductive Step: Assume after increasing the capacity of any edge by k units, the maximum flow
is found in O(m) time. Now, increasing the capacity of an edge by k+1 units involves:
o Finding Augmenting Path: Use BFS or DFS to find a new augmenting path in the
residual graph, which takes O(m) time.
o Updating Flow: If a new augmenting path is found, adjust the flow, which also takes
O(m) time.
2. Conclusion: Therefore, the new maximum flow can be computed in O(m) time after increasing
the capacity of an edge by 1 unit.
When we reduce the capacity of a specific edge e∗ by 1 unit, we need to recompute the maximum
flow in the graph. The key observation here is that reducing the capacity of one edge could invalidate
some of the previous flow, potentially requiring us to adjust the flow in the residual graph.
Solution Approach:
1. Check for Flow Violation: After reducing the capacity of edge e∗, check if the flow on that edge
exceeds the new capacity. If it does, we need to perform a flow adjustment.
2. Undo Flow Along Affected Path: If the flow on edge e∗ exceeds its new capacity, we need to
undo some of the flow along the path. This can be done by following the residual graph and
finding where the flow can be pushed back.
3. Augment Flow: After adjusting the flow, we can use a BFS/DFS to find new augmenting paths
and push flow through those paths.
Pseudo Code:
// Function to reduce the flow along affected paths when the capacity of edge e* is decreased
int n = residualGraph.size();
vector<int> parent(n);
if (bfs(residualGraph, s, t, parent)) {
int u = parent[v];
11 | P a g e
}
int n = graph.size();
residualGraph[e_u][e_v] -= 1;
Time Complexity:
• Max Flow Recalculation: Using the Edmonds-Karp algorithm to recompute the max flow takes
O(mn) in the worst case.
• Overall Complexity: Since we are updating the flow with BFS and possibly recalculating the
max flow, the overall time complexity is O(m) for the adjustments and O(mn) for max flow
recalculation.
Inductive Proof:
1. Base Case: Initially, the flow is computed, and the residual graph is updated. Decreasing the
capacity of edge e∗ by 1 unit may invalidate the flow on that edge if it exceeds the new
capacity.
2. Inductive Step: Assume after decreasing the capacity of any edge by k units, the maximum
flow is found in O(m) time. Now, decreasing the capacity of an edge by k+1 units involves:
o Undoing Flow: If the flow exceeds the new capacity, reduce it by finding an
augmenting path in reverse, which takes O(m) time.
o Finding New Augmenting Path: Use BFS or DFS to find a new valid augmenting path,
which also takes O(m) time.
12 | P a g e
3. Conclusion: Therefore, the new maximum flow can be computed in O(m) time after decreasing
the capacity of an edge by 1 unit.
**********************************************************************************
Q4) There are many common variations of the maximum flow problem. Here are four of them. (a)
There are many sources and many sinks, and we wish to maximize the total flow from all sources to all
sinks. (b) Each vertex also has a capacity on the maximum flow that can enter it. Each of these can be
solved efficiently. Show this by reducing (a) and (b) to the original max-flow problem.
1. Problem: We have multiple sources (S = {s1, s2, ..., sk}) and multiple sinks (T = {t1, t2, ..., tm}).
2. Reduction:
o Connect Super Source to Sources: Add an edge from "super source" to each source
(s1, s2, ..., sk) with infinite capacity (or a large enough capacity to avoid limiting the
flow).
o Connect Sinks to Super Sink: Add an edge from each sink (t1, t2, ..., tm) to "super sink"
with infinite capacity.
3. Result: The max-flow from "super source" to "super sink" will be the maximum total flow from
all sources to all sinks.
int n = graph.size();
13 | P a g e
graph[superSource][s] = INT_MAX;
graph[t][superSink] = INT_MAX;
Time Complexity
• Reduction Complexity: Adding a super source, super sink, and edges is O(n).
• Max-Flow Complexity: Using Edmonds-Karp, the max-flow complexity on the extended graph
is O(m^2 * n), where n is the number of vertices and m is the number of edges.
• Justification: This reduction introduces only a few extra vertices and edges, so the complexity
remains within the expected bounds for the max-flow problem.
Inductive Proof:
1. Base Case: For one source and one sink, this is a standard max-flow problem. Solving max flow
from s1 to t1 gives the correct answer.
2. Inductive Hypothesis: Assume that for k sources and m sinks, adding S_super and T_super as
described gives the correct total maximum flow from all sources to all sinks.
o Connect the new source (s(k+1)) to S_super and the new sink (t(m+1)) to T_super with
high-capacity edges.
o The previous configuration remains valid, and the added connections simply extend
the solution.
4. Conclusion: By induction, adding a super source and super sink converts any number of
sources and sinks into a single-source, single-sink problem. The solution remains efficient with
time complexity O(m) for m edges, since each edge is processed only once.
1. Problem: Each vertex v has a capacity, c(v), limiting the flow entering v.
2. Reduction:
14 | P a g e
o Split Each Vertex: For each vertex v, create two nodes, vin and vout.
o Add Capacity Edge: Add an edge from vin to vout with capacity c(v).
o Redirect Outgoing Edges: Redirect each original outgoing edge of v from vout.
3. Result: The maximum flow in this modified graph now respects the original vertex capacities.
int n = graph.size();
int newSize = 2 * n;
if (graph[u][v] > 0) {
// Assume source and sink have been remapped to appropriate split nodes
15 | P a g e
Time Complexity
• Reduction Complexity: Splitting each vertex and redirecting edges requires O(m + n)
operations.
• Max-Flow Complexity: Using Edmonds-Karp on the new graph with 2n vertices and 2m edges
has a complexity of O((2m)^2 * 2n) = O(m^2 * n).
• Justification: This modification keeps the complexity within expected bounds for a max-flow
computation on the expanded graph.
Inductive Proof:
1. Base Case: For one vertex with capacity c, splitting it as described ensures that flow through
the vertex cannot exceed c, enforcing the vertex capacity.
2. Inductive Hypothesis: For a graph with k vertices, each transformed by splitting into "in" and
"out" nodes connected by an edge of the vertex’s capacity, the flow respects all vertex
capacities.
o Apply the same transformation to the (k+1)-th vertex, creating v(k+1)in and v(k+1)out
connected by an edge of its capacity.
o This preserves the flow capacities through previous vertices and correctly enforces the
new vertex’s capacity.
4. Conclusion: By induction, this transformation enforces all vertex capacities by using only edge
capacities. Thus, the problem reduces to a standard max-flow problem with O(m) complexity
for m edges in the transformed graph.
**********************************************************************************
16 | P a g e