Joe Wilson

Joe Wilson

  • NA
  • 7.8k
  • 417.9k

How to implement search tree algorithms in c#?

Nov 3 2015 11:13 AM

 Hi, there are many classes and methods below which are related to BFS AND UCS algorithms and I have problem in writing a main method which I ask user to first enter the size of adjacency matrix of a graph then ask he/she to fill the elements of matrix ( the elements of matrix are the cost of each node (state), then at last I need to print out visited nodes in each algorithms and the main answer (answer path initial state to goal state) and I don't know how to calculate the time complexity of each algorithms too, please guide me , I need it soon. Thank you.

 
 
 
 
class Node
{
      public  int depth;
      public  int State;
      public  int Cost;
      public  Node Parent;
      // Parent Node which has depth =0 and parent = null;       public Node (int State)
      {
          this.State = State;
          this.Parent = null;
          this.depth = 0;
      }
      // another form of Node Constructor which accepts only the State;
      public Node(int State)
      {
          this.State = State;
      }
      // this form of Generalization of Node Constructor which accepts       // any node root or ordinary node;
      public Node(int State,Node Parent)
      {
          this.State = State;
          this.Parent = Parent;
          if (Parent == null)
              this.depth = 0;
          else
              this.depth = Parent.depth + 1;       
      }

      // this form of Generalization of Node Constructor which accept       // any node root or ordinary node and accept the cost of each node;
      public Node(int State, Node Parent, int Cost)
      {
        this.State = State;
        this.Parent = Parent;
        this.Cost = Cost;
        if (Parent == null)
           this.depth = 0;
        else
           this.depth = Parent.depth + 1
      }
}

 

class GetSucc
{
   //if search go forward from 0 to number n
    public ArrayList GetSussessor(int State)
    {
        ArrayList Result = new ArrayList();
        Result.Add(2 * State + 1);
        Result.Add(2 * State + 2);
        return Result;

    }
    //if search go backward from n to number 0     public ArrayList GetSussessor_Reverse(int State)
    {
        ArrayList Result = new ArrayList();
        if (State % 2 == 0)
        {
            int P = State / 2 - 1;
            Result.Add(P);
        }
        else
        {
            int Sib = State + 1;
            Result.Add(Sib / 2 - 1);
                      
        }
      
        return Result; 
    }
    //if the cost of each node must be determined     public ArrayList GetSussessor(int State,Node Parent)
    {
        ArrayList Result = new ArrayList();
        Random n = new Random();
        Test s = new Test();
        Result.Add(new Node(2* State + 1,Parent,n.Next(1,100)+Parent.Cost));
        Result.Add(new Node(2* State + 2, Parent,n.Next(1,100) + Parent.Cost));
        Result.Sort(s);
        return Result;

    }
}//end class
//implement the interface IComparer to compare objects in ArrayList public class Test : IComparer
{
    public int Compare(object x, object y)
    {
        int val1 = ((Node)x).Cost;
        int val2 = ((Node)y).Cost;
        if (val1 <= val2)
            return 1;
        else
            return 0;
    }
}
 
public static void Breadth_First_Search(Node Start, Node Goal)
{
    GetSucc x = new GetSucc();
    ArrayList children = new ArrayList();
    Queue Fringe = new Queue();
    Fringe.Enqueue(Start);
    while (Fringe.Count != 0)
    {
        Node Parent = (Node)Fringe.Dequeue();
        Console.WriteLine("Node {0} Visited ", Parent.State);
        //Console.ReadKey();         if (Parent.State == Goal.State)
        {
            Console.WriteLine();
            Console.WriteLine("Find Goal " + Parent.State);
            break;
        }//end if         children = x.GetSussessor(Parent.State);
        for (int i = 0; i < children.Count; i++)
        {
            int State = (int)children[i];
            Node Tem = new Node(State, Parent);
            Fringe.Enqueue(Tem);

        }//end for
    }//end while
}//end method
 
 
public static void Depth_First_Search(Node Start, Node Goal)
{
    GetSucc x = new GetSucc();
    ArrayList children = new ArrayList();
    Stack Fringe = new Stack();
    Fringe.Push(Start);
    while (Fringe.Count != 0)
    {
        Node Parent = (Node)Fringe.Pop();
        Console.WriteLine("Node {0} Visited ", Parent.State);
        Console.ReadKey();
        if (Parent.State == Goal.State)
        {
            Console.WriteLine();
            Console.WriteLine("Find Goal " + Parent.State);
            break;
        }//end if         children = x.GetSussessor(Parent.State);
        for (int i = 0; i < children.Count; i++)
        {
            int State = (int)children[i];
            Node Tem = new Node(State, Parent);
            Fringe.Push(Tem);

        }//end for
    }//end while
}//end method
 
 
 

Answers (1)