Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding all disconnected subgraphs in a graph

I have a graph which contains an unknown number of disconnected subgraphs. What's a good algorithm (or Java library) to find them all?

like image 241
Ollie Glass Avatar asked Aug 28 '09 19:08

Ollie Glass


People also ask

How do you find disconnected components on a graph?

A graph is disconnected if at least two vertices of the graph are not connected by a path. If a graph G is disconnected, then every maximal connected subgraph of G is called a connected component of the graph G.

Can Subgraphs be disconnected?

A graph G that is not connected has two or more connected components that are disjoint and have G as their union. Removing a cut vertex from a connected graph produces a subgraph that is disconnected.

How do you find a subgraph on a graph?

A subgraph G′ = (V′, E′) of G is a graph with V′ ⊆ V and E ⊆ E1, where E1 is a subset of E, whose edges connect vertices that lie in V′. Clearly, G is a subgraph of itself. A subgraph G′ = (V′, E′) is connected if there exists at least one path connecting any pair of vertices in V′ (Figure 13.5c).


1 Answers

I think what you are looking for is generally called a Flood Fill. It is up to you whether you traverse the graph through a BFS or a DFS.

Basically you take an unlabeled (AKA uncoloured) node and assign a new label to it. You assign the same label to all nodes adjacent to that one, and so on to all nodes that are reachable from that node.

When no more reachable nodes can be labeled, you start over by picking another unlabeled node. Notice that the fact that this new node is unlabeled implies that it is not reachable from our earlier node and is thus in a different disconnected component.

When there are no more unlabeled nodes, the number of distinct labels you had to use is the number of components of the graph. The label for each node tells you which node is part of which component.

like image 134
MAK Avatar answered Oct 25 '22 16:10

MAK