Which approach is best Bfs or Dfs or Disjoint set for finding all disconnected graphs - depth-first-search

Which approach should we use to find all disconnected graphs and why ?
As BFS and DFS traversal both are traversal methods and by multiple traversals. We can find all disconnected components.
And another approach can be Disjoint Sets as used in kruskal (MST) to find disconnected components.

Simply because you stated that you need to find all disconnected graphs, I would recommend using BFS as it is complete, depth-first search is not. When applied to infinite graphs represented implicitly, BFS will find the goal state, in your scenario, will find all disconnected graphs. DFS, on the other hand, may get lost in parts of the graph that contain no goal state, and essentially, may get lost.

Related

Detect Disconnected Subtours in Graph

I have used an Integer Linear Program (ILP) to generate a path from source to sink in a graph. Each variable Pij in the solution represents a transition from node i to j in the path. Unfortunately, the result is a collection of 2 or more disconnected directed subtours, that do not have a transition from one to the other, thereby making the path useless.
There are ways to prevent subtours from happening, but they first require the detection of such disconnected subtours in primitive solution. Now here's my question: how do I detect disconnected subtours?
Some features of the problem:
I have the primitive solution in the form of both, an adjacency matrix and adjacency list
The source and sink vertices are distinct. No looping back to source
Any vertex in the graph may be traversed any number of times. No rule that it should be traversed only once
My thoughts:
If there are disconnected subtours, then they are actually acting as independent, directed graphs on their own, and hence the problem can be restated as detecting all graphs within an adjacency matrix/list
My first instict was to detect all Strongly Connected Components (SCC's) in the graph, but I retracted after realising that Kosaraju's and other such algorithms are infeasible over disconnected subtours. I can apply such algorithms within each subtour, though.
What could solve the problem (According to me):
Modification of existing SCC detection algorithms to operate on disconnected graphs
Adaptation of graph traversal algorithms to operate on disconnected graphs
Any existing method (of course)
Experience of anyone to have faced a similar issue.

How to traverse all nodes of a graph using DFS algorithm if the graph is not connected

What's the algorithm to traverse all the nodes of a graph if the graph is not connected? An explanation would be helpful! Thanks
One common strategy is to do something like this:
for each node in the graph:
if we haven't yet explored it:
run a DFS starting at that node
The idea is that each DFS will visit some cluster of nodes (a full connected component if the graph is undirected; the strongly connected component and all descendant SCCs if the graph is directed). The outer loop ensures that each node is eventually processed. You'll see this used in, for example, Kosaraju's algorithm for strongly connected components or a DFS-based topological sort.
This, of course, assumes the graph is given to you in a way where you can easily iterate over all nodes. If the graph is defined implicitly, then the process of generating all the nodes may be interesting in and of itself.

Breadth First Search(BFS) and Depth First Search(DFS)

Came across this piece of information on an online course for algorithms:
BFS is used to find connected components for a undirected graph whereas DFS is used to find connected components for a directed graph.
Can i do the opposite here and if I do what would be the drawbacks on performance?
This is not the main difference between DFS and BFS. Both of them can be applied on either directed graph or undirected graph. Usually DFS consumes much lower memory than BFS, because BFS has to store all child pointers at each level of the search tree. But DFS can store the queue (only one path) in the stack.
Usually DFS is faster than BFS, has less complexity of space, and is easy to be implemented. But in some problems (like finding shortest path), DFS is not as useful or efficient as BFS.
From the aspect of the search tree, BFS and DFS are actually the same algorithm but with different data structure.

What's the purpose of BFS and DFS?

I've learned how these algorithms work, but what are they used for?
Do we use them to:
find a certain node in a graph or
to find a shortest path or
to find a cycle in a graph
?
Both of them just visit all the nodes and mark them visited, and I don't see the point of doing that.
I am sort of lost here what I am learning.
BFS and DFS are graph search algorithms that can be used for a variety of different purposes.
One common application of the two search techniques is to identify all nodes that are reachable from a given starting node. For example, suppose that you have a collection of computers that each are networked to a handful of other computers. By running a BFS or DFS from a given node, you will discover all other computers in the network that the original computer is capable of directly or indirectly talking to. These are the computers that come back marked.
BFS specifically can be used to find the shortest path between two nodes in an unweighted graph. Suppose, for example, that you want to send a packet from one computer in a network to another, and that the computers aren't directly connected to one another. Along what route should you send the packet to get it to arrive at the destination as quickly as possible? If you run a BFS and at each iteration have each node store a pointer to its "parent" node, you will end up finding route from the start node to each other node in the graph that minimizes the number of links that have to be traversed to reach the destination computer.
DFS is often used as a subroutine in more complex algorithms. For example, Tarjan's algorithm for computing strongly-connected components is based on depth-first search. Many optimizing compiler techniques run a DFS over an appropriately-constructed graph in order to determine in which order to apply a specific series of operations. Depth-first search can also be used in maze generation: by taking a grid of nodes and linking each node to its neighbors, you can construct a graph representing a grid. Running a random depth-first search over this graph then produces a maze that has exactly one solution.
This is by no means an exhaustive list. These algorithms have all sorts of applications, and as you start to explore more advanced algorithms you will often find yourself relying on DFS and BFS as building blocks. It's similar to sorting - sorting by itself isn't all that interesting, but being able to sort a list of values is enormously useful as a subroutine in more complex algorithms.
Hope this helps!

Should I use BFS, DFS for tree traversal or in-order, post -order, pre-order?

This question may be simple for experts but for a beginner like me it is important. My question is are there any problems involving tree traversals that can be solved by BFS , DFS and not in-order, pre-order etc. In other words, whenever i see a tree problem, should I ONLY think of the 3 tree traversal methods, or also consider BFS,DFS
Pre-order, in-order and post-order traversal are the three different kinds of depth first search that are possible. So it's not a question of whether to use DFS or one of those three. If you are using one of those three traversals, you are using DFS.
As for whether there are cases where BFS is preferable over DFS: Yes, there are. For example to find the shortest path between two nodes in an unweighted graph, you can use BFS because the first path found by a BFS happens to be the one with the fewest edges. The same is not true for DFS.
An obvious example where DFS doesn't work and you have to use BFS is an infinitely (or at least arbitrarily) high tree.

Resources