I need to draw a weighted graph using LaTeX but I don't know how to put the numbers on the edges. The numbers are like the costs of each edge. The graph can be drawn by this:
\begin{tikzpicture}
\draw
(1, 1) node[circle, black, draw](m){M}
(1, 3) node[circle, black, draw](i){I}
(1, 5) node[circle, black, draw](e){E}
(1, 7) node[circle, black, draw](a){A}
(3, 1) node[circle, black, draw](n){N}
(3, 3) node[circle, black, draw](j){J}
(3, 5) node[circle, black, draw](f){F}
(3, 7) node[circle, black, draw](b){B}
(5, 1) node[circle, black, draw](o){O}
(5, 3) node[circle, black, draw](k){K}
(5, 5) node[circle, black, draw](g){G}
(5, 7) node[circle, black, draw](c){C}
(7, 1) node[circle, black, draw](p){P}
(7, 3) node[circle, black, draw](l){L}
(7, 5) node[circle, black, draw](h){H}
(7, 7) node[circle, black, draw](d){D};
\draw[-] (a) -- (b);
\draw[-] (b) -- (c);
\draw[-] (c) -- (d);
\draw[-] (f) -- (g);
\draw[-] (g) -- (h);
\draw[-] (i) -- (j);
\draw[-] (j) -- (k);
\draw[-] (k) -- (l);
\draw[-] (m) -- (n);
\draw[-] (n) -- (o);
\draw[-] (o) -- (p);
\draw[-] (a) -- (e);
\draw[-] (b) -- (f);
\draw[-] (c) -- (g);
\draw[-] (d) -- (h);
\draw[-] (e) -- (i);
\draw[-] (f) -- (j);
\draw[-] (g) -- (k);
\draw[-] (i) -- (m);
\draw[-] (j) -- (n);
\draw[-] (k) -- (o);
\draw[-] (l) -- (p);
\end{tikzpicture}
We can now discuss the packages that we can use to draw graphs in LaTeX. The most common LaTeX package used for drawing, in general, is TikZ, which is a layer over PGF that simplifies its syntax.
As an example of a weighted graph, imagine you run an airline and you'd like a model to help you estimate fuel costs based on the routes you fly. In this example the nodes would be airports, edges would represent flights between airports, and the edge weight would be the estimated cost of flying between those airports.
You can check the Tikz example for many examples of annotated graphs (in particular, there's more than one way to place the edge labels).
A quick way to add an edge label is to add a node on your path:
\draw[-] (a) -- node[above] {1} (b);
\begin{tikzpicture}
\draw
(1, 1) node[circle, black, draw](m){M}
(1, 3) node[circle, black, draw](i){I}
(1, 5) node[circle, black, draw](e){E}
(1, 7) node[circle, black, draw](a){A}
(3, 1) node[circle, black, draw](n){N}
(3, 3) node[circle, black, draw](j){J}
(3, 5) node[circle, black, draw](f){F}
(3, 7) node[circle, black, draw](b){B}
(5, 1) node[circle, black, draw](o){O}
(5, 3) node[circle, black, draw](k){K}
(5, 5) node[circle, black, draw](g){G}
(5, 7) node[circle, black, draw](c){C}
(7, 1) node[circle, black, draw](p){P}
(7, 3) node[circle, black, draw](l){L}
(7, 5) node[circle, black, draw](h){H}
(7, 7) node[circle, black, draw](d){D};
\draw[-] (a) -- node[above] {1} (b);
\draw[-] (b) -- node[below] {2} (c);
\draw[-] (c) -- (d);
\draw[-] (f) -- (g);
\draw[-] (g) -- (h);
\draw[-] (i) -- (j);
\draw[-] (j) -- (k);
\draw[-] (k) -- (l);
\draw[-] (m) -- (n);
\draw[-] (n) -- (o);
\draw[-] (o) -- (p);
\draw[-] (a) -- (e);
\draw[-] (b) -- (f);
\draw[-] (c) -- (g);
\draw[-] (d) -- (h);
\draw[-] (e) -- (i);
\draw[-] (f) -- (j);
\draw[-] (g) -- (k);
\draw[-] (i) -- (m);
\draw[-] (j) -- (n);
\draw[-] (k) -- (o);
\draw[-] (l) -- (p);
\end{tikzpicture}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With