Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DFS on disconnected Graphs

How does DFS(G,v) behaves for disconnected graphs ?

Suppose a graph has 3 connected components and DFS is applied on one of these 3 Connected components, then do we visit every component or just the on whose vertex DFS is applied.

Means Is it correct to say that

DFS on a graph having many components covers only 1 component.

I also tried online DFS visualization tools for disconnected graphs and they also support that it covers only 1 component. But still I want to confirm

  • https://www.cs.usfca.edu/~galles/visualization/DFS.html
  • https://visualgo.net/dfsbfs
like image 760
mcjoshi Avatar asked Jan 25 '17 05:01

mcjoshi


People also ask

Does DFS work for disconnected graph?

In fact, DFS is often used to determine whether or not a graph is disconnected or not - if we run DFS and do not reach all of the nodes in the graph, the graph must be disconnected.

Which algorithm is used for disconnected graphs?

Kruskal Algorithm on disconnected graph - Codeforces.

Does BFS work on disconnected graphs?

Disconnected graph is a Graph in which one or more nodes are not the endpoints of the graph i.e. they are not connected. Now, the Simple BFS is applicable only when the graph is connected i.e. all vertices of the graph are accessible from one node of the graph.

Is DFS only for directed graphs?

Depth First Search (DFS) is a systematic way of visiting the nodes of either a directed or an undirected graph. As with breadth first search, DFS has a lot of applications in many problems in Graph Theory. It comprises the main part of many graph algorithms. DFS visits the vertices of a graph in the following manner.


1 Answers

Starting a search on a single component of a disconnected graph will search only that component; how could it be otherwise? There's no information to make a decision about when, how or where to move the search to a different component.

If you want to perform a complete search over a disconnected graph, you have two high level options:

  • Spin up a separate search of each component, then add some logic to make a choice among multiple results (if necessary). This has the advantage of easy partitioning logic for running searches in parallel.
  • Add logic to indicate how to combine the components into a "single" graph. Two ideas for this:
    • Add a dummy root node, and have the components be its children. This would be a neat workaround for tools which only cover one component, but you want to work with all three at once. It also means that if you're searching for a single best result, you're guaranteed to get the global best without any additional checks.
    • Put your components in a list and add logic that jumps to the next one when the search on the current completes. Add additional logic to compare multiple potential search results, if necessary.

Note that the same reasoning applies to breadth first searches as well.

like image 195
Esoteric Screen Name Avatar answered Oct 11 '22 02:10

Esoteric Screen Name