0% found this document useful (0 votes)
14 views7 pages

Ai Exp 1

The document outlines an assignment focused on implementing Depth First Search (DFS) and Breadth First Search (BFS) algorithms using an undirected graph. It details the objectives, algorithms, complexities, and applications of both search strategies, emphasizing their recursive nature and the use of data structures like stacks and queues. Additionally, it includes questions for differentiation and analysis of the algorithms.
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)
14 views7 pages

Ai Exp 1

The document outlines an assignment focused on implementing Depth First Search (DFS) and Breadth First Search (BFS) algorithms using an undirected graph. It details the objectives, algorithms, complexities, and applications of both search strategies, emphasizing their recursive nature and the use of data structures like stacks and queues. Additionally, it includes questions for differentiation and analysis of the algorithms.
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/ 7

Assignment I

Title: 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.
Prerequisite: Basic knowledge of Arrays, Lists, Stack, Queue, Graph, Tree etc.
Objective: In this experiment, we will be able to do the following:
● To understand Uninformed Search Strategies.
● To make use of Graph and Tree Data Structure for implementation of Uninformed
Searchstrategies.
● Study the various Graph traversal algorithms and the difference between them.
● Understand the BFS Graph traversal using queues.
Demonstrate knowledge of time complexity and space
complexity of performing a BFS on agiven graph
Outcome: Successfully able to find depth first search algorithm and Breadth First Search
algorithm.
Software and Hardware Requirement:
Open Source C++ Programming tool like G++/GCC, python,java and Ubuntu.
Theory: BFS
Breadth-first search is a graph traversal algorithm that starts traversing the graph from the
root node and explores all the neighboring nodes. Then, it selects the nearest node and
explores all the unexplored nodes. While using BFS for traversal, any node in the graph can
be considered as the root node.

There are many ways to traverse the graph, but among them, BFS is the most commonly
used approach. It is a recursive algorithm to search all the vertices of a tree or graph data
structure. BFS puts every vertex of the graph into two categories - visited and non-visited.
It selects a single node in a graph and, after that, visits all the nodes adjacent to the
selected node.

Algorithm

The steps involved in the BFS algorithm to explore a graph are given as follows -

Step 1: SET STATUS = 1 (ready state) for each node in G


Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)

Step 3: Repeat Steps 4 and 5 until QUEUE is empty

Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).

Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS =

1) and set their STATUS = 2

(waiting state)

[END OF LOOP]

Step 6: EXIT

Example of BFS algorithm


In the example given below, there is a directed graph having 7 vertices.

In the above graph, minimum path 'P' can be found by using the BFS that will start from
Node A and end at Node E. The algorithm uses two queues, namely QUEUE1 and QUEUE2.
QUEUE1 holds all the nodes that are to be processed, while QUEUE2 holds all the nodes
that are processed and deleted from QUEUE1.

Now, let's start examining the graph starting from Node A.


Step 1 - First, add A to queue1 and NULL to queue2.
QUEUE1 = {A}
QUEUE2 = {NULL}
Step 2 - Now, delete node A from queue1 and add it into queue2. Insert all neighbors
of node A toqueue1.

QUEUE1 = {B, D}

QUEUE2 = {A}

Step 3 - Now, delete node B from queue1 and add it into queue2. Insert all neighbors
of node B toqueue1.
QUEUE1 = {D, C, F}
QUEUE2 = {A, B}
Step 4 - Now, delete node D from queue1 and add it into queue2. Insert all neighbors
of node D toqueue1. The only neighbor of Node D is F since it is already inserted, so it will
not be inserted again.

QUEUE1 = {C, F}

QUEUE2 = {A, B, D}

Step 5 - Delete node C from queue1 and add it into queue2. Insert all neighbors of node C to
queue1.

QUEUE1 = {F, G}

QUEUE2 = {A, B, D, C}

Step 6 - Delete node F from queue1 and add it into queue2. Insert all neighbors of node F
to queue1. Since all the neighbors of node F are already present, we will not insert them
again.

QUEUE1 = {E, G}

QUEUE2 = {A, B, D, C, F}

Step 7 - Delete node E from queue1. Since all of its neighbors have already been added, so
we will not insert them again. Now, all the nodes are visited, and the target node E is
encountered into queue2.

QUEUE1 = {G}

QUEUE2 = {A, B, D, C, F, E}
Complexity of BFS algorithm

Time complexity of BFS depends upon the data structure used to represent the graph. The
time complexity of BFS algorithm is O(V+E), since in the worst case, BFS algorithm explores
every node and edge. In a graph, the number of vertices is O(V), whereas the number of
edges is O(E).

