Home > documentation, graph theory, LaTeX > Simple graphs, bridges of Konigsberg and directed graphs

## Simple graphs, bridges of Konigsberg and directed graphs

Some combinatorial graphs drawn using TikZ/PGF. The seven bridges of Konigsberg:

\begin{tikzpicture}
[lineDecorate/.style={-,thick},%
nodeDecorate/.style={shape=circle,inner sep=2pt,draw,thick}]
%% nodes or vertices
\foreach \nodename/\x/\y/\direction/\navigate in {
a/0/0/left/west, b/0/2/left/west, c/0/4/left/west, d/4/2/right/east}
{
\node (\nodename) at (\x,\y) [nodeDecorate] {};
\node [\direction] at (\nodename.\navigate) {\footnotesize$\nodename$};
}
%% edges or lines
\path
\foreach \startnode/\endnode in {a/d, b/d, c/d}
{
(\startnode) edge[lineDecorate] node {} (\endnode)
}
\foreach \startnode/\endnode in {a/b, b/c, c/b, b/a}
{
(\startnode) edge[lineDecorate,bend left] node {} (\endnode)
};
\end{tikzpicture}


A house graph:

\begin{tikzpicture}
[lineDecorate/.style={-,thick},%
nodeDecorate/.style={shape=circle,inner sep=2pt,draw,thick}]
%% nodes or vertices
\foreach \nodename/\x/\y/\direction/\navigate in {
a/0/5/above/north, b/2/3/right/east, e/-2/3/left/west,
c/2/0/right/east, d/-2/0/left/west}
{
\node (\nodename) at (\x,\y) [nodeDecorate] {};
\node [\direction] at (\nodename.\navigate) {\footnotesize$\nodename$};
}
%% edges or lines
\path
\foreach \startnode/\endnode in {a/b, b/c, b/e, c/d, d/e, e/a}
{
(\startnode) edge[lineDecorate] node {} (\endnode)
};
\end{tikzpicture}


A weighted multigraph:

\begin{tikzpicture}
[nodeDecorate/.style={shape=circle,inner sep=1pt,draw,thick}]
%% nodes or vertices
\foreach \nodename/\x/\y in {v_1/4/0, v_2/0/0, v_3/0/3, v_4/4/3, v_5/7/1.5}
{
\node (\nodename) at (\x,\y) [nodeDecorate] {\scriptsize$\nodename$};
}
%% edges or lines
\tikzstyle{EdgeStyle}=[->,>=stealth,thick]
\tikzstyle{LabelStyle}=[fill=white]
\foreach \startnode/\endnode/\bend/\weight in {
v_1/v_2/bend left/1, v_1/v_3/bend left/3, v_2/v_3/bend left/1,
v_2/v_4/bend left/3, v_3/v_1/bend left/1, v_3/v_2/bend left=0/2,
v_3/v_4/bend left/1, v_4/v_1/bend left=0/3, v_4/v_5/bend left/2,
v_5/v_1/bend left=0/3, v_5/v_1/bend left/6, v_5/v_4/bend left/1}
{
\scriptsize
\Edge[label=$\weight$,style=\bend](\startnode)(\endnode)
}
\end{tikzpicture}