コード例 #1
0
        public static List <T> Search <T>(IUnweightedGraph <T> graph, T start, T goal)
        {
            Dictionary <T, T> cameFrom;
            var foundPath = Search(graph, start, goal, out cameFrom);

            return(foundPath ? AStarPathfinder.RecontructPath(cameFrom, start, goal) : null);
        }
コード例 #2
0
        public static void Search <T>(IUnweightedGraph <T> graph, T start, int length, out Dictionary <T, T> cameFrom)
        {
            var frontier = new Queue <T>();

            frontier.Enqueue(start);

            cameFrom = new Dictionary <T, T>();
            cameFrom.Add(start, start);

            var forNextLevel = 1;

            while (frontier.Count > 0 && length > 0)
            {
                var current = frontier.Dequeue();

                foreach (var next in graph.GetNeighbors(current))
                {
                    if (!cameFrom.ContainsKey(next))
                    {
                        frontier.Enqueue(next);
                        cameFrom.Add(next, current);
                    }
                }

                forNextLevel--;
                if (forNextLevel == 0)
                {
                    forNextLevel = frontier.Count;
                    length--;
                }
            }
        }
コード例 #3
0
        public static bool Search <T>(IUnweightedGraph <T> graph, T start, T goal, out Dictionary <T, T> cameFrom)
        {
            var foundPath = false;
            var frontier  = new Queue <T>();

            frontier.Enqueue(start);

            cameFrom = new Dictionary <T, T>();
            cameFrom.Add(start, start);

            while (frontier.Count > 0)
            {
                var current = frontier.Dequeue();
                if (current.Equals(goal))
                {
                    foundPath = true;
                    break;
                }

                foreach (var next in graph.GetNeighbors(current))
                {
                    if (!cameFrom.ContainsKey(next))
                    {
                        frontier.Enqueue(next);
                        cameFrom.Add(next, current);
                    }
                }
            }

            return(foundPath);
        }
コード例 #4
0
        public static List <T> Search <T>(IUnweightedGraph <T> graph, T start, HashSet <T> goals)
        {
            var foundPath = Search(graph, start, goals, out var cameFrom);

            if (!foundPath)
            {
                return(null);
            }

            foreach (var goal in goals)
            {
                if (cameFrom.ContainsKey(goal))
                {
                    return(PathConstructor.RecontructPath(cameFrom, start, goal));
                }
            }

            return(null);
        }
コード例 #5
0
ファイル: Tarjan.cs プロジェクト: Atljh0987/Graphs
        private static void Passer(IUnweightedGraph graph, int v, State[] states, UnmanagedNumbersArray sorted, ref int sortedCount)
        {
            if (states[v] == State.Checked)
            {
                return;
            }
            if (states[v] == State.Checking)
            {
                throw new ArgumentException("Cycle in graph");
            }

            states[v] = State.Checking;

            foreach (var arc in graph.InComingArcs(v))
            {
                Passer(graph, arc, states, sorted, ref sortedCount);
            }

            sorted[sortedCount++] = v;
            states[v]             = State.Checked;
        }
コード例 #6
0
ファイル: Tarjan.cs プロジェクト: Atljh0987/Graphs
        public static int[] Run(IUnweightedGraph graph)
        {
            if (graph == null)
            {
                throw new ArgumentNullException("graph");
            }
            if (!graph.Oriented)
            {
                throw new ArgumentException("Graph must be oriented");
            }

            using (UnmanagedNumbersArray SortedElements = new UnmanagedNumbersArray(graph.PeakCount))
            {
                State[] states      = new State[graph.PeakCount];
                int     sortedCount = 0;

                for (int i = 0; i < graph.PeakCount; i++)
                {
                    Passer(graph, i, states, SortedElements, ref sortedCount); // переизучить что тако ref
                }

                return(SortedElements.ToArray());
            }
        }
コード例 #7
0
        /**
         * Finds a minimum spanning tree in the graph.
         *
         * The MST is found using a modifed DFS algorithm. The only difference
         * to the algorithm implemented by DepthFirstSearch() method, is that
         * here we record the edges that were crossed throughout the algorithm.
         * These edges make up the MST at the end of the DFS search.
         *
         * @param start_index           The index of the vertex where search starts at.
         * @param mst                   Unweighted tree instance that will become
         *                              an MST in this method. The MST instance must
         *                              already contain all the original's graph
         *                              vertices.
         *
         * @return The unweighted graph instance representing the MST. Note
         * that NULL is returned if there is no path from the vertex at
         * start_index to one or more vertices in the graph.
         *
         * @throws ArgumentException exception if start_index is negative
         * or greater-or-equal to the number of vertices in the graph.
         */
        protected IUnweightedGraph <VertexT> FindMinimumSpanningTree(
            int start_index, IUnweightedGraph <VertexT> mst)
        {
            if (start_index < 0 || start_index >= Size)
            {
                throw new ArgumentException();
            }

            // Tracks the visited vertices so that we don't visit the same
            // vertex multiple times
            BitArray visited = new BitArray(Size, false);

            // After they are visited, the vertices are pushed to the stack
            // so that search can be continued at them at later time
            StackViaLinkedList <int> stack = new StackViaLinkedList <int>();

            // Push the start vertex to the queue and mark it as visited.
            stack.Push(start_index);
            visited[start_index] = true;
            int added_edges_count = 0;

            // The column of the adjacency matrix where the search for adjacent
            // vertices needs to start from. This is set to stack.Pop() + 1 so
            // that we don't re-scan the entire row of the adjacency matrix
            // when we continue looking at the adjacent vertices of the vertex
            // that was pushed to the stack before
            int column = 0;

            while (!stack.IsEmpty())
            {
                for (; column < Size; ++column)
                {
                    if (Edges.EdgeExists(stack.Peak(), column) && !visited[column])
                    {
                        // Found an unvisited adjacent vertex. Add an edge between
                        // this vertex and the vertex at the top of the stack.
                        // Note that if this is an undirected graph, the AddEdge
                        // method bellow will also add an edge in the opposite
                        // direction.
                        mst.AddEdge(stack.Peak(), column);

                        // Mark it as visited and push it to the stack
                        stack.Push(column);
                        visited[column] = true;
                        ++added_edges_count;

                        // Reset the column to 0 so that the adjacency matrix row
                        // corresponding to unvisited vertex is scanned from the
                        // beginning
                        column = 0;

                        // Break the the loop as we need to look at the adjecent
                        // vertices of the new vertex at the top of the stack next
                        break;
                    }
                }

                if (column == Size)
                {
                    // We've found no unvisited adjecent vertices of the vertex
                    // at the top of the stack. Pop it off the stack so the next
                    // iteration will look at other adjacent vertices of the
                    // new top of the stack. Note that column is set to
                    // stack.Pop() + 1 so we start scanning that vertex's
                    // adjacency matrix row from where we left off
                    column = stack.Pop() + 1;
                }
            }

            // The MST must consists of Size - 1 edges. If this is not the
            // case, the graph is disconnected and MST doesn't exist (in
            // which case NULL is returned)
            return(added_edges_count == Size - 1 ? mst : null);
        }
コード例 #8
0
        public static List <T> Search <T>(IUnweightedGraph <T> graph, T start, T goal)
        {
            var foundPath = Search(graph, start, goal, out var cameFrom);

            return(foundPath ? PathConstructor.RecontructPath(cameFrom, start, goal) : null);
        }