The space complexity of BFS can be expressed as O(V), where V is the number of vertices.
Applications of BFS algorithm

The applications of breadth-first-algorithm are given as follows -

o BFS can be used to find the neighboring locations from a given source location.

o In a peer-to-peer network, BFS algorithm can be used as a traversal method to find all
the neighboring nodes. Most torrent clients, such as BitTorrent, uTorrent, etc. employ
this process to find "seeds" and "peers" in the network.

o BFS can be used in web crawlers to create web page indexes. It is one of the main
algorithms that can be used to index web pages. It starts traversing from the source
page and follows the links associated with the page. Here, every web page is considered
as a node in the graph.

o BFS is used to determine the shortest path and minimum spanning tree.

o BFS is also used in Cheney's technique to duplicate the garbage collection.

o It can be used in ford-Fulkerson method to compute the maximum flow in a flow network.

DFS
It is a recursive algorithm to search all the vertices of a tree data structure or a graph. The
depth-first search (DFS) algorithm starts with the initial node of graph G and goes deeper
until we find the goal nodeor the node with no children.

Because of the recursive nature, stack data structure can be used to implement the DFS
algorithm. The process of implementing the DFS is similar to the BFS algorithm.

The step by step process to implement the DFS traversal is given as follows -

1. First, create a stack with the total number of vertices in the graph.

2. Now, choose any vertex as the starting point of traversal, and push that vertex into the stack.

3. After that, push a non-visited vertex (adjacent to the vertex on the top of the stack)
to the top ofthe stack.
4. Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex on the stack's top.

5. If no vertex is left, go back and pop a vertex from the stack.

6. Repeat steps 2, 3, and 4 until the stack is empty.


Algorithm
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
Step 3: Repeat Steps 4 and 5 until STACK is empty
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
Step 5: Push on the stack all the neighbors of N that are in the ready state (whose STATUS
= 1) and set their STATUS = 2 (waiting state)

[END OF LOOP]
Step 6: EXIT
Example of DFS algorithm

In the example given below, there is a directed graph having 7 vertices.

Now, let's start examining the graph starting from Node H.

Step 1 - First, push H onto the stack.


STACK: H
Step 2 - POP the top element from the stack, i.e., H, and print it. Now, PUSH all the
neighbors of H ontothe stack that are in ready state.
Print: H

STACK: A
Step 3 - POP the top element from the stack, i.e., A, and print it. Now, PUSH all the
neighbors of A ontothe stack that are in ready state.

Print: A

STACK: B, D

Step 4 - POP the top element from the stack, i.e., D, and print it. Now, PUSH all the
neighbors of D ontothe stack that are in ready state.

Print: D

STACK: B, F

Step 5 - POP the top element from the stack, i.e., F, and print it. Now, PUSH all the
neighbors of F ontothe stack that are in ready state.

Print: F

STACK: B

Step 6 - POP the top element from the stack, i.e., B, and print it. Now, PUSH all the
neighbors of B ontothe stack that are in ready state.

Print: B

STACK: C

Step 7 - POP the top element from the stack, i.e., C, and print it. Now, PUSH all the
neighbors of C ontothe stack that are in ready state.

Print: C

STACK: E, G

Step 8 - POP the top element from the stack, i.e., G and PUSH all the neighbors of G onto
the stack thatare in ready state.

Print: G
STACK: E

Step 9 - POP the top element from the stack, i.e., E and PUSH all the neighbors of E onto
the stack thatare in ready state.

Print: E

STACK:
Now, all the graph nodes have been traversed, and the stack is empty.

Complexity of Depth-first search algorithm

The time complexity of the DFS algorithm is O(V+E), where V is the number of vertices
and E is thenumber of edges in the graph.

The space complexity of the DFS algorithm is O(V).

Applications of DFS algorithm

The applications of using the DFS algorithm are given as follows -

o DFS algorithm can be used to implement the topological sorting.

o It can be used to find the paths between two vertices.

o It can also be used to detect cycles in the graph.


o DFS algorithm is also used for one solution puzzles.

o DFS is used to determine if a graph is bipartite or not.


Questions:
Q 1: Differentiate between DFS and BFS Algorithms.
Q 2: Write down the Time and Space Complexity of DFS and BFS.
Q 3: Which data structure is used by DFS and BFS?
Conclusion:
In This way we have studied uninformed search strategy, how to 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.

You might also like