0% found this document useful (0 votes)
6 views11 pages

Research

The document outlines an experiment at Vidyavardhini’s College of Engineering & Technology focused on implementing packet routing in computer networks using Depth-First Search (DFS) and Breadth-First Search (BFS). It details the objectives, theory, algorithms, advantages, disadvantages, and applications of both search methods, along with pseudocode and Java code implementations. The conclusion emphasizes the trade-offs between DFS and BFS in terms of efficiency, memory usage, and suitability for various network scenarios.

Uploaded by

ggucyff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Research

The document outlines an experiment at Vidyavardhini’s College of Engineering & Technology focused on implementing packet routing in computer networks using Depth-First Search (DFS) and Breadth-First Search (BFS). It details the objectives, theory, algorithms, advantages, disadvantages, and applications of both search methods, along with pseudocode and Java code implementations. The conclusion emphasizes the trade-offs between DFS and BFS in terms of efficiency, memory usage, and suitability for various network scenarios.

Uploaded by

ggucyff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Vidyavardhini’s College of Engineering & Technology

Department of Computer Engineering


Academic Year: 2024-25

Experiment No.2
Implement packet routing in a computer network using DFS
and BFS.
Date of Performance: 27/01/2025
Date of Submission: 27/01/2025

CSL604: Artificial Intelligence Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2024-25

Aim: Study and Implementation of Depth first search for problem solving.
Objective: To study the uninformed searching techniques and its implementation for problem
solving.
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.

The basic idea is as follows:

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
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.

CSL604: Artificial Intelligence Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2024-25

The DFS algorithm works as follows:


1.Start by putting any one of the graph's vertices on top of a stack.
2.Take the top item of the stack and add it to the visited list.
3.Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited
list to the top of the stack.
4.Keep repeating steps 2 and 3 until the stack is empty.

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

DFS-recursive (G, s):


mark s as visited
for all neighbours w of s in Graph G:
if w is not visited:
DFS-recursive(G, w)

CSL604: Artificial Intelligence Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2024-25

DFS Working: Example

Path: 1 → 2→ 4→ 5→ 3

Searching Strategies are evaluated along the following dimensions:

1. Completeness: does it always find a solution if one exists?


2. Time complexity: number of nodes generated
3. Space complexity: maximum number of nodes in memory
4. Optimality: does it always find a least-cost solution?

Properties of depth-first search:

1. Complete: - No: fails in infinite-depth spaces, spaces with loops.


2. Time Complexity: O(bm)
3. Space Complexity: O(bm), i.e., linear space!
4. Optimal: No

Advantages of Depth-First Search:

1. Memory requirement is only linear with respect to the search graph.


2. The time complexity of a depth-first Search to depth d is O(b^d)
3. If depth-first search finds solution without exploring much in a path then the time and
space it takes will be very less.

CSL604: Artificial Intelligence Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2024-25

Disadvantages of Depth-First Search:

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:

How to find connected components using DFS?

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.

• First connected component is 1 → 2 → 3 as they are linked to each other


• Second connected component 4 → 5
• Third connected component is vertex 6

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

CSL604: Artificial Intelligence Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2024-25

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( )

//processing all the neighbours of v


for all neighbours w of v in Graph G
if w is not visited
Q.enqueue( w ) //Stores w in Q to further visit its neighbour
mark w as visited.

Working of BFS:

Example: Initial Node: A Goal Node: C

CSL604: Artificial Intelligence Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2024-25

Searching Strategies are evaluated along the following dimensions:

1. Completeness: does it always find a solution if one exists?


2. Time complexity: number of nodes generated
3. Space complexity: maximum number of nodes in memory
4. Optimality: does it always find a least-cost solution?

Properties of Breadth-first search:

1. Complete: - Yes: if b is finite.


2. Time Complexity: O(b^d+1)
3. Space Complexity: O(b^d+1)
4. Optimal: Yes

Advantages of Breadth-First Search:

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.

Disadvantages of Breadth-First Search:

CSL604: Artificial Intelligence Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2024-25

1. The main drawback of Breadth first search is its memory requirement. Since each level
of the tree must be saved in order to generate the next level, and the amount of memory
is proportional to the number of nodes stored, the space complexity of BFS is O(bd).
2. If the solution is farther away from the root, breath first search will consume lot of time.

Applications:

How to determine the level of each node in the given tree?

As you know in BFS, you traverse level wise. You can also use BFS to determine the level of
each node.

Code:

import java.util.*;

public class BFS {

static class Edge {


int src;
int dest;

public Edge(int s, int d) {


this.src = s;
this.dest = d;
}
}

public static void createGraph(ArrayList<Edge> graph[]) {


for (int i = 0; i < graph.length; i++) {
graph[i] = new ArrayList<>();
}

graph[0].add(new Edge(0, 1));


graph[0].add(new Edge(0, 2));

graph[1].add(new Edge(1, 0));


graph[1].add(new Edge(1, 3));

graph[2].add(new Edge(2, 0));

CSL604: Artificial Intelligence Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2024-25

graph[2].add(new Edge(2, 4));

graph[3].add(new Edge(3, 1));


graph[3].add(new Edge(3, 4));
graph[3].add(new Edge(3, 5));

graph[4].add(new Edge(4, 2));


graph[4].add(new Edge(4, 3));
graph[4].add(new Edge(4, 5));

graph[5].add(new Edge(5, 3));


graph[5].add(new Edge(5, 4));
graph[5].add(new Edge(5, 6));

graph[6].add(new Edge(6, 5));


}

public static void bfs(ArrayList<Edge> graph[], int start, boolean vis[]) {


Queue<Integer> queue = new LinkedList<>();
queue.add(start);
vis[start] = true;

while (!queue.isEmpty()) {
int curr = queue.poll();
System.out.print(curr + " ");

for (Edge e : graph[curr]) {


if (!vis[e.dest]) {
queue.add(e.dest);
vis[e.dest] = true;
}
}
}
}

public static void dfs(ArrayList<Edge> graph[], int curr, boolean vis[]) {


System.out.print(curr + " ");
vis[curr] = true;

for (Edge e : graph[curr]) {


if (!vis[e.dest]) {
dfs(graph, e.dest, vis);
}
}
}

CSL604: Artificial Intelligence Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2024-25

public static void main(String[] args) {


int v = 7;
@SuppressWarnings("unchecked")
ArrayList<Edge> graph[] = new ArrayList[v];
createGraph(graph);

boolean[] vis = new boolean[v];

System.out.println("BFS Traversal:");
for (int i = 0; i < v; i++) {
if (!vis[i]) {
bfs(graph, i, vis);
}
}

// Reset visited array for DFS


Arrays.fill(vis, false);

System.out.println("\nDFS Traversal:");
for (int i = 0; i < v; i++) {
if (!vis[i]) {
dfs(graph, i, vis);
}
}
}
}

Output:

CSL604: Artificial Intelligence Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2024-25

Conclusion: In conclusion, the experiment on implementing packet routing in a computer network


using Depth-First Search (DFS) and Breadth-First Search (BFS) provided valuable insights into their
performance and applicability in network scenarios. BFS demonstrated its efficiency in finding the
shortest path in terms of hop count, making it suitable for real-time applications requiring minimal
latency. On the other hand, DFS, while not always yielding the shortest path, proved useful in exploring
deeper network paths where resource constraints or specific routing policies are involved. The
experiment highlighted the trade-offs between the two algorithms in terms of speed, memory usage,
and suitability for different network topologies, emphasizing the importance of selecting the appropriate
routing approach based on network requirements.

CSL604: Artificial Intelligence Lab

You might also like