# Graph Layout

> Layered directed, bidirectional, and undirected graphs with subgraphs, cycle-safe ranking, and animated Bezier flows.

Use `graph` for branching architectures, multi-agent routers, bidirectional syncs, and Mermaid flowcharts with subgraphs.

## Live Interactive Example — Layered Multi-Agent DAG

Hover any node to trace its upstream and downstream paths, or click to inspect:

```diagram
type: graph
label: "Incoming requests pass through the router to specialized agents and a verifier."
eyebrow: Agent Router · Layered DAG
direction: LR
nodes:
  - id: client
    title: Client
    note: "Web · CLI · Bot"
    description: "Any surface sending a prompt over HTTPS or WebSocket."
  - id: router
    title: Router Agent
    note: "Intent + policy"
    description: "Classifies intent, enforces guardrails, and dispatches work."
    highlight: true
  - id: coder
    title: Code Agent
    note: "Repo + tests"
    code: "Agent(role='coder', tools=[shell, edit])"
  - id: research
    title: Research Agent
    note: "Docs + RAG"
    code: "Agent(role='researcher', knowledge=['docs/'])"
  - id: verify
    title: Verifier
    note: "Quality gate"
    description: "Validates output against acceptance checks before returning."
edges:
  - { from: client, to: router, label: "prompt" }
  - { from: router, to: coder, label: "code task" }
  - { from: router, to: research, label: "lookup" }
  - { from: coder, to: verify, label: "patch" }
  - { from: research, to: verify, label: "citations" }
  - { from: verify, to: router, label: "retry", fallback: true }
```

## Bidirectional & Subgraph Support

Set `bidirectional: true` to draw arrowheads and travelling pulses in **both** directions, or `undirected: true` for plain links with no arrowheads:

```diagram
type: graph
label: "Client and Server stay in bidirectional sync while writing audit logs."
direction: LR
groups:
  - { id: edge, title: "Edge Tier" }
  - { id: core, title: "Core Services" }
nodes:
  - { id: a, title: Client, note: "Local cache", group: edge }
  - { id: b, title: Sync API, note: "CRDT state", highlight: true, group: core }
  - { id: c, title: Audit Log, note: "Append-only", group: core }
edges:
  - { from: a, to: b, label: "Sync", bidirectional: true }
  - { from: b, to: c, label: "events" }
```

## Step-by-Step Guide

1. **Choose `direction: LR`** (left-to-right) or **`TB`** (top-to-bottom).
2. **Define `nodes`** with unique `id` and `title` (plus optional `note`, `group`, `highlight`, `description`, `code`, `href`).
3. **Connect them with `edges`** (`{ from, to, label, fallback, bidirectional, undirected }`).
4. **Optionally group nodes** by adding `groups: [{ id, title }]` and setting `group: <id>` on member nodes.

## Field Reference

| Field | Required | Description |
|---|---|---|
| `type` | Yes | Must be `graph` |
| `label` | Yes | Accessible 1-sentence summary |
| `nodes` | Yes | At least 2 nodes with `{ id, title, ... }` |
| `edges` | No | Connections `{ from, to, label?, fallback?, bidirectional?, undirected? }` |
| `direction` | No | `LR` (default) or `TB` |
| `groups` | No | Optional frame boxes `[{ id, title }]` drawn behind member nodes |

## Related pages

- [Flow Layout](https://diagram.praison.ai/docs/layouts/flow.md)
- [Sequence Layout](https://diagram.praison.ai/docs/layouts/sequence.md)
- [Map Layout](https://diagram.praison.ai/docs/layouts/map.md)
