Thursday, 3 April 2008
Different Search Strategies
Hill Climbing Algorithm
Hill climbing is a local search. It looks for local maxima to reach from source node to goal node. It looks for node closer to destination node and then extend path from current node. But limitation is that it looks for only local maxima. A local maximum is having the highest value among its neighbouring nodes but less than node in global space. So it get stucked if there is no another better node in local search space. It does not look for global search space.
Suppose for above drawn graph (figure 6(a)) showing connected cities
Source node – 3
Destination node – 5
1. Source Node is 3, which is shown in figure 6(b) with another circle of red colour.
Figure 6(b)
2. It will look for neighbouring nodes of Node 3. Node 2 and Node 4 are its neighbouring nodes. So it path will extend towards Node 4 which seems to be more closer to solution node as shown in figure 6(c)
Figure 6(c)
3. Node 2 is found to be near to destination node, so is explored next as shown in fig 6(d).
Figure 6(d)
But now there is no other node near to this local search. Hill Climbing cannot look for global search so it cannot move to next node. Hence it is stucked here. This is incomplete search.
A* Search Algo
Figure 5(a)
A* Search
A* search algorithm is an informed search works on weighted graph. This search uses the sum of the heuristic function (i.e. estimated value to reach from a given node to goal node) and the actual distance to find the shortest distance from source node to destination node.
Suppose for above drawn graph (figure 5(a)) showing connected cities
Source node – 2
Destination node – 5
1. We need to add heuristic value to each node. Heuristic value is estimated value to reach from a given node to goal node. It should be less than or equal to actual distance.
Heuristic values added to each node for destination node 5 is shown below in figure 5(b).
Figure 5(b)
2. Source Node is Node 2. On pressing any key, it is highlighted with another round of red colour as shown in figure 5(c).
Figure 5(c)
3. The Child nodes or the connected nodes to Node 2 are Node 4 (heuristic value 3) and Node 3 (heuristic value 2). A* uses the sum of the heuristic value and actual distance from source node to explore next node.
Cost to reach Node 3 is 5 (actual distance) + 3 (heuristic value) = 8
Cost to reach Node 4 is 3 + 3 = 6
The cost to reach node 4 is less than the cost to reach node 3.So next explored node will be 4 as shown in figure 5(d).
Figure 5(d)
4. The connected nodes to Node 4 are Node2, Node 3 and Node 1. Same as Best first Search, it will also not consider visited nodes. So we left with Node 1 and Node 3.
Cost to reach node 1 is 6 (actual distance from source node) + 2 (heuristic value) = 8
Cost to reach node 3 is 7 (actual distance from source node) + 2 (heuristic value) = 9
It is clear that next node to explore is Node 1 as shown in figure 5(e).
Figure 5(e)
5. Node 1 has child nodes Node 4 and Node 5. Node 5 is having 0 heuristic value, which shows it is destination node. So it is next visited node as shown in figure 5(f).
Figure 5(f)
Hence Destination Node 5 is found. So search is complete.
NB: The system displays the path from destination node to source node. It first checks for the shortest path route and then displays nodes in reverse direction.
Best First Search
Best First Search
Best First Search is abbreviated as BEFS. It is an informed search. It uses heuristic function (i.e. estimated value to reach from a given node to goal node) to find the shortest distance from source node to destination node.
Suppose for above drawn graph (figure 4(a)) showing connected cities
Source node – 2
Destination node – 5
1. We need to add heuristic value to each node. Heuristic value is the estimated value to reach from a given node to the goal node. It should be less than or equal to actual distance. Heuristic values added to each node for destination node 5 is shown below in figure 4(b).
2. Source Node is Node 2. On pressing any key, it is highlighted with another round of red colour as shown in figure 4(c).
3. Child nodes or connected nodes to Node 2 are Node 4 (heuristic value 3) and Node 3 (heuristic value 2). Node 3 is having less heuristic value so it explores next as shown in figure 4(d). Although actual distance to reach Node 3 is more than distance to reach Node 4 but it does not consider actual distances at all.
4. It does not consider visited nodes again. Like for Node 3, Child nodes are Node 2 (heuristic value 4) and Node 4 (heuristic value 3). Here heuristic value of Node 4 is less than heuristic value of Node 2, so it is explored next as shown in figure 4(e). Suppose even if heuristic value of Node 2 is less, still it will consider Node 4 because Node 2 is visited node.
5. Node 4 has two child nodes Node 1 and Node 3. Here both Node 1 and Node 3 are having same heuristic value with value 2. But Node 3 is already visited node, so Node 1 will be visited next.
Even if there are two unvisited nodes having same heuristic value, then it works in increasing order. Node lesser in number will explore first. Suppose there are two unvisited nodes Node 2 and Node 4 having same heuristic value, Node 2 will be explored than Node 4.
So here next visited node is Node 1 as shown in figure 4(f)
NB: The system displays the path from destination node to source node. It first checks for the shortest path route and then displays nodes in reverse direction.
Dijkstras Algorithm
Figure 3(a)
Suppose for above drawn graph (figure 3(a)) showing connected cities
Destination node is 5
1. On pressing any key, Source Node 1 gets highlighted with red colour another circle as shown in fig 3(b).
Figure 3(b)
Figure 3(c)
In Dijkstras Algorithm, it does not explore all nodes like in Breadth First Search. Only nodes which are used to reach from source node to destination node having shortest path are explored. It saves a lot of space.
NB: The system displays the path from destination node to source node. It first checks for the shortest path route and then displays nodes in reverse direction.
Breadth First Search
Figure 2(a)
Beadth First Search (BFS)
BFS works level by level. In Breadth First Algorithm contrast to Depth first Search, all the child nodes of a parent node are explored before moving to next level and then further child nodes of these explored nodes until a goal node is found.
Suppose for above drawn graph (figure 2(a)) showing connected cities
Destination node is 5
1) Source node is Node 1. On pressing any key, it shows Node 1 with highlighting colour another round as shown in figure 2(b)
2. Looks for child nodes of Parent Node 1. Node 4 and Node 5 are child nodes. It works in increasing order like Node 1 then Node 2 then Node 3, if Node 1, Node 2 and Node 3 are children node of any Parent node. So here Node 4 is explored first as shown in figure 2(c).
Figure 2(c)
3. In breadth First Search, all the child nodes of a node are explored at a level. So another child Node of Node 1 is Node 5. Hence Node 5 is explored after Node 4 as shown in figure 2(d).
Figure 2(d)
Depth First Search
Let us say for above graph figure 1(a)
Source node - 1
Destination node – 5
1. On pressing any key it starts with source node that is Node 1. It is shown by another round of red colour on particular node as shown in figure 1(b)
Figure 1(b)
2.Node 1 has two child or neighbour nodes Node 4 and Node 5. As DFS go for the first most child, it explores Node 4 as shown in figure 1(c)
Figure 1(c)
3.Further Node 4 behaves as parent node. It looks for child node. Node 2 and Node 3 are child or neighbour nodes of Node 4. Again as Node 2 is first most child, it is explored next as shown in figure 1(d)
Figure 1(d)
4. After Node 2, Node 3 explores as the first most child as shown in figure 1(e) but node 3 has no unvisited child nodes so here search gets stuck to move more deeply. So here concept of backtracking works.
Figure 1(e)
Backtracking is a concept according to which if search reaches to dead end or leaf node, search goes back to previous explored node or can say previous execution.
In above example, Search gets stuck at Node 3. Node 3 does not have any unvisited child node so Node 3 is known as dead end. Here using backtracking concept search goes back to Parent node of Node 3 that is Node 2. But both child nodes Node 3 and Node 4 of Node 2 are already visited. It backtracks to Node 4 as this is parent node of Node 2 and further back tracks to Node 1. Node 1 has two child nodes Node 4 and Node 5. Node 4 is already visited; next Node 5 is explored as shown in figure 1(f)
Figure 1(f)
Node 5 is destination node. Hence Depth First Search completed.