Ejemplo n.º 1
0
        public override void AddEdge(Node from, Node to, int cost)
        {
            if (!HasNode(from) || !HasNode(to))
            {
                throw new NodeNotFoundException();
            }

            from.AddNeighbor(to, cost);
        }
Ejemplo n.º 2
0
        public bool AddEdge(T value1, T value2, int weight)
        {
            Node <T> node1 = Find(value1);
            Node <T> node2 = Find(value2);

            if (node1 == null || node2 == null)
            {
                return(false);
            }
            else if (node1.Neighbors.Contains(node2))
            {
                return(false);
            }
            else
            {
                node1.AddNeighbor(node2, weight);
                node2.AddNeighbor(node1, weight);
                return(true);
            }
        }
Ejemplo n.º 3
0
        public void AddEdge(Node <T> from, Node <T> to, int?weight = null)
        {
            if (!Nodes.Contains(from))
            {
                throw new ArgumentException("<from> node doesn't exist in the graph!");
            }

            if (!Nodes.Contains(to))
            {
                throw new ArgumentException("<to> node doesn't exist in the graph!");
            }

            if (_isWeighted && weight == null)
            {
                throw new ArgumentNullException("Weight cannot be null for weighted graph!");
            }

            if (!_isWeighted && weight != null)
            {
                throw new ArgumentNullException("Weight cannot be set for non-weighted graph!");
            }

            if (ContainsEdge(from, to))
            {
                throw new ArgumentNullException("Edge between <from> and <to> already exists!");
            }

            from.AddNeighbor(to, weight);

            if (!_isDirected)
            {
                if (ContainsEdge(to, from))
                {
                    throw new ArgumentNullException("Edge between <from> and <to> already exists!");
                }

                to.AddNeighbor(from, weight);
            }
        }