0% found this document useful (0 votes)
2 views

Chap 3-Informed Search Algorithm

The document discusses informed search algorithms, focusing on Uniform-cost, Best-first, and A* Search Algorithms. Informed search algorithms utilize heuristics to efficiently navigate large search spaces by estimating distances to goal nodes. Each algorithm has distinct advantages and disadvantages regarding optimality, completeness, and time complexity.

Uploaded by

Elias Khoury
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chap 3-Informed Search Algorithm

The document discusses informed search algorithms, focusing on Uniform-cost, Best-first, and A* Search Algorithms. Informed search algorithms utilize heuristics to efficiently navigate large search spaces by estimating distances to goal nodes. Each algorithm has distinct advantages and disadvantages regarding optimality, completeness, and time complexity.

Uploaded by

Elias Khoury
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Artificial Intelligence

Chap III – Informed Search Algorithms


Outline

 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.

 Admissibility of the heuristic function is given as:


h(n) <= h*(n)

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:

Best First Search Algorithm(Greedy search)


A* Search Algorithm

8
Best-first Search Algorithm (Greedy
Search)

 Greedy best-first search algorithm always selects the path which


appears best at that moment. It is the combination of depth-first
search and breadth-first search algorithms. It uses the heuristic
function and search.

 Best-first search allows us to take the advantages of both


algorithms. With the help of best-first search, at each step, we
can choose the most promising node. In the best first search
algorithm, we expand the node which is closest to the goal node
and the closest cost is estimated by heuristic function, i.e.
f(n)= h(n). Were, h(n)= estimated cost from node n to the goal.
The greedy best first algorithm is implemented by the priority
queue.
9
Best first Search
Best first search algorithm:
 Step 1: Place the starting node into the OPEN list.
 Step 2: If the OPEN list is empty, Stop and return failure.
 Step 3: Remove the node n, from the OPEN list which has the lowest
value of h(n), and places it in the CLOSED list.
 Step 4: Find whether the node n is a goal node or not. If the node is
goal node, then return success and terminate the search, else proceed
to Step 5.
 Step 5: Expand the node n, and generate the successors of node n.
 Step 6: For each successor node, algorithm checks for evaluation
function f(n), and then check if the node has been in either OPEN or
CLOSED list. If the node has not been in both list, then add it to the
OPEN list.
 Step 7: Return to Step 2.
10
Best first 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

 A* search is the most commonly known form of best-first search. It uses


heuristic function h(n), and cost to reach the node n from the start state g(n). It
has combined features of UCS and greedy best-first search, by which it solve the
problem efficiently.
 A* search algorithm finds the shortest path through the search space using the
heuristic function. This search algorithm expands less search tree and provides
optimal result faster. A* algorithm is similar to UCS except that it uses g(n)+h(n)
instead of g(n).
 In A* search algorithm, we use search heuristic as well as the cost to reach the
node. Hence we can combine both costs as following, and this sum is called as a
fitness number.

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 f(n) = g(n) + h(n)

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

In this method, the sort criteria is the


function f.
The choice of the path depends on f
minimal.

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))

 The node B generates 3 successors A, C and D. the 3 corresponding


paths are:
((A B S) 7 12) ((C B S) 6 10) ((D B S) 9 12)
 The first path ((A B S) 7 12) is eliminated because the path
((A S) 3 8) is shortest (3 , 8).

 The second path is inserted (6 > 7) and the 3rd also.


(((A S) 3 8) ((C B S) 6 10) ((D B S) 9 12))

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.

If we use this notation, the following 9 states are possible:


1:(1 1), 2:(1 2), 3:(1 3), 4:(2 1), 5:(2 2), 6:(2 3), 7:(3 1), 8 :(3 2), 9:(3 3)
Suppose that each move costs 1 second and the heuristic function h for a state is equal to the
minimum number of moves needed to move from this state to the goal. 1.
a) Draw the search graph and calculate the value of h for each state.
b) Provide traces of the following methods:
3) Best-first research method.
4) Research Method A*.
Implement the A*methods for the same graph and call these functions from a main program using a
Main Menu.
19
Implemetation of A* in C++

20
Implemetation of A* in C++
#define MAX 8
#define MAX_PATH 100

char* city[8] = {"Arad", "Zerind", "Oradea", "Sibu", "Faragas", "Vilcea", "Pitesti",


"Bugarest"};
int A[8][8] = {{0, 16, 0, 113, 0, 0, 0, 0 }, {0, 0, 50, 0, 0, 0, 0, 0},{0, 0, 0, 47,
0, 0, 0, 0}, {0, 0, 0, 0, 77, 54, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 176},{0, 0, 0, 0, 0, 0,
79, 0}, {0, 0, 0, 0, 0, 0, 0, 120}, {0, 0, 0, 0, 0, 0, 0, 0}};

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);
}
}

//Find an element in an Array

int find(PATH* s, Node head){


int i;
for(i=0; i< s->NbElt; i++)
if((head.path[head.N-1] == s->p[i].path[s->p[i].N-1]) && (head.path[0]
== s->p[i].path[0]))
return i;
return -1; 22
}
Implementation of A* in C++
void sort(PATH* s) {
Node temp; // Add an Element to an Array
int i, j; bool Adds(PATH* s , const Node & item){
for(i=0; i< s->NbElt-1; i++) if ( s->NbElt == MAX_PATH)
for(j=i+1; j < s->NbElt; j++)
return false;
if (s->p[i].h < s->p[j].h) {
temp = s->p[i]; s->p[s->NbElt++] = item;
s->p[i] = s->p[j]; return true;
s->p[j] = temp; }
} int main(){
Node P;
} PATH* S;
Node I, F;
int i;
bool Successeurs(Node head, PATH* s) {
Node Tmp; I.path[0]= 0;
int j; I.g = 0;
I.h = 366;
for (j = 0 ; j < MAX ; j++) I.N = 1;
if (A[head.path[head.N-1]][j] > 0) {
Tmp = head; F.path[0]= 7;
Tmp.path[Tmp.N++] = j; F.N = 1;
Tmp.g = Tmp.g + A[head.path[head.N-1]][j] ; Creates(S);
Tmp.h = Tmp.g + T[j].cost; Adds(S,I);
if(!Adds(s,Tmp)) return false;
Astar(S, F, P);
} for(i=0; i<P.N ; i++)
return true; cout << city[P.path[i]] << ' ';
} return 0;
} 23
h(S)  | xO  x S |  0.99( | y O  y S |)

Exercise Search algorithm


In this problem, the initial state is S, and the goal states are O1
and O2. The transition cost is indicated near the arc, and the
cost of a node S relative to the goal O is defined as follows:

𝐻 𝑆 = 𝑥𝑜 − 𝑥𝑠 + |𝑦𝑜 − 𝑦𝑠 |
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

You might also like