Skip to content

refactor: Enhance docs, add more tests in FordFulkerson #5953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,30 @@
import java.util.LinkedList;
import java.util.Queue;

/**
* This class implements the Ford-Fulkerson algorithm to compute the maximum flow
* in a flow network.
*
* <p>The algorithm uses breadth-first search (BFS) to find augmenting paths from
* the source vertex to the sink vertex, updating the flow in the network until
* no more augmenting paths can be found.</p>
*/
public final class FordFulkerson {
private static final int INF = Integer.MAX_VALUE;

private FordFulkerson() {
}

/**
* Computes the maximum flow in a flow network using the Ford-Fulkerson algorithm.
*
* @param vertexCount the number of vertices in the flow network
* @param capacity a 2D array representing the capacity of edges in the network
* @param flow a 2D array representing the current flow in the network
* @param source the source vertex in the flow network
* @param sink the sink vertex in the flow network
* @return the total maximum flow from the source to the sink
*/
public static int networkFlow(int vertexCount, int[][] capacity, int[][] flow, int source, int sink) {
int totalFlow = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,126 @@ public void testComplexNetwork() {
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 4);
assertEquals(19, maxFlow);
}

@Test
public void testLargeNetwork() {
int vertexCount = 8;
int[][] capacity = new int[vertexCount][vertexCount];
int[][] flow = new int[vertexCount][vertexCount];

// Setting up a large network
capacity[0][1] = 10;
capacity[0][2] = 5;
capacity[1][3] = 15;
capacity[2][3] = 10;
capacity[1][4] = 10;
capacity[3][5] = 10;
capacity[4][5] = 5;
capacity[4][6] = 10;
capacity[5][7] = 10;
capacity[6][7] = 15;

int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 7);
assertEquals(15, maxFlow); // Maximum flow should be 15
}

@Test
public void testMultipleSourcesAndSinks() {
int vertexCount = 7;
int[][] capacity = new int[vertexCount][vertexCount];
int[][] flow = new int[vertexCount][vertexCount];

// Creating multiple sources and sinks scenario
capacity[0][1] = 10; // Source 1
capacity[0][2] = 5;
capacity[1][3] = 15;
capacity[2][3] = 10;
capacity[3][4] = 10; // Sink 1
capacity[3][5] = 5;
capacity[3][6] = 10; // Sink 2
capacity[5][6] = 10;

int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 4);
assertEquals(10, maxFlow); // Maximum flow should be 10
}

@Test
public void testDisconnectedGraph() {
int vertexCount = 6;
int[][] capacity = new int[vertexCount][vertexCount];
int[][] flow = new int[vertexCount][vertexCount];

// No connection between source and sink
capacity[0][1] = 10; // Only one edge not connected to the sink
capacity[1][2] = 10;
capacity[3][4] = 10;

int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 5);
assertEquals(0, maxFlow); // No flow should be possible
}

@Test
public void testZeroCapacityEdge() {
int vertexCount = 4;
int[][] capacity = new int[vertexCount][vertexCount];
int[][] flow = new int[vertexCount][vertexCount];

// Including a zero capacity edge
capacity[0][1] = 10;
capacity[0][2] = 0; // Zero capacity
capacity[1][3] = 5;
capacity[2][3] = 10;

int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 3);
assertEquals(5, maxFlow); // Flow only possible through 0 -> 1 -> 3
}

@Test
public void testAllEdgesZeroCapacity() {
int vertexCount = 5;
int[][] capacity = new int[vertexCount][vertexCount];
int[][] flow = new int[vertexCount][vertexCount];

// All edges with zero capacity
capacity[0][1] = 0;
capacity[1][2] = 0;
capacity[2][3] = 0;
capacity[3][4] = 0;

int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 4);
assertEquals(0, maxFlow); // No flow should be possible
}

@Test
public void testCycleGraph() {
int vertexCount = 4;
int[][] capacity = new int[vertexCount][vertexCount];
int[][] flow = new int[vertexCount][vertexCount];

// Setting up a cycle
capacity[0][1] = 10;
capacity[1][2] = 5;
capacity[2][0] = 5; // This creates a cycle
capacity[1][3] = 15;
capacity[2][3] = 10;

int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 3);
assertEquals(10, maxFlow); // Maximum flow should be 10
}

@Test
public void testFlowWithExcessCapacity() {
int vertexCount = 5;
int[][] capacity = new int[vertexCount][vertexCount];
int[][] flow = new int[vertexCount][vertexCount];

// Extra capacity in the flow
capacity[0][1] = 20;
capacity[1][2] = 10;
capacity[2][3] = 15;
capacity[1][3] = 5;

int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 3);
assertEquals(15, maxFlow); // Maximum flow should be 15 (20 from 0->1 and 10->2, limited by 15->3)
}
}