Unit1-B Key
Unit1-B Key
Problem Solving
• Define the problem precisely by including specification of initial situation, and final situation
constituting the solution of the problem.
• Analyse the problem to find a few important features for appropriateness of the solution
technique.
• Isolate and represent the knowledge that is necessary for the solution
• Optimality: If a solution found for an algorithm is guaranteed to be the best solution (lowest path
cost) among all other solutions, then such a solution for is said to be an optimal solution.
• Time Complexity: Time complexity is a measure of time for an algorithm to complete its task.
• Space Complexity: It is the maximum storage space required at any point during the search, as
the complexity of the problem.
State Space
• Number of state in which problem can go.
S : {S,A,Action(s), Result(s,a),Cost(s,a)}
2 3 4 1 2 3
5 1 8 4
8 7 6 7 6 5
• Heuristic function : maps each state to a numerical value which depicts goodness of a node.
H(n)=value
UNINFORMED / INFORMED
BREADTH FIRST
A* SERACH
DEPTH FIRST
BEST FIRST
BIDIRECTIONAL
DEPTH-LIMITED
Water Jug Problem
• Problem: There are two jugs of volume A litre and B litre. Neither has any measuring mark on
it.There is a pump that can be used to fill the jugs with water.How can you get exactly x litre of
water into the A litre jug.Assuming that we have unlimited supply of water.
Note:Let's assume we have A=4 litre and B= 3 litre jugs. And we want exactly 2 Litre water into jug
A (i.e 4 litre jug) how we will do this.
• Solution:
The state space for this problem can be described as the set of ordered pairs of integers (x,y)
Where, x represents the quantity of water in the 4-gallon jug and y represents the quantity of water
in 3-gallon jug.
• Start State: (0,0) and Goal State: (2,0)
• We basically perform three operations to achieve the goal :
1. Fill water jug.
2. Empty water jug
3. Transfer water jug
Breadth First Search
• Breadth-first search is the most common search strategy for traversing a tree or graph. This
algorithm searches breadthwise in a tree or graph, so it is called breadth-first search.
• BFS algorithm starts searching from the root node of the tree and expands all successor node at
the current level before moving to nodes of next level.
• Time Complexity: Time Complexity of BFS algorithm can be obtained by the number of nodes
traversed in BFS until the shallowest Node. Where the d= depth of shallowest solution and b is a
node at every state. T (b) = 1+b2+b3+.......+ bd= O (bd)
• Space Complexity: Space complexity of BFS algorithm is given by the Memory size of frontier
which is O(b ).
d
• Completeness: BFS is complete, which means if the shallowest goal node is at some finite
depth, then BFS will find a solution.
• Optimality: BFS is optimal if path cost is a non-decreasing function of the depth of the node.
Rules :
1. A queue (FIFO-First in First Out) data structure is used by BFS.
2. You mark any node in the graph as root and start traversing the data from it.
3. BFS traverses all the nodes in the graph and keeps dropping them as completed.
4. BFS visits an adjacent unvisited node, marks it as done, and inserts it into a queue.Removes the
previous vertex from the queue in case no adjacent vertex is found.
5. BFS algorithm iterates until all the vertices in the graph are successfully traversed and marked
as completed.
6. There are no loops caused by BFS during the traversing of data from any node.
Applications:
• Un-weighted Graphs: BFS algorithm can easily create the shortest path and a minimum
spanning tree to visit all the vertices of the graph in the shortest time possible with high accuracy.
• P2P Networks: BFS can be implemented to locate all the nearest or neighbouring nodes in a peer
to peer network. This will find the required data faster.
• Web Crawlers: Search engines or web crawlers can easily build multiple levels of indexes by
employing BFS. BFS implementation starts from the source, which is the web page, and then it
visits all the links from that source.
• Navigation Systems: BFS can help find all the neighboring locations from the main or source
location.
• Network Broadcasting: A broadcasted packet is guided by the BFS algorithm to find and reach
all the nodes it has the address for.
• Advantages :
If there are more than one solutions for a given problem, then BFS will provide the minimal
solution which requires the least number of steps.
• 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.
Depth First Search
• Depth-first search isa recursive algorithm for traversing a tree or graph data structure.
• It is called the depth-first search because it starts from the root node and follows each path to its
greatest depth node before moving to the next path.
• DFS uses a stack data structure for its implementation.
• The process of the DFS algorithm is similar to the BFS algorithm.
Properties :
1. Complete : No
2. Optimal : No
3. Time Complexity: O (b m ) ; where b is branching factor, and m is maximum depth of the tree
DFS requires very less memory as it only needs to store a stack of the nodes on the path
from root node to the current node.
It takes less time to reach to the goal node than BFS algorithm (if it traverses in the right
path).
• Disadvantage:
There is the possibility that many states keep re-occurring, and there is no guarantee of
finding the solution.
DFS algorithm goes for deep down searching and sometime it may go to the infinite loop.
Depth Limited Search
• A depth-limited search algorithm is similar to depth-first search with a predetermined limit. Depth-
limited search can solve the drawback of the infinite path in the Depth-first search.
• In this algorithm, the node at the depth limit will treat as it has no successor nodes further.
Standard failure value: It indicates that problem does not have any solution.
Cutoff failure value: It defines no solution for the problem within a given depth limit.
Properties :
1. Complete : No
2. Optimal : No
• Disadvantages:
It may not be optimal if the problem has more than one solution.
Iterative Deepening Search
• Complete : Yes
• Optimal : yes
• Time Complexity: O(bd ) ; where b is branching factor and d is depth of the solution
• 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.
Properties :
• Complete : Yes
• Optimal : yes
• Time Complexity: O(bm ) ; where b is branching factor and m is maximum depth of the tree;
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.
Bidirectional Search
• In this method the search has a running technique in two directions. Indeed, two searches run
simultaneously. One of them is done in a forward direction from the initial state, and another one is
done backward from the goal state. The aim is that these two searches should join in the midway.
Properties :
1. Complete: Yes (may be or may be not)
2. Optimal: Yes
4. Space Complexity: b d / 2 .
For example, for the branching factor = 8 and depth = 4, the breadth-first search needs time
b d
which is proportional to 8 = 4096 whereas the bidirectional search is more effective because of
4
1 2 3 1 2 3
4 6 4 5 6
7 5 8 7 8
1 2 3 2 3 1 2 3
4 6 1 4 6 7 4 6
7 5 8 7 5 8 5 8
1 2 3 1 2 3 1 2 3 1 3
4 5 6 4 6 4 6 4 2 6
7 8 7 5 8 7 5 8 7 5 8
1 2 3 1 2 3
4 5 6 4 5 6
7 8 7 8
Water Jug problem using DFS