This project is a Python implementation of the Maximum Flow problem, specifically designed to handle graphs that have capacities on both edges and nodes (vertices). This was developed for an Algorithms & Data Structures course and demonstrates the "vertex-splitting" transformation required to solve this problem.
The program uses the Edmonds-Karp algorithm to find the maximum flow and reports the total flow as well as the flow distribution across all original nodes and edges.
Imagine a computer network where you want to send a continuous stream of data from a source computer to a sink (destination) computer at the maximum possible speed. To do this, you can split the data into packets and send them along multiple paths.
This network has two types of bottlenecks:
- Line Capacity (Edge Capacity): Each physical connection between two computers (an edge) has a maximum bandwidth (e.g., 100 MB/s).
- Router Capacity (Node Capacity): Each computer or router in the network (a node) can only process a certain amount of data per second (e.g., 500 MB/s).
The Goal: Find the absolute maximum flow of data (in data per second) that can be sent from the source to the sink, respecting both the edge capacities and the node capacities.
The core challenge of this problem is that standard max-flow algorithms (like Edmonds-Karp) only support capacities on edges. They cannot enforce a capacity limit on a node.
To solve this, we perform a graph transformation to create an equivalent graph that only has edge capacities.
-
Split Nodes: Every node
vin the original graph (with capacityC_v) is split into two new nodes: an "in-node"v_inand an "out-node"v_out. -
Add Internal Edge: A new internal edge is created from
v_intov_out. The capacity of this new edge is set to the capacity of the original node:capacity(v_in, v_out) = C_v. This new edge now correctly models the capacity of the node. -
Redirect Edges: Every original edge
(u, v)(with capacityC_uv) is re-routed in the new graph to connect the out-node ofuto the in-node ofv. The capacity remains the same:capacity(u_out, v_in) = C_uv. -
New Source & Sink:
- The new source becomes the out-node of the original source (
source_out). - The new sink becomes the in-node of the original sink (
sink_in).
- The new source becomes the out-node of the original source (
This new, larger graph now perfectly represents the original problem using only edge capacities. We can run the standard Edmonds-Karp algorithm on it to find the max flow from source_out to sink_in.
- Solves Node Capacities: Correctly finds the max flow in a graph with both node and edge capacities.
- Edmonds-Karp Algorithm: Uses a clean, standard implementation of Edmonds-Karp (BFS on a residual graph).
- Modular Package Structure: All logic is contained within the
maxflow/Python package.transformer.py: Handles the vertex-splitting transformation.edmonds_karp.py: Contains the pure max-flow algorithm.
- Data-Driven: The graph is loaded from a flexible
JSONfile instead of being hard-coded. - Clear Results: The output shows the total max flow and a detailed breakdown of the flow used on every node and edge.
python-max-flow-vertex-capacity/
├── .gitignore # Git ignore file
├── LICENSE # MIT license file
├── README.md # This documentation
├── main.py # Main runnable script (CLI)
├── sample_graph.json # An example graph input file
└── maxflow/
├── __init__.py # Makes 'maxflow' a Python package
├── edmonds_karp.py # The pure max-flow algorithm
└── transformer.py # The vertex-splitting transformation logic
Create a .json file to define your graph, nodes, and edges. See sample_graph.json for an example.
my_graph.json:
{
"nodes": {
"s": 100,
"a": 20,
"b": 30,
"t": 100
},
"edges": [
["s", "a", 15],
["s", "b", 20],
["a", "t", 25],
["b", "t", 10]
],
"source": "s",
"sink": "t"
}Note: Node capacities for the source and sink can be set to Infinity (or just a very large number) if they are uncapacitated.
Run main.py and pass your JSON file as an argument.
python main.py sample_graph.json$ python main.py sample_graph.json
Loading graph from 'sample_graph.json'...
Calculating max flow using Edmonds-Karp...
--- Results ---
TOTAL MAXIMUM FLOW: 29
Flow through NODES:
- Node 0: 15 / 20
- Node 1: 14 / 15
- Node 2: 15 / 17
- Node 3: 25 / 25
Flow through EDGES:
- Edge 0->1: 15 / 15
- Edge 1->2: 14 / 14
- Edge 2->3: 15 / 30
- Edge 3->1: 0 / 18
- Edge 3->2: 0 / 15
(Note: The exact flow distribution may vary, but the Total Max Flow will be the same.)
Feel free to connect or reach out if you have any questions!
- Maryam Rezaee
- GitHub: @msmrexe
- Email: ms.maryamrezaee@gmail.com
This project is licensed under the MIT License. See the LICENSE file for full details.