// diagram cheat sheet
Quick reference for adding diagrams to your Blogger posts using Mermaid.js. Always use HTML view. Wrap every diagram in <div class="mermaid">...</div>
Base template:
<div class="mermaid">
graph TD
A[Start] --> B[End]
</div>Flowcharts — Algorithms & Logic
Use for: algorithm steps, if/else logic, program flow, decision trees
Direction options
graph TD | Top → Down (most common) |
graph LR | Left → Right |
graph TB | Top → Bottom (same as TD) |
graph RL | Right → Left |
Node shapes
A[Rectangle] | □ — process / step |
A(Rounded) | ⬭ — start / end |
A{Diamond} | ◇ — decision / condition |
A((Circle)) | ○ — connector / state |
A[/Parallelogram/] | ⬱ — input / output |
A[(Cylinder)] | ⌀ — database |
Arrow types
A --> B | → plain arrow |
A -->|label| B | → labeled arrow |
A --- B | — line, no arrow |
A -.> B | ⇢ dashed arrow |
A ==> B | ⟹ thick arrow |
Example — Binary Search algorithm
<div class="mermaid">
graph TD
A([Start]) --> B[/Input: array A, target t/]
B --> C[low = 0, high = len-1]
C --> D{low <= high?}
D -->|No| E([Return -1])
D -->|Yes| F[mid = floor low+high / 2]
F --> G{A at mid == t?}
G -->|Yes| H([Return mid])
G -->|No| I{A at mid < t?}
I -->|Yes| J[low = mid + 1]
I -->|No| K[high = mid - 1]
J --> D
K --> D
</div>
Example — If/Else logic
<div class="mermaid">
graph TD
A{n mod 2 == 0?} -->|Yes| B[Even]
A -->|No| C[Odd]
</div>
Trees — Data Structures
Use for: binary trees, BSTs, parse trees, expression trees, decision trees
Example — Binary Search Tree
<div class="mermaid">
graph TD
50 --> 30
50 --> 70
30 --> 20
30 --> 40
70 --> 60
70 --> 80
</div>
Example — Expression Tree for (a+b) * (c-d)
<div class="mermaid">
graph TD
M["*"] --> P["+"]
M --> S["-"]
P --> A["a"]
P --> B["b"]
S --> C["c"]
S --> D["d"]
</div>
State Diagrams — Automata & Theory of Computation
Use for: finite automata (DFA/NFA), state machines, transition diagrams
Example — Simple DFA (accepts strings ending in 1)
<div class="mermaid">
stateDiagram-v2
[*] --> q0
q0 --> q0 : 0
q0 --> q1 : 1
q1 --> q0 : 0
q1 --> q1 : 1
q1 --> [*]
</div>
Example — Traffic light state machine
<div class="mermaid">
stateDiagram-v2
[*] --> Red
Red --> Green : timer
Green --> Yellow : timer
Yellow --> Red : timer
</div>
Sequence Diagrams — Systems & Networking
Use for: client-server communication, API calls, protocol flows, function call sequences
Example — HTTP request/response
<div class="mermaid">
sequenceDiagram
participant Client
participant Server
participant Database
Client->>Server: GET /users/1
Server->>Database: SELECT * FROM users WHERE id=1
Database-->>Server: {id:1, name:"Rose"}
Server-->>Client: 200 OK {user data}
</div>
Arrow types for sequences
A->>B: msg | → solid, no arrowhead |
A->>B: msg | → solid with arrowhead (most common) |
A-->>B: msg | ⇢ dashed — response/return |
A->+B: msg | activate B (show as active box) |
A->-B: msg | deactivate B |
Class Diagrams — OOP & Software Engineering
Use for: object-oriented design, inheritance, interfaces, UML
Example — OOP inheritance
<div class="mermaid">
classDiagram
class Animal {
+String name
+int age
+speak() void
}
class Dog {
+String breed
+fetch() void
}
class Cat {
+bool indoor
+purr() void
}
Animal <|-- Dog
Animal <|-- Cat
</div>
Relationship types
A <|-- B | inheritance (B extends A) |
A *-- B | composition (B is part of A) |
A o-- B | aggregation |
A --> B | association |
A ..> B | dependency |
A ..|> B | implements interface |
Graphs — Graph Theory & Networks
Use for: undirected/directed graphs, weighted edges, network topology
Example — Undirected weighted graph
<div class="mermaid">
graph LR
A --10--- B
A --5--- C
B --3--- C
B --7--- D
C --2--- D
</div>
Example — Directed graph (digraph)
<div class="mermaid">
graph LR
A --> B
A --> C
B --> D
C --> D
D --> E
</div>
Truth Tables — Logic (plain HTML table)
Mermaid doesn't do tables — use plain HTML for truth tables:
<table>
<thead>
<tr>
<th>p</th><th>q</th><th>p ∧ q</th><th>p ∨ q</th><th>¬p</th><th>p → q</th>
</tr>
</thead>
<tbody>
<tr><td>T</td><td>T</td><td>T</td><td>T</td><td>F</td><td>T</td></tr>
<tr><td>T</td><td>F</td><td>F</td><td>T</td><td>F</td><td>F</td></tr>
<tr><td>F</td><td>T</td><td>F</td><td>T</td><td>T</td><td>T</td></tr>
<tr><td>F</td><td>F</td><td>F</td><td>F</td><td>T</td><td>T</td></tr>
</tbody>
</table>
Using All Three Together in One Post
A typical CS study post might combine all three — this is the pattern to follow:
/* 1. Explain the concept in text */
<p>A binary search tree satisfies the property that for any node
with value \(k\), all left subtree values are \(< k\) and
all right subtree values are \(> k\).</p>
/* 2. Show the structure as a diagram */
<div class="mermaid">
graph TD
8 --> 3
8 --> 10
3 --> 1
3 --> 6
</div>
/* 3. Show the search complexity as math */
$$T(n) = O(\log n) \text{ average case}$$
/* 4. Show the implementation as code */
<pre class="line-numbers"><code class="language-python">
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
</code></pre>
0 comments