i am trying to graph a sparse matrix with dot language, in fact the node connections are ok, but the problem is the vertical alignment of these nodes, i have tried to use pos and place nodes with x and y values, but just doesn't work (I think because of the default layout). If you can help me a stressed student will thank you. My dot code is bellow and a link of the image generated.
https://github.com/Gualtix/Learn_CPP/blob/master/Matrix.png
Thank you.
digraph Sparce_Matrix {
node [shape=box]
Mt [label = "Matrix" width = 1.5 style = filled, fillcolor = firebrick1];
//(^< ............ ............ ............ ............ ............ U S U A R I O S
U0 [label = "Estructuras" pos = "5.3,3.5!" width = 1.5 style = filled, fillcolor = bisque1];
U1 [label = "Redes" width = 1.5 style = filled, fillcolor = bisque1];
U2 [label = "Compiladores" width = 1.5 style = filled, fillcolor = bisque1];
U3 [label = "Investigacion" width = 1.5 style = filled, fillcolor = bisque1];
U4 [label = "Lenguajes" width = 1.5 style = filled, fillcolor = bisque1];
//(^< ............ Links
U0 -> U1 { constraint = true };
U1 -> U0 { constraint = true };
U1 -> U2 { constraint = true };
U2 -> U1 { constraint = true };
U2 -> U3 { constraint = true };
U3 -> U2 { constraint = true };
U3 -> U4 { constraint = true };
U4 -> U3 { constraint = true };
//(^< ............ ............ ............ ............ ............ A R C H I V O S
A0 [label = "Josefina" width = 1.5 style = filled, fillcolor = lightskyblue];
A1 [label = "Alejandro" width = 1.5 style = filled, fillcolor = lightskyblue];
A2 [label = "Marco" width = 1.5 style = filled, fillcolor = lightskyblue];
A3 [label = "Julian" width = 1.5 style = filled, fillcolor = lightskyblue];
A4 [label = "Pamela" width = 1.5 style = filled, fillcolor = lightskyblue];
//(^< ............ Links
A0 -> A1;
A1 -> A0;
A1 -> A2;
A2 -> A1;
A2 -> A3;
A3 -> A2;
A3 -> A4;
A4 -> A3;
Mt -> U0;
Mt -> A0 { constraint = true };
{ rank = same; Mt; A0; A1; A2; A3; A4; }
//(^< ............ ............ ............ ............ ............ P E R M I S O S
//(^< ............ ............ L E V E L 0
N0_L0 [label = "Jose-Estr" width = 1.5];
N1_L0 [label = "Marc-Estr" width = 1.5];
N2_L0 [label = "Juli-Estr" width = 1.5];
//(^< ............ ............ L E V E L 2
N0_L2 [label = "Marc-Comp" width = 1.5];
N1_L2 [label = "Juli-Comp" width = 1.5];
//(^< ............ ............ L E V E L 4
N0_L4 [label = "Marc-Leng" width = 1.5];
N1_L4 [label = "Juli-Leng" width = 1.5];
N2_L4 [label = "Pame-Leng" width = 1.5];
//(^< ............ ............ ............ ............ ............ L I N K I N G
//(^< ............ ............ L E V E L 0
U0 -> N0_L0;
A0 -> N0_L0;
N0_L0 -> N1_L0;
N1_L0 -> N0_L0;
A2 -> N1_L0;
N1_L0 -> N2_L0;
N2_L0 -> N1_L0;
A3 -> N2_L0;
{ rank = same; U0; N0_L0;N1_L0;N2_L0; }
//(^< ............ ............ L E V E L 2
U2 -> N0_L2;
N0_L2 ->N1_L0;
N1_L0 ->N0_L2;
N0_L2 -> N1_L2;
N1_L2 -> N0_L2;
N1_L2 ->N2_L0;
N2_L0 ->N1_L2;
{ rank = same; U2; N0_L2;N1_L2; }
//(^< ............ ............ L E V E L 4
U4 -> N0_L4;
N0_L4 ->N0_L2;
N0_L2 ->N0_L4;
N0_L4 -> N1_L4;
N1_L4 -> N0_L4;
N1_L4 ->N1_L2;
N1_L2 ->N1_L4;
N1_L4 -> N2_L4;
N2_L4 -> N1_L4;
A4 -> N2_L4;
{ rank = same; U4; N0_L4;N1_L4;N2_L4; }
}
enter code here
The most important tool to get closer to the sparse matrix you want is group
:
group
If the end points of an edge belong to the same group, i.e., have the same group attribute, parameters are set to avoid crossings and keep the edges straight.
As your sparse matrix is very sparse, I needed some empty nodes as well. I have also removed [constraint = true]
as it is the default anyway. The changes I made are as comments in the source code:
digraph Sparce_Matrix {
node [shape=box]
/* add group 1 for vertical alignment */
Mt[ label = "Matrix", width = 1.5, style = filled, fillcolor = firebrick1, group = 1 ];
/* empty nodes, needed to override graphiz' default node placement */
e0[ shape = point, width = 0 ];
e1[ shape = point, width = 0 ];
//(^< ............ ............ ............ ............ ............ U S U A R I O S
/* groups added for vertical alignment */
U0 [label = "Estructuras" pos = "5.3,3.5!" width = 1.5 style = filled, fillcolor = bisque1, group = 1 ];
U1 [label = "Redes" width = 1.5 style = filled, fillcolor = bisque1, group = 1 ];
U2 [label = "Compiladores" width = 1.5 style = filled, fillcolor = bisque1, group = 1 ];
U3 [label = "Investigacion" width = 1.5 style = filled, fillcolor = bisque1, group = 1 ];
U4 [label = "Lenguajes" width = 1.5 style = filled, fillcolor = bisque1, group = 1 ];
//(^< ............ Links
U0 -> U1;
U1 -> U0;
U1 -> U2;
U2 -> U1;
U2 -> U3;
U3 -> U2;
U3 -> U4;
U4 -> U3;
//(^< ............ ............ ............ ............ ............ A R C H I V O S
/* groups 2 to 6 added for vertical alignment */
A0 [label = "Josefina" width = 1.5 style = filled, fillcolor = lightskyblue, group = 2 ];
A1 [label = "Alejandro" width = 1.5 style = filled, fillcolor = lightskyblue, group = 3 ];
A2 [label = "Marco" width = 1.5 style = filled, fillcolor = lightskyblue, group = 4 ];
A3 [label = "Julian" width = 1.5 style = filled, fillcolor = lightskyblue, group = 5 ];
A4 [label = "Pamela" width = 1.5 style = filled, fillcolor = lightskyblue, group = 6 ];
//(^< ............ Links
A0 -> A1;
A1 -> A0;
A1 -> A2;
A2 -> A1;
A2 -> A3;
A3 -> A2;
A3 -> A4;
A4 -> A3;
Mt -> U0;
Mt -> A0;
{ rank = same; Mt; A0; A1; A2; A3; A4; }
//(^< ............ ............ ............ ............ ............ P E R M I S O S
//(^< ............ ............ L E V E L 0
/* groups 2 to 6 added for vertical alignment */
N0_L0 [label = "Jose-Estr" width = 1.5, group = 2 ];
N1_L0 [label = "Marc-Estr" width = 1.5, group = 4 ];
N2_L0 [label = "Juli-Estr" width = 1.5, group = 5 ];
//(^< ............ ............ L E V E L 2
N0_L2 [label = "Marc-Comp" width = 1.5, group = 4 ];
N1_L2 [label = "Juli-Comp" width = 1.5, group = 5 ];
//(^< ............ ............ L E V E L 4
N0_L4 [label = "Marc-Leng" width = 1.5, group = 4 ];
N1_L4 [label = "Juli-Leng" width = 1.5, group = 5 ];
N2_L4 [label = "Pame-Leng" width = 1.5, group = 6 ];
//(^< ............ ............ ............ ............ ............ L I N K I N G
//(^< ............ ............ L E V E L 0
U0 -> N0_L0;
A0 -> N0_L0;
N0_L0 -> N1_L0;
N1_L0 -> N0_L0;
A2 -> N1_L0;
N1_L0 -> N2_L0;
N2_L0 -> N1_L0;
A3 -> N2_L0;
{ rank = same; U0; N0_L0;N1_L0;N2_L0; }
//(^< ............ ............ L E V E L 2
U2 -> N0_L2;
N0_L2 ->N1_L0;
N1_L0 ->N0_L2;
N0_L2 -> N1_L2;
N1_L2 -> N0_L2;
N1_L2 ->N2_L0;
N2_L0 ->N1_L2;
{ rank = same; U2; N0_L2;N1_L2; }
//(^< ............ ............ L E V E L 4
U4 -> N0_L4;
N0_L4 -> N0_L2;
N0_L2 -> N0_L4;
N0_L4 -> N1_L4;
N1_L4 -> N0_L4;
N1_L4 -> N1_L2;
N1_L2 -> N1_L4;
N1_L4 -> N2_L4;
N2_L4 -> N1_L4;
{ rank = same; U4; N0_L4;N1_L4;N2_L4; }
/* we divide the edge from A4 to N2_L4 into 'sub-edges',
thus indirectly making sure that the U nodes stay in their place */
{ rank = same; U2; e0 }
{ rank = same; U3; e1 }
A4 -> e0 -> e1[ dir = none ];
e1 -> N2_L4;
}
yields