Алгоритм Форда-Фалкерсона: различия между версиями
Перейти к навигации
Перейти к поиску
Ctrlalt (обсуждение | вклад) Нет описания правки |
Ctrlalt (обсуждение | вклад) Нет описания правки |
||
| (не показано 7 промежуточных версий этого же участника) | |||
| Строка 1: | Строка 1: | ||
class Graph { | |||
struct Edge { | |||
int a, b, capacity, flow = 0; | |||
Edge(int a, int b, int capacity) : | |||
a(a), b(b), capacity(capacity) {} | |||
int other(int v) const { | |||
return v == b ? a : b; | |||
} | |||
int capacityTo(int v) const { | |||
return v == b ? capacity - flow : flow; | |||
} | |||
void addFlowTo(int v, int deltaFlow) { | |||
flow += (v == b ? deltaFlow : -deltaFlow); | |||
} | |||
}; | |||
vector<Edge> edges; | vector<Edge> edges; | ||
vector< vector<int> > | vector<vector<int>> graph; | ||
vector<int> visited, edgeTo; | |||
vector<int> edgeTo; | |||
void dfs(int v) { | void dfs(int v) { | ||
visited[v] = 1; | |||
for (int | for (int edgeIndex : graph[v]) { | ||
int | int to = edges[edgeIndex].other(v); | ||
if (! | if (!visited[to] && edges[edgeIndex].capacityTo(to)) { | ||
edgeTo[to] = | edgeTo[to] = edgeIndex; | ||
dfs(to); | dfs(to); | ||
} | } | ||
} | } | ||
} | } | ||
bool hasPath(int | |||
bool hasPath(int start, int finish) { | |||
dfs( | visited.assign(graph.size(), 0); | ||
return | edgeTo.assign(graph.size(), -1); | ||
dfs(start); | |||
return visited[finish]; | |||
} | } | ||
int | |||
int | int getMinCapacity(int start, int finish) { | ||
for (int v = | int minCapacity = 1e9; | ||
for (int v = finish; v != start; v = edges[edgeTo[v]].other(v)) | |||
return | minCapacity = min(minCapacity, edges[edgeTo[v]].capacityTo(v)); | ||
return minCapacity; | |||
} | } | ||
void addFlow(int | |||
for (int v = | void addFlow(int start, int finish, int deltaFlow) { | ||
edges[edgeTo[v]].addFlowTo(v, | for (int v = finish; v != start; v = edges[edgeTo[v]].other(v)) | ||
edges[edgeTo[v]].addFlowTo(v, deltaFlow); | |||
} | } | ||
public: | public: | ||
Graph(int | Graph(int vertexCount) : graph(vertexCount) {} | ||
void addEdge(int from, int to, int capacity) { | void addEdge(int from, int to, int capacity) { | ||
edges.push_back(Edge(from, to, capacity)); | edges.push_back(Edge(from, to, capacity)); | ||
graph[from].push_back(edges.size() - 1); | |||
graph[to].push_back(edges.size() - 1); | |||
} | } | ||
long long maxFlow(int | |||
long long maxFlow(int start, int finish) { | |||
long long flow = 0; | long long flow = 0; | ||
while (hasPath( | while (hasPath(start, finish)) { | ||
int deltaFlow = | int deltaFlow = getMinCapacity(start, finish); | ||
addFlow( | addFlow(start, finish, deltaFlow); | ||
flow += deltaFlow; | flow += deltaFlow; | ||
} | } | ||
| Строка 70: | Строка 72: | ||
} | } | ||
}; | }; | ||
== Ссылки == | == Ссылки == | ||
Теория: | Теория: | ||
* [ | * [https://algs4.cs.princeton.edu/lectures/keynote/64MaxFlow.pdf algs4.cs.princeton.edu — 6.4 Maximum Flow] | ||
* [http://neerc.ifmo.ru/wiki/index.php?title=%D0%90%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC_%D0%A4%D0%BE%D1%80%D0%B4%D0%B0-%D0%A4%D0%B0%D0%BB%D0%BA%D0%B5%D1%80%D1%81%D0%BE%D0%BD%D0%B0,_%D1%80%D0%B5%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D1%8F_%D1%81_%D0%BF%D0%BE%D0%BC%D0%BE%D1%89%D1%8C%D1%8E_%D0%BF%D0%BE%D0%B8%D1%81%D0%BA%D0%B0_%D0%B2_%D0%B3%D0%BB%D1%83%D0%B1%D0%B8%D0%BD%D1%83 neerc.ifmo.ru/wiki | * [http://neerc.ifmo.ru/wiki/index.php?title=%D0%90%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC_%D0%A4%D0%BE%D1%80%D0%B4%D0%B0-%D0%A4%D0%B0%D0%BB%D0%BA%D0%B5%D1%80%D1%81%D0%BE%D0%BD%D0%B0,_%D1%80%D0%B5%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D1%8F_%D1%81_%D0%BF%D0%BE%D0%BC%D0%BE%D1%89%D1%8C%D1%8E_%D0%BF%D0%BE%D0%B8%D1%81%D0%BA%D0%B0_%D0%B2_%D0%B3%D0%BB%D1%83%D0%B1%D0%B8%D0%BD%D1%83 neerc.ifmo.ru/wiki — Алгоритм Форда-Фалкерсона] | ||
* [http://brilliant.org/wiki/ford-fulkerson-algorithm Brilliant.org — Ford-Fulkerson Algorithm] | |||
* [https://codeforces.com/blog/entry/145343 codeforces.com — Worst-Case Graphs for Maximum Flow Algorithms] | |||
Демонстрация: | Демонстрация: | ||
* [ | * [https://visualgo.net/en/maxflow VisuAlgo — Network Flow] | ||
Код: | Код: | ||
* [ | * [https://github.com/indy256/codelibrary/blob/master/java/graphs/flows/MaxFlowFordFulkerson.java CodeLibrary — Maximum flow. Ford-Fulkerson alogithm in O(V^2 * FLOW)] | ||
* [http://github.com/ADJA/algos/blob/master/Graphs/FordFulkerson.cpp Algos — Ford-Fulkerson maxflow] | * [http://github.com/ADJA/algos/blob/master/Graphs/FordFulkerson.cpp Algos — Ford-Fulkerson maxflow] | ||
* algs4.cs.princeton.edu/code — [http://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/FlowEdge.java.html capacitated edge with flow], [http://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/FlowNetwork.java.html capacitated network], [http://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/FordFulkerson.java.html maxflow–mincut] (несмотря на название, используется алгоритм Эдмондса-Карпа) | * algs4.cs.princeton.edu/code — [http://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/FlowEdge.java.html capacitated edge with flow], [http://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/FlowNetwork.java.html capacitated network], [http://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/FordFulkerson.java.html maxflow–mincut] (несмотря на название, используется алгоритм Эдмондса-Карпа) | ||
Текущая версия от 19:45, 22 октября 2025
class Graph {
struct Edge {
int a, b, capacity, flow = 0;
Edge(int a, int b, int capacity) :
a(a), b(b), capacity(capacity) {}
int other(int v) const {
return v == b ? a : b;
}
int capacityTo(int v) const {
return v == b ? capacity - flow : flow;
}
void addFlowTo(int v, int deltaFlow) {
flow += (v == b ? deltaFlow : -deltaFlow);
}
};
vector<Edge> edges;
vector<vector<int>> graph;
vector<int> visited, edgeTo;
void dfs(int v) {
visited[v] = 1;
for (int edgeIndex : graph[v]) {
int to = edges[edgeIndex].other(v);
if (!visited[to] && edges[edgeIndex].capacityTo(to)) {
edgeTo[to] = edgeIndex;
dfs(to);
}
}
}
bool hasPath(int start, int finish) {
visited.assign(graph.size(), 0);
edgeTo.assign(graph.size(), -1);
dfs(start);
return visited[finish];
}
int getMinCapacity(int start, int finish) {
int minCapacity = 1e9;
for (int v = finish; v != start; v = edges[edgeTo[v]].other(v))
minCapacity = min(minCapacity, edges[edgeTo[v]].capacityTo(v));
return minCapacity;
}
void addFlow(int start, int finish, int deltaFlow) {
for (int v = finish; v != start; v = edges[edgeTo[v]].other(v))
edges[edgeTo[v]].addFlowTo(v, deltaFlow);
}
public:
Graph(int vertexCount) : graph(vertexCount) {}
void addEdge(int from, int to, int capacity) {
edges.push_back(Edge(from, to, capacity));
graph[from].push_back(edges.size() - 1);
graph[to].push_back(edges.size() - 1);
}
long long maxFlow(int start, int finish) {
long long flow = 0;
while (hasPath(start, finish)) {
int deltaFlow = getMinCapacity(start, finish);
addFlow(start, finish, deltaFlow);
flow += deltaFlow;
}
return flow;
}
};
Ссылки
Теория:
- algs4.cs.princeton.edu — 6.4 Maximum Flow
- neerc.ifmo.ru/wiki — Алгоритм Форда-Фалкерсона
- Brilliant.org — Ford-Fulkerson Algorithm
- codeforces.com — Worst-Case Graphs for Maximum Flow Algorithms
Демонстрация:
Код:
- CodeLibrary — Maximum flow. Ford-Fulkerson alogithm in O(V^2 * FLOW)
- Algos — Ford-Fulkerson maxflow
- algs4.cs.princeton.edu/code — capacitated edge with flow, capacitated network, maxflow–mincut (несмотря на название, используется алгоритм Эдмондса-Карпа)
Задачи: