/// <summary>
        /// Connects two vertices together with a weight, in the direction: first->second.
        /// </summary>
        public bool AddEdge(T source, T destination, int weight)
        {
            // Check existence of nodes, the validity of the weight value, and the non-existence of edge
            if (weight == EMPTY_EDGE_VALUE)
            {
                return(false);
            }
            else if (!HasVertex(source) || !HasVertex(destination))
            {
                return(false);
            }
            else if (_doesEdgeExist(source, destination))
            {
                return(false);
            }

            // Add edge from source to destination
            var edge = new WeightedEdge <T>(source, destination, weight);

            _adjacencyList[source].Append(edge);

            // Increment edges count
            ++_edgesCount;

            return(true);
        }
        /// <summary>
        /// Get all outgoing weighted edges from vertex
        /// </summary>
        public virtual IEnumerable <WeightedEdge <T> > OutgoingEdges(T vertex)
        {
            if (!HasVertex(vertex))
            {
                return(null);
            }

            int source = _vertices.IndexOf(vertex);
            var list   = new DLinkedList <WeightedEdge <T> >();

            // Check existence of vertex
            if (source != -1)
            {
                for (int adjacent = 0; adjacent < _vertices.Count; ++adjacent)
                {
                    if (_vertices[adjacent] != null && _doesEdgeExist(source, adjacent))
                    {
                        var edge = new WeightedEdge <T>(
                            (T)_vertices[source],
                            (T)_vertices[adjacent],
                            _getEdgeWeight(source, adjacent));

                        list.Append(edge);
                    }
                }
            }

            return(list);
        }
        public int CompareTo(WeightedEdge <T> other)
        {
            bool areNodesEqual = Source.IsEqualTo <T>(other.Source) && Destination.IsEqualTo <T>(other.Destination);

            if (!areNodesEqual)
            {
                return(-1);
            }
            else
            {
                return(Weight.CompareTo(other.Weight));
            }
        }
        /// <summary>
        /// Helper function. Returns edge object from source to destination, if exists; otherwise, null.
        /// </summary>
        protected virtual WeightedEdge <T> _tryGetEdge(T source, T destination)
        {
            WeightedEdge <T> edge = null;

            try
            {
                edge = _adjacencyList[source].FindFirst(item => item.Source.IsEqualTo <T>(source) && item.Destination.IsEqualTo <T>(destination));
            }
            catch (Exception)
            {
                return(null);
            }

            // Could return a null object.
            return(edge);
        }
Exemple #5
0
        /// <summary>
        /// Helper function. Returns edge object from source to destination, if exists; otherwise, null.
        /// </summary>
        protected virtual WeightedEdge <T> _tryGetEdge(T source, T destination)
        {
            WeightedEdge <T> edge = null;

            // Predicate
            var sourceToDestinationPredicate = new Predicate <WeightedEdge <T> >((item) => item.Source.IsEqualTo <T>(source) && item.Destination.IsEqualTo <T>(destination));

            // Try to find a match
            if (_adjacencyList.ContainsKey(source))
            {
                _adjacencyList[source].TryFindFirst(sourceToDestinationPredicate, out edge);
            }

            // Return!
            // Might return a null object.
            return(edge);
        }
Exemple #6
0
        /// <summary>
        /// Get all incoming directed weighted edges to a vertex.
        /// </summary>
        public virtual IEnumerable <WeightedEdge <T> > IncomingEdges(T vertex)
        {
            if (!HasVertex(vertex))
            {
                throw new KeyNotFoundException("Vertex doesn't belong to graph.");
            }

            var predicate = new Predicate <WeightedEdge <T> >((edge) => edge.Destination.IsEqualTo(vertex));

            foreach (var adjacent in _adjacencyList.Keys)
            {
                WeightedEdge <T> incomingEdge = null;

                if (_adjacencyList[adjacent].TryFindFirst(predicate, out incomingEdge))
                {
                    yield return(incomingEdge);
                }
            }//end-foreach
        }
Exemple #7
0
        /// <summary>
        /// Helper function. Returns edge object from source to destination, if exists; otherwise, null.
        /// </summary>
        protected virtual WeightedEdge <T> _tryGetEdge(T source, T destination)
        {
            var success           = false;
            WeightedEdge <T> edge = null;

            var sourceToDestinationPredicate = new Predicate <WeightedEdge <T> >((item) => item.Source.IsEqualTo <T>(source) && item.Destination.IsEqualTo <T>(destination));
            var destinationToSourcePredicate = new Predicate <WeightedEdge <T> >((item) => item.Source.IsEqualTo <T>(destination) && item.Destination.IsEqualTo <T>(source));

            if (_adjacencyList.ContainsKey(source))
            {
                success = _adjacencyList[source].TryFindFirst(sourceToDestinationPredicate, out edge);
            }

            if (!success && _adjacencyList.ContainsKey(destination))
            {
                _adjacencyList[destination].TryFindFirst(destinationToSourcePredicate, out edge);
            }

            // Could return a null object.
            return(edge);
        }