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;
public Node (int State)
{
this.State = State;
this.Parent = null;
this.depth = 0;
}
public Node(int State)
{
this.State = State;
}
public Node(int State,Node Parent)
{
this.State = State;
this.Parent = Parent;
if (Parent == null)
this.depth = 0;
else
this.depth = Parent.depth + 1;
}
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
{
public ArrayList GetSussessor(int State)
{
ArrayList Result = new ArrayList();
Result.Add(2 * State + 1);
Result.Add(2 * State + 2);
return Result;
}
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;
}
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;
}
}
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);
if (Parent.State == Goal.State)
{
Console.WriteLine();
Console.WriteLine("Find Goal " + Parent.State);
break;
} 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);
}
}
}
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;
} 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);
}
}
}