private bool TryGetEdge(TNode source, TNode destination, out AdjacentNodeEdge <TNode> nodeedge)
        {
            nodeedge = null;
            if (DoesNodeExist(source))
            {
                nodeedge = GetGraphNode(source).Neighbours.SingleOrDefault(x => x.Node.Equals(destination));
            }

            return(nodeedge != null);
        }
        public void AddEdge(AbstractAdjacentGraphNode <TNode> graphNode, AdjacentNodeEdge <TNode> adjacentNodeEdge)
        {
            //Validate Arguments
            if (graphNode == null)
            {
                throw new ArgumentNullException("graphNode");
            }
            if (adjacentNodeEdge == null)
            {
                throw new ArgumentNullException("adjacentNodeEdge");
            }
            if (graphNode.Node.Equals(adjacentNodeEdge.Node))
            {
                throw new ArgumentException(
                          string.Format("The source {0} node and the destinate node cannot be the same", graphNode.Node));
            }

            if (adjacentNodeEdge.Weight <= 0)
            {
                throw new InvalidEdgeWeightException(
                          string.Format("The edge weight {0} is invalid. It must be greater than zero",
                                        adjacentNodeEdge.Weight));
            }

            AdjacentNodeEdge <TNode> existingNodeEdge;

            if (TryGetEdge(graphNode.Node, adjacentNodeEdge.Node, out existingNodeEdge))
            {
                throw new DuplicateEdgeException(
                          string.Format(
                              "An edge with weight {0} from source node {1} to destination node {2} already exists! ",
                              existingNodeEdge.Weight, graphNode.Node, adjacentNodeEdge.Node));
            }

            //Create Nodes and Edge


            AddNodeIfDoesntExist(graphNode.Node).AddEdgeNeighbour(adjacentNodeEdge);
            AddNodeIfDoesntExist(adjacentNodeEdge.Node);
        }
Exemple #3
0
 public override void AddEdgeNeighbour(AdjacentNodeEdge <TNode> adjacentNodeEdge)
 {
     AdjacentNodeEdges.Add(adjacentNodeEdge);
 }