KCA301 AI Unit 2 Searching Techniques
KCA301 AI Unit 2 Searching Techniques
• Else if x is greater than the mid element, then x can only lie in the right (greater) half
subarray after the mid element. Then we apply the algorithm again for the right half.
• Else if x is smaller, the target x must lie in the left (lower) half. So we apply the
algorithm for the left half.
Binary Search
while low <= high:
1. Breadth-first Search
2. Depth-first Search
3. Depth-limited Search
4. Iterative deepening depth-first search
5. Uniform cost search
6. Bidirectional Search
Breadth First Search
ROOT
LEAVES
Not Only in Computer Science but also In
Bhagwat Geeta Chapter 15 Verse 1
• Root (God) is Top and Leaves (Wordly affairs) are down
Breadth First Search: Graph
Representation
graph = { • Node 'A' is connected to 'B'
'A' : ['B', 'C'], and 'C'.
'B' : ['D', 'F'], • Node 'B' is connected to 'D'
'C' : ['F', 'G'], and 'F'.
'D' : [], • Node 'C' is connected to 'F'
and 'G'.
'E' : [],
• Nodes 'D', 'E', 'F', and 'G'
'F' : [],
have no outgoing connections
'G' : [] (empty lists).
}
Breadth First Search Function
def bfs(visited, graph, node):
visited.append(node) # Mark the starting node as visited
queue.append(node) # Add the starting node to the queue
while queue:
s = queue.pop(0) # Remove the first node from the queue
• This search algorithm finds out the best depth limit and does it by
gradually increasing the limit until a goal is found.
• The idea of Best First Search is to use an evaluation function to decide which adjacent is
most promising and then explore.
• Best First Search falls under the category of Heuristic Search or Informed Search.
• The worst-case time complexity for Best First Search is O(n * log n) where n is the
number of nodes. In the worst case, we may have to visit all nodes before we reach goal.
Note that priority queue is implemented using Min(or Max) Heap, and insert and remove
operations take O(log n) time.
Best First Search
Best First Search
• The BFS begins at node 'A' and tries to find the target node 'H'.
• The traversal order and queue updates work as follows:
• Starts at 'A':
• Prints 'A'.
• Adds neighbors 'B' (weight 12) and 'C' (weight 4) to the queue.
• The queue is sorted: [('C', 4), ('B', 12)].
• Processes 'C' next (smallest weight):
• Prints 'C'.
• Adds neighbors 'F' (weight 8) and 'G' (weight 2) to the queue.
• The queue is sorted: [('G', 2), ('F', 8), ('B', 12)].
• Processes 'G':
• Prints 'G'.
• Adds neighbor 'H' (weight 0) to the queue.
• The queue is sorted: [('H', 0), ('F', 8), ('B', 12)].
• The first element in the queue is now 'H', the target node.
• It prints 'H' and terminates the search.
Best First Search in Python
• graph = {
• 'A': [('B', 12), ('C', 4)],
• 'B': [('D', 7), ('E', 3)],
• 'C': [('F', 8), ('G', 2)],
• 'D': [],
• 'E': [('H', 0)],
• 'F': [('H', 0)],
• 'G': [('H', 0)]
• }
• Each key is a node, and each value is a list of tuples representing connected nodes and the
weights of the edges to those nodes.
• For example, node 'A' is connected to 'B' with a weight of 12 and to 'C' with a weight of 4.
Best First Search in Python
• If the target is not found, the function processes the next node in the queue (i.e.,
the node with the smallest weight), removing it from the queue (queue.pop(0)).
• The function then calls itself recursively with this node as the new start node.
A* Search Algorithm
• A* Search Algorithm is a simple and efficient search algorithm that can be
used to find the optimal path between two nodes in a graph. It will be used for
the shortest path finding.
algorithms.
❖ A* search algorithm is optimal and complete.
❑ Disadvantages:
❖ It does not always produce the shortest path as it mostly
❑A* algorithm returns the path which occurred first, and it does not search for all
remaining paths.
❑ The efficiency of A* algorithm depends on the quality of heuristic.
❑A* algorithm expands all nodes which satisfy the condition f(n)
❑
❑
It combined approach of BFS and DFS.
It uses the heuristic function and search.
❑ We can choose most promising node at each step with help of BFS.
In BFS search algorithm, we expand node which is close to the goal node and the minimum cost is
❑
estimated by heuristic function,
❑ The heuristic function f(n)=h(n). Where h(n)=estimated cost from node n to goal.
❑ Greedy search algorithm ignores the cost of the path that has already been traversed to reach n.
The solution given is not necessarily optimal
Greedy BFS Example
140 B
C A 366
111 B 374
E
C 329
D 99
80
D 244
F
G
E 253
F 178
97
211 G 193
H
H 98
101 I
I 0
A
C
B
Greedy BFS
Example
F
G
Path A--->E--->E--->F--->I
Estimated Path Cost=140+99+211
Heuristic Cost=253+178
Greedy BFS
❑AND-OR graph is useful for representing the solution of problem that can be solved by
decomposing them into a set of smaller problems, that can be solved by decomposing them into a
set of smaller problems, all of which must then be solved.
Advantages
Disadvantages
➢Sometimes for unsolvable nodes, it can’t find the optimal path. Its
complexity is than other algorithms.
Local Search and Optimization Problems