I would like to merge nodes which are semantically identically in my application. Is there a tool or algorithm I can use to process my graph?

**Input Example:** nodes **a** and **b** should be merged.

```
digraph g {                                                                     
  a -> {b;c;d;e};                                                               
  b -> {a;c;d;e};                                                               
}

```

Image of graph using `dot`:

![nodes a and b should be merged](https://i.stack.imgur.com/JO87g.png)

**Output Example:** nodes **a** and **b** have been merged into a node **ab**.

```
digraph g {                                                                     
  ab -> {c;d;e};                                                                
}

```

![enter image description here](https://i.stack.imgur.com/2290n.png)

**Rough Sketch Algorithm:**

```
# XE = a set of nodes, represent a directed edge (x,_)
# YE = a set of nodes, representing a directed edge (y,_)
# XE \ y = XE except y
# YE \ x = YE except x

For each pair of nodes x,y
  If (edges (x,y) and (y,x) exists) AND (XE \ y == YE \ x)
    create new node xy -> xedges\y
    delete nodes x and y and their edges

```