# Agglomerate output to Graph

I'm using the HierarchicalClustering package to cluster some data. So let's say I have this:

Needs["HierarchicalClustering"]

Agglomerate[{1, 1, 2, 10, 4, 8}, Linkage -> "Single"]

(*
Cluster[
Cluster[Cluster[Cluster[1, 1, 0, 1, 1], 2, 1, 2, 1], 4, 4, 3, 1],
Cluster[8, 10, 4, 1, 1],
16, 4, 2
]
*)


Is there a direct way to go from that output to an undirected graph structure?

The HierarchicalClustering package can create graphs as Dendrograms:

DendrogramPlot[{1, 1, 2, 10, 4, 8}, LeafLabels -> (# &),  Linkage -> "Single"]


But I would like to use the graph functions to work with the data. Maybe I'm doing this wrong and I don't even need the HierarchicalClustering to cluster my data.

I would appreciate any ideas.

Thanx!

data = {1, 1, 2, 10, 4, 8};
leafverts = Table[Symbol["leaf" <> ToString[i]], {i, Length@data}];
Needs["HierarchicalClustering"]
clusters = Agglomerate[{1, 1, 2, 10, 4, 8} -> leafverts, Linkage -> "Single"];


Re-using this answer to transform expressions to trees:

expr = (i = 1; Replace[clusters, Cluster[a_, b_, ___] :>
Symbol["cluster" <> ToString[i++]][a, b], {0, Infinity}]);
ett = SparseArrayExpressionToTree[expr];
edges = ett[[All, All, 1]];
options = {VertexLabels -> Placed["Name", {Center, Center}],
VertexShapeFunction -> "Rectangle", VertexSize -> .6,
VertexLabelStyle -> Directive[Red, Italic, 14], ImagePadding -> 20,
ImageSize -> 400,
GraphLayout -> {"LayeredEmbedding",  "RootVertex" -> edges[[1, 1]]}};

Graph[edges, options]


Update: Several ways to get undirected edges:

Graph[UndirectedEdge @@@ edges, options] (* thanks: xtian777x *)


or

Graph[edges, AppendTo[options, EdgeShapeFunction -> "Line"]]


or

Graph[edges, AppendTo[options, EdgeStyle -> Arrowheads[0]]]


all give

• This is working good enough! The only tiny change I did was on the edges variable: edges = UndirectedEdge@@@(ett[[All, All, 1]]); Commented Jul 25, 2014 at 20:56
• @xtian777x, i updated the answer with your suggestion to get undirected edges.
– kglr
Commented Jul 26, 2014 at 1:13
• Cool, thanx! One question...why the leaves in the edges variable turn out as HoldForm of the original expression? (You can see that if you do edges//FullForm) Commented Jul 27, 2014 at 0:18

You can use ClusteringTree (introduced in M10.4) to do this:

tree = ClusteringTree[{1, 1, 2, 10, 4, 8}]


Check that the output is a Graph:

Head @ tree
`

Graph