# Graph

`Graph()`

A graph that supported directional edges, parallel edges, self-edges and expression data on the nodes and edges.

Warning: modifying the graph if it is contained in a `dict`

or `set`

will invalidate the hash.

## Methods

Name | Description |
---|---|

add_edge | Add an edge between the `source` and `target` nodes, returning the index of the edge. |

add_node | Add a node with data `data` to the graph, returning the index of the node. |

canonize | Write the graph in a canonical form. Returns the canonized graph, the vertex map, the automorphism group size, and the orbit. |

canonize_edges | Sort and relabel the edges of the graph, keeping the vertices fixed. |

edge | Get the `idx` th edge, consisting of the the source vertex, target vertex, whether the edge is directed, and the data. |

generate | Generate all connected graphs with `external_edges` half-edges and the given allowed list |

is_isomorphic | Check if the graph is isomorphic to another graph. |

node | Get the `idx` th node, consisting of the edge indices and the data. |

num_edges | Get the number of edges in the graph. |

num_loops | Get the number of loops in the graph. |

num_nodes | Get the number of nodes in the graph. |

set_directed | Set the directed status of the edge at index `index` , returning the old value. |

set_edge_data | Set the data of the edge at index `index` , returning the old data. |

set_node_data | Set the data of the node at index `index` , returning the old data. |

to_dot | Convert the graph to a graphviz dot string. |

to_mermaid | Convert the graph to a mermaid string. |

### add_edge

`Graph.add_edge(source, target, directed=False, data=None)`

Add an edge between the `source`

and `target`

nodes, returning the index of the edge.

Optionally, the edge can be set as directed. The default data is the number 0.

### add_node

`Graph.add_node(data=None)`

Add a node with data `data`

to the graph, returning the index of the node. The default data is the number 0.

### canonize

`Graph.canonize()`

Write the graph in a canonical form. Returns the canonized graph, the vertex map, the automorphism group size, and the orbit.

### canonize_edges

`Graph.canonize_edges()`

Sort and relabel the edges of the graph, keeping the vertices fixed.

### edge

`Graph.edge(idx)`

Get the `idx`

th edge, consisting of the the source vertex, target vertex, whether the edge is directed, and the data.

### generate

`Graph.generate(_cls, external_nodes, vertex_signatures, max_vertices=None, max_loops=None, max_bridges=None, allow_self_edges=False)`

Generate all connected graphs with `external_edges`

half-edges and the given allowed list of vertex connections.

Returns the canonical form of the graph and the size of its automorphism group (including edge permutations).

#### Examples

```
>>> from symbolica import *
>>> g, q, qb, gh, ghb = S('g', 'q', 'qb', 'gh', 'ghb')
>>> Graph.generate([(1, g), (2, g)],
>>> [[g, g, g], [g, g, g, g],
>>> [q, qb, g], [gh, ghb, g]], max_loops=2)
>>> for (g, sym) in graphs.items():
>>> print(f'Symmetry factor = 1/{sym}:')
>>> print(g.to_dot())
```

generates all connected graphs up to 2 loops with the specified vertices.

#### Parameters

Name | Type | Description | Default |
---|---|---|---|

`external_nodes` |
Sequence[tuple[Expression | int, Expression | int]] | The external edges, consisting of a tuple of the node data and the edge data. If the node data is the same, flip symmetries will be recongized. | required |

`vertex_signatures` |
Sequence[Sequence[Expression | int]] | The allowed connections for each vertex. | required |

`max_vertices` |
Optional[int] | The maximum number of vertices in the graph. | `None` |

`max_loops` |
Optional[int] | The maximum number of loops in the graph. | `None` |

`max_bridges` |
Optional[int] | The maximum number of bridges in the graph. | `None` |

`allow_self_edges` |
bool | Whether self-edges are allowed. | `False` |

### is_isomorphic

`Graph.is_isomorphic(other)`

Check if the graph is isomorphic to another graph.

### node

`Graph.node(idx)`

Get the `idx`

th node, consisting of the edge indices and the data.

### num_edges

`Graph.num_edges()`

Get the number of edges in the graph.

### num_loops

`Graph.num_loops()`

Get the number of loops in the graph.

### num_nodes

`Graph.num_nodes()`

Get the number of nodes in the graph.

### set_directed

`Graph.set_directed(index, directed)`

Set the directed status of the edge at index `index`

, returning the old value.

### set_edge_data

`Graph.set_edge_data(index, data)`

Set the data of the edge at index `index`

, returning the old data.

### set_node_data

`Graph.set_node_data(index, data)`

Set the data of the node at index `index`

, returning the old data.

### to_dot

`Graph.to_dot()`

Convert the graph to a graphviz dot string.

### to_mermaid

`Graph.to_mermaid()`

Convert the graph to a mermaid string.