Professional Documents
Culture Documents
Intelligence
ASSISTANT PROFESSOR. KANCHAN TAKSALE
Problem Solving by
Searching
UNIT II
Unit II
8-Puzzle
N-queen problem
https://www.youtube.com/watch?v=-ugN9bh1EU0
Asst Prof. Kanchan A. Taksale
The river problem
• There is a farmer who wishes to cross a
river but he is not alone. He also has a
goat, a wolf, and a cabbage along with him.
• There is only one boat available which can
support the farmer and either of the goat,
wolf or the cabbage.
• So at a time, the boat can have only two
objects (farmer and one other).
But the problem is, if the goat and wolf
are left alone (either in the boat or
onshore), the wolf will eat the goat.
• Similarly, if the Goat and cabbage are left
alone, then goat will eat the cabbage.
• The farmer wants to cross the river with
all three of his belongings: goat, wolf, and
cabbage. https://www.youtube.com/watch?v=WAWAQQuyWCc
What strategy he should use to do so?
State space: The state space of a problem is the set of all states reachable
from the initial state by executing any sequence of actions. State is
representation of all possible outcomes.
The state space specifies the relation among various problem states thereby,
forming a directed network or graph in which the nodes are states and the links
between nodes represent actions.
Initial State
Actions
Successor Function
Goal test
Path Cost
1. Initial state: The initial state is the one in which the agent starts in.
2. Actions: It is the set of actions that can be executed or applicable in all possible states. A
description of what each action does; the formal name for this is the transition model.
4. Goal test: It is a test to determine whether the current state is a goal state.
In some problems the goal test can be carried out just by comparing current state with the
defined goal state, called as explicit goal test.
Whereas, in some of the problems, state cannot be defined explicitly but needs to be generated
by carrying out some computations, it is called as implicit goal test.
For example: In Tic-Tac-Toe game making diagonal or vertical or horizontal combination declares
the winning state which can be compared explicitly; but in the case of chess game, the goal state
cannot be predefined but it's a scenario called as "Checkmate", which has to be evaluated
implicitly.
Asst Prof. Kanchan A. Taksale
5.Path cost :
• It is simply the cost associated with each step to be taken to reach to the goal state.
• To determine the cost to reach to each state, there is a cost function, which is chosen by the
problem solving agent.
Problem solution:
• A well-defined problem with specification of initial state, goal test, successor function, and
path cost.
• It can be represented as a data structure and used to implement a program which can search
for the goal state.
• A solution to a problem is a sequence of actions chosen by the problem solving agent that leads
from the initial state to a goal state.
• Solution quality is measured by the path cost function.
Optimal solution:
• An optimal solution is the solution with least path cost among all solutions.
• A general sequence followed by a simple problem solving agent is, first it formulates the
problem with the goal to be achieved, then it searches for a sequence of actions that would
solve the problem, and then executes the actions one at a time.
Asst Prof. Kanchan A. Taksale
• Informed Search • Uninformed Search
• It uses knowledge for the searching • It doesn’t use knowledge for searching
process. process.
• Greedy Search, A* Search, Graph Search • Depth First Search, Breadth First Search
Asst Prof. Kanchan A. Taksale
• An uninformed search is a searching technique that
has no additional information about the distance
from the current state to the goal.
1.Breadth-First Search
2.Depth First Search
3.Bidirectional search
4.Uniform Cost Search
1.A* Algorithm
2.Best First Search
3.AO* Algorithm
4.Constraints satisfaction
5.Hill Climbing algorithm
6.beam search
Asst Prof. Kanchan A. Taksale
Before we go into these search types let’s get to know the few terms
which will be frequently used in the upcoming sections.
* State: It provides all the information about the environment.
* Goal State: The desired resulting condition in a given problem and
the kind of search algorithm we are looking for.
* Goal Test: The test to determine whether a particular state is a
goal state.
* Path/Step Cost: These are integers that represent the cost to
move from one node to another node.
* Space Complexity: A function describing the amount of
space(memory) an algorithm takes in terms of input to the algorithm.
* Time Complexity: A function describing the amount of time the
algorithm takes in terms of input to the algorithm.
* Optimal: Extent of preference of the algorithm
* ‘b‘ – maximum branching factor in a tree.
* ‘d‘ – the depth of the least-cost solution.
* ‘m‘ – maximum depth state space(maybe infinity)
Asst Prof. Kanchan A. Taksale
1. Depth First Search (DFS):
• It is a search algorithm where the search tree will be
traversed from the root node.
• It will be traversing, searching for a key at the leaf of a
particular branch.
• If the key is not found the searching retraces its steps
back to the point from where the other branch was left
unexplored and the same procedure is repeated for that
other branch.
• First, the searching starts in the root node A and then
goes to the branch where node B is present
(lexicographical order).
• Then it goes do node D because of DFS and from D there is the only node to traverse i.e. node H.
• But after node H does not have any child nodes so we retrace the path in which we traversed earlier and
again reach node B but this time we traverse through in the untraced path a traverse through node E.
• There are two branches at node E but let’s traverse node I (lexicographical order) and then retrace the
path as we have no further nodes after E to traverse.
• Then we traverse node J as it is the untraced branch and then again find we are at the end and retrace
the path and reach node B and then we will traverse the untraced branch i.e. through node C and repeat
the same process. This is called the DFS Algorithm.
Asst Prof. Kanchan A. Taksale
Advantages:
•DFS requires very little memory as it only needs to store a stack of the nodes on
the path from the root node to the current node.
•It takes less time to reach the goal node than the BFS algorithm [which is
explained later](if it traverses in the right path).
Disadvantages:
•There is the possibility that many states keep reoccurring, and there is no
guarantee of finding the solution.
•The DFS algorithm goes for deep down searching and sometimes it may go to the
infinite loop.
Conclusions:
It occupies a lot of memory space, and time to execute when the solution is at the
bottom or end of the tree and is implemented using LIFO Stack data structure[DS].
Complete: No
Time Complexity: O(bm)
Space complexity: O(bm)
Optimal: Yes
Disadvantages:
•It requires lots of memory since each level of the tree must be saved into memory to expand the
next level.
•BFS needs lots of time if the solution is far away from the root node.
Conclusions:
It requires a lot of memory space and time consuming if the goal state is at the bottom or end. It
uses a FIFO queue DS to implement.
Complete: Yes (assuming b is finite)
Time Complexity: O(bd)
Space complexity: O(bd)
Optimal: Yes, if Step cost = 1 (i.e. no cost/all step costs are same)
• This algorithm is mainly used when the step costs are not
the same but we need the optimal solution to the goal
state.
• In such cases, we use Uniform Cost Search to find the goal
and the path including the cumulative cost to expand each
node from the root node to the goal node.
• It does not go depth or breadth, it searches for the next
node with the lowest cost and in the case of the same path
cost, let’s consider lexicographical order in our case.
• In the above figure consider S to be the start node and G to be the goal state.
• From node S we look for a node to expand and we have nodes A and G but since it’s a uniform cost
search it’s expanding the node with the lowest step cost so node A becomes the successor rather than
our required goal node G.
• From A we look at its children nodes B and C. So since C has the lowest step cost it traverses through
node C and then we look at successors of C i.e. D and G since the cost to D is low we expand along with
the node D.
• Since D has only one child G which is our required goal state we finally reach the goal state D by
implementing UFS Algorithm.
• If we have traversed this way definitely our total path cost from S to G is just 6 even after traversing
through many nodes rather than going to G directly where the cost is 12 and 6<<12(in terms of step
cost). But this may not work with all cases. (https://www.youtube.com/watch?v=wIEfTDu5gII)
Asst Prof. Kanchan A. Taksale
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 involved in searching and
only concerned about path cost. Due to which this algorithm may be
stuck in an infinite loop.
Conclusions:
Complete: Yes (if b is finite and costs are stepped costs are zero)
Time Complexity: O(b(c/ϵ)) where, ϵ -> is the lowest cost, c ->
optimal cost
Space complexity: O(b(c/ϵ))
Optimal: Yes (even for non-even cost)
Advantages:
Depth-limited search is Memory efficient.
Disadvantages:
The DLS has disadvantages of completeness and is not
optimal if it has more than one goal state.
Conclusions:
Complete: Complete (if solution > depth-limit) s.a.c.e.f.d.g.b.i.h.j
Time Complexity: O(bl) where, l -> depth-limit
sacdbij
Space complexity: O(bl)
Optimal: Yes (only if l > d)
Asst Prof. Kanchan A. Taksale
• The heuristic function is a way to inform the search
about the direction to a goal. It provides an informed way
to guess which neighbor of a node will lead to a goal.
• There is nothing magical about a heuristic function.
• It must use only information that can be readily obtained
about a node.