Professional Documents
Culture Documents
I008 - Khemal - Experiment - PAI - 2
I008 - Khemal - Experiment - PAI - 2
Experiment: 2
PART A
(PART A: TO BE REFFERED BY STUDENTS)
Now let’s take a look at the steps involved in traversing a graph by using Breadth-First
Search:
Step 1: Take an Empty Queue.
Step 2: Select a starting node (visiting a node) and insert it into the Queue.
Step 3: Provided that the Queue is not empty, extract the node from the Queue and insert its
child nodes (exploring a node) into the Queue.
Step 4: Print the extracted node.
Example:
We will use the Breadth-First Search algorithm to traverse through the graph.
We’ll assign node ‘a’ as the root node and start traversing downward and follow the steps
mentioned above.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Principles of Artificial Intelligence
The above image depicts the end-to-end process of Breadth-First Search Algorithm.
1. Assign ‘a’ as the root node and insert it into the Queue.
2. Extract node ‘a’ from the queue and insert the child nodes of ‘a’, i.e., ‘b’ and ‘c’.
3. Print node ‘a’.
4. The queue is not empty and has node ‘b’ and ‘c’. Since ‘b’ is the first node in the
queue, let’s extract it and insert the child nodes of ‘b’, i.e., node ‘d’ and ‘e’.
5. Repeat these steps until the queue gets empty. Note that the nodes that are already
visited should not be added to the queue again.
BFS (G, s)
let Q be queue.
Q.enqueue( s )
mark s as visited
while ( Q is not empty)
v = Q.dequeue( )
Time complexity: Equivalent to the number of nodes traversed in BFS until the shallowest
solution.
2 3 s s
T ( n )=1+n +n + …+n =O(n )
Space complexity: Equivalent to how large can the fringe get.
S ( n )=O( ns )
Completeness: BFS is complete, meaning for a given search tree, BFS will come up with a
solution if it exists.
Optimality: BFS is optimal as long as the costs of all edges are equal.
Applications of BFS:
1. Crawlers in Search Engines
2. GPS Navigation systems
3. Find the Shortest Path & Minimum Spanning Tree for an un-weighted graph
4. Broadcasting
5. Peer to Peer Networking
Depth-first search:
Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data
structures. The algorithm starts at the root node (selecting some arbitrary node as the root
node in the case of a graph) and explores as far as possible along each branch before
backtracking.
Example:
For the following graph:
A Depth-First Search starting at A, assuming that the left edges in the shown graph are
chosen before right edges, and assuming the search remembers previously visited nodes and
will not repeat them (since this is a small graph), will visit the nodes in the following order:
A, B, D, F, E, C, G. Performing the same search without remembering previously visited
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Principles of Artificial Intelligence
Pseudocode
Input: A graph G and a vertex v of G
Output: All vertices reachable from v labeled as discovered
A recursive implementation of DFS:
procedure DFS(G,v):
label v as discovered
for all directed edges from v to w that are in G.adjacentEdges(v) do
if vertex w is not labeled as discovered then
recursively call DFS(G,w)
The order in which the vertices are discovered by this algorithm is called the lexicographic
order.
A non-recursive implementation of DFS with worst-case space complexity
procedure DFS-iterative(G,v):
let S be a stack
S.push(v)
while S is not empty
v = S.pop()
if v is not labeled as discovered:
label v as discovered
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Principles of Artificial Intelligence
Applications of DFS:
Algorithms that use depth-first search as a building block include:
1. Finding connected components.
2. Topological sorting.
3. Finding 2-(edge or vertex)-connected components.
4. Finding 3-(edge or vertex)-connected components.
5. Finding the bridges of a graph.
6. Generating words in order to plot the limit set of a group.
7. Finding strongly connected components.
8. Planarity testing.[7][8]
9. Solving puzzles with only one solution, such as mazes. (DFS can be adapted to find
all solutions to a maze by only including nodes on the current path in the visited set.)
10. Maze generation may use a randomized depth-first search.
11. Finding bi-connectivity in graphs.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Principles of Artificial Intelligence
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the portal at the end of the practical. The
filename should be PAI_batch_rollno_Labno Example: PAI_A2_A001_Lab2
Roll No.: Name:
Prog/Yr/Sem: Batch:
Date of Experiment: Date of Submission:
# Constructor
def __init__(self):
while queue:
# Driver code
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Principles of Artificial Intelligence
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
# Driver Code
print("Following is the Depth-First Search")
dfs(visited, graph, '5')