public static Graph CreateResidualNetwork(FlowNetwork fn) { FlowNetwork copy = new FlowNetwork(); fn.CopyTo(copy); Graph g = new Graph(); g.Directed = true; for (int i = 0; i < copy.GetVertices().Count; i++) { Vertex v = copy.GetVertices()[i]; v.Tag = i; v.RemoveAllEdges(); g.AddVertex(v); } foreach (FlowEdge e in copy.GetEdges()) { float backflow = e.CurrentFlow; float capacity = e.Capacity; float forwardFlow = capacity - backflow; Vertex fromVertex = e.GetFromVertex(); Vertex toVertex = e.GetToVertex(); int fromIndex = (int)fromVertex.Tag; int toindex = (int)toVertex.Tag; Vertex newTo = g.GetVertices()[toindex]; Vertex newFrom = g.GetVertices()[fromIndex]; if (backflow > 0) { Edge backEdge = new Edge(newTo, newFrom, backflow); g.AddEdge(backEdge); } if (forwardFlow > 0) { Edge forwardEdge = new Edge(newFrom, newTo, forwardFlow); g.AddEdge(forwardEdge); } } return(g); }
private void copyLinkedGraph(FlowNetwork to) { to.Clear(); for (int i = 0; i < this.GetVertices().Count; i++) { vertices[i].Tag = i; Vertex v = new Vertex(); v.Tag = i; to.AddVertex(v); } for (int i = 0; i < this.GetEdges().Count; i++) { Edge currentEdge = this.GetEdges()[i]; Vertex v1 = new Vertex(); v1.Label = currentEdge.GetFromVertex().Label; int cd = (int)currentEdge.GetFromVertex().Tag; if ((int)to.GetVertices()[cd].Tag == cd) { v1 = to.GetVertices()[cd]; } Vertex v2 = new Vertex(); v2.Label = currentEdge.GetToVertex().Label; int cd2 = (int)currentEdge.GetToVertex().Tag; if ((int)to.GetVertices()[cd2].Tag == cd2) { v2 = to.GetVertices()[cd2]; } FlowEdge fe = currentEdge as FlowEdge; Edge e = new FlowEdge(v1, v2, fe.CurrentFlow, fe.Capacity); to.AddEdge(e); } FlowNetwork fn = this as FlowNetwork; if (fn != null) { to.SetSource(fn.GetSource()); to.SetSink(fn.GetSink()); } }
private void copyLinkedGraph(FlowNetwork to) { to.Clear(); for (int i = 0; i < this.GetVertices().Count; i++) { vertices[i].Tag = i; Vertex v = new Vertex(); v.Tag = i; to.AddVertex(v); } for (int i = 0; i < this.GetEdges().Count; i++) { Edge currentEdge = this.GetEdges()[i]; Vertex v1 = new Vertex(); v1.Label = currentEdge.GetFromVertex().Label; int cd = (int)currentEdge.GetFromVertex().Tag; if ((int)to.GetVertices()[cd].Tag == cd) { v1 = to.GetVertices()[cd]; } Vertex v2 = new Vertex(); v2.Label = currentEdge.GetToVertex().Label; int cd2 = (int)currentEdge.GetToVertex().Tag; if ((int)to.GetVertices()[cd2].Tag == cd2) { v2 = to.GetVertices()[cd2]; } FlowEdge fe = currentEdge as FlowEdge; Edge e = new FlowEdge(v1, v2, fe.CurrentFlow, fe.Capacity); to.AddEdge(e); } FlowNetwork fn = this as FlowNetwork; if (fn != null) { to.SetSource(fn.GetSource()); to.SetSink(fn.GetSink()); } }
public static Graph CreateResidualNetwork(FlowNetwork fn) { FlowNetwork copy = new FlowNetwork(); fn.CopyTo(copy); Graph g = new Graph(); g.Directed = true; for (int i = 0; i < copy.GetVertices().Count; i++) { Vertex v = copy.GetVertices()[i]; v.Tag = i; v.RemoveAllEdges(); g.AddVertex(v); } foreach (FlowEdge e in copy.GetEdges()) { float backflow = e.CurrentFlow; float capacity = e.Capacity; float forwardFlow = capacity - backflow; Vertex fromVertex = e.GetFromVertex(); Vertex toVertex = e.GetToVertex(); int fromIndex = (int)fromVertex.Tag; int toindex = (int)toVertex.Tag; Vertex newTo = g.GetVertices()[toindex]; Vertex newFrom = g.GetVertices()[fromIndex]; if (backflow > 0) { Edge backEdge = new Edge(newTo, newFrom, backflow); g.AddEdge(backEdge); } if (forwardFlow > 0) { Edge forwardEdge = new Edge(newFrom, newTo, forwardFlow); g.AddEdge(forwardEdge); } } return g; }