You are on page 1of 5

AI - LAB - NLU

(Semester 2, 2018/2019)

Lab #2: Uninformed search


For a given tree with nodes and weights of each edge described in the following figure:

Fig. 1. Tree 1

First, the implementation of tree structure used for the tasks of uninformed search is defined
as follows (code is sent to students):

Fig. 2. Class diagram of tree structure

Each Node has a label and a path cost (computed from the start node) and a list of children
presented as edges where the current node is the begin and the end node is given by methods:

1
AI - LAB - NLU
(Semester 2, 2018/2019)
 addEdge (Node that, double weight): add an edge connecting the current node with
that node. The weight is given as the second parameter.
 addEdge(Node that): add an edge connecting the current node with that node. The
default weight is 1. This option is used for non-weighted tree.

Parent attribute is used to track the found path from the Start node to the Goal node (or
from the Root node to the Goal node).

Other comments for the implementation of tree structure is given in the code.

Next, the interface ISearchAlgo.java defined 3 execute methods:

public void execute(Node tree); // traversal all nodes in the tree

public Node execute(Node tree, String goal);// find the path from root node
//to the goal node

public Node execute(Node tree, String start, String goal); // find the path
//from start node to the goal node

Fig. 3. Class diagram of algorithms

==================================================================

For BreadthFirstSearchAlgo, the structure of the frontier is Queue:

Queue<Node> frontier = new LinkedList<Node>();

For DepthFirstSearchAlgo, the structure of the frontier is Stack:

Stack<Node> frontier = new Stack<Node>();

==================================================================

Task 1: Implement execute(Node tree) in BreadthFirstSearchAlgo.java and


DepthFirstSearchAlgo.java

==================================================================
2
AI - LAB - NLU
(Semester 2, 2018/2019)
The result of the test (BreadthFirstSearchAlgo) is as follows:

Node nodeS = new Node("S");


Node nodeA = new Node("A"); Node nodeB = new Node("B");
Node nodeC = new Node("C"); Node nodeD = new Node("D");
Node nodeE = new Node("E"); Node nodeF = new Node("F");
Node nodeG = new Node("G"); Node nodeH = new Node("H");

nodeS.addEdge(nodeA, 5); nodeS.addEdge(nodeB, 2);


nodeS.addEdge(nodeC, 4); nodeA.addEdge(nodeD, 9);
nodeA.addEdge(nodeE, 4); nodeB.addEdge(nodeG, 6);

nodeC.addEdge(nodeF, 2); nodeD.addEdge(nodeH, 7);

nodeE.addEdge(nodeG, 6); nodeF.addEdge(nodeG, 1);

ISearchAlgo algo1 = new BreadthFirstSearchAlgo();

algo1.execute(nodeS);

For BreadthFirstSearchAlgo: S A B C D E G F H ?

For DepthFirstSearchAlgo: S C F G B A E D H ?

==================================================================

Task 2: Implement execute(Node tree, String goal) in BreadthFirstSearchAlgo.java and


DepthFirstSearchAlgo.java

==================================================================

In this task, the parent attribute is used to track the found path from the Root node (or the
Start node) to the Goal node. The method for convert from a GOAL node with setting its
parent node is defined as follows (NodeUtils.java):

public static List<String> printPath(Node node) {


List<String> result = new ArrayList<String>();
result.add(node.getLabel());
Node tmp;
while ((tmp = node.getParent()) != null) {

result.add(tmp.getLabel());
node = tmp;
}
Collections.reverse(result);
return result;
}

3
AI - LAB - NLU
(Semester 2, 2018/2019)

Fig. 4. Breadth-first search

The result of the test (BreadthFirstSearchAlgo) with the above tree is as follows:

//Test for BreadthFirstSearchAlgo:


Node result = algo.execute(nodeS, "G");
System.out.println(NodeUtils.printPath(result));

For BreadthFirstSearchAlgo: S B G ?

For DepthFirstSearchAlgo: S C F G ?

==================================================================

Task 3: Implement execute(Node tree, String goal) in UniformCostSearchAlgo.java


(implements ISearchAlgo)

==================================================================

For UniformCostSearchAlgo, the structure of the frontier is PriorityQueue:

PriorityQueue<Node> frontier = new PriorityQueue<Node>(new


NodeComparator());

NodeComparator is used for comparing the pathcosts of two nodes.

The result of the test is as follows:

ISearchAlgo algo1 = new BreadthFirstSearchAlgo();

Node result = algo1.execute(nodeS, "G");


System.out.println(NodeUtils.printPath(result));

UniformCostSearchAlgo: S C F G, pathcost = 7

==================================================================

4
AI - LAB - NLU
(Semester 2, 2018/2019)
Task 4: Similar task 3, implement execute(Node tree, String start, String goal) in
BreadthFirstSearchAlgo.java, DepthFirstSearchAlgo.java and
UniformCostSearchAlgo.java

==================================================================

Additional task: test all implementations with the following tree:

5 3
9

2
3
4 7

1
2

Fig. 4. Tree 2

You might also like