# Weighted graph

> Distances between five towns, written on the edges. A Graphs and networks example with its complete LaTeX source, ready to copy or open in TeXeR.

A Graphs and networks example in the TeXeR TikZ library: https://texer.org/library/graphs/weighted-graph

Package: [Graphs and networks](https://texer.org/library/graphs.md), on CTAN at https://ctan.org/pkg/pgf.

Tags: weighted graph, distances, edges

## Source

```latex
\begin{tikzpicture}[
  town/.style={circle, draw, thick, minimum size=9mm, font=\small},
  weight/.style={font=\footnotesize, inner sep=1.5pt},
]
  \node[town] (A) at (0,0) {A};
  \node[town] (B) at (3,1.6) {B};
  \node[town] (C) at (3,-1.6) {C};
  \node[town] (D) at (6,1.2) {D};
  \node[town] (E) at (6,-1.2) {E};

  \foreach \u/\v/\w in {A/B/12, A/C/9, B/C/4, B/D/7, C/E/11, D/E/3, C/D/8}
  \draw[thick, gray!70!black] (\u) -- node[weight] {\w} (\v);
\end{tikzpicture}
```

## As a complete document

The same example in a document that compiles on its own with pdfLaTeX:

```latex
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}
\begin{tikzpicture}[
  town/.style={circle, draw, thick, minimum size=9mm, font=\small},
  weight/.style={font=\footnotesize, inner sep=1.5pt},
]
  \node[town] (A) at (0,0) {A};
  \node[town] (B) at (3,1.6) {B};
  \node[town] (C) at (3,-1.6) {C};
  \node[town] (D) at (6,1.2) {D};
  \node[town] (E) at (6,-1.2) {E};

  \foreach \u/\v/\w in {A/B/12, A/C/9, B/C/4, B/D/7, C/E/11, D/E/3, C/D/8}
  \draw[thick, gray!70!black] (\u) -- node[weight] {\w} (\v);
\end{tikzpicture}
\end{document}
```
