> For the complete documentation index, see [llms.txt](https://emory.gitbook.io/dsa-java/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emory.gitbook.io/dsa-java/network-flow/flow-network.md).

# 9.1. Flow Network

A flow network is a directed graph that has the following properties:

{% embed url="<https://www.slideshare.net/jchoi7s/flow-network>" %}

## Max Flow

Let us define the [`MaxFlow`](https://github.com/emory-courses/dsa-java/blob/master/src/main/java/edu/emory/cs/graph/flow/MaxFlow.java) class that keeps track of the amount of flows in every edge used to find the maximum flow:

```java
public class MaxFlow {
    private Map<Edge, Double> flow_map;
    private double maxflow;

    public MaxFlow(Graph graph) {
        init(graph);
    }

    public void init(Graph graph) {
        flow_map = new HashMap<>();
        maxflow = 0;

        for (Edge edge : graph.getAllEdges())
            flow_map.put(edge, 0d);
    }
}
```

* `L2`: consists of (edge, flow) as the key-value pair.
* `L3`: stores the total amount of flows to the target vertices.
* `L13-14`: initializes all flows to `0`.

Let us define getter methods:

```java
public double getMaxFlow() {
    return maxflow;
}

public double getResidual(Edge edge) {
    return edge.getWeight() - flow_map.get(edge);
}
```

* `L5`: returns the remaining residual that can be used to push more flows.
  * `L6`: the edge weight represents the total capacity.

Finally, let us define setter methods:

```java
public void updateFlow(List<Edge> path, double flow) {
    path.forEach(e -> updateFlow(e, flow));
    maxflow += flow;
}

public void updateFlow(Edge edge, double flow) {
    Double prev = flow_map.getOrDefault(edge, 0d);
    flow_map.put(edge, prev + flow);
}
```

* `L1`: takes the path from a source and a target.
  * `L2`: updates the flow of every edge in the path with the specific flow.
  * `L3`: updates the overall flow.
* `L6`: adds the specific flow to the specific edge.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://emory.gitbook.io/dsa-java/network-flow/flow-network.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
