Edge disjoint paths with Ford-Fulkerson algorithm
来源:互联网 发布:python模拟鼠标点击 编辑:程序博客网 时间:2024/05/21 19:35
//// main.cpp// Edge Disjoint paths//// Created by Longxiang Lyu on 8/15/16.// Copyright © 2016 Longxiang Lyu. All rights reserved.//#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <stack>using namespace std;bool bfs(vector<vector<int>> &rGraph, vector<int> &parent, int s, int t){ int V = static_cast<int>(rGraph.size()); vector<int> visited(V, 0); // mark all vertices as unvisited queue<int> q; q.push(s); visited[s] = 1; // mark source vertex as visited parent.clear(); parent.resize(V); parent[s] = -1; while (!q.empty()) { int u = q.front(); // get the first element q.pop(); for (int v = 0; v != V; ++v) { if (visited[v] == 0 && rGraph[u][v] > 0) { q.push(v); parent[v] = u; visited[v] = 1; } } } return visited[t] == 1;}void printM(const vector<vector<int>> &M){ for (int i = 0; i != M.size(); ++i) { for (int j = 0; j != M[i].size(); ++j) cout << M[i][j] << " "; cout << endl; }}void buildPath(vector<vector<int>> &fGraph, vector<int> &path, int u, int dest){ if (u == dest) return; int sz = static_cast<int>(fGraph[u].size()); for (int j = 0; j != sz; ++j) { if (fGraph[u][j] > 0) { path.push_back(j); fGraph[u][j] = 0; buildPath(fGraph, path, j, dest); break; } } return;}int fordFulkerson(vector<vector<int>> &graph, vector<vector<int>> &paths, int s, int t){ int u, v; vector<int> parent; auto rGraph = graph; int max_flow = 0; // initially zero flow while (bfs(rGraph, parent, s, t)) { int bottleneck = INT_MAX; // get the bottleneck capacity of the augment path for (v = t; v != s; v = parent[v]) { u = parent[v]; bottleneck = min(bottleneck, rGraph[u][v]); } for (v = t; v != s; v = parent[v]) { u = parent[v]; rGraph[u][v] -= bottleneck; rGraph[v][u] += bottleneck; } max_flow += bottleneck; } for (int i = 0; i != graph.size(); ++i) { for (int j = 0; j != graph.size(); ++j) graph[i][j] = graph[i][j] - rGraph[i][j] >= 0 ? graph[i][j] - rGraph[i][j] : 0; } paths.clear(); paths.resize(max_flow); for (int i = 0; i != max_flow; ++i) { paths[i].push_back(s); buildPath(graph, paths[i], s, t); } return max_flow;}int main(int argc, const char * argv[]){ // insert code here... vector<vector<int>> graph = { {0, 1, 1, 1, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 1, 0, 0, 0, 0, 1}, {0, 1, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0} }; vector<vector<int>> paths; cout << "Number of edge disjoint paths: " << endl << fordFulkerson(graph, paths, 0, 7) << endl; for (auto a : paths) { for (auto b : a) cout << b << " "; cout << endl; } return 0;}
reference:
http://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/07NetworkFlowII-2x2.pdf
http://www.geeksforgeeks.org/find-edge-disjoint-paths-two-vertices/
0 0
- Edge disjoint paths with Ford-Fulkerson algorithm
- vertex disjoint paths with ford-fulkerson algorithm
- Ford-Fulkerson algorithm with capacity scaling
- Max bipartite matching with Ford-Fulkerson algorithm
- [max-flow]ford-fulkerson algorithm (转)
- 算法实现(1):Ford-Fulkerson Algorithm
- Ford-Fulkerson
- Ford-Fulkerson
- 【资料】Ford-Fulkerson Algorithm for Maximum Flow Problem
- Ford-Fulkerson algorithm to solve the max flow problem
- max_flow(Ford-Fulkerson)
- Ford Fulkerson 算法
- Ford-Fulkerson(最大流)
- Geeks Ford-Fulkerson Algorithm for Maximum Flow Problem 最大网络流问题
- Ford-Fulkerson算法的效率
- Ford-Fulkerson求解最大流
- 最大流 Ford-Fulkerson算法
- 网络流--ford-fulkerson算法
- 选择排序之Java实现
- cookie自动登录认证
- pdf.js插件乱码问题
- SD/MMC 卡读写模块---SD/MMC 卡的外部物理接口
- 垃圾收集算法
- Edge disjoint paths with Ford-Fulkerson algorithm
- 宗教里的节日
- TestNG的IHookable监听器详解
- 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
- 模拟长按图标,图标抖动效果
- Factory模式(工厂模式 创建型)
- JS一切皆对象的由来
- 解释器模式
- 从零基础接触java第五周第二节