コード例 #1
0
        /// <inheritdoc />
        public override void AddEdge(T from, T to, TWeight weight)
        {
            if (!ContainsKey(from))
            {
                throw new KeyNotFoundException(nameof(from) + " value is not found.");
            }
            if (!ContainsKey(to))
            {
                throw new KeyNotFoundException(nameof(to) + " value is not found.");
            }

            KeyedCollection <T, GraphEdge <T, TWeight> > fromEdges = this[from];

            if (fromEdges == null)
            {
                fromEdges  = MakeContainer();
                this[from] = fromEdges;
            }

            if (fromEdges.TryGetValue(to, out GraphEdge <T, TWeight> fromEdge))
            {
                if (fromEdge.Weight.CompareTo(weight) > 0)
                {
                    fromEdge.Weight = weight;
                }
            }
            else
            {
                fromEdge = new GraphEdge <T, TWeight>(to, weight);
                fromEdges.Add(fromEdge);
            }
        }
コード例 #2
0
        public void AddEdge([NotNull] T from, [NotNull] T to, TWeight weight, bool undirected)
        {
            if (!ContainsKey(from))
            {
                throw new KeyNotFoundException(nameof(from) + " value is not found.");
            }
            if (!ContainsKey(to))
            {
                throw new KeyNotFoundException(nameof(to) + " value is not found.");
            }

            KeyedCollection <T, GraphEdge <T, TWeight> > fromEdges = this[from];

            if (fromEdges == null)
            {
                fromEdges  = MakeContainer();
                this[from] = fromEdges;
            }

            if (fromEdges.TryGetValue(to, out GraphEdge <T, TWeight> fromEdge))
            {
                if (fromEdge.Weight.CompareTo(weight) > 0)
                {
                    fromEdge.Weight = weight;
                }
            }
            else
            {
                fromEdge = new GraphEdge <T, TWeight>(to, weight);
                fromEdges.Add(fromEdge);
            }

            // short-circuit - loop edge
            if (!undirected || Comparer.Equals(from, to))
            {
                return;
            }

            KeyedCollection <T, GraphEdge <T, TWeight> > toEdges = this[to];

            if (toEdges == null)
            {
                toEdges  = MakeContainer();
                this[to] = toEdges;
            }

            if (toEdges.TryGetValue(from, out GraphEdge <T, TWeight> toEdge))
            {
                if (toEdge.Weight.CompareTo(weight) > 0)
                {
                    toEdge.Weight = weight;
                }
            }
            else
            {
                toEdge = new GraphEdge <T, TWeight>(from, weight);
                toEdges.Add(toEdge);
            }
        }