public UnweightedDirectedPath(
     IUnweightedDirectedGraph <TVertex> graph,
     TVertex origin
     ) : this(graph, origin, origin, new List <TVertex> {
     origin
 })
 {
 }
Exemplo n.º 2
0
 public UnweightedDirectedPathCollection(
     IUnweightedDirectedGraph <TVertex> graph,
     TVertex origin,
     IDictionary <TVertex, TVertex> vertices
     ) : base(graph, origin)
 {
     Graph    = graph;
     Vertices = vertices;
 }
 public UnweightedDirectedPath(
     IUnweightedDirectedGraph <TVertex> graph,
     TVertex origin,
     TVertex destination,
     IEnumerable <TVertex> vertices
     ) : base(graph, origin, destination, vertices)
 {
     Graph = graph;
 }
Exemplo n.º 4
0
        public DepthFirstSearchUnweightedDirectedPathEnumerator(
            IUnweightedDirectedGraph <TVertex> graph,
            TVertex origin
            )
        {
            Graph   = graph;
            Origin  = origin;
            Stack   = new Stack <TVertex>();
            Visited = new Dictionary <TVertex, TVertex>();

            Initialize();
        }
        public BreadthFirstSearchUnweightedDirectedPathEnumerator(
            IUnweightedDirectedGraph <TVertex> graph,
            TVertex origin
            )
        {
            Graph   = graph;
            Origin  = origin;
            Queue   = new Queue <TVertex>();
            Visited = new Dictionary <TVertex, TVertex>();

            Initialize();
        }
        public static IUnweightedDirectedGraph <TVertex> DepthFirstSearch <TVertex>(
            this IUnweightedDirectedGraph <TVertex> graph,
            TVertex origin
            )
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            if (origin == null)
            {
                throw new ArgumentNullException(nameof(origin));
            }

            if (!graph.Vertices.Contains(origin))
            {
                throw new ArgumentException();
            }

            var visited = new HashSet <TVertex>();
            var output  = new UnweightedDirectedGraph <TVertex>();

            var stack = new Stack <TVertex>();

            visited.Add(origin);
            stack.Push(origin);

            while (stack.Count > 0)
            {
                var vertex = stack.Pop();

                bool Predicate(TVertex successorVertex)
                {
                    return(!visited.Contains(successorVertex));
                }

                foreach (var successorVertex in graph.SuccessorVertices(vertex).Where(Predicate))
                {
                    output.AddEdge(vertex, successorVertex);

                    visited.Add(successorVertex);
                    stack.Push(successorVertex);
                }
            }

            return(output);
        }
Exemplo n.º 7
0
 public DepthFirstSearchUnweightedDirectedPathAlgorithm(IUnweightedDirectedGraph <TVertex> graph)
 {
     Graph = graph;
 }
        public static IEnumerable <IUnweightedDirectedPath <TVertex> > AllSimplePaths <TVertex>(
            this IUnweightedDirectedGraph <TVertex> graph,
            TVertex origin,
            TVertex destination,
            int maxDepth = int.MaxValue
            )
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            if (origin == null)
            {
                throw new ArgumentNullException(nameof(origin));
            }
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (!graph.Vertices.Contains(origin))
            {
                throw new ArgumentException();
            }
            if (!graph.Vertices.Contains(destination))
            {
                throw new ArgumentException();
            }

            var visited = new Stack <TVertex>();
            var paths   = new HashSet <IUnweightedDirectedPath <TVertex> >();

            visited.Push(origin);

            var stack = new Stack <Tuple <TVertex, Queue <IUnweightedEndpoint <TVertex> > > >();

            stack.Push(new Tuple <TVertex, Queue <IUnweightedEndpoint <TVertex> > >(
                           origin,
                           graph.SuccessorEndpoints(origin).ToQueue()
                           ));

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

                if (Equals(vertex, destination) || successorEndpoints.Count == 0 || stack.Count > maxDepth)
                {
                    if (Equals(vertex, destination))
                    {
                        var unweightedDirectedPath = new UnweightedDirectedPath <TVertex>(
                            graph,
                            origin,
                            destination,
                            new Stack <TVertex>(visited)
                            );

                        paths.Add(unweightedDirectedPath);
                    }

                    visited.Pop();

                    stack.Pop();
                }
                else
                {
                    var successorEndpoint = successorEndpoints.Dequeue();

                    visited.Push(successorEndpoint.Vertex);

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

                    stack.Push(new Tuple <TVertex, Queue <IUnweightedEndpoint <TVertex> > >(
                                   successorEndpoint.Vertex,
                                   graph.SuccessorEndpoints(successorEndpoint.Vertex).Where(Predicate).ToQueue()
                                   ));
                }
            }

            return(paths);
        }