Hasse Diagrams in Mathematica using an arbitrary relation
I'm not sure if I understand your question. I'm trying to answer:
Does mathematica have some object to draw a Hasse Diagram from DirectedEdges or adjacency matrices
So, let's draw a Hasse Diagram starting from its adjacency matrix:
<< Combinatorica`;
am = {{1, 1, 1, 1, 1, 1, 1}, {0, 1, 1, 0, 1, 1, 1}, {0, 0, 1, 0, 1, 0, 0},
{0, 0, 0, 1, 0, 1, 0}, {0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 1}};
g = FromAdjacencyMatrix[am, Type -> Directed];
h = HasseDiagram[SetVertexLabels[g, CharacterRange["a", "g"]]];
ShowGraph[h, BaseStyle -> {FontSize -> 18}]
The key function used in Combinatorica`HasseDiagram
is the transitive reduction function TR
.
TR = Compile[{{closure, _Integer, 2}},
Module[{reduction = closure, n = Length[closure], i, j, k},
Do[If[reduction[[i, j]] != 0 && reduction[[j, k]] != 0 &&
reduction[[i, k]] != 0 && (i != j) && (j != k) && (i != k), reduction[[i, k]] = 0],
{i, n}, {j, n}, {k, n}];
reduction]]
You can use the Combinatorica
function TR
on your adjacency matrix to get its transitive reduction and use the resulting matrix with AdjacencyGraph
.
The function trF
below is an alternative implementation of TR
.
ClearAll[trF];
trF = Module[{r = #, m = Length@#},
Table[r[[i, k]] = r[[i, k]] (1 - Times @@ Unitize[{r[[i, j]], r[[j, k]], r[[i, k]],
i - j, j - k, i - k}]), {i, m}, {j, m}, {k, m}]; r] &;
Using the example matrix in belisarius's answer:
am = {{1, 1, 1, 1, 1, 1, 1}, {0, 1, 1, 0, 1, 1, 1}, {0, 0, 1, 0, 1, 0,
0}, {0, 0, 0, 1, 0, 1, 0}, {0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0,
1, 0}, {0, 0, 0, 0, 0, 0, 1}};
tram0 = TR[am - IdentityMatrix[Length@am]];
tram = trF[am - IdentityMatrix[Length@am]];
tram == tram0;
(* True *)
The Hasse Diagram
can be obtained using AdjacencyGraph[tram]
:
options = {VertexLabels -> "Name", ImagePadding -> 20,
DirectedEdges -> False, ImageSize -> 300};
agam = AdjacencyGraph[CharacterRange["a", "g"], am, options];
agtram = AdjacencyGraph[CharacterRange["a", "g"], tram, options,
GraphLayout -> {"LayeredEmbedding", "RootVertex" -> "a", "Orientation" -> Bottom}];
{{"am", "trF[am]"}, {am // MatrixForm, tram2 // MatrixForm}, {agam, agtram}} // Grid
OP's example:
am2 = {{1, 0, 1, 1, 0, 0}, {0, 1, 0, 1, 0, 0}, {0, 0, 1, 0, 0, 0},
{0, 0, 0, 1, 0, 0}, {1, 1, 1, 1, 1, 0}, {0, 1, 0, 1, 0, 1}};
tram2 = trF[am2 - IdentityMatrix[Length@am2]];
agam2 = AdjacencyGraph[CharacterRange["a", "f"], am2, options];
agtram2 = AdjacencyGraph[CharacterRange["a", "f"], tram2, options,
GraphLayout -> {"LayeredEmbedding", "RootVertex" -> "a", "Orientation" -> Bottom}];
{{"am2", "trF(am2)"}, {am2 // MatrixForm, tram2 // MatrixForm }, {agam2, agtram2}} // Grid