# Hierarchycal graph on Tulip

Some variables:

• `input`: the input (original) graph
• `orig`: the same `input` graph, but it is made as a sub graph of `input`.
• `meta`: a node that represents a sub graph

### Creating a subgraph

Case: Some of nodes in the original graph will be grouped as a sub graph. Those nodes in the original graph will then be replaced with a (meta) node that represents that sub graph.

Precondition: The original graph must not be the root graph. Create a sub graph of it first that contains the same nodes and edges.

```Graph* orig = input->getRoot()->addSubGraph();
copy(input, orig);
```

#### Subgraph creation

```set<node> nodes = [ list of nodes of the original graph (input) that will be subgraph-ed ]
node meta = tlp::createMetaNode(orig, nodes);
```

Two local properties of the `input` will be created: `viewMetaNode` and `viewColor`.

`viewMetaNode` property contains mapping from meta node to the sub graph that it represents.

Edges to nodes in the sub graph will changed. They will be pointed to the meta node after the sub graph is created. However, the original information about the edges is saved, so it can be restored later.

#### Getting created subgraph

```GraphProperty* gp = input->getLocalProperty<GraphProperty*>("viewMetaNode");
Graph* subgraph = gp->getNodeValue(meta);
```

### Restoring a sub graph to its “parent”

```tlp::openMetaNode(orig, meta)
```

Edges connections will be restored.

### Remarks

1. The `copy` function (taken from GrouseFlocks):
```void copy (Graph *src, Graph *dest) {
StableIterator <node> nodes (src->getNodes());
while (nodes.hasNext()) dest->addNode (nodes.next());
StableIterator <edge> edges (src->getEdges());
while (edges.hasNext()) dest->addEdge (edges.next());
}//end copyFeature
```
2. Edges in a directed graph are handled nicely. Might be a problem if you only expect at most one edge for every pair of nodes (simple graph)
3. Sub graphs can contain other sub graphs
4. Opening a sub graph inside another sub graph may lead into a problem. Open the outer subgraph first to avoid unexpected problem.