0% found this document useful (0 votes)
22 views

Assignment 01

The document discusses depth first search and breadth first search algorithms for traversing graph and tree data structures. It provides details on the theory, advantages, and disadvantages of DFS and BFS. Example applications of each algorithm are also given.

Uploaded by

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

Assignment 01

The document discusses depth first search and breadth first search algorithms for traversing graph and tree data structures. It provides details on the theory, advantages, and disadvantages of DFS and BFS. Example applications of each algorithm are also given.

Uploaded by

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

Assignment 01

Aim: Implement depth first search & breadth first search and breadth first search. Use an
undirected graph and develops recursive algorithm for searching all the vertices of a graph or
tree data structure
Software Requirements; Anaconda/ Python
Operating System: 64-bit Linux
Theory:
Depth First Search
Depth First Search for a graph is similar to DFS of a tree. The only catch here is that unlike
trees, graphs may contain cycles to avoid processing a more than once, we use Boolean
visited array.
Input: n =4, e = 6
Output: DFS from vertex 1: 1 2 0 3
DFS is an algorithm for traversing or searching tree or graph data structure. The algorithm
starts from root node and explores as far as possible along each branch before backtracking.
So, basic idea is to start from root or arbitrary node and mark the node and move adjacent
unmarked node and continue this loop unite there is no unmarked adjacent node then
backtrack and check for other unmarked nodes and traverse them.

Advantages of Depth First Search

 Memory requirement is only linear with respect to the search graph. This is in contrast
with breadth-first search which requires more space. The reason is that the algorithm
only needs to store a stack of nodes on the path from the root to the current node.
 The time complexity of a depth-first Search to depth d and branching factor b (the
number of children at each node, the outdegree) is O(bd) since it generates the same
set of nodes as breadth-first search, but simply in a different order. Thus practically
depth-first search is time-limited rather than space-limited.
 If depth-first search finds solution without exploring much in a path then the time and
space it takes will be very less.
 DFS requires less memory since only the nodes on the current path are stored. By
chance DFS may find a solution without examining much of the search space at all.

Disadvantages of Depth First Search

 The disadvantage of Depth-First Search is that there is a possibility that it may down
the left-most path forever. Even a finite graph can generate an infinite solution to this
problem is to impose a cutoff depth on the search. Although ideal cutoff is the
solution depth d and this value is rarely known in advance of actually solving the
problem. If the chosen cutoff depth is less than d, the algorithm will fail to find a
solution, whereas if the cutoff depth is greater than d, a large price is paid in execution
time, and the first solution found may not be an optimal one.
 Depth-First Search is not guaranteed to find the solution.
 And there is no guarantee to find a minimal solution, if more than one solution.

Applications of DFS

1. Detecting Cycle in a graph.

2. Path finding

3. Topological sorting

4. Finding strongly connected component of a graph

Breadth First Search

Breadth First Search (BFS) algorithm is used to search a tree or graph data structure for a
node that means a set of criteria. It starts at the tree’s root or graph and searches all nodes at
current depth level before moving on the next node

BFS for a graph is similar to the BFS of a tree. The only catch here is that unlike trees, graphs
may contain cycles. So we can return to the same node during traversal. To avoid this, we
divide vertices into visited and not visited

Applications of BFS
1. Shortest path and minimum spanning tree for unweighted graph.
2. Peer-Perr Network.
3. Crawlers in search engines
4. Social networking websites
5. GPS navigation system
6. In garbage collection

Advantages of BFS

 BFS will never get trapped exploring the useful path forever.
 If there is a solution, BFS will definitely find it.
 If there is more than one solution then BFS can find the minimal one that requires less
number of steps.
 Low storage requirement – linear with depth.
 Easily programmable.

Disadvantages of BFS

 The main disadvantage of BFS is memory requirement


 BFS is severely space bound in practice so will exhaust the memory available on
typical computer in matter of minutes.

Conclusion: Thus we can have successfully implemented BFS and DFS with an example.

You might also like