4
$\begingroup$

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

enter image description here

The desired output is:

enter image description here

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

$\endgroup$
  • 1
    $\begingroup$ Look up ConnectedComponents. $\endgroup$ – Szabolcs Jul 9 '15 at 15:08
4
$\begingroup$
u = UndirectedEdge @@@ edgelist;
g = First@SortBy[ConnectedComponents@Graph@u, -Length@# &]
Subgraph[Graph@edgelist, g]

Mathematica graphics

$\endgroup$
4
$\begingroup$

Selecting the subgraph containing a specific node:

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

Mathematica graphics

$\endgroup$
  • 1
    $\begingroup$ Why "A"? mmmm.. Perhaps we understood the question in a different way $\endgroup$ – Dr. belisarius Jul 10 '15 at 1:11
  • $\begingroup$ @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. $\endgroup$ – C. E. Jul 10 '15 at 1:32
  • $\begingroup$ Ah,ok. Thanks for the explanation $\endgroup$ – Dr. belisarius Jul 10 '15 at 1:35
3
$\begingroup$

In recent versions, probably the shortest way is:

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.

$\endgroup$
2
$\begingroup$
g =  UndirectedGraph[edgelist];

ConnectedComponents

largecomponent = First @ ConnectedComponents[g]

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

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

enter image description here

ConnectedGraphComponents

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

First[ConnectedGraphComponents[g]]

enter image description here

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

enter image description here

KCoreComponents

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

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.