# Clusters in sfdp

**URL:** <https://forum.graphviz.org/t/clusters-in-sfdp/2126>\
**Category:** Help\
**Created:** [April 9, 2024, 2:29pm UTC](https://forum.graphviz.org/t/clusters-in-sfdp/2126 "2024-04-09T14:29:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nathanael-fijalkow](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@nathanael-fijalkow](https://forum.graphviz.org/u/nathanael-fijalkow)\
**Post date:** [April 9, 2024, 2:29pm UTC](https://forum.graphviz.org/t/clusters-in-sfdp/2126/1 "2024-04-09T14:29:49Z")

</div>

Hello everyone,

For context, my end goal is to draw a collaboration graph using gvmap, similar to

> **[Cluster relations in a graph highlighted using gvmap](https://graphviz.org/Gallery/undirected/gd_1994_2007.html)**
>
> This is a co-authorship graph. Each node is an author and an edge exists between two authors if their names appear on a paper from one of the International Symposia on Graph Drawing bewteeen 1994-2007. The top 8 connected components are taken, and...

but specifying my own clusters.

So, I have a graph (see minimal example below) with clusters (which represents coauthors, each cluster is a “topic”, but it’s irrelevant for my question). Inspired by the code above, I try:

`sfdp -Goverlap=prism example.gv | gvmap -e -D | neato -Ecolor="#5555555522" -Tpng > out.png`

Now, I have three issues:

- sfdp does not “respect” the clusters: what I’d like is that it positions clusters as much as possible “away from each other”. For larger examples, the clusters are spread throughout the graph (this is not visible for such a small example!).
- more concretely on my example (see below), each cluster comes with a “box” which hides the rest of the picture
- also on my example, one of the cluster is filled with black, making it unreadable.

How can I get rid of the “box”, and why is one cluster filled with black?

The main question is the first one:

**Given a clustered graph, how to make sfdp “respect” clusters (meaning, put vertices in a cluster all together, and as much as possible clusters far from one another)?**

I tried playing with the constant K, to make vertices inside a cluster attract each other, but I couldn’t make them repulse the other ones.

Many thanks for your help!

```auto
graph {

"A" [fontsize=30,fontcolor=red]
	subgraph cluster1{
		"B" [fontsize=10]
		"C" [fontsize=10]
		"D" [fontsize=20]
		"A" -- "B" [weight=1]
		"A" -- "C" [weight=1]
		"A" -- "D" [weight=1]
		"B" -- "C" [weight=1]
	}
	subgraph cluster2{
		"E" [fontsize=10]
		"F" [fontsize=10]
		"G" [fontsize=20]
		"A" -- "E" [weight=1]
		"A" -- "F" [weight=1]
		"A" -- "G" [weight=1]
		"E" -- "E" [weight=1]
	}
}

```

---

<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:** [April 9, 2024, 7:38pm UTC](https://forum.graphviz.org/t/clusters-in-sfdp/2126/2 "2024-04-09T19:38:31Z")

</div>

Sorry to disappoint. sfdp doesn’t support clusters. At an algorithmic level, it only handles basic graphs, and doesn’t cope with variable edge lengths either.

graphviz fdp can solve this problem. One other thing - none of the graphviz layout programs allow a node to belong to more than one cluster (as siblings, not parent/child) and in your example, A is in both clusters, so it’s simpler to do something like this:

```auto
graph {

        "A" [fontsize=30,fontcolor=red]
        subgraph cluster1{
                "B" [fontsize=10]
                "C" [fontsize=10]
                "D" [fontsize=20]
        }
        subgraph cluster2{
                "E" [fontsize=10]
                "F" [fontsize=10]
                "G" [fontsize=20]
        }
        "A" -- "E" [weight=1]
        "A" -- "F" [weight=1]
        "A" -- "G" [weight=1]
        "E" -- "E" [weight=1]
        "A" -- "B" [weight=1]
        "A" -- "C" [weight=1]
        "A" -- "D" [weight=1]
        "B" -- "C" [weight=1]
}

```

---

<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:** [April 9, 2024, 7:46pm UTC](https://forum.graphviz.org/t/clusters-in-sfdp/2126/3 "2024-04-09T19:46:41Z")

</div>

[I am not expert in \*\*sfdp\*\* or \*\*gvmap\*\*, so no guarantees]

- FYI, I do not get any black cluster boxes, but I doubt that is important to my answers
- your command line misuses **sfdp** , **gvmap** , and **neato**.
  - **sfdp** and **gvmap** each will do a layout from scratch, i.e. the **sfdp** layout will be ignored by **gvmap**
  - **neato** _can_ make use of layout from a previous engine, but only if **-n** or **-n2** is on the command line (note that **-n2** is used on the **gvmap** example)

- I do not believe that **sfdp** recognizes _clusters_ as input. As I remember, the documentation used to be more clear about this.
- placing your edges _inside_ the clusters will cause problems, move the to the bottom of the input . **gvmap** will assume all the referenced nodes are members of the current cluster - e.g. _A_ can’t be part of two clusters

My suggestion:

- drop **sfdp**
- move your edges
- add **-n2** to the **neato** command line

[It took me too long to compose my answer. I bow (again) to Stephen]
