Ejemplo n.º 1
0
        public void GreedySearch(string destinationName)
        {
            /// Şuan bulunduğumuz nodu ağacın kök noduna eşitle (başlangıç noktası)
            Node currentNode = Root;
            /// Hedef noktasına giden yolu tutacak stack
            var destinationPath = new Stack <Node>();
            /// Bütün gezilen yolları tutacak stack
            var fullPath = new Stack <Node>();

            for (;;)
            {
                /// Şuanki bulunduğumuz nodu yola ekle
                destinationPath.Push(currentNode);
                parent_return :;
                /// Şuanki bulunduğumuz nodu gezilen nodlar stackına ekle
                fullPath.Push(currentNode);
                currentNode.Visited = true;

                Trace.WriteLine("node : {0}", currentNode.Name);

                /// Hedef noda varıldı mı?
                if (currentNode.Name.CompareTo(destinationName) == 0)
                {
                    Trace.WriteLine("Destination found");

                    break;
                }
                /// Şuan bulunduğumuz nodun alt nodları arasından seçilen nod
                Node selectedNode = null;
                /// Bütün alt nodlar için
                foreach (Node n in currentNode.Nodes)
                {
                    /// Eğer alt nodun altında kalan nodlar gezildiyse
                    if (n.VisitedAllNodes)
                    {
                        continue;
                    }
                    /// Seçilen nod boşsa, şuankine eşitle
                    if (selectedNode == null)
                    {
                        selectedNode = n;
                    }
                    else
                    {
                        /// Değilse, heuristic değerlerini karşılaştır ve küçük olanı seç
                        if (selectedNode.HeruisticValue > n.HeruisticValue)
                        {
                            selectedNode = n;
                        }
                    }
                }
                /// Eğer herhangi bir nod seçilmediyse
                if (selectedNode == null)
                {
                    /// Şuanki bulunduğumuz nodun bütün nodlarını gezildi olarak işaretle
                    currentNode.VisitedAllNodes = true;
                    /// Bir üst noda dön
                    currentNode = currentNode.Parent;
                    destinationPath.Pop();
                    goto parent_return;
                }
                else
                {
                    currentNode = selectedNode;
                    continue;
                }
            } /// (break) döngünün bitişi

            /// Sonucu değişkenlere aktar
            ///
            while (fullPath.Count > 0)
            {
                VisitedNodes += "," + fullPath.Pop().Name;
            }

            char[] chars = VisitedNodes.ToCharArray();
            Array.Reverse(chars);
            VisitedNodes = new string(chars);


            int totalpathcost = 0;

            while (destinationPath.Count > 0)
            {
                var point = destinationPath.Pop();
                Path          += "," + point.Name;
                totalpathcost += point.PathCost;
            }

            TotalPathCost = totalpathcost;

            Trace.WriteLine("{0}", totalpathcost.ToString());

            char[] charss = Path.ToCharArray();
            Array.Reverse(charss);
            Path = new string(charss);
        }