# graph

Python API: `luxonis_ml.utils.graph`

## Functions

| Name | Description |
| --- | --- |
|
[`is_acyclic`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/graph.md)
| Test if graph is acyclic. |
|
[`traverse_graph`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/graph.md)
| Traverse the graph in topological order, starting from the nodes with no predecessors. |

### function `is_acyclic`

```python
def is_acyclic(graph: dict[str, list[str]]) -> bool
```

Test if graph is acyclic.

> **Examples**
> ```pycon
>>> is_acyclic({"a": ["b"], "b": []})
True
>>> is_acyclic({"a": ["b"], "b": ["a"]})
False
>>> is_acyclic({})
True
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `graph` | `dict[str, list[str]]` | Graph represented as a dictionary of predecessors. | required |

Returns

| Type | Description |
| --- | --- |
| `bool` | `True` if graph is acyclic, `False` otherwise. |

### function `traverse_graph`

```python
def traverse_graph(
    graph: dict[str, list[str]],
    nodes: Mapping[str, T],
) -> Iterator[tuple[str, T, list[str], list[str]]]
```

Traverse the graph in topological order, starting from the nodes with no predecessors.

> **Example**
> ```pycon
>>> graph = {"a": ["b"], "b": []}
>>> nodes = {"a": 1, "b": 2}
>>> for name, value, preds, rem in traverse_graph(graph, nodes):
...     print(name, value, preds, rem)
b 2 [] ['a']
a 1 ['b'] []
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `graph` | `dict[str, list[str]]` | Graph represented as a dictionary of predecessors. Keys are node names, values are node
predecessors. | required |
| `nodes` | `Mapping[str, T]` | Dictionary mapping node names to values. | required |

Returns

| Type | Description |
| --- | --- |
| `Iterator[tuple[str, T, list[str], list[str]]]` |  |

Yields

| Type | Description |
| --- | --- |
|  | Tuples containing node name, node value, node predecessors, and remaining unprocessed nodes. |

Raises

| Type | Description |
| --- | --- |
| `RuntimeError` | If the graph is malformed. |

## Module Attributes

| Name | Description |
| --- | --- |
|
[`T`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/graph.md)
| |

### type variable `T`

```python
T = TypeVar('T')
```
