Mermaid Arbitrary Graphs
Mermaid supports creating arbitrary graphs (both directed and undirected) for visualizing networks, relationships, and complex graph structures. Perfect for network topologies, social graphs, dependency graphs, and any graph-based visualization.
Use Case
Use Mermaid graphs when you need to:
- Visualize network topologies
- Show relationships between entities
- Create dependency graphs
- Model social networks
- Display graph data structures
- Show arbitrary connections between nodes
Basic Syntax
Directed Graph
1```mermaid
2graph TD
3 A --> B
4 B --> C
5 C --> A
6```
Result:
Undirected Graph
1```mermaid
2graph LR
3 A --- B
4 B --- C
5 C --- A
6```
Result:
Examples
Example 1: Network Topology
1```mermaid
2graph TB
3 Internet[Internet]
4 Router[Router]
5 Switch[Switch]
6 Server1[Server 1]
7 Server2[Server 2]
8 PC1[PC 1]
9 PC2[PC 2]
10
11 Internet --> Router
12 Router --> Switch
13 Switch --> Server1
14 Switch --> Server2
15 Switch --> PC1
16 Switch --> PC2
17```
Result:
Example 2: Social Network Graph
1```mermaid
2graph LR
3 Alice[Alice]
4 Bob[Bob]
5 Charlie[Charlie]
6 Diana[Diana]
7 Eve[Eve]
8
9 Alice --- Bob
10 Alice --- Charlie
11 Bob --- Diana
12 Charlie --- Eve
13 Diana --- Eve
14 Bob --- Eve
15```
Result:
Example 3: Dependency Graph
1```mermaid
2graph TD
3 A[Module A] --> B[Module B]
4 A --> C[Module C]
5 B --> D[Module D]
6 C --> D
7 D --> E[Module E]
8 B --> F[Module F]
9 C --> F
10```
Result:
Example 4: Weighted Graph
1```mermaid
2graph LR
3 A[A] -->|5| B[B]
4 A -->|3| C[C]
5 B -->|2| D[D]
6 C -->|4| D
7 B -->|1| E[E]
8 D -->|6| E
9```
Result:
Example 5: Complex Graph with Styling
1```mermaid
2graph TB
3 Start([Start]) --> A{Decision}
4 A -->|Yes| B[Process 1]
5 A -->|No| C[Process 2]
6 B --> D[End]
7 C --> D
8
9 style Start fill:#90EE90
10 style D fill:#FFB6C1
11 style A fill:#87CEEB
12```
Result:
Example 6: Graph with Subgraphs
1```mermaid
2graph TB
3 subgraph Cluster1[Cluster 1]
4 A1[A1]
5 A2[A2]
6 A3[A3]
7 end
8
9 subgraph Cluster2[Cluster 2]
10 B1[B1]
11 B2[B2]
12 end
13
14 A1 --> A2
15 A2 --> A3
16 B1 --> B2
17 A3 --> B1
18```
Result:
Example 7: Multi-directional Graph
1```mermaid
2graph LR
3 A[A] <--> B[B]
4 B --> C[C]
5 C -.->|dashed| D[D]
6 D ==>|thick| E[E]
7 E -->|normal| A
8```
Result:
Edge Types
Directed Edges
A --> B- Solid arrowA ==> B- Thick arrowA -.-> B- Dashed arrowA -..-> B- Dotted arrow
Undirected Edges
A --- B- Solid lineA === B- Thick lineA -.- B- Dashed lineA -..- B- Dotted line
Bidirectional
A <--> B- Bidirectional solidA <==> B- Bidirectional thick
With Labels
A -->|label| B- Edge with labelA ---|label| B- Undirected edge with label
Node Shapes
A[Rectangle]- RectangleA(Rounded)- Rounded rectangleA([Stadium])- Stadium shapeA[[Subroutine]]- Double rectangleA[(Database)]- CylinderA((Circle))- CircleA>Asymmetric]- Asymmetric shapeA{Diamond}- DiamondA{{Hexagon}}- Hexagon
Graph Directions
graph TD- Top to bottomgraph TB- Top to bottom (same as TD)graph BT- Bottom to topgraph LR- Left to rightgraph RL- Right to left
Styling
Node Styling
1```mermaid
2graph LR
3 A[A] --> B[B]
4 C[C] --> D[D]
5
6 style A fill:#f9f,stroke:#333,stroke-width:2px
7 style B fill:#bbf,stroke:#333,stroke-width:4px
8 style C fill:#bfb,stroke:#333,stroke-width:2px
9 style D fill:#fbf,stroke:#333,stroke-width:2px
10```
Result:
Class-based Styling
1```mermaid
2graph TD
3 A[Node A] --> B[Node B]
4 C[Node C] --> D[Node D]
5
6 classDef default fill:#f9f9f9,stroke:#333,stroke-width:2px
7 classDef highlight fill:#ff6,stroke:#333,stroke-width:4px
8
9 class A,C highlight
10```
Result:
Notes
- Graphs automatically layout nodes - you can't control exact positions
- Use subgraphs to group related nodes visually
- Edge labels can contain text and some HTML
- Styling supports CSS color formats (hex, rgb, named colors)
- Complex graphs may need optimization for readability
Gotchas/Warnings
- ⚠️ Layout: Automatic layout - nodes position themselves
- ⚠️ Complexity: Very large graphs can be slow to render
- ⚠️ Node IDs: Must be unique and simple (alphanumeric, no spaces)
- ⚠️ Edge Labels: Keep labels short for readability
- ⚠️ Subgraphs: Must start with
subgraphkeyword - ⚠️ Styling: CSS color names work, but hex is more reliable
Related Snippets
- Architecture Diagrams (C4 Model)
Create system architecture diagrams using C4 model with Mermaid - Chart.js Bar Chart
Create bar charts for data visualization - Graphviz DOT Diagrams
Create complex graph layouts with Graphviz DOT language - KaTeX Math Examples and Tips
Comprehensive guide to KaTeX math notation with examples and tips - Mermaid Block Diagrams
Create block diagrams for system architecture with Mermaid - Mermaid Charts (Pie, Bar, Line)
Create pie charts, bar charts, and line charts with Mermaid - Mermaid Class Diagrams (UML)
Create UML class diagrams with Mermaid - Mermaid Entity Relationship Diagrams
Create Entity Relationship Diagrams (ERD) with Mermaid - Mermaid Flowchart
Create flowcharts and decision trees with Mermaid - Mermaid Gantt Chart
Create Gantt charts for project timelines and scheduling - Mermaid Git Diagrams
Create Git branch and commit diagrams with Mermaid - Mermaid Kanban Boards
Create Kanban boards for project management with Mermaid - Mermaid Mindmaps
Create mindmaps for brainstorming and organizing ideas with Mermaid - Mermaid Packet Diagrams
Create packet diagrams for network protocols with Mermaid - Mermaid Quadrant Charts
Create quadrant charts for prioritization with Mermaid - Mermaid Radar Charts
Create radar charts for multi-dimensional comparisons with Mermaid - Mermaid Requirement Diagrams
Create requirement diagrams for system requirements with Mermaid - Mermaid Sankey Diagrams
Create Sankey diagrams for flow visualization with Mermaid - Mermaid Sequence Diagram
Create sequence diagrams for interactions and API flows - Mermaid State Diagrams
Create state diagrams and state machines with Mermaid - Mermaid Timeline Diagrams
Create timeline diagrams for chronological events with Mermaid - Mermaid Treemap Diagrams
Create treemap diagrams for hierarchical data visualization with Mermaid - Mermaid User Journey Diagrams
Create user journey maps with Mermaid - Mermaid ZenUML Diagrams
Create ZenUML sequence diagrams with Mermaid - P5.js Interactive Visualizations
Create interactive visualizations and animations with P5.js