DAA: Application of DFS and BFS

Application of DFS and BFS

Depth-first search and breadth-first searches are the most famous algorithms used in daily life and the programming world. Let us now explore each application in which these algorithms are used as a building block:

DFS Applications

  1. DFS of a graph produces the minimum spanning tree and pair of all shortest-path trees.
  2. Detects a cycle in a graph; A graph contains cycle if there exists a back edge for a node. This can be done with a depth-first search algorithm because we when we can maintain the backtrack in DFS, we can maintain the back edge.
  3. To find the path between cities: The DFS algorithm finds the path between two vertices(u and v) by the following method:
  • U is the initial vertex.
  • Maintain a stack to count the current vertex from the start.
  • When we reach v, pop the stack, which will be the path from u to z.

    4)  Topological sorting: In this sorting, we schedule jobs from the given dependencies among jobs. In computer science, applications of this type arise in instruction scheduling, ordering formula cell evaluation when recomputing formula values in spreadsheets, logic synthesis, determining the order of compilation tasks to perform in makefiles, data serialization, and resolving symbol dependencies in linkers.

5) DFS algorithm also checks if a graph is bipartite or not.

6)  Using DFS, we can find the strongly connected components in a graph. Strongly connected components are the ones that are connected to every node in the graph.

7) We can solve maze puzzles with the DFS algorithm’s help as it can be implemented to find all possible paths to a maze end by only including nodes on the visited set’s current path.

8) The bridges in a graph can be found using DFS.

9) It is also used in planarity testing and finding biconnectivity in graphs.

10) It is also used to generate words in order to plot the limit set of a group.

BFS Applications

  1.  Shortest Path and MST for unweighted graph: The path with the least number of edges is the shortest path. With the help of BFS, we can reach a vertex from a given source using the minimum number of edges. We can also use DFS or BFS to find a spanning tree in the unweighted graph.
  2. To find all neighbor nodes BFS can be a great help. It is useful in finding the peer to peer networks. Example - BitTorrent
  3. The crawlers in the search engine use the BFS approach. The idea used behind it is to start from the source page and repeat it. DFS can also be used for crawlers, but it has some limits.
  4. DFS is also used in social networking websites where we see a mutual friend in the friend list, and we can search anyone via mutual connections.
  5. BFS is also helpful in navigation as it is easier to find the neighboring locations on maps.
  6. In broadcasting, a packet uses the BFS algorithm to reach all the destinations.
  7. It is also used in Chenney’s algorithm in garbage collection. Due to better locality of reference, we use BFS over DFS.
  8. The cycle detection can be done using BFS or DFS. For weighted graphs, BFS helps in cycle detection.
  9. The BFS algorithm is beneficial in the ford-Fulkerson algorithm to find the maximum flow because it reduces O’s time complexity(VE^2).
  10. The BFS algorithm also checks the bipartiteness of a graph.
  11. To find all the reachable nodes from a given node BFS is much beneficial.

Related Topics

Segregate the given Linked List in DAA

Segregate Even and Odd Nodes in a Linked List A linked list is a linear data structure in which each node has two blocks. One contains the node’s value or data,...

3 minutes read.

DAA: Breadth First Search (BFS) for a Graph

Breadth First Search (Bfs) For A Graph The algorithm in which all the graph nodes are traversed is known as the breadth-first search algorithm. In this algorithm, we select one node,...

5 minutes read.

DAA: KMP Algorithm

KMP ALGORITHM The KMP algorithm is abbreviated as the "Knuth Morris Pratt” algorithm. This algorithm was developed by all of them.  This algorithm searches a pattern of length m in a string...

10 minutes read.

DAA: Find the Height or Maximum Depth of a Binary Tree

Find the Height or Maximum Depth of a Binary Tree We have a binary tree structure and we need to find its height. It is defined by the distance from the...

3 minutes read.

DAA: Insert a node in Binary Search Tree

Insert a node in Binary Search Tree (BST) We have a Binary search tree and a key. Insert the key in the binary search tree if not present. In the above figure,...

4 minutes read.

DAA: Algorithm to Find the Maximum Width of a Tree

Algorithm to Find the Maximum Width of a Tree The width of a binary tree is defined as the maximum number of nodes at a given level. The level having the...

5 minutes read.

DAA: Depth-First Search Algorithm

Depth-first search: DFS is a traversing algorithm of a graph or tree in which one node is taken as arbitrary, and with the help of that arbitrary node, all its...

6 minutes read.

DAA: Bead Sort Algorithm

Bead Sort Algorithm The bead sort is also known as the gravity sort algorithm. The algorithm is based on the natural phenomena of gravity. The phenomenon is the falling of things...

3 minutes read.

DAA: Expression Trees

Expression Trees Expression trees are those in which the leaf nodes have the values to be operated, and internal nodes contain the operator on which the leaf node will be performed. Example:...

4 minutes read.

DAA: Floyd Cycle Detection

Floyd Cycle Detection Floyd Cycle algorithm is one of the cycle detection algorithms to detect the cycle in a given singly linked list. In the Floyd Cycle algorithm, we have two pointers...

4 minutes read.

DAA: Interpolation Search Algorithm

Interpolation Search Algorithm There is no doubt that binary search is a great algorithm with average time complexity of log n. The feature of discarding one half of the array reduces...

4 minutes read.

DAA: Bubble Sort Algorithm on Linked List

Bubble Sort Algorithm on Linked List In this article, we will sort a Link List using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The bubble sort technique...

4 minutes read.

Symmetric Trees in DAA

Symmetric Trees The trees that are mirror images of themselves are known as symmetric trees. Look at the following tree image below: The tree is symmetric as the left subtree is the mirror...

4 minutes read.

DAA: Construct a Tree from Inorder and Preorder Traversals

Construct a Tree from Inorder and Preorder Traversals We are given inorder and preorder traversals of a tree. We need to generate a tree from these traversals. Example: Inorder[]   = { 3, 1,...

4 minutes read.

DAA: Application of DFS and BFS

Application of DFS and BFS Depth-first search and breadth-first searches are the most famous algorithms used in daily life and the programming world. Let us now explore each application in which...

3 minutes read.

DAA: Density of a Binary Tree Algorithm

The Density of a Binary Tree Algorithm The density of a binary tree is defined as the ratio of the tree’s size to the tree’s height.  The height of the tree is...

2 minutes read.

DAA: Euclid Algorithm

Euclid Algorithm The Euclid algorithm finds the GCD of two numbers in the efficient time complexity. To find the GCD of two numbers, we take the two numbers’ common factors and multiply...

8 minutes read.

DAA: Dynamic Programming

Dynamic Programming Introduction The technique of breaking a problem statement into subproblems and using the optimal result of subproblems as an optimal result of the problem statement is known as dynamic programming....

2 minutes read.

Recurrence relation in DAA

Recurrence relation in DAA The model that uses mathematical concepts to calculate the time complexity of an algorithm is known as the recurrence relational model. A recursive relation, T(n), is a recursive...

5 minutes read.

DAA: Dijkstra’s Algorithm (Shortest Path)

Dijkstra’s Algorithm (Shortest Path) Dijkstra’s algorithm finds the shortest distance from a source to all the vertices in a graph. This algorithm is used in network protocols like IS-IS and OSPF(Open...

3 minutes read.