コード例 #1
0
ファイル: Dijkstra.cs プロジェクト: Neo4Net/Neo4Net
        protected internal virtual bool LimitReached(CostType cost1, CostType cost2)
        {
            if (MaxCost != default(CostType))
            {
                CostType totalCost = CostAccumulator.addCosts(cost1, cost2);
                if (CostComparator.Compare(totalCost, MaxCost) > 0)
                {
                    FoundPathsMiddleNodes = null;
                    FoundPathsCost        = default(CostType);
                    return(true);
                }
            }

            return(false);
        }
コード例 #2
0
        /// <summary>
        /// Internal calculate method that will do the calculation. This can however
        /// be called externally to manually trigger the calculation.
        /// </summary>
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") public void calculate()
        public virtual void Calculate()
        {
            // Don't do it more than once
            if (DoneCalculation)
            {
                return;
            }
            DoneCalculation = true;
            // Build initial matrix
            int n = NodeSet.Count;

//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: CostMatrix = (CostType[][]) new object[n][n];
            CostMatrix = ( CostType[][] )RectangularArrays.RectangularObjectArray(n, n);
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: Predecessors = new System.Nullable<int>[n][n];
            Predecessors = RectangularArrays.RectangularSystemNullableArray <int>(n, n);
            IndexedNodes = new Node[n];
            NodeIndexes  = new Dictionary <Node, int>();
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    CostMatrix[i][j] = InfinitelyBad;
                }
                CostMatrix[i][i] = StartCost;
            }
            int nodeIndex = 0;

            foreach (Node node in NodeSet)
            {
                NodeIndexes[node]       = nodeIndex;
                IndexedNodes[nodeIndex] = node;
                ++nodeIndex;
            }
            // Put the relationships in there
            foreach (Relationship relationship in RelationshipSet)
            {
                int?i1 = NodeIndexes[relationship.StartNode];
                int?i2 = NodeIndexes[relationship.EndNode];
                if (i1 == null || i2 == null)
                {
                    // TODO: what to do here? pretend nothing happened? cast
                    // exception?
                    continue;
                }
                if (RelationDirection.Equals(Direction.BOTH) || RelationDirection.Equals(Direction.OUTGOING))
                {
                    CostMatrix[i1][i2]   = CostEvaluator.getCost(relationship, Direction.OUTGOING);
                    Predecessors[i1][i2] = i1;
                }
                if (RelationDirection.Equals(Direction.BOTH) || RelationDirection.Equals(Direction.INCOMING))
                {
                    CostMatrix[i2][i1]   = CostEvaluator.getCost(relationship, Direction.INCOMING);
                    Predecessors[i2][i1] = i2;
                }
            }
            // Do it!
            for (int v = 0; v < n; ++v)
            {
                for (int i = 0; i < n; ++i)
                {
                    for (int j = 0; j < n; ++j)
                    {
                        CostType alternative = CostAccumulator.addCosts(CostMatrix[i][v], CostMatrix[v][j]);
                        if (CostComparator.Compare(CostMatrix[i][j], alternative) > 0)
                        {
                            CostMatrix[i][j]   = alternative;
                            Predecessors[i][j] = Predecessors[v][j];
                        }
                    }
                }
            }
            // TODO: detect negative cycles?
        }