AI Exp3
AI Exp3
Lab Outcome: -
CSL604.2: Understand and implement uninformed search algorithms
Date of Performance:
Date of Submission:
______________________________
Practical In charge
EXPERIMENT NO. 03
Theory:
Uniformed 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.
The breadth-first search algorithm is an example of a general-graph search algorithm.
Breadth-first search implemented using FIFO queue data structure.
Algorithm:
Example:
In the above tree structure, we have shown the traversing of the tree using BFS algorithm
from the root node S to goal node K. BFS search algorithm traverse in layers, so it will follow
the path which is shown by the dotted arrow, and the traversed path will be:
S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K
Depth-first Search
Algorithm:
1. Enter the root node on the stack
2. Do until stack is not empty
3. Remove Node
a. if node=goal then stop
4. push all children of the node in stack
Example:
In the below search tree, we have shown the flow of depth-first search, and it will follow the
order as:
Root node--->Left node ----> right node.
It will start searching from root node S, and traverse A, then B, then D and E, after traversing
E, it will backtrack the tree as E has no other successor and still goal node is not found. After
backtracking it will traverse node C and then G, and here it will terminate as it found goal
node.
Implementation:
graph = {
0: [1, 2, 3],
1: [4, 5],
2: [6],
3: [7, 8],
4: [9],
5: [],
6: [],
7: [10],
8: [],
9: [],
10: []
}
print("BFS Traversal:")
bfs(graph, 0) # Starting from vertex 0
print("\nDFS Traversal:")
dfs(graph, 0) # Starting from vertex 0
print("\nDLS Traversal:")
dls(graph, 0, 0, 2) # Starting from vertex 0 with maximum depth 2
Graph as input:
Output:
Conclusion: Hence BFS, DFS, Depth limited search algorithm implemented successfully.