# How to draw a force graph with fixed clustered nodes?

How to draw a graph like the following with TikZ?

In particular:

1. It is an undirected graph.
2. There are groups of 3 nodes that always stay close to each other and are linked by the bold edges.
3. Some of the other nodes may be linked by (normal) edges.
4. Nodes do not have a specific position. I want to use a force (or similar) layout to position them automatically.

I'm a beginner at TikZ. I haven't tried anything because I don't really know what to try: I know how to make normal force layout graphs, but I don't know how to constraint those groups of 3 nodes.

• Welcome to TeX.SE! Dec 17, 2021 at 20:59

There are a few approaches offered by TikZ for force diagrams that have a lot of options. Since the handling is quite special, I would suggest that you look at the relevant chapter ("Graph Drawing Algorithms: Force-Based Methods") in the TikZ manual.

I only got solutions where the distances of the nodes are more or less equal, though. The following code needs LuaLaTeX for compilation:

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{graphs.standard, graphdrawing}
\usegdlibrary{force}
\begin{document}

\tikz \graph[spring electrical layout, nodes={draw, circle, electric charge=2}, edges={very thick}] {
// {
4 -- 2 -- 1 -- 4
},
// {
3 -- 5bar[as={$\overline{5}$}] -- 4bar[as={$\overline{4}$}] -- 3
},
// {
5 -- 1a[as={1}] -- 2a[as={2}] -- 5
},
4[electric charge=5] --[thin, length=2] 4bar;
5[electric charge=5] --[thin] 5bar;
};

\tikz \graph[spring layout, node distance=2cm, nodes={draw, circle}, edges={very thick}] {
// {
4 -- 2 -- 1 -- 4
},
// {
3 -- 5bar[as={$\overline{5}$}] -- 4bar[as={$\overline{4}$}] -- 3
},
// {
5 -- 1a[as={1}] -- 2a[as={2}] -- 5
},
4 --[thin] 4bar;
5 --[thin] 5bar;
};
\end{document}


Update

Somehow, the effect of the spring layout, which aims to place all the nodes equidistantly to one another, cannot easily be overridden using other layouts to form clusters. For example, I failed to use circular subgraphs or the simple necklace layout, since both became distorted by the placement mechanism. The tree layout seems to be more or less immune against this influence, but you need to manually set the orientation of single clusters.

After a lot of trial and error, I came up with this approach that might work for you:

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{graphs.standard, graphdrawing}
\usegdlibrary{force, trees}
\begin{document}

\tikz \graph[spring layout, node distance=4cm, nodes={draw, circle}, ] {
// [tree layout, node distance=1cm, edges={very thick}] {
[clique] 1 -- 2 -- 4;
};

// [tree layout, node distance=1cm, edges={very thick}] {
[clique] 4bar[as={$\overline{4}$}] -- 3 -- 5bar[as={$\overline{5}$}]
};

// [tree layout, node distance=1cm, edges={very thick}, orient=90] {
[clique] 5 -- 1a[as={1}] -- 2a[as={2}]
};

4 -- 4bar;

5bar -- 5;

4 --[draw=none] 5; % to keep the graph together, probably unnecessary if you have more connected clusters
};

\end{document}


• This is close, but not quite. What's the meaning of the length=2 attribute? I played with it and it seems to not be honored by the spring layout at all. The main issue is the layout not discriminating between the two types of edges: the bold edges should be very short, ideally of fixed length while the other edges can be anything. Dec 18, 2021 at 11:37
• Yea, I had the same problem. There are different types of layouts that use different kinds of options. Some options have just no effect with certain layouts. I did not really find a way to make the edges longer between the subnodes and leave them shorter within. I also tried with subnodes with a circular layout, but those would not be rotated automatically and somehow are not positioned like regular nodes. =( Dec 18, 2021 at 11:42
• Yes, those are the kind of results I got. I tried to set some spring layout values to specific edges (such as trying to set a different spring constant to the bold edges) but doesn't seem to have effect as well. On the bright side, I did learn a few new things from your answer though (e.g. how to name nodes and override the labels) :) so thanks! Dec 18, 2021 at 11:45
• I found that somehow the effect of the spring layout is very hard to be overridden using other layouts to form clusters. The tree layout seems to be more or less immune against this influence, but you may need to manually set the orientation of single clusters. See my edit. Dec 18, 2021 at 13:22