Documente Academic
Documente Profesional
Documente Cultură
This article helps the beginner of an AI course to learn the objective and implementation of
Uninformed Search Strategies (Blind Search) which use only information available in the
problem definition.
To go ahead in these series, I will use some AI terminologies such as the following:
A problem can be defined by four components:
1. Initial State that the problem starts in
2. Goal State that the problem ends in
3. Finding sequences of actions that lead to Goal State
4. A path Cost to solve problem
Together, the initial state, goal state, actions and path cost define the state space of the
problem (the set of all states reachable from the initial state by any sequence of actions).
The path in the state space is a sequence of states connected by a sequence of actions. A
solution to the problem is an action sequence that leads from the initial state to the goal
state.
The process of looking at a sequence of actions that reaches the goal is called search. A
search algorithm takes a problem as input and returns a solution in the form of an action
sequences. The search starts from the initial state which represents the root node in the
problem search space. The branches are actions and the nodes corresponding to the
states in the problem state space.
Algorithm
Let T=(V,E) be a tree with weighted edges and let w(p) be the weight of path p in T. For
convenience, let r be the root of the tree and t(p) denote the end vertex of a path p in T.[1]
With that said, let K, the set of known paths starting with r, be {(r)}. It should be noted that
the weight of (r) is zero.
1. If there exists p in K such that p minimizes w(p) and t(p) is a goal state of T, i.e., a
leaf, stop. p is the path of minimal cost to the goal.
2. Otherwise, identify p in K where p minimizes w(p) and expand t(p). In other words,
add to K all paths formed by adding one of the child vertices of t(p) (if any) to p.
Remove p from K; it is considered 'visited', and failure to remove it may result in
non-termination of the search.
3. Repeat first step.
Code:
import java.util.PriorityQueue;
public int uniformCostSearch(int adjacencyMatrix[]
import java.util.Scanner; [], int source, int destination)
import java.util.Set; {
{ this.source = source;
{ {
distances[destination] =
adjacencyMatrix[evaluationNode][destination]
priorityQueue.add(new Node(source, 0));
+
distances[source] = 0; distances[evaluationNode];
parent[destination] = evaluationNode;
while (!priorityQueue.isEmpty()) }
}
private int
getNodeWithMinDistanceFromPriorityQueue()
{ {
if (adjacencyMatrix[evaluationNode] path.add(destination);
[destination] != MAX_VALUE)
boolean found = false;
{
int vertex = destination;
if (distances[destination] >
adjacencyMatrix[evaluationNode][destination] while (!found)
+ distances[evaluationNode]) {
if (vertex == source) adjacency_matrix = new
int[number_of_vertices + 1][number_of_vertices + 1];
{
System.out.println("Enter the Weighted Matrix
found = true; for the graph");
} {
vertex = parent[vertex]; {
} adjacency_matrix[i][j] = scan.nextInt();
if (i == j)
while (iterator.hasNext()) }
{ if (adjacency_matrix[i][j] == 0)
System.out.print(iterator.next() + "\t"); {
} adjacency_matrix[i][j] = MAX_VALUE;
} }
int source = 0;
{ return -1;
} return 1;
} return -1;
} return 0;
{ @Override
public Node() {
if (this.node == node.node)
} {
return true;
{ }
this.cost = cost; }
} }
$javac UniformCostSearch.java
$java UniformCostSearch
Enter the number of vertices
7
Enter the Weighted Matrix for the graph
0503000
0010000
0000608
0000220
0400000
0000003
0000400
Enter the source
1
Enter the destination
7
The Path between 1 and 7 is
1 4 6 7
The Distance between source 1 and destination 7 is 8