Chap 3-Informed Search Algorithm
Chap 3-Informed Search Algorithm
Uniform-cost search
Best-first Search Algorithm
A* Search Algorithm
2
Informed Search Algorithms
We have talked about the uninformed search algorithms which
looked through search space for all possible solutions of the
problem without having any additional knowledge about search
space.
Informed search algorithm contains an array of knowledge such
as how far we are from the goal, path cost, how to reach to goal
node, etc. This knowledge help agents to explore less to the
search space and find more efficiently the goal node.
The informed search algorithm is more useful for large search
space. Informed search algorithm uses the idea of heuristic, so it
is also called Heuristic search.
3
Uniform-cost Search Algorithm
Uniform-cost search is a searching algorithm used for traversing a
weighted tree or graph. This algorithm comes into play when a
different cost is available for each edge. The primary goal of the
uniform-cost search is to find a path to the goal node which has the
lowest cumulative cost.
Uniform-cost search expands nodes according to their path costs
from the root node. A uniform-cost search algorithm is
implemented by the priority queue.
It gives maximum priority to the lowest cumulative cost. Uniform
cost search is equivalent to BFS algorithm if the path cost of all
edges is the same.
4
Uniform-cost Search Algorithm
Advantages:
Uniform cost search is optimal because at every state the
path with the least cost is chosen.
Disadvantages:
It does not care about the number of steps involve in
searching and only concerned about path cost. Due to
which this algorithm may be stuck in an infinite loop.
5
Uniform-cost Search Algorithm
Completeness:
Uniform-cost search is complete, such as if there is
a solution, UCS will find it.
Time Complexity:
Let C* is Cost of the optimal solution, and ε is
each step to get closer to the goal node. Then the
number of steps is = C*/ε+1. Here we have taken
+1, as we start from state 0 and end to C*/ε.
Hence, the worst-case time complexity of Uniform-
cost search is O(b1 + [C*/ε])/.
Space Complexity:
The same logic is for space complexity so, the
worst-case space complexity of Uniform-cost
search is O(b1 + [C*/ε]).
Optimal:
Uniform-cost search is always optimal as it only
selects a path with the lowest path cost.
6
Heuristics function
Heuristic is a function which is used in Informed Search, and it finds the most
promising path. It takes the current state of the agent as its input and
produces the estimation of how close agent is from the goal.
The heuristic method, however, might not always give the best solution, but it
guaranteed to find a good solution in reasonable time. Heuristic function
estimates how close a state is to the goal. The value of the heuristic function is
always positive.
Here h(n) is heuristic cost, and h*(n) is the estimated cost. Hence heuristic cost
should be less than or equal to the estimated cost.
7
Heuristics function
Pure heuristic search is the simplest form of heuristic search
algorithms. It expands nodes based on their heuristic value h(n). It
maintains two lists, OPEN and CLOSED list. In the CLOSED list, it
places those nodes which have already expanded and in the OPEN
list, it places nodes which have yet not been expanded.
On each iteration, each node n with the lowest heuristic value is
expanded and generates all its successors and n is placed to the
closed list.The algorithm continues unit a goal state is found.
In the informed search we will discuss two main algorithms which
are given below:
8
Best-first Search Algorithm (Greedy
Search)
Advantages:
Best first search can switch between BFS and DFS by gaining
the advantages of both the algorithms.
This algorithm is more efficient than BFS and DFS algorithms.
Disadvantages:
It can behave as an unguided depth-first search in the worst
case scenario.
It can get stuck in a loop as DFS.
This algorithm is not optimal.
11
Best first Search
Example:
Consider the below search problem, and we will traverse it using greedy best-first
search. At each iteration, each node is expanded using evaluation function f(n)=h(n) ,
which is given in the below table.
12
Best-first Search
In this search example, we are using two lists which are
OPEN and CLOSED Lists. Following are the iteration for
traversing the above example.
13
Best-first Search
Expand the nodes of S and put in the CLOSED list
Initialization: Open [A, B], Closed [S]
Iteration 1: Open [A], Closed [S, B]
Iteration 2: Open [E, F, A], Closed [S, B]
: Open [E, A], Closed [S, B, F]
Iteration 3: Open [I, G, E, A], Closed [S, B, F]
: Open [I, E, A], Closed [S, B, F, G]
Hence the final solution path will be: S----> B----->F----> G
Time Complexity: The worst case time complexity of Greedy best first search is
O(bm).
Space Complexity: The worst case space complexity of Greedy best first search is
O(bm). Where, m is the maximum depth of the search space.
Complete: Greedy best-first search is also incomplete, even if the given state space is
finite.
Optimal: Greedy best first search algorithm is not optimal.
14
A* Search Algorithm
15
A* Search Algorithm
Algorithm A*:
1. If memory is empty, return NIL.
2. If the 1st element of current path is equal to the final state, then return the
current path.
3. Default:
a. Develop the successor paths of the current path. We obtain than a list of paths.
b. Delete the current path from memory and insert in memory the successor’s paths
verifying the optimization's condition.
The insertion of (a … n …s) in memory is done based on:
If there exist in memory a path (x … s) / x=a, then
If (g(x) > g(a)) then
Replace (x … s) by (a … n … s)
Else
We keep (x … s) and we ignore (a … n … s)
c. Sort the memory
d. Call A* with sorted memory.
16
A* Search Algorithm
We define: h(A) = C1
H(A): Cost to go from A to objective.
f(A) = g(A) + h(A).
g(A): Cost to go from S to A
17
A* Search algorithm
In memory, we have:
(((S) 0 20) // 0 g & 20 f
((B S) 5 7)
((A S) 3 8)
((C S) 7 11)
(((B S) 5 7)((A S) 3 8)((C S) 7 11))
18
A* Search Exercise
Our goal is to move 2 discs from peg 1 to peg 3 provided we only move one disc at a time and
never place a disc on a smaller diameter disc. A state is represented by a:(b c) where a is the
state number, b the number of the peg containing the largest disk and c the number of the peg
containing the smallest disk.
20
Implemetation of A* in C++
#define MAX 8
#define MAX_PATH 100
struct Node {
int path[8];
int g;
int h;
int N;
};
struct Table {
int idx;
int cost;
};
Table T[8] = {{0,366}, {1,350}, {2,300}, {3,253}, {4,176} , {5,199}, {6,120}, {7,0}};
struct PATH {
Node p[MAX_PATH];
int NbElt;
}; 21
Implemetation of A* in C++
bool Astar(PATH* S, Node fs, Node & P)
{
Node head;
int i, k ;
PATH* SUCC;
if (!StackTop(S,head)) return false;
if (!Pop(S)) return false;
if (head.path[head.N-1] == fs.path[fs.N-1]) {
P = head;
return true;
} else
{
Creates(SUCC);
if (!Successeurs(head, SUCC)) return false;
for (i = 0; i < SUCC->NbElt ; i++) {
k = find(S,SUCC->p[i]);
if(k > 0) {
if (S->p[k].g > SUCC->p[i].g)
S->p[k] = SUCC->p[i];
} else {
if (!Adds(S,SUCC->p[i])) return false;
}
}
delete(SUCC);
sort(S);
return Astar(S, fs, P);
}
}
𝐻 𝑆 = 𝑥𝑜 − 𝑥𝑠 + |𝑦𝑜 − 𝑦𝑠 |
To calculate h, we assume that O is equal to O1 for S, J, N,
O1 and that O is equal to O2 for K, L, M, O2.
Draw the tree and provide the order of nodes expanded for
the Depth-First Search (DFS) and Breadth-First
Search (BFS) methods.
Provide the trace, the order of nodes expanded, and the cost
of the path from S to the goal for the A* search method.
In case of choosing nodes with same priority, we consider
selection in alphabetical order.
24
Exercise Search algorithm
25