Ejemplo n.º 1
0
        /// <summary>
        /// Search in depth
        /// </summary>
        /// <param name="start"></param>
        /// <param name="finish"></param>
        public void DepthSearch(typename start, typename finish)
        {
            int         startI  = Find(start);
            int         finishI = Find(finish);
            Stack <int> stack   = new Stack <int>();

            if (startI == -1 || finishI == -1)
            {
                System.Console.WriteLine("Wrong start of finish node");
            }
            LinkedList <int> visited = new LinkedList <int>();

            stack.Push(startI);
            visited.InsertFirst(startI);
            for (; ;)
            {
                List <int> neighbours = matrix.GetNeighbours(stack.Peek());
                bool       end        = true;
                foreach (int i in neighbours)
                {
                    if (!visited.Exist(i))
                    {
                        stack.Push(i);
                        visited.InsertFirst(i);
                        end = false;
                        break;
                    }
                }
                if (end)
                {
                    stack.Pop();
                }
                if (stack.Peek() == finishI)
                {
                    while (!stack.StackEmpty())
                    {
                        System.Console.WriteLine(vertexList[stack.Pop()].Data);
                    }
                    break;
                }
            }
        }