public static IWeightedEndpointPair <TVertex, TEdge> ToEndpointPair <TVertex, TEdge>(
     this IWeightedEndpoint <TVertex, TEdge> endpoint,
     TVertex origin
     )
 {
     return(new WeightedEndpointPair <TVertex, TEdge>(origin, endpoint.Vertex, endpoint.Edge));
 }
Beispiel #2
0
        private IEnumerable <IWeightedEndpoint <TVertex, TEdge> > CreatePath(
            IWeightedEndpoint <TVertex, TEdge> origin,
            TVertex destination
            )
        {
            if (origin == null)
            {
                throw new ArgumentNullException(nameof(origin));
            }
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            return(CreatePath(origin, destination, new HashSet <TVertex>()));
        }
Beispiel #3
0
        private IEnumerable <IWeightedEndpoint <TVertex, TEdge> > CreatePath(
            IWeightedEndpoint <TVertex, TEdge> origin,
            TVertex destination,
            ICollection <TVertex> visited
            )
        {
            if (origin == null)
            {
                throw new ArgumentNullException(nameof(origin));
            }
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }
            if (visited == null)
            {
                throw new ArgumentNullException(nameof(visited));
            }

            var random = new Random();

            visited.Add(origin.Vertex);

            var stack = new Stack <Tuple <IWeightedEndpoint <TVertex, TEdge>, IList <IWeightedEndpoint <TVertex, TEdge> > > >();

            stack.Push(new Tuple <IWeightedEndpoint <TVertex, TEdge>, IList <IWeightedEndpoint <TVertex, TEdge> > >(
                           origin,
                           Graph.SuccessorEndpoints(origin.Vertex).ToList()
                           ));

            while (stack.Count > 0)
            {
                var(endpoint, successorEndpoints) = stack.Peek();

                if (Equals(endpoint.Vertex, destination))
                {
                    return(stack.Select(tuple => tuple.Item1).Reverse());
                }

                if (successorEndpoints.Count == 0)
                {
                    stack.Pop();
                }
                else
                {
                    var index = random.Next(successorEndpoints.Count);

                    var successorEndpoint = successorEndpoints[index];

                    visited.Add(successorEndpoint.Vertex);

                    bool Predicate(IWeightedEndpoint <TVertex, TEdge> grandchildEndpoint)
                    {
                        return(!visited.Contains(grandchildEndpoint.Vertex));
                    }

                    stack.Push(new Tuple <IWeightedEndpoint <TVertex, TEdge>, IList <IWeightedEndpoint <TVertex, TEdge> > >(
                                   successorEndpoint,
                                   Graph.SuccessorEndpoints(successorEndpoint.Vertex).Where(Predicate).ToList()
                                   ));

                    successorEndpoints.RemoveAt(index);
                }
            }

            return(null);
        }