# How to draw the physical tree with balls under both the stress of strings and gravity?

I want to draw the physical model of graph that has a ball for each vertex and a piece of string for each edge. In this model, if you lift a ball high enough, the other balls are pulled up along with it, affected by both the stress of strings and gravity. An illustration is shown in the following figure.

However, using tikz-qtree, I can only draw an ordinary tree which cannot visualize the strings or gravity (and is somewhat ugly).

Therefore, I want to know that

How to draw the physical tree with balls under both the stress of strings and gravity?

My code using tikz-qtree is as follows.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree}

\begin{document}

\begin{tikzpicture}
\node [draw, circle] (r) at (0,0) {$r$};
\node [draw, circle, blue, very thick] (s) at (2,0) {$s$};
\node [draw, circle] (t) at (4,0) {$t$};
\node [draw, circle] (u) at (6,0) {$u$};

\node [draw, circle] (v) at (0,-2) {$v$};
\node [draw, circle] (w) at (2,-2) {$w$};
\node [draw, circle] (x) at (4,-2) {$x$};
\node [draw, circle] (y) at (6,-2) {$y$};

\draw (r) to (v);
\draw (r) to (s);
\draw (s) to (w);
\draw (t) to (u);
\draw (t) to (w);
\draw (t) to (x);
\draw (u) to (x);
\draw (u) to (y);
\draw (x) to (y);
\draw (y) to (u);
\end{tikzpicture}

\begin{tikzpicture}[]
\tikzset{level distance = 36pt, sibling distance = 25pt}
\tikzset{every node/.style = {draw, circle}}

\Tree [.\node[blue, very thick](s){$s$};
[.$r$ $v$ ]
[.\node(w){$w$};
[.\node(t){$t$}; \node(u){$u$}; ]
[.\node(x){$x$}; \node(y){$y$}; ]
]
]

% cross edges
\draw [dashed, thick, red] (t) to [out = -45, in = 225] (x);
\draw [dashed, thick, red] (u) to [out = -45, in = 225] (y);
\draw [dotted, thick, purple] (x.-110) to (u);
\end{tikzpicture}

\end{document}

• You might want to check out the tkz-graph package. Documentation in French, but with lots of examples to play with. It doesn't do exactly what you want, but it is designed for graphs and not trees; anything you do with tikz-qtree will likely be very hacky, since it's not designed for that. See also Multi-rooted Tree-like Structures and Nodes with Multiple Parents in LaTeX. Nov 18, 2014 at 6:30
• @AlanMunn Thanks. It seems more suitable for my purpose. I will check it carefully later. The hardest part (IMO) is to determine the positions of the balls under gravity. Nov 18, 2014 at 6:53
• I don't get the question exactly. Do you want to know how to draw those bent lines connecting the nodes? If yes, I have an answer.
– user11232
Nov 18, 2014 at 7:41
• @HarishKumar "Bent lines connecting nodes" is one aspect. The other aspect is how to put the balls in a common subtree at the same layer closely next to each other (due to strings and gravity). Nov 18, 2014 at 7:56
• The new version of TikZ 3.00 has electrical charge and spring tension properties for the placement of graph elements. Nov 18, 2014 at 8:34

Here's some examples with the graph drawing stuff from the latest PGF version. I'm not quite sure about the best way to define new edges but at least the method I used seems to work. It requires lualatex:

\documentclass[border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{topaths,graphs,graphdrawing,decorations}
\usegdlibrary{layered,trees}
\pgfdeclaredecoration{squiggle}{draw}{
\state{draw}[width=\pgfdecoratedpathlength]{
\pgfpathmoveto{\pgfpointorigin}
\pgfpathcurveto{\pgfpoint{0.25*\pgfdecoratedpathlength}{0.25*\pgfdecoratedpathlength}}%
{\pgfpoint{0.75*\pgfdecoratedpathlength}{-0.25*\pgfdecoratedpathlength}}%
{\pgfpoint{\pgfdecoratedpathlength}{0pt}}
}}

\tikzgraphsset{
edge squiggle/.style={
new --/.code n args={4}{
\path [-, every new --/.try]
(##1\tikzgraphleftanchor)
edge[##3, decoration=squiggle, decorate] ##4
(##2\tikzgraphrightanchor);}
},
edge loop/.style={
new --/.code n args={4}{
\draw [-, every new --/.try]
(##1)
edge [out=260, in=280, looseness=3] ##4
(##2);}
}
}

\begin{document}
\tikz\graph [nodes={shape=circle, fill=gray!50}, edge squiggle,
chain shift=(0:2cm), group shift=(270:2cm)]{
E -- S -- A;
D -!- C -- B;
D -- E; D -- S;
C -- S; B -- A;
};

\hskip1cm

\tikz\graph [layered layout, nodes={shape=circle, fill=gray!50},
level distance=1.5cm] {
S -- {A -- B, C -- B, E, D};
{[edge loop] E -- D};
{[same layer] A, C, D, E};
};
\end{document}


• Actually I suppose the second row of nodes in the graph on the right should have no space between them. Nov 18, 2014 at 14:47