5
$\begingroup$

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!

$\endgroup$
6
$\begingroup$
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 = SparseArray`ExpressionToTree[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]

enter image description here

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

enter image description here

$\endgroup$
  • $\begingroup$ This is working good enough! The only tiny change I did was on the edges variable: edges = UndirectedEdge@@@(ett[[All, All, 1]]); $\endgroup$ – xtian777x Jul 25 '14 at 20:56
  • $\begingroup$ @xtian777x, i updated the answer with your suggestion to get undirected edges. $\endgroup$ – kglr Jul 26 '14 at 1:13
  • $\begingroup$ 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) $\endgroup$ – xtian777x Jul 27 '14 at 0:18
1
$\begingroup$

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

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

enter image description here

Check that the output is a Graph:

Head @ tree

Graph

$\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.