Graphs in TikZ
Create network diagrams, graph theory visualizations, and connected node structures.
Basic Graph with Manual Positioning
\begin{tikzpicture}[
every node/.style={draw, circle, minimum size=8mm}
]
% Nodes
\node (a) at (0,0) {A};
\node (b) at (2,0) {B};
\node (c) at (1,1.5) {C};
\node (d) at (3,1.5) {D};
% Edges
\draw (a) -- (b);
\draw (a) -- (c);
\draw (b) -- (c);
\draw (b) -- (d);
\draw (c) -- (d);
\end{tikzpicture}Directed Graph (Digraph)
\begin{tikzpicture}[
every node/.style={draw, circle, minimum size=8mm, fill=blue!20},
every edge/.style={draw, ->, >=stealth, thick}
]
\node (1) at (0,0) {1};
\node (2) at (2,1) {2};
\node (3) at (2,-1) {3};
\node (4) at (4,0) {4};
\draw[->] (1) -- (2);
\draw[->] (1) -- (3);
\draw[->] (2) -- (4);
\draw[->] (3) -- (4);
\draw[->] (2) -- (3);
\end{tikzpicture}Weighted Graph
\begin{tikzpicture}[
vertex/.style={draw, circle, minimum size=8mm},
weight/.style={font=\small, fill=white, inner sep=1pt}
]
\node[vertex] (a) at (0,0) {A};
\node[vertex] (b) at (3,0) {B};
\node[vertex] (c) at (1.5,2) {C};
\node[vertex] (d) at (4.5,2) {D};
\draw (a) -- node[weight, below] {5} (b);
\draw (a) -- node[weight, left] {3} (c);
\draw (b) -- node[weight, right] {2} (c);
\draw (b) -- node[weight, below right] {4} (d);
\draw (c) -- node[weight, above] {1} (d);
\end{tikzpicture}Using the Graphs Library
\usetikzlibrary{graphs, graphs.standard}
\begin{tikzpicture}
\graph[nodes={draw, circle}, clockwise, radius=2cm, n=5] {
subgraph C_n [name=inner] -- [shorten >=1pt, shorten <=1pt]
subgraph C_n [name=outer]
};
\end{tikzpicture}
% Petersen graph
\begin{tikzpicture}
\graph[nodes={draw, circle, fill=blue!20}] {
subgraph K_n [n=5, clockwise, radius=2cm];
};
\end{tikzpicture}Circular Layout
\begin{tikzpicture}[
every node/.style={draw, circle, minimum size=8mm, fill=green!20}
]
% Place nodes in a circle
\foreach \i in {1,...,6} {
\node (n\i) at ({60*\i}:2cm) {\i};
}
% Connect all nodes (complete graph K6)
\foreach \i in {1,...,6} {
\foreach \j in {\i,...,6} {
\draw (n\i) -- (n\j);
}
}
\end{tikzpicture}Bipartite Graph
\begin{tikzpicture}[
left/.style={draw, circle, fill=red!30},
right/.style={draw, circle, fill=blue!30}
]
% Left partition
\foreach \i in {1,2,3} {
\node[left] (l\i) at (0, -\i) {$u_\i$};
}
% Right partition
\foreach \i in {1,2,3,4} {
\node[right] (r\i) at (3, -\i+0.5) {$v_\i$};
}
% Edges between partitions
\draw (l1) -- (r1);
\draw (l1) -- (r2);
\draw (l2) -- (r2);
\draw (l2) -- (r3);
\draw (l3) -- (r3);
\draw (l3) -- (r4);
\draw (l1) -- (r4);
\end{tikzpicture}State Machine / Automaton
\usetikzlibrary{automata, positioning}
\begin{tikzpicture}[
shorten >=1pt,
node distance=2.5cm,
on grid,
auto,
every state/.style={draw, circle, minimum size=1cm}
]
% States
\node[state, initial] (q0) {$q_0$};
\node[state] (q1) [right=of q0] {$q_1$};
\node[state, accepting] (q2) [right=of q1] {$q_2$};
% Transitions
\path[->]
(q0) edge node {0} (q1)
(q0) edge [loop above] node {1} ()
(q1) edge node {1} (q2)
(q1) edge [loop above] node {0} ()
(q2) edge [bend left] node {0,1} (q0);
\end{tikzpicture}Social Network Style Graph
\begin{tikzpicture}[
person/.style={
draw, circle, minimum size=1.2cm, fill=blue!20,
font=\small
}
]
% Central node
\node[person, fill=red!30] (center) at (0,0) {You};
% Friends
\node[person] (f1) at (60:2.5cm) {Alice};
\node[person] (f2) at (120:2.5cm) {Bob};
\node[person] (f3) at (180:2.5cm) {Carol};
\node[person] (f4) at (240:2.5cm) {Dave};
\node[person] (f5) at (300:2.5cm) {Eve};
\node[person] (f6) at (0:2.5cm) {Frank};
% Connections to center
\foreach \f in {f1,f2,f3,f4,f5,f6} {
\draw[thick, blue!50] (center) -- (\f);
}
% Some cross-connections
\draw[gray] (f1) -- (f2);
\draw[gray] (f2) -- (f3);
\draw[gray] (f4) -- (f5);
\draw[gray] (f1) -- (f6);
\end{tikzpicture}Curved Edges and Self-Loops
\begin{tikzpicture}[
every node/.style={draw, circle, minimum size=8mm}
]
\node (a) at (0,0) {A};
\node (b) at (3,0) {B};
\node (c) at (1.5,2) {C};
% Straight edge
\draw (a) -- (b);
% Curved edges
\draw[bend left=30] (a) to (c);
\draw[bend right=30] (a) to (c);
% Self loop
\draw (b) to[loop right] (b);
% Directed curved edge with label
\draw[->, bend left] (b) to node[above] {edge} (c);
\end{tikzpicture}Layered Graph
\begin{tikzpicture}[
every node/.style={draw, rectangle, minimum width=1cm, minimum height=0.6cm}
]
% Layer 1 (input)
\node[fill=green!20] (i1) at (0,2) {$x_1$};
\node[fill=green!20] (i2) at (0,0) {$x_2$};
% Layer 2 (hidden)
\node[fill=blue!20] (h1) at (2,3) {$h_1$};
\node[fill=blue!20] (h2) at (2,1) {$h_2$};
\node[fill=blue!20] (h3) at (2,-1) {$h_3$};
% Layer 3 (output)
\node[fill=red!20] (o1) at (4,1) {$y$};
% Connect all
\foreach \i in {i1,i2} {
\foreach \h in {h1,h2,h3} {
\draw[->] (\i) -- (\h);
}
}
\foreach \h in {h1,h2,h3} {
\draw[->] (\h) -- (o1);
}
\end{tikzpicture}Next Steps
Continue learning TikZ diagrams:
- Mind Maps - Concept mapping diagrams
- Trees - Hierarchical tree structures
- Circuit Diagrams - Electronic circuits
Graphs in TikZ
Create network diagrams, graph theory visualizations, and connected node structures.
Basic Graph with Manual Positioning
\begin{tikzpicture}[
every node/.style={draw, circle, minimum size=8mm}
]
% Nodes
\node (a) at (0,0) {A};
\node (b) at (2,0) {B};
\node (c) at (1,1.5) {C};
\node (d) at (3,1.5) {D};
% Edges
\draw (a) -- (b);
\draw (a) -- (c);
\draw (b) -- (c);
\draw (b) -- (d);
\draw (c) -- (d);
\end{tikzpicture}Directed Graph (Digraph)
\begin{tikzpicture}[
every node/.style={draw, circle, minimum size=8mm, fill=blue!20},
every edge/.style={draw, ->, >=stealth, thick}
]
\node (1) at (0,0) {1};
\node (2) at (2,1) {2};
\node (3) at (2,-1) {3};
\node (4) at (4,0) {4};
\draw[->] (1) -- (2);
\draw[->] (1) -- (3);
\draw[->] (2) -- (4);
\draw[->] (3) -- (4);
\draw[->] (2) -- (3);
\end{tikzpicture}Weighted Graph
\begin{tikzpicture}[
vertex/.style={draw, circle, minimum size=8mm},
weight/.style={font=\small, fill=white, inner sep=1pt}
]
\node[vertex] (a) at (0,0) {A};
\node[vertex] (b) at (3,0) {B};
\node[vertex] (c) at (1.5,2) {C};
\node[vertex] (d) at (4.5,2) {D};
\draw (a) -- node[weight, below] {5} (b);
\draw (a) -- node[weight, left] {3} (c);
\draw (b) -- node[weight, right] {2} (c);
\draw (b) -- node[weight, below right] {4} (d);
\draw (c) -- node[weight, above] {1} (d);
\end{tikzpicture}Using the Graphs Library
\usetikzlibrary{graphs, graphs.standard}
\begin{tikzpicture}
\graph[nodes={draw, circle}, clockwise, radius=2cm, n=5] {
subgraph C_n [name=inner] -- [shorten >=1pt, shorten <=1pt]
subgraph C_n [name=outer]
};
\end{tikzpicture}
% Petersen graph
\begin{tikzpicture}
\graph[nodes={draw, circle, fill=blue!20}] {
subgraph K_n [n=5, clockwise, radius=2cm];
};
\end{tikzpicture}Circular Layout
\begin{tikzpicture}[
every node/.style={draw, circle, minimum size=8mm, fill=green!20}
]
% Place nodes in a circle
\foreach \i in {1,...,6} {
\node (n\i) at ({60*\i}:2cm) {\i};
}
% Connect all nodes (complete graph K6)
\foreach \i in {1,...,6} {
\foreach \j in {\i,...,6} {
\draw (n\i) -- (n\j);
}
}
\end{tikzpicture}Bipartite Graph
\begin{tikzpicture}[
left/.style={draw, circle, fill=red!30},
right/.style={draw, circle, fill=blue!30}
]
% Left partition
\foreach \i in {1,2,3} {
\node[left] (l\i) at (0, -\i) {$u_\i$};
}
% Right partition
\foreach \i in {1,2,3,4} {
\node[right] (r\i) at (3, -\i+0.5) {$v_\i$};
}
% Edges between partitions
\draw (l1) -- (r1);
\draw (l1) -- (r2);
\draw (l2) -- (r2);
\draw (l2) -- (r3);
\draw (l3) -- (r3);
\draw (l3) -- (r4);
\draw (l1) -- (r4);
\end{tikzpicture}State Machine / Automaton
\usetikzlibrary{automata, positioning}
\begin{tikzpicture}[
shorten >=1pt,
node distance=2.5cm,
on grid,
auto,
every state/.style={draw, circle, minimum size=1cm}
]
% States
\node[state, initial] (q0) {$q_0$};
\node[state] (q1) [right=of q0] {$q_1$};
\node[state, accepting] (q2) [right=of q1] {$q_2$};
% Transitions
\path[->]
(q0) edge node {0} (q1)
(q0) edge [loop above] node {1} ()
(q1) edge node {1} (q2)
(q1) edge [loop above] node {0} ()
(q2) edge [bend left] node {0,1} (q0);
\end{tikzpicture}Social Network Style Graph
\begin{tikzpicture}[
person/.style={
draw, circle, minimum size=1.2cm, fill=blue!20,
font=\small
}
]
% Central node
\node[person, fill=red!30] (center) at (0,0) {You};
% Friends
\node[person] (f1) at (60:2.5cm) {Alice};
\node[person] (f2) at (120:2.5cm) {Bob};
\node[person] (f3) at (180:2.5cm) {Carol};
\node[person] (f4) at (240:2.5cm) {Dave};
\node[person] (f5) at (300:2.5cm) {Eve};
\node[person] (f6) at (0:2.5cm) {Frank};
% Connections to center
\foreach \f in {f1,f2,f3,f4,f5,f6} {
\draw[thick, blue!50] (center) -- (\f);
}
% Some cross-connections
\draw[gray] (f1) -- (f2);
\draw[gray] (f2) -- (f3);
\draw[gray] (f4) -- (f5);
\draw[gray] (f1) -- (f6);
\end{tikzpicture}Curved Edges and Self-Loops
\begin{tikzpicture}[
every node/.style={draw, circle, minimum size=8mm}
]
\node (a) at (0,0) {A};
\node (b) at (3,0) {B};
\node (c) at (1.5,2) {C};
% Straight edge
\draw (a) -- (b);
% Curved edges
\draw[bend left=30] (a) to (c);
\draw[bend right=30] (a) to (c);
% Self loop
\draw (b) to[loop right] (b);
% Directed curved edge with label
\draw[->, bend left] (b) to node[above] {edge} (c);
\end{tikzpicture}Layered Graph
\begin{tikzpicture}[
every node/.style={draw, rectangle, minimum width=1cm, minimum height=0.6cm}
]
% Layer 1 (input)
\node[fill=green!20] (i1) at (0,2) {$x_1$};
\node[fill=green!20] (i2) at (0,0) {$x_2$};
% Layer 2 (hidden)
\node[fill=blue!20] (h1) at (2,3) {$h_1$};
\node[fill=blue!20] (h2) at (2,1) {$h_2$};
\node[fill=blue!20] (h3) at (2,-1) {$h_3$};
% Layer 3 (output)
\node[fill=red!20] (o1) at (4,1) {$y$};
% Connect all
\foreach \i in {i1,i2} {
\foreach \h in {h1,h2,h3} {
\draw[->] (\i) -- (\h);
}
}
\foreach \h in {h1,h2,h3} {
\draw[->] (\h) -- (o1);
}
\end{tikzpicture}Next Steps
Continue learning TikZ diagrams:
- Mind Maps - Concept mapping diagrams
- Trees - Hierarchical tree structures
- Circuit Diagrams - Electronic circuits