# Network Graph: show highly connected components

I want to create a network graph without the small subgraphs which are disconnected from the big network graph.

Suppose I want to visualize the edgelist:

edgelist = {"A" -> "B", "A" -> "c", "B" -> "C",  "B" -> "D",
"D" -> "C",  "E" -> "F",  "G" -> "H"};

The network looks like

The desired output is:

As jou see, in the desired output the edges {"E" -> "F"} and {"G" -> "H"} are deleted. How can I do that automatically?

• Look up ConnectedComponents. Commented Jul 9, 2015 at 15:08

u = UndirectedEdge @@@ edgelist;
g = First@SortBy[ConnectedComponents@Graph@u, -Length@# &]
Subgraph[Graph@edgelist, g]

Selecting the subgraph containing a specific node:

Subgraph[
Graph[edgelist],
VertexComponent[Graph[UndirectedEdge @@@ edgelist], {"A"}]
]

• Why "A"? mmmm.. Perhaps we understood the question in a different way Commented Jul 10, 2015 at 1:11
• @belisarius It's not clear how automatic the solution needs to be, so I decided to provide this semiautomatic alternative. If the graph can be visualized with node labels and the goal is to keep just some of the clusters, it can be done using this solution. F.e. all clusters can be selected using {"A", "E", "G"}, or the two small ones using {"E", "G"}. The nodes are arbitrary, one from each cluster that is to be kept. Commented Jul 10, 2015 at 1:32
• Ah,ok. Thanks for the explanation Commented Jul 10, 2015 at 1:35

First@WeaklyConnectedGraphComponents[edgelist]

WeaklyConnectedGraphComponents orders components by size, even if the input graph is directed. ConnectedGraphComponents orders by size only if the graph is undirected.

g =  UndirectedGraph[edgelist];

## ConnectedComponents

largecomponent = First @ ConnectedComponents[g]

{"A","B","c","C","D"}

Subgraph[g, largecomponent ,
VertexLabels -> Placed["Name", Center], VertexSize -> Medium,
GraphLayout -> "LayeredDigraphEmbedding"]

## ConnectedGraphComponents

You can also use ConnectedGraphComponents to get the subgraphs rather than the vertex lists corresponding to different components:

First[ConnectedGraphComponents[g]]

SetProperty[%, {VertexLabels -> Placed["Name", Center],
VertexSize -> Medium, GraphLayout -> "LayeredDigraphEmbedding"}]

## KCoreComponents

First[KCoreComponents[edgelist, 1]] (* or *)
First[KCoreComponents[g, 1]]

{"A", "B", "c", "C", "D"}