Пример #1
0
        protected void Initialize()
        {
            this.costs                    = new VertexDoubleDictionary();
            this.priorityQueue            = new PriorithizedVertexBuffer(costs);
            this.unvisitedSuccessorCounts = new VertexIntDictionary();
            this.states.Clear();

            foreach (IVertex v in this.goals)
            {
                this.costs.Add(v, 0);
                this.priorityQueue.Push(v);
            }

            foreach (IVertex v in this.NotGoals)
            {
                this.costs.Add(v, double.PositiveInfinity);
            }

            foreach (IVertex v in this.TestGraph.ChoicePoints)
            {
                this.unvisitedSuccessorCounts.Add(v, this.testGraph.Graph.OutDegree(v));
            }

            foreach (IVertex v in this.TestGraph.Graph.Vertices)
            {
                this.states.Add(v, null);
            }
        }
Пример #2
0
        private static void swapRanks(VertexDoubleDictionary left, VertexDoubleDictionary right)
        {
            VertexDoubleDictionary temp = left;

            left  = right;
            right = temp;
        }
 /// <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();
 }
Пример #4
0
		private bool IsFlow(MaximumFlowAlgorithm maxFlow)
		{
			// check edge flow values
			foreach (IVertex u in maxFlow.VisitedGraph.Vertices) 
			{
				foreach (IEdge a in maxFlow.VisitedGraph.OutEdges(u))
				{
					if (maxFlow.Capacities[a] > 0)
						if ((maxFlow.ResidualCapacities[a] + maxFlow.ResidualCapacities[maxFlow.ReversedEdges[a]] 
							!= maxFlow.Capacities[a])
							|| (maxFlow.ResidualCapacities[a] < 0)
							|| (maxFlow.ResidualCapacities[maxFlow.ReversedEdges[a]] < 0))
							return false;
				}
			}

			// check conservation
			VertexDoubleDictionary inFlows = new VertexDoubleDictionary();
			VertexDoubleDictionary outFlows = new VertexDoubleDictionary();
			foreach (IVertex u in maxFlow.VisitedGraph.Vertices)
			{
				inFlows[u] = 0;
				outFlows[u] = 0;
			}

			foreach (IVertex u in maxFlow.VisitedGraph.Vertices)
			{
				foreach (IEdge e in maxFlow.VisitedGraph.OutEdges(u))
				{
					if (maxFlow.Capacities[e] > 0) 
					{
						double flow = maxFlow.Capacities[e] - maxFlow.ResidualCapacities[e];

						inFlows[e.Target] += flow;
						outFlows[e.Source] += flow;
					}
				}
			}

			foreach (IVertex u in maxFlow.VisitedGraph.Vertices)
			{
				if (u != source && u != sink)
					if (inFlows[u] != outFlows[u])
						return false;
			}

			return true;
		}
        /// <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();
        }
Пример #6
0
        /// <summary>
        /// Computes the PageRank over the <see cref="VisitedGraph"/>.
        /// </summary>
        public void Compute()
        {
            VertexDoubleDictionary tempRanks = new VertexDoubleDictionary();
            // create filtered graph
            FilteredBidirectionalGraph fg = new FilteredBidirectionalGraph(
                this.VisitedGraph,
                Preds.KeepAllEdges(),
                new InDictionaryVertexPredicate(this.ranks)
                );

            int    iter  = 0;
            double error = 0;

            do
            {
                // compute page ranks
                error = 0;
                foreach (DictionaryEntry de in this.Ranks)
                {
                    IVertex v    = (IVertex)de.Key;
                    double  rank = (double)de.Value;
                    // compute ARi
                    double r = 0;
                    foreach (IEdge e in fg.InEdges(v))
                    {
                        r += this.ranks[e.Source] / fg.OutDegree(e.Source);
                    }

                    // add sourceRank and store
                    double newRank = (1 - this.damping) + this.damping * r;
                    tempRanks[v] = newRank;
                    // compute deviation
                    error += Math.Abs(rank - newRank);
                }

                // swap ranks
                VertexDoubleDictionary temp = ranks;
                ranks     = tempRanks;
                tempRanks = temp;

                iter++;
            }while(error > this.tolerance && iter < this.maxIterations);
            Console.WriteLine("{0}, {1}", iter, error);
        }
        /// <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;
        }
Пример #8
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;
        }