我实施了Ford-Fulkerson算法,但在增加阶段后更新图表时遇到了一些问题。我的数据结构并不能让我觉得这很容易。
为了表示图表我使用:
private Map<Vertex, ArrayList<Edge>> outgoingEdges;
也就是说,我在每个顶点关联它的传出边缘列表。
为了管理后方边缘,我将一个&#34;对面的&#34;图中每条边的边缘。
任何建议都表示赞赏。
public class FF {
/**
* Associates each Vertex with his list of outgoing edges
*/
private Map<Vertex, ArrayList<Edge>> outgoingEdges;
public FF() {
outgoingEdges = new HashMap<Vertex, ArrayList<Edge>>();
}
/**
* Returns the nodes of the graph
*/
public Collection<Vertex> getNodes() {
return outgoingEdges.keySet();
}
/**
* Returns the outgoing edges of a node
*/
public Collection<Edge> getIncidentEdges(Vertex v) {
return outgoingEdges.get(v);
}
/**
* Adds a new edge to the graph
*/
public void insertEdge(Vertex source, Vertex destination, float capacity) throws Exception {
if (!(outgoingEdges.containsKey(source) && outgoingEdges.containsKey(destination)))
throw new Exception("Unable to add the edge");
Edge e = new Edge(source, destination, capacity);
Edge opposite = new Edge(destination, source, capacity);
outgoingEdges.get(source).add(e);
outgoingEdges.get(destination).add(opposite);
e.setOpposite(opposite);
opposite.setOpposite(e);
}
/**
* Adds a new node to the graph
*/
public void insertNode(Vertex v) {
if (!outgoingEdges.containsKey(v))
outgoingEdges.put(v, new ArrayList<Edge>());
}
/**
* Ford-Fulkerson algorithm
*
* @return max flow
*/
public int fordFulkerson(Vertex source, Vertex destination) {
List<Edge> path = new ArrayList<Edge>();
int maxFlow = 0;
while(bfs(source, destination, path)) {
// finds the bottleneck
float minCap = bottleneck(path);
// updates the maxFlow
maxFlow += minCap;
// updates the graph <-- this updates only the local list path, not the graph!
for(Edge e : path) {
try {
e.addFlow(minCap);
e.getOpposite().addFlow(-minCap);
} catch (Exception e1) {
e1.printStackTrace();
}
}
path.clear();
}
return maxFlow;
}
/**
* @param Path of which we have to find the bottleneck
* @return bottleneck of the path
*/
private float bottleneck(List<Edge> path) {
float min = Integer.MAX_VALUE;
for(Edge e : path) {
float capacity = e.getCapacity();
if(capacity <= min) {
min = capacity;
}
}
return min;
}
/**
* BFS to obtain a path from the source to the destination
*
* @param source
* @param destination
* @param path
* @return
*/
private boolean bfs(Vertex source, Vertex destination, List<Edge> path) {
Queue<Vertex> queue = new LinkedList<Vertex>();
List<Vertex> visited = new ArrayList<Vertex>(); // list of visited vertexes
queue.add(source);
//source.setVisited(true);
visited.add(source);
while(!queue.isEmpty()) {
Vertex d = queue.remove();
if(!d.equals(destination)) {
ArrayList<Edge> d_outgoingEdges = outgoingEdges.get(d);
for(Edge e : d_outgoingEdges) {
if(e.getCapacity() - e.getFlow() > 0) { // there is still available flow
Vertex u = e.getDestination();
if(!visited.contains(u)) {
//u.setVisited(true);
visited.add(u);
queue.add(u);
path.add(e);
}
}
}
}
}
if(visited.contains(destination)) {
return true;
}
return false;
}
}
边
public class Edge {
private Vertex source;
private Vertex destination;
private float flow;
private final float capacity;
private Edge opposite;
public Edge(Vertex source, Vertex destination, float capacity) {
this.source = source;
this.destination = destination;
this.capacity = capacity;
}
public Edge getOpposite() {
return opposite;
}
public void setOpposite(Edge e) {
opposite = e;
}
public void setSource(Vertex v) {
source = v;
}
public void setDestination(Vertex v) {
destination = v;
}
public void addFlow(float f) throws Exception {
if(flow == capacity) {
throw new Exception("Unable to add flow");
}
flow += f;
}
public Vertex getSource() {
return source;
}
public Vertex getDestination() {
return destination;
}
public float getFlow() {
return flow;
}
public float getCapacity() {
return capacity;
}
public boolean equals(Object o) {
Edge e = (Edge)o;
return e.getSource().equals(this.getSource()) && e.getDestination().equals(this.getDestination());
}
}
顶点
public class Vertex {
private String label;
public Vertex(String label) {
this.label = label;
}
public boolean isVisited() {
return visited;
}
public String getLabel() {
return label;
}
public boolean equals(Object o) {
Vertex v = (Vertex)o;
return v.getLabel().equals(this.getLabel());
}
}
答案 0 :(得分:0)
虽然严格来说,这个问题可以被认为是“非主题”(因为你主要是寻找调试帮助),这是你的第一个问题,所以有一些一般的提示:
当您在此处发布问题时,请考虑此处的人员是志愿者。让他们轻松让他们回答这个问题。在这种特殊情况下:您应该创建一个MCVE,以便人们可以快速复制&amp;粘贴您的代码(最好是在一个代码块中)并毫不费力地运行程序。例如,您应该包含一个测试类,包含main
方法,如下所示:
public class FFTest
{
/**
* B---D
* / \ / \
* A . F
* \ / \ /
* C---E
*/
public static void main(String[] args) throws Exception
{
FF ff = new FF();
Vertex vA = new Vertex("A");
Vertex vB = new Vertex("B");
Vertex vC = new Vertex("C");
Vertex vD = new Vertex("D");
Vertex vE = new Vertex("E");
Vertex vF = new Vertex("F");
ff.insertNode(vA);
ff.insertNode(vB);
ff.insertNode(vC);
ff.insertNode(vD);
ff.insertNode(vE);
ff.insertNode(vF);
ff.insertEdge(vA, vB, 3.0f);
ff.insertEdge(vA, vC, 2.0f);
ff.insertEdge(vB, vD, 1.0f);
ff.insertEdge(vB, vE, 4.0f);
ff.insertEdge(vC, vD, 2.0f);
ff.insertEdge(vC, vE, 1.0f);
ff.insertEdge(vD, vF, 2.0f);
ff.insertEdge(vE, vF, 1.0f);
float result = ff.fordFulkerson(vA, vF);
System.out.println(result);
}
}
(无论如何,当你写这个问题时,你应该已经创建了这样一个测试类!)
你应该明确表示你是不使用StackOverflow作为“神奇的解决问题的机器”。在这种情况下:我已经提到你应该包括调试输出。如果您使用这些方法扩展了FF
课程....
private static void printPath(List<Edge> path)
{
System.out.println("Path: ");
for (int i=0; i<path.size(); i++)
{
Edge e = path.get(i);
System.out.println(
"Edge "+e+
" flow "+e.getFlow()+
" cap "+e.getCapacity());
}
}
可以在主循环中调用,如下所示:
while(bfs(source, destination, path)) {
...
System.out.println("Before updating with "+minCap);
printPath(path);
// updates the maxFlow
....
System.out.println("After updating with "+minCap);
printPath(path);
...
}
那么您已经注意到代码的主要问题:...
bfs
方法错了!您不正确重建导致您到目标顶点的路径。相反,您将每个访问过的顶点添加到路径中。您必须跟踪用于到达特定节点的边缘,当您到达目标顶点时,必须向后移动。
快速而肮脏的方法可以粗略地(!)看起来像这样:
private boolean bfs(Vertex source, Vertex destination, List<Edge> path) {
Queue<Vertex> queue = new LinkedList<Vertex>();
List<Vertex> visited = new ArrayList<Vertex>(); // list of visited vertexes
queue.add(source);
visited.add(source);
Map<Vertex, Edge> predecessorEdges = new HashMap<Vertex, Edge>();
while(!queue.isEmpty()) {
Vertex d = queue.remove();
if(!d.equals(destination)) {
ArrayList<Edge> d_outgoingEdges = outgoingEdges.get(d);
for(Edge e : d_outgoingEdges) {
if(e.getCapacity() - e.getFlow() > 0) { // there is still available flow
Vertex u = e.getDestination();
if(!visited.contains(u)) {
visited.add(u);
queue.add(u);
predecessorEdges.put(u, e);
}
}
}
}
else
{
constructPath(destination, predecessorEdges, path);
return true;
}
}
return false;
}
private void constructPath(Vertex destination,
Map<Vertex, Edge> predecessorEdges, List<Edge> path)
{
Vertex v = destination;
while (true)
{
Edge e = predecessorEdges.get(v);
if (e == null)
{
return;
}
path.add(0, e);
v = e.getSource();
}
}
(你应该总是独立地测试这样一个中心方法。你可以轻松地创建一个计算多个路径的小测试程序,你很快就会注意到这些路径是错误的 - 因此,福特富克森根本无法正常工作。)
进一步评论:
每当您覆盖equals
方法时,您还必须覆盖hashCode
方法。这里适用一些规则,您应绝对引用documentation of Object#equals
和Object#hashCode
。
额外覆盖toString
方法通常很有用,这样您就可以轻松地将对象打印到控制台。
在您的情况下,对于Vertex
@Override
public int hashCode()
{
return getLabel().hashCode();
}
@Override
public boolean equals(Object o) {
Vertex v = (Vertex)o;
return v.getLabel().equals(this.getLabel());
}
@Override
public String toString()
{
return getLabel();
}
适用于Edge
@Override
public String toString()
{
return "("+getSource()+","+getDestination()+")";
}
@Override
public int hashCode()
{
return source.hashCode() ^ destination.hashCode();
}
@Override
public boolean equals(Object o) {
Edge e = (Edge)o;
return e.getSource().equals(this.getSource()) &&
e.getDestination().equals(this.getDestination());
}
边缘的容量为float
值,因此生成的流量也应为float
值。
通过上述修改,程序运行并打印出“似是而非”的结果。我 NOT 验证了它的正确性。但这是你的任务,现在应该更容易了。
P.S:不,io sono tedesco。