コード例 #1
0
        /// <summary>Constructs a maximum flow algorithm.</summary>
        /// <param name="g">Graph to compute maximum flow on.</param>
        /// <param name="capacities">edge capacities</param>
        /// <param name="reversedEdges">reversed edge map</param>
        /// <exception cref="ArgumentNullException"><paramref name="g"/> or
        /// <paramref name="capacities"/> or <paramref name="reversedEdges"/> is a null
        /// reference.
        /// </exception>
        public MaximumFlowAlgorithm(
            IVertexListGraph g,
            EdgeDoubleDictionary capacities,
            EdgeEdgeDictionary reversedEdges
            )
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (capacities == null)
            {
                throw new ArgumentNullException("capacities");
            }
            if (reversedEdges == null)
            {
                throw new ArgumentNullException("reversedEdges");
            }

            this.visitedGraph  = g;
            this.capacities    = capacities;
            this.reversedEdges = reversedEdges;

            this.predecessors       = new VertexEdgeDictionary();
            this.residualCapacities = new EdgeDoubleDictionary();
            this.colors             = new VertexColorDictionary();
        }
コード例 #2
0
        public void CheckPredecessorLineGraph()
        {
            AdjacencyGraph g  = new AdjacencyGraph(true);
            IVertex        v1 = g.AddVertex();
            IVertex        v2 = g.AddVertex();
            IVertex        v3 = g.AddVertex();

            IEdge e12 = g.AddEdge(v1, v2);
            IEdge e23 = g.AddEdge(v2, v3);

            EdgeDoubleDictionary          weights = DijkstraShortestPathAlgorithm.UnaryWeightsFromEdgeList(g);
            DijkstraShortestPathAlgorithm dij     = new DijkstraShortestPathAlgorithm(g, weights);
            PredecessorRecorderVisitor    vis     = new PredecessorRecorderVisitor();

            dij.RegisterPredecessorRecorderHandlers(vis);

            dij.Compute(v1);

            EdgeCollection col = vis.Path(v2);

            Assert.AreEqual(1, col.Count);
            Assert.AreEqual(e12, col[0]);

            col = vis.Path(v3);
            Assert.AreEqual(2, col.Count);
            Assert.AreEqual(e12, col[0]);
            Assert.AreEqual(e23, col[1]);
        }
コード例 #3
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="residualCapacities">Residual Edge capacities map</param>
 /// <exception cref="ArgumentNullException">residualCapacities is null</exception>
 public ResidualEdgePredicate(EdgeDoubleDictionary residualCapacities)
 {
     if (residualCapacities == null)
     {
         throw new ArgumentNullException("residualCapacities");
     }
     m_ResidualCapacities = residualCapacities;
 }
コード例 #4
0
 /// <summary>
 /// Construct a markov <see cref="IEdge"/> chain based on the
 /// <see cref="EdgeDoubleDictionary"/> edge weight dictionary.
 /// </summary>
 /// <param name="weights">edge weight dictionary</param>
 /// <exception cref="ArgumentNullException">weights is a null reference
 /// </exception>
 public WeightedMarkovEdgeChain(EdgeDoubleDictionary weights)
 {
     if (weights == null)
     {
         throw new ArgumentNullException("weights");
     }
     this.weights = weights;
 }
コード例 #5
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="g"></param>
 /// <param name="capacities"></param>
 /// <param name="reversedEdges"></param>
 public EdmondsKarpMaximumFlowAlgorithm(
     IVertexListGraph g,
     EdgeDoubleDictionary capacities,
     EdgeEdgeDictionary reversedEdges
     )
     : base(g, capacities, reversedEdges)
 {
 }
コード例 #6
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);
        }
コード例 #7
0
 /// <summary>
 /// Constructs a edge weight scaler
 /// </summary>
 /// <param name="weights">edge weight dictionary</param>
 /// <param name="factor">weight scale factor</param>
 public EdgeWeightScalerVisitor(EdgeDoubleDictionary weights, double factor)
 {
     if (weights == null)
     {
         throw new ArgumentNullException("weights");
     }
     this.weights = weights;
     this.factor  = factor;
 }
コード例 #8
0
		public void Init()
		{
			this.capacities = new EdgeDoubleDictionary();
			this.reversedEdges = new EdgeEdgeDictionary();
			this.graph = new AdjacencyGraph();

			this.source = graph.AddVertex();
			this.sink = graph.AddVertex();

			BuildSimpleGraph(source, sink);
		}
コード例 #9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="g"></param>
 /// <param name="capacities"></param>
 /// <param name="reversedEdges"></param>
 public PushRelabelMaximumFlowAlgorithm(
     IIndexedVertexListGraph g,
     EdgeDoubleDictionary capacities,
     EdgeEdgeDictionary reversedEdges
     )
     : base(g, capacities, reversedEdges)
 {
     this.visitedGraph = g;
     this.excessFlow   = new VertexDoubleDictionary();
     this.current      = new VertexIntDictionary();
     this.distances    = new VertexIntDictionary();
 }
