Ejemplo n.º 1
0
		public void AttachDistanceRecorderVisitor()
		{
			AdjacencyGraph g = new AdjacencyGraph(true); 
			EdgeDoubleDictionary weights = DijkstraShortestPathAlgorithm.UnaryWeightsFromEdgeList(g);
			DijkstraShortestPathAlgorithm dij = new DijkstraShortestPathAlgorithm(g,weights);
			DistanceRecorderVisitor vis = new DistanceRecorderVisitor();
			dij.RegisterDistanceRecorderHandlers(vis);
		}
        private bool IsOptimal(MaximumFlowAlgorithm maxFlow)
        {
            // check if mincut is saturated...
            FilteredVertexListGraph residualGraph = new FilteredVertexListGraph(
                maxFlow.VisitedGraph,
                new ReversedResidualEdgePredicate(maxFlow.ResidualCapacities, maxFlow.ReversedEdges)
                );
            BreadthFirstSearchAlgorithm bfs = new BreadthFirstSearchAlgorithm(residualGraph);

            VertexIntDictionary distances = new VertexIntDictionary();
            DistanceRecorderVisitor vis = new DistanceRecorderVisitor(distances);
            bfs.RegisterDistanceRecorderHandlers(vis);
            bfs.Compute(sink);

            return distances[source] >= maxFlow.VisitedGraph.VerticesCount;
        }
        //=======================================================================
        // This is a breadth-first search over the residual graph
        // (well, actually the reverse of the residual graph).
        // Would be cool to have a graph view adaptor for hiding certain
        // edges, like the saturated (non-residual) edges in this case.
        // Goldberg's implementation abused "distance" for the coloring.
        private void GlobalDistanceUpdate()
        {
            foreach (IVertex u in VisitedGraph.Vertices)
            {
                Colors[u] = GraphColor.White;
                Distances[u] = n;
            }
            Distances[sink] = 0;

            for (int l = 0; l <= maxDistance; ++l)
            {
                layers[l].ActiveVertices.Clear();
                layers[l].InactiveVertices.Clear();
            }

            maxDistance = maxActive = 0;
            minActive = n;

            BreadthFirstSearchAlgorithm bfs = new BreadthFirstSearchAlgorithm(
                ResidualGraph,
                new VertexBuffer(),
                Colors
                );

            DistanceRecorderVisitor vis = new DistanceRecorderVisitor(Distances);
            bfs.TreeEdge += new EdgeEventHandler(vis.TreeEdge);
            bfs.DiscoverVertex += new VertexEventHandler(GlobalDistanceUpdateHelper);
            bfs.Compute(sink);
        }
 private void GlobalDistanceUpdate()
 {
     IVertexEnumerator enumerator = this.VisitedGraph.get_Vertices().GetEnumerator();
     while (enumerator.MoveNext())
     {
         IVertex vertex = enumerator.get_Current();
         base.Colors.set_Item(vertex, 0);
         this.Distances.set_Item(vertex, this.n);
     }
     this.Distances.set_Item(this.sink, 0);
     for (int i = 0; i <= this.maxDistance; i++)
     {
         this.layers[i].ActiveVertices.Clear();
         this.layers[i].InactiveVertices.Clear();
     }
     this.maxDistance = this.maxActive = 0;
     this.minActive = this.n;
     BreadthFirstSearchAlgorithm algorithm = new BreadthFirstSearchAlgorithm(this.ResidualGraph, new VertexBuffer(), base.Colors);
     DistanceRecorderVisitor visitor = new DistanceRecorderVisitor(this.Distances);
     algorithm.TreeEdge += new EdgeEventHandler(visitor, (IntPtr) this.TreeEdge);
     algorithm.DiscoverVertex += new VertexEventHandler(this, (IntPtr) this.GlobalDistanceUpdateHelper);
     algorithm.Compute(this.sink);
 }