# Edges between clusters

**URL:** <https://forum.graphviz.org/t/edges-between-clusters/2855>\
**Category:** Help\
**Created:** [March 23, 2025, 8:03pm UTC](https://forum.graphviz.org/t/edges-between-clusters/2855 "2025-03-23T20:03:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![us12](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@us12](https://forum.graphviz.org/u/us12)\
**Post date:** [March 23, 2025, 8:03pm UTC](https://forum.graphviz.org/t/edges-between-clusters/2855/1 "2025-03-23T20:03:06Z")

</div>

My goal is to create an edge between clusters but align them so that the edge started in the middle of the cluster A and connected to the middle of the cluster B.

How can I:

1. make the edge looks like an edge, not like a triangle?
2. how to remove empty space in place of invisible nodes?
3. how to align clusters in the center to each other?
4. maybe you know better way to make edges between clusters without using invisible nodes? I create them only for the edge.

Here is my solution:

```auto
digraph {
  compound=true;

  subgraph cluster_a {
    label="Cluster A";
    1; 3; 5; a [style=invis]; 7; 9; 11; 13; 
  }
  subgraph cluster_b {
    label="Cluster B";
    2; 4; b[style=invis]; 6; 8; 
  }

  a -> b [ltail="cluster_a" lhead="cluster_b"];
}

```

 ![изображение](https://global.discourse-cdn.com/graphviz/original/2X/c/cdee3e65ccc003e458cf073de838ac48713ad3bc.png)

---

<div class="post-metadata">

**Author:** ![scnorth](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/scnorth/32/89_2.png) [@scnorth](https://forum.graphviz.org/u/scnorth)\
**Post date:** [March 24, 2025, 2:01am UTC](https://forum.graphviz.org/t/edges-between-clusters/2855/3 "2025-03-24T02:01:24Z")

</div>

It will help a little to set ranksep=1 (or at least increase it). You can decrease the width of the invisible nodes, too.

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [March 25, 2025, 1:30am UTC](https://forum.graphviz.org/t/edges-between-clusters/2855/4 "2025-03-25T01:30:39Z")

</div>

1. **ranksep** ([ranksep | Graphviz](https://www.graphviz.org/docs/attrs/ranksep/)) takes care of problem #1.
2. Invisible nodes do occupy some space, but you can reduce that space by setting **shape=point** (or **plain** ) ([Node Shapes | Graphviz](https://www.graphviz.org/doc/info/shapes.html)) and reducing **nodesep** ([nodesep | Graphviz](https://www.graphviz.org/docs/attrs/nodesep/)).
3. aligning clusters is a challenge. In recent versions of Graphviz (last 6 months?), you can use **packmode** (the array option) to force node order and cluster positioning ([packMode | Graphviz](https://www.graphviz.org/docs/attr-types/packMode/))
4. **fdp** ([fdp | Graphviz](https://www.graphviz.org/docs/layouts/fdp/)) allows cluster-cluster edges, no invisible nodes needed. (I can’t find documentation about this). But **fdp** does node layout entirely differently
5. If you want a gap-less result, I have a program that will produce it. All automated, but you need to make some odd changes to the input (move all cluster-cluster edges to a second graph) and then run a series of programs. Let me know if this sounds _fun_. See “gapless”, below.

This gets fairly close. Note it only uses one invisible node, to “fix” the problem of even number of nodes in one cluster.

```auto
digraph {
  compound=true;
  ranksep=1
  nodesep=.1

  subgraph cluster_a {
    label="Cluster A";
    1; 3; 5; 7; 9; 11; 13; 
  }
  subgraph cluster_b {
    label="Cluster B";
    2; 4;
    mid [shape=point style=invis]
    6; 8; 
  }
  7 -> mid [ltail="cluster_a" lhead="cluster_b"];
}

```

Giving:

 ![forum2855_1](https://global.discourse-cdn.com/graphviz/original/2X/6/64b2dbc7566e074490b8d9ae30436b00583a81c9.png)

Gapless:

 ![forum2855_3](https://global.discourse-cdn.com/graphviz/original/2X/4/4ad1dc4f7e883d80ed77bc28eff8538e352842d0.png)

---

<div class="post-metadata">

**Author:** ![jjlong](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/jjlong/32/887_2.png) [@jjlong](https://forum.graphviz.org/u/jjlong)\
**Post date:** [March 25, 2025, 2:00am UTC](https://forum.graphviz.org/t/edges-between-clusters/2855/5 "2025-03-25T02:00:06Z")

</div>

This is the best approximation I could achieve. While the nodes in Cluster B are not evenly spaced, they exhibit a degree of symmetry.

Here is what I changed:

1. Added `ranksep=1` to move the clusters apart, as @scnorth suggested.
2. Removed node `a` from Cluster A.
3. Changed node `b` to have no width by adding the attributes `width=0 fixedsize=true`.
4. Changed the edge to connect center node `7` in Cluster A to invisible node `b` in Cluster B.

```auto
digraph "gvforum"
{
    compound=true;
    ranksep=1
    subgraph "cluster_a" { label="Cluster A"
        1; 3; 5; 7; 9; 11; 13;
    }
    subgraph "cluster_b" { label="Cluster B"
        2; 4; b; 6; 8;
        b [width=0 fixedsize=true style=invis];
    }
    7 -> b[ltail="cluster_a" lhead="cluster_b"];
}

```

 ![center_clusters](https://global.discourse-cdn.com/graphviz/original/2X/2/26613e627a92aaf3575e309cb6883bea364e8e92.png)

You can add additional 0 width nodes in Cluster B to achieve even spacing, however the gaps will be wider than what occurs in Cluster A

```auto
digraph "gvforum"
{
    compound=true;
    ranksep=1
    subgraph "cluster_a" { label="Cluster A"
        1; 3; 5; 7; 9; 11; 13;
    }
    subgraph "cluster_b" { label="Cluster B"
        2; a; 4; b; 6; c; 8;
        a [width=0 fixedsize=true style=invis];
        b [width=0 fixedsize=true style=invis];
        c [width=0 fixedsize=true style=invis];
    }
    7 -> b[ltail="cluster_a" lhead="cluster_b"];
}

```

 ![center_clusters_equal_spacing](https://global.discourse-cdn.com/graphviz/original/2X/6/61bedc3854d40b0a8fb671b4bd0cd57a95d3530f.png)