コード例 #10
0
        public EdmundsKarpMaximumFlow(
            Graph g,
            EdgeDoubleDictionary edgeCapacities
            ) : base(g)
        {
            if (edgeCapacities == null)
            {
                throw new ArgumentNullException("Edge capacities");
            }

            m_EdgeCapacities         = edgeCapacities;
            m_ResidualEdgeCapacities = null;
            m_Predecessors           = new VertexEdgeDictionary();
        }
コード例 #11
0
        /// <summary>
        /// Create a edge unary weight dictionary.
        /// </summary>
        /// <param name="graph">graph to map</param>
        /// <returns>Dictionary where each edge wheight is 1</returns>
        public static EdgeDoubleDictionary UnaryWeightsFromEdgeList(IEdgeListGraph graph)
        {
            if (graph == null)
            {
                throw new ArgumentNullException("graph");
            }
            EdgeDoubleDictionary weights = new EdgeDoubleDictionary();

            foreach (IEdge e in graph.Edges)
            {
                weights[e] = 1;
            }
            return(weights);
        }
コード例 #12
0
        /// <summary>
        /// Builds a new Bellman Ford searcher.
        /// </summary>
        /// <param name="g">The graph</param>
        /// <param name="weights">Edge weights</param>
        /// <exception cref="ArgumentNullException">Any argument is null</exception>
        /// <remarks>This algorithm uses the <seealso cref="BreadthFirstSearchAlgorithm"/>.</remarks>
        public BellmanFordShortestPathAlgorithm(
            IVertexAndEdgeListGraph g,
            EdgeDoubleDictionary weights
            )
        {
            if (weights == null)
            {
                throw new ArgumentNullException("Weights");
            }

            m_VisitedGraph = g;
            m_Colors       = new VertexColorDictionary();
            m_Weights      = weights;
            m_Distances    = new VertexDoubleDictionary();
            m_Predecessors = new VertexVertexDictionary();
        }
コード例 #13
0
        public MinimumFlowAlgorithm(
            BidirectionalGraph visitedGraph,
            EdgeDoubleDictionary capacities)
        {
            if (visitedGraph == null)
            {
                throw new ArgumentNullException("visitedGraph");
            }
            if (capacities == null)
            {
                throw new ArgumentNullException("capacities");
            }
            this.visitedGraph = visitedGraph;
            this.capacities   = capacities;

            this.Initialize();
        }
コード例 #14
0
ファイル: MainForm.cs プロジェクト: tmauldin/mb-unit
        private void menuItem8_Click(object sender, System.EventArgs e)
        {
            if (this.netronPanel.Graph == null)
            {
                throw new Exception("Generate a graph first");
            }
            if (this.netronPanel.Populator == null)
            {
                throw new Exception("Populator should not be null.");
            }

            ResetVertexAndEdgeColors();

            // create algorithm
            this.vertexCounts = new VertexIntDictionary();
            this.edgeCounts   = new EdgeIntDictionary();
            foreach (IVertex vertex in this.netronPanel.Graph.Vertices)
            {
                this.vertexCounts[vertex] = 0;
            }
            foreach (IEdge edge in this.netronPanel.Graph.Edges)
            {
                this.edgeCounts[edge] = 0;
            }

            this.edgeWeights = new EdgeDoubleDictionary();
            foreach (IEdge edge in this.netronPanel.Graph.Edges)
            {
                edgeWeights[edge] = 1;
            }
            WeightedMarkovEdgeChain chain  = new WeightedMarkovEdgeChain(edgeWeights);
            RandomWalkAlgorithm     walker = new RandomWalkAlgorithm(
                this.netronPanel.Graph
                );

            walker.TreeEdge += new EdgeEventHandler(walker_WeightedTreeEdge);

            LayoutAlgorithmTraverVisitor tracer = new LayoutAlgorithmTraverVisitor(this.netronPanel.Populator);

            walker.TreeEdge += new EdgeEventHandler(tracer.TreeEdge);


            Thread thread = new Thread(new ThreadStart(walker.Generate));

            thread.Start();
        }
コード例 #15
0
        public KruskalMinimumSpanningTreeAlgorithm(
            IVertexAndEdgeListGraph visitedGraph,
            EdgeDoubleDictionary weights
            )
        {
            if (visitedGraph == null)
            {
                throw new ArgumentNullException("visitedGraph");
            }
            if (weights == null)
            {
                throw new ArgumentNullException("weights");
            }

            this.visitedGraph      = visitedGraph;
            this.weights           = weights;
            this.spanningTreeEdges = new EdgeCollection();
        }
コード例 #16
0
        public MinimumFlowAlgorithm(BidirectionalGraph visitedGraph)
        {
            if (visitedGraph == null)
            {
                throw new ArgumentNullException("visitedGraph");
            }
            if (capacities == null)
            {
                throw new ArgumentNullException("capacities");
            }
            this.visitedGraph = visitedGraph;
            this.capacities   = new EdgeDoubleDictionary();
            foreach (IEdge edge in this.visitedGraph.Edges)
            {
                this.capacities.Add(edge, double.MaxValue);
            }

            this.Initialize();
        }
コード例 #17
0
        public void RunOnLineGraph()
        {
            AdjacencyGraph g  = new AdjacencyGraph(true);
            IVertex        v1 = g.AddVertex();
            IVertex        v2 = g.AddVertex();
            IVertex        v3 = g.AddVertex();

            IEdge e12 = g.AddEdge(v1, v2);
            IEdge e23 = g.AddEdge(v2, v3);

            EdgeDoubleDictionary          weights = DijkstraShortestPathAlgorithm.UnaryWeightsFromEdgeList(g);
            DijkstraShortestPathAlgorithm dij     = new DijkstraShortestPathAlgorithm(g, weights);

            dij.Compute(v1);

            Assert.AreEqual(0, dij.Distances[v1]);
            Assert.AreEqual(1, dij.Distances[v2]);
            Assert.AreEqual(2, dij.Distances[v3]);
        }
コード例 #18
0
        public GraphBalancerAlgorithm(
            IMutableBidirectionalVertexAndEdgeListGraph visitedGraph,
            IVertex source,
            IVertex sink,
            EdgeDoubleDictionary capacities)
        {
            if (visitedGraph == null)
            {
                throw new ArgumentNullException("visitedGraph");
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (!visitedGraph.ContainsVertex(source))
            {
                throw new ArgumentException("source is not part of the graph");
            }
            if (sink == null)
            {
                throw new ArgumentNullException("sink");
            }
            if (!visitedGraph.ContainsVertex(sink))
            {
                throw new ArgumentException("sink is not part of the graph");
            }
            if (capacities == null)
            {
                throw new ArgumentNullException("capacities");
            }

            this.visitedGraph = visitedGraph;
            this.source       = source;
            this.sink         = sink;
            this.capacities   = capacities;

            // setting preflow = l(e) = 1
            foreach (IEdge edge in this.VisitedGraph.Edges)
            {
                this.preFlow.Add(edge, 1);
            }
        }
コード例 #19
0
        /// <summary>
        /// Builds a new Dijsktra searcher.
        /// </summary>
        /// <param name="g">The graph</param>
        /// <param name="weights">Edge weights</param>
        /// <exception cref="ArgumentNullException">Any argument is null</exception>
        /// <remarks>This algorithm uses the <seealso cref="BreadthFirstSearchAlgorithm"/>.</remarks>
        public DijkstraShortestPathAlgorithm(
            IVertexListGraph g,
            EdgeDoubleDictionary weights
            )
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (weights == null)
            {
                throw new ArgumentNullException("Weights");
            }

            this.visitedGraph = g;
            this.colors       = new VertexColorDictionary();
            this.distances    = new VertexDoubleDictionary();
            this.weights      = weights;
            this.vertexQueue  = null;
        }
コード例 #20
0
        /// <summary>
        /// Builds a new Dijsktra searcher.
        /// </summary>
        /// <param name="g">The graph</param>
        /// <param name="weights">Edge weights</param>
        /// <exception cref="ArgumentNullException">Any argument is null</exception>
        /// <remarks>This algorithm uses the <seealso cref="BreadthFirstSearchAlgorithm"/>.</remarks>
        public DijkstraShortestPathAlgorithm(
            IVertexListGraph g,
            EdgeDoubleDictionary weights
            )
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (weights == null)
            {
                throw new ArgumentNullException("Weights");
            }

            m_VisitedGraph = g;
            m_Colors       = new VertexColorDictionary();
            m_Distances    = new VertexDoubleDictionary();
            m_Predecessors = new VertexVertexDictionary();
            m_Weights      = weights;
            m_VertexQueue  = null;
        }
コード例 #21
0
        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);
        }
コード例 #22
0
 public void UnaryWeights()
 {
     AdjacencyGraph       g       = new AdjacencyGraph(true);
     EdgeDoubleDictionary weights = DijkstraShortestPathAlgorithm.UnaryWeightsFromEdgeList(g);
 }
コード例 #23
0
 public VanishingWeightedMarkovEdgeChain(EdgeDoubleDictionary weights, double factor)
     : base(weights)
 {
     this.factor = factor;
 }