【发布时间】:2019-04-01 22:57:48
【问题描述】:
有一个 C++ implementation 我尝试使用的最大流量问题的 Dinic 算法。在该 C++ 代码中,假定所有参数都是整数。我尝试将代码转换为等效的代码,其中边的容量可以是连续的(非整数)。这是我的代码:
// C++ implementation of Dinic's Algorithm
// #include "HEADERS.h"
#include<bits/stdc++.h>
using namespace std;
double EPSILON = 1e-6 ;
// A structure to represent a edge between
// two vertex
struct Edge
{
int v ; // Vertex v (or "to" vertex)
// of a directed edge u-v. "From"
// vertex u can be obtained using
// index in adjacent array.
double flow ; // flow of data in edge
double C; // capacity
int rev ; // To store index of reverse
// edge in adjacency list so that
// we can quickly find it.
};
// Residual Graph
class Graph
{
int V; // number of vertex
int *level ; // stores level of a node
vector< Edge > *adj;
public :
Graph(int V)
{
adj = new vector<Edge>[V];
this->V = V;
level = new int[V];
}
// add edge to the graph
void addEdge(int u, int v, double C)
{
// Forward edge : 0 flow and C capacity
Edge a{v, 0, C, static_cast<int> (adj[v].size()) };
// Back edge : 0 flow and 0 capacity
Edge b{u, 0, 0, static_cast<int> (adj[u].size()) };
adj[u].push_back(a);
adj[v].push_back(b); // reverse edge
}
bool BFS(int s, int t);
int sendFlow(int s, double flow, int t, int ptr[]);
double DinicMaxflow(int s, int t);
};
// Finds if more flow can be sent from s to t.
// Also assigns levels to nodes.
bool Graph::BFS(int s, int t)
{
for (int i = 0 ; i < V ; i++)
level[i] = -1;
level[s] = 0; // Level of source vertex
// Create a queue, enqueue source vertex
// and mark source vertex as visited here
// level[] array works as visited array also.
list< int > q;
q.push_back(s);
vector<Edge>::iterator i ;
while (!q.empty())
{
int u = q.front();
q.pop_front();
for (i = adj[u].begin(); i != adj[u].end(); i++)
{
Edge &e = *i;
if (level[e.v] < 0 && e.flow < e.C)
{
// Level of current vertex is,
// level of parent + 1
level[e.v] = level[u] + 1;
q.push_back(e.v);
}
}
}
// IF we can not reach to the sink we
// return false else true
return level[t] < 0 ? false : true ;
}
// A DFS based function to send flow after BFS has
// figured out that there is a possible flow and
// constructed levels. This function called multiple
// times for a single call of BFS.
// flow : Current flow send by parent function call
// start[] : To keep track of next edge to be explored.
// start[i] stores count of edges explored
// from i.
// u : Current vertex
// t : Sink
int Graph::sendFlow(int u, double flow, int t, int start[])
{
// Sink reached
if (u == t)
return flow;
// Traverse all adjacent edges one -by - one.
for ( ; start[u] < static_cast <int> (adj[u].size()) ; start[u]++)
{
// Pick next edge from adjacency list of u
Edge &e = adj[u][start[u]];
if (level[e.v] == level[u]+1 && e.flow < e.C)
{
// find minimum flow from u to t
double curr_flow = min(flow, e.C - e.flow);
double temp_flow = sendFlow(e.v, curr_flow, t, start);
// flow is greater than zero
if (temp_flow > 0)
{
// add flow to current edge
e.flow += temp_flow;
// subtract flow from reverse edge
// of current edge
adj[e.v][e.rev].flow -= temp_flow;
return temp_flow;
}
}
}
return 0;
}
// Returns maximum flow in graph
double Graph::DinicMaxflow(int s, int t)
{
// Corner case
if (s == t)
return -1;
double total = 0; // Initialize result
// Augment the flow while there is path
// from source to sink
while (BFS(s, t) == true)
{
// store how many edges are visited
// from V { 0 to V }
int *start = new int[V+1];
double flow = sendFlow(s, INT_MAX, t, start) ;
// while flow is not zero in graph from S to D
while (flow > EPSILON )
{
// Add path flow to overall flow
total += flow;
flow = sendFlow(s, INT_MAX, t, start) ;
}
}
// return maximum flow
return total;
}
// Driver program to test above functions
int main()
{
Graph g(6);
g.addEdge(0, 1, 16 );
g.addEdge(0, 2, 13 );
g.addEdge(1, 2, 10 );
g.addEdge(1, 3, 12 );
g.addEdge(2, 1, 4 );
g.addEdge(2, 4, 14);
g.addEdge(3, 2, 9 );
g.addEdge(3, 5, 20 );
g.addEdge(4, 3, 7 );
g.addEdge(4, 5, 4);
// next exmp
/*g.addEdge(0, 1, 3 );
g.addEdge(0, 2, 7 ) ;
g.addEdge(1, 3, 9);
g.addEdge(1, 4, 9 );
g.addEdge(2, 1, 9 );
g.addEdge(2, 4, 9);
g.addEdge(2, 5, 4);
g.addEdge(3, 5, 3);
g.addEdge(4, 5, 7 );
g.addEdge(0, 4, 10);
// next exp
g.addEdge(0, 1, 10);
g.addEdge(0, 2, 10);
g.addEdge(1, 3, 4 );
g.addEdge(1, 4, 8 );
g.addEdge(1, 2, 2 );
g.addEdge(2, 4, 9 );
g.addEdge(3, 5, 10 );
g.addEdge(4, 3, 6 );
g.addEdge(4, 5, 10 ); */
cout << "Maximum flow " << g.DinicMaxflow(0, 5) << endl ;
return 0;
}
在这段代码中,我将flow 和C 的类型更改为double。我还更改了部分代码以使其适应这种新的double 类型。该代码仅偶尔起作用!它要么输出正确的输出Maximum flow 23,要么抛出Segmentation fault (core dumped) 错误。我真的不知道这段代码有什么问题。有什么想法吗?
【问题讨论】:
-
#include<bits/stdc++.h>否。请包含适当的头文件,而不是这个。其次,您在某些地方使用std::vector,而在其他可以使用它来避免内存泄漏的地方,您不使用它。喜欢这里:adj = new vector<Edge>[V];。这可能是std::vector<std::vector<Edge>> adj;然后是adj.resize(V);或类似的。 -
@PaulMcKenzie 我不知道那个库是什么!我认为可以改用
#include <vector> #include <list> #include <iostream> #include <fstream> #include <climits>。 -
这里有很多未初始化的值:
int *start = new int[V+1];,它们被传递到sendFlow并被使用。打赌您可以使用开发环境附带的debugging tools 找到有关此问题的更多信息。调试器是程序员最好的朋友。 -
Big explanation of the dangers of and links to explanation of
#include<bits/stdc++.h>经验法则是bits中的任何内容,是不供公众使用的 g++ 实现细节。它们未包含在 C++ 标准中,并且可能会立即发生根本性变化。 -
@Opt
sendFlow应该返回double。
标签: c++ type-conversion segmentation-fault coredump max-flow