public double Compute(Vertex src, Vertex sink) { m_ResidualEdgeCapacities = new EdgeDoubleDictionary(); // initializing foreach(Vertex u in VisitedGraph.Vertices) foreach(Edge e in u.OutEdges) ResidualCapacities[e] = Capacities[e]; Colors[sink] = GraphColor.Gray; while (Colors[sink] != GraphColor.White) { VertexBuffer Q = new VertexBuffer(); ResidualEdgePredicate ep = new ResidualEdgePredicate(ResidualCapacities); BreadthFirstSearchAlgorithm bfs = new BreadthFirstSearchAlgorithm(resg,Q,Colors); PredecessorVisitor pred = new PredecessorVisitor(Predecessors); pred.RegisterHandlers(bfs); bfs.Compute(src); if (Colors[sink] != GraphColor.White) Augment(src, sink, pred.Predecessors); } // while double flow = 0; foreach(Edge e in src.OutEdges) flow += (EdgeCapacities[e] - ResidualEdgeCapacities[e]); return flow; }
public BreadthFirstSearchAlgorithm(IVertexListGraph g) { if (g == null) { throw new ArgumentNullException("g"); } this.m_VisitedGraph = g; this.m_Colors = new VertexColorDictionary(); this.m_Q = new VertexBuffer(); }
public BreadthFirstSearchAlgorithm(IVertexListGraph g, VertexBuffer q, VertexColorDictionary colors) { if (g == null) { throw new ArgumentNullException("g"); } if (q == null) { throw new ArgumentNullException("Stack Q is null"); } if (colors == null) { throw new ArgumentNullException("Colors"); } this.m_VisitedGraph = g; this.m_Colors = colors; this.m_Q = q; }
public override double Compute(IVertex src, IVertex sink) { if (src == null) { throw new ArgumentNullException("src"); } if (sink == null) { throw new ArgumentNullException("sink"); } IVertexEnumerator enumerator = base.VisitedGraph.get_Vertices().GetEnumerator(); while (enumerator.MoveNext()) { IVertex vertex = enumerator.get_Current(); IEdgeEnumerator enumerator2 = base.VisitedGraph.OutEdges(vertex).GetEnumerator(); while (enumerator2.MoveNext()) { IEdge edge = enumerator2.get_Current(); base.ResidualCapacities.set_Item(edge, base.Capacities.get_Item(edge)); } } base.Colors.set_Item(sink, 2); while (base.Colors.get_Item(sink) != null) { PredecessorRecorderVisitor vis = new PredecessorRecorderVisitor(base.Predecessors); VertexBuffer q = new VertexBuffer(); BreadthFirstSearchAlgorithm algorithm = new BreadthFirstSearchAlgorithm(this.ResidualGraph, q, base.Colors); algorithm.RegisterPredecessorRecorderHandlers(vis); algorithm.Compute(src); if (base.Colors.get_Item(sink) != null) { this.Augment(src, sink); } } double num = 0.0; IEdgeEnumerator enumerator3 = base.VisitedGraph.OutEdges(src).GetEnumerator(); while (enumerator3.MoveNext()) { IEdge edge2 = enumerator3.get_Current(); num += base.Capacities.get_Item(edge2) - base.ResidualCapacities.get_Item(edge2); } return num; }
/// <summary> /// Computes the maximum flow between <paramref name="src"/> and /// <paramref name="sink"/> /// </summary> /// <param name="src"></param> /// <param name="sink"></param> /// <returns></returns> public override double Compute(IVertex src, IVertex sink) { if (src==null) throw new ArgumentNullException("src"); if (sink==null) throw new ArgumentNullException("sink"); foreach(IVertex u in VisitedGraph.Vertices) { foreach(IEdge e in VisitedGraph.OutEdges(u)) { ResidualCapacities[e] = Capacities[e]; } } Colors[sink] = GraphColor.Gray; while (Colors[sink] != GraphColor.White) { PredecessorRecorderVisitor vis = new PredecessorRecorderVisitor( Predecessors ); VertexBuffer Q = new VertexBuffer(); BreadthFirstSearchAlgorithm bfs = new BreadthFirstSearchAlgorithm( ResidualGraph, Q, Colors ); bfs.RegisterPredecessorRecorderHandlers(vis); bfs.Compute(src); if (Colors[sink] != GraphColor.White) Augment(src, sink); } // while double flow=0; foreach(IEdge e in VisitedGraph.OutEdges(src)) flow += (Capacities[e] - ResidualCapacities[e]); return flow; }
/// <summary> /// BreadthFirstSearch searcher contructor /// </summary> /// <param name="g">Graph to visit</param> /// <param name="Q">Vertex buffer</param> /// <param name="colors">Vertex color map</param> public BreadthFirstSearchAlgorithm( IVertexListGraph g, VertexBuffer Q, VertexColorDictionary colors ) { if (g == null) throw new ArgumentNullException("g"); if (Q == null) throw new ArgumentNullException("Stack Q is null"); if (colors == null) throw new ArgumentNullException("Colors"); m_VisitedGraph = g; m_Colors = colors; m_Q = Q; }