AIL2
AIL2
Experiment No.2
Implement packet routing in a computer network using DFS
and BFS.
Date of Performance: 04-02-25
Date of Submission: 11-02-25
Theory:
Artificial Intelligence is the study of building agents that act rationally. Most of the time,
these agents perform some kind of search algorithm in the background in order to achieve
their tasks.
• A search problem consists of:
•A State Space. Set of all possible states where you can be.
•A Start State. The state from where the search begins.
•A Goal Test. A function that looks at the current state returns whether or not it is the goal
state.
• The Solution to a search problem is a sequence of actions, called the plan that
transforms the start state to the goal state.
• This plan is achieved through search algorithms.
Depth First Search: DFS is an uninformed search method. It is also called blind search. Uninformed
search strategies use only the information available in the problem definition. A search strategy is
defined by picking the order of node expansion. Depth First Search (DFS) searches deeper into the
problem space. It is a recursive algorithm that uses the idea of backtracking. It involves exhaustive
searches of all the nodes by going ahead, if possible, else by backtracking.
1. Pick a starting node and push all its adjacent nodes into a stack.
2. Pop a node from stack to select the next node to visit and push all its adjacent nodes into
a stack.
3. Repeat this process until the stack is empty.
However, ensure that the nodes that are visited are marked. This will prevent you from
visiting the same node more than once. If you do not mark the nodes that are visited and
you visit the same node more than once, you may end up in an infinite loop.
Algorithm:
A standard DFS implementation puts each vertex of the graph into one of two
categories: 1. Visited
2. Not Visited
Pseudocode:
DFS-iterative (G, s):
//Where G is graph and s is source vertex
let S be stack
S.push( s ) //Inserting s in stack mark s as
visited.
while (S is not empty):
//Pop a vertex from stack to visit next
v = S.top()
S.pop()
//Push all the neighbours of v in stack that are not visited
for all neighbours w of v in Graph G:
if w is not visited:
S.push( w )
mark w as visited
Path: 1 → 2→ 4→ 5→ 3
Searching Strategies are evaluated along the following dimensions:
1. There is a possibility that it may go down the left-most path forever. Even a finite graph
can generate an infinite tree.
2. Depth-First Search is not guaranteed to find the solution.
3. No guarantee to find a optimum solution, if more than one solution exists.
Applications:
A graph is said to be disconnected if it is not connected, i.e. if two nodes exist in the
graph such that there is no edge in between those nodes. In an undirected graph, a
connected component is a set of vertices in a graph that are linked to each other by
paths.
Consider the example given in the diagram. Graph G is a disconnected graph and has
the following 3 connected components.
Breadth First Search: BFS is a uninformed search method. It is also called blind search.
Uninformed search strategies use only the information available in the problem definition. A search
strategy is defined by picking the order of node expansion. It expands nodes from the root of the tree
and then generates one level of the tree at a time until a solution is found. It is very easily
implemented by maintaining a queue of nodes. Initially the queue contains just the root. In each
iteration, node at the head of the queue is removed and then expanded. The generated child nodes are
then added to the tail of the queue.
BFS is a traversing algorithm where you should start traversing from a selected node (source
or starting node) and traverse the graph layerwise thus exploring the neighbour nodes (nodes
which are directly connected to source node). You must then move towards the next-level
neighbour nodes.
As the name BFS suggests, you are required to traverse the graph breadthwise as follows:
1. First move horizontally and visit all the nodes of the current layer
2. Move to the next layer
Apply DFS algorithm on given graph to find path from node A to node G.
Show and explain the status of all the nodes that are to be processed in stack STK and
status of all the nodes that are already processed.
Solution:
To apply the Depth First Search (DFS) algorithm to find a path from node A to node G, we use a stack
(STK) to keep track of nodes to be explored and maintain a list of processed nodes to track visited
nodes.
Graph Representation (Adjacency List)
From the given directed graph:
● A → {B, D}
● B → {C, F, E}
● C → {G}
● D → {F}
● F → {E}
● E → {G}
Step-by-Step Execution of DFS
We use the stack-based DFS approach:
Step 1: Start at Node A
● Stack (STK): [A]
● Processed Nodes: []
Step 2: Process A → Push its children (B, D)
● Stack (STK): [B, D]
● Processed Nodes: [A]
Step 3: Pop D → Push its child (F)
● Stack (STK): [B, F]
● Processed Nodes: [A, D]
Step 4: Pop F → Push its child (E)
● Stack (STK): [B, E]
● Processed Nodes: [A, D, F]
CSL604: Artificial Intelligence Lab
Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Step 5: Pop E → Push its child (G)
● Stack (STK): [B, G]
● Processed Nodes: [A, D, F, E]
Step 6: Pop G (Goal Node Reached)
● Stack (STK): [B]
● Processed Nodes: [A, D, F, E, G]
Since we reached G, the DFS terminates. The path found is:
A→D→F→E→G
Alternative Path via B
If DFS had explored B first instead of D, another valid path would be:
● A→B→C→G
DFS follows a Last-In-First-Out (LIFO) order, so the path depends on the order of node expansion.
BFS Algorithm:
Pseudocode:
BFS (G, s) //Where G is the graph and s is the source node let Q be
queue.
Q.enqueue( s ) //Inserting s in queue until all its neighbour
vertices are marked.
mark s as visited.
while ( Q is not empty)
//Removing that vertex from queue,whose neighbour will be visited
now
v = Q.dequeue( )
Working of BFS:
1. Breadth first search will never get trapped exploring the useless path forever. 2.
If there is a solution, BFS will definitely find it out.
3. If there is more than one solution then BFS can find the minimal one that requires less
number of steps.
Applications:
As you know in BFS, you traverse level wise. You can also use BFS to determine the level
of each node.
Question 2:
Apply BFS algorithm on given graph to find path from node A to node
E. Show and explain the status of both the queues Q1 and Q2.
Q2 holds all the nodes that are processed and deleted from Q1.
Solution:
Code:
from collections import deque
while queue:
node, path = queue.popleft() # Get the first added node (FIFO order)
if node == goal:
return path # Return path when goal is reached
Output:
Conclusion:
Both Depth-First Search (DFS) and Breadth-First Search (BFS) are effective graph
traversal algorithms, but they serve different purposes. DFS explores deeper paths first
using a stack (LIFO) approach, which can be useful for exploring all possible routes but
may not always find the shortest path first. In our case, DFS found the path A → D → F →
E → G. On the other hand, BFS explores nodes level by level using a queue (FIFO),
ensuring the shortest path is found first, as seen in our BFS traversal where the path A →
B → E was discovered efficiently. While DFS is better suited for exhaustive searches and
solving puzzles, BFS is more effective for finding the shortest path in unweighted graphs.