AI Lab Assignment 1
AI Lab Assignment 1
: 01
Aim:- Implement depth first search algorithm and Breadth First Search algorithm, Use an undirected graph
and develop a recursive algorithm for searching all the vertices of a graph or tree data structure.
Theory:-
Depth First Search
Depth first search (DFS) algorithm starts with the initial node of the graph G, and then goes to deeper and
deeper until we find the goal node or the node which has no children. The algorithm, then backtracks from
the dead end towards the most recent node that is yet to be completely unexplored.
The data structure which is being used in DFS is stack. In DFS, the edges that leads to an unvisited node are
called discovery edges while the edges that leads to an already visited node are called block edges.
Depth first Search or Depth first traversal is a recursive algorithm for searching all the vertices of a graph
or tree data structure. Traversal means visiting all the nodes of a graph.
1 Visited
2 Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles. The
DFS algorithm works as follows:
Next, we visit the element at the top of stack i.e. 1 and go to its adjacent nodes. Since 0 has already been
visited, we visit 2 instead.
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack and visit it.
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack and visit it.
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack and visit it.
After we visit the last element 3, it doesn't have any unvisited adjacent nodes, so we have
completed the Depth First Traversal of the graph.
The pseudocode for DFS is shown below. In the init() function, notice that we run the DFS function on
every node. This is because the graph might have two different disconnected parts so to make sure that we
cover every vertex, we can also run the DFS algorithm on every node.
DFS(G, u)
u.visited = true
for each v ∈ G.Adj[u]
if v.visited == false
DFS(G,v)
init() {
For each u ∈ G
u.visited = false
For each u ∈ G
DFS(G, u)
}
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
The algorithm works as follows:
1 Start by putting any one of the graph's vertices at the back of a queue.
2 Take the front item of the queue 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 back of
the queue.
4 Keep repeating steps 2 and 3 until the queue is empty.
The graph might have two different disconnected parts so to make sure that we cover every vertex, we can
also run the BFS algorithm on every node.
BFS pseudocode
create a queue Q
mark v as visited and put v into Q
while Q is non-empty
remove the head u of Q
mark and enqueue all (unvisited) neighbours of u
Conclusion:
Thus, we have implemented depth first search algorithm and Breadth First Search algorithm, Using
undirected graph and developed a recursive algorithm for searching all the vertices of a graph.