Алгоритм Эдмондса-Карпа: различия между версиями
Перейти к навигации
Перейти к поиску
Ctrlalt (обсуждение | вклад) (→Ссылки) |
Ctrlalt (обсуждение | вклад) Нет описания правки |
||
Строка 1: | Строка 1: | ||
class Graph { | |||
struct Edge { | |||
int a, b, capacity, flow; | |||
Edge(int a, int b, int capacity) : | |||
a(a), b(b), capacity(capacity), flow(0) {} | |||
int other(int v) const { | |||
return v == a ? b : a; | |||
} | |||
int capacityTo(int v) const { | |||
return v == b ? capacity - flow : flow; | |||
} | |||
void addFlowTo(int v, int f) { | |||
flow += (v == b ? f : -f); | |||
} | |||
}; | |||
vector<Edge> edges; | vector<Edge> edges; | ||
vector<vector<int>> g; | vector<vector<int>> g; | ||
vector<bool> | vector<bool> visited; | ||
vector<int> edgeTo; | vector<int> edgeTo; | ||
void bfs(int v) { | void bfs(int v) { | ||
queue<int> q; | queue<int> q; | ||
visited[v] = 1; | |||
q.push(v); | q.push(v); | ||
while (!q.empty()) { | while (!q.empty()) { | ||
Строка 34: | Строка 33: | ||
for (int e : g[v]) { | for (int e : g[v]) { | ||
int to = edges[e].other(v); | int to = edges[e].other(v); | ||
if (! | if (!visited[to] && edges[e].capacityTo(to)) { | ||
edgeTo[to] = e; | edgeTo[to] = e; | ||
visited[to] = 1; | |||
q.push(to); | q.push(to); | ||
} | } | ||
Строка 42: | Строка 41: | ||
} | } | ||
} | } | ||
bool hasPath(int from, int to) { | bool hasPath(int from, int to) { | ||
fill( | fill(visited.begin(), visited.end(), 0); | ||
bfs(from); | bfs(from); | ||
return | return visited[to]; | ||
} | } | ||
int bottleneckCapacity(int from, int to) { | int bottleneckCapacity(int from, int to) { | ||
int bCapacity = 1e9; | int bCapacity = 1e9; | ||
Строка 53: | Строка 54: | ||
return bCapacity; | return bCapacity; | ||
} | } | ||
void addFlow(int from, int to, int flow) { | void addFlow(int from, int to, int flow) { | ||
for (int v = to; v != from; v = edges[edgeTo[v]].other(v)) | for (int v = to; v != from; v = edges[edgeTo[v]].other(v)) | ||
edges[edgeTo[v]].addFlowTo(v, flow); | edges[edgeTo[v]].addFlowTo(v, flow); | ||
} | } | ||
public: | public: | ||
Graph(int | Graph(int vertexCount) { | ||
g.resize( | g.resize(vertexCount); | ||
visited.resize(vertexCount); | |||
edgeTo.resize( | edgeTo.resize(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)); | ||
Строка 68: | Строка 72: | ||
g[to].push_back(edges.size() - 1); | g[to].push_back(edges.size() - 1); | ||
} | } | ||
long long maxFlow(int from, int to) { | long long maxFlow(int from, int to) { | ||
long long flow = 0; | long long flow = 0; | ||
Строка 78: | Строка 83: | ||
} | } | ||
}; | }; | ||
== Ссылки == | == Ссылки == |
Версия от 02:48, 26 декабря 2021
class Graph { struct Edge { int a, b, capacity, flow; Edge(int a, int b, int capacity) : a(a), b(b), capacity(capacity), flow(0) {} int other(int v) const { return v == a ? b : a; } int capacityTo(int v) const { return v == b ? capacity - flow : flow; } void addFlowTo(int v, int f) { flow += (v == b ? f : -f); } }; vector<Edge> edges; vector<vector<int>> g; vector<bool> visited; vector<int> edgeTo; void bfs(int v) { queue<int> q; visited[v] = 1; q.push(v); while (!q.empty()) { v = q.front(); q.pop(); for (int e : g[v]) { int to = edges[e].other(v); if (!visited[to] && edges[e].capacityTo(to)) { edgeTo[to] = e; visited[to] = 1; q.push(to); } } } } bool hasPath(int from, int to) { fill(visited.begin(), visited.end(), 0); bfs(from); return visited[to]; } int bottleneckCapacity(int from, int to) { int bCapacity = 1e9; for (int v = to; v != from; v = edges[edgeTo[v]].other(v)) bCapacity = min(bCapacity, edges[edgeTo[v]].capacityTo(v)); return bCapacity; } void addFlow(int from, int to, int flow) { for (int v = to; v != from; v = edges[edgeTo[v]].other(v)) edges[edgeTo[v]].addFlowTo(v, flow); } public: Graph(int vertexCount) { g.resize(vertexCount); visited.resize(vertexCount); edgeTo.resize(vertexCount); } void addEdge(int from, int to, int capacity) { edges.push_back(Edge(from, to, capacity)); g[from].push_back(edges.size() - 1); g[to].push_back(edges.size() - 1); } long long maxFlow(int from, int to) { long long flow = 0; while (hasPath(from, to)) { int deltaFlow = bottleneckCapacity(from, to); addFlow(from, to, deltaFlow); flow += deltaFlow; } return flow; } };
Ссылки
Теория:
- algs4.cs.princeton.edu — 6.4 Maximum Flow
- e-maxx.ru — Алгоритм Эдмондса-Карпа нахождения максимального потока за O (NM^2)
- neerc.ifmo.ru/wiki — Алгоритм Эдмондса-Карпа
- Brilliant.org — Edmonds-Karp Algorithm
Демонстрация:
Код:
- CodeLibrary — Maximum flow. Edmonds-Karp algorithm in O(min(E^2 * V, E * FLOW))
- algs4.cs.princeton.edu/code — capacitated edge with flow, capacitated network, maxflow–mincut (несмотря на название, используется алгоритм Эдмондса-Карпа)
Задачи: