开发者

LaTeX - Adding node labels above a sorted list of edges for a weighted graph

开发者 https://www.devze.com 2022-12-22 00:08 出处:网络
This seems like it would be fairly simple, but I\'ve been unable to find anything about how to do this on the web. I\'m writing the steps for Kruskal\'s algorithm.I need to create a list of edges sort

This seems like it would be fairly simple, but I've been unable to find anything about how to do this on the web. I'm writing the steps for Kruskal's algorithm. I need to create a list of edges sorted by weight, with labels above the weight referencing the edges, something like this:

a-b b-c c-e
 1   3   5  ...

Design and Analysis of Algorithms by Anany Levitin uses similar formatting on p. 316. If there are no other options, I suppose verbatim might work, though I was hoping to make it 开发者_JAVA百科look a little nicer. Any recommendations?


As someone suggested, TikZ might be overkill, but it does work.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) node[anchor=east]{a} -- (0.4,0) node[anchor=west]{b};
\draw (0.2,0) node[anchor=north]{1};
\draw (2,0) node[anchor=east]{b} -- (2.4,0) node[anchor=west]{c};
\draw (2.2,0) node[anchor=north]{3};
\draw (4,0) node[anchor=east]{c} -- (4.4,0) node[anchor=west]{e};
\draw (4.2,0) node[anchor=north]{5};
\end{tikzpicture}
\end{document}

EDIT: Actually, this is probably simpler and does what you want:

\[ \left. \begin{array}{ccc}
   {a-b \atop 1} & {b-c \atop 3} & {c-e \atop 5}
   \end{array} \right. \]

You can compare for yourself: tikzedgeweights.pdf


What about something like:

$a\xrightarrow{1}b$

or

$b\xrightarrow[{3}]{}c$

or

$c\overset{5}{-}e$

or

$c\underset{3}{-}f$

(with \usepackage{amsmath} in your preamble)

You can find more advanced math techniques here: http://en.wikibooks.org/wiki/LaTeX/Advanced_Mathematics


For a bit more finesse and better looks use

\usepackage{amsmath}
\begin{document}

\[ \overset{a-b}{1} \qquad \overset{\text{\it c-b}}{2} \]

\end{document}

I gave you two different-looking possibilities here -- pick what you like more.

You can achieve the same effect without amsmath by modifying Steve's code to

\[ \left. \begin{array}{ccc}
   {a-b \atop {\displaystyle 1}} & {b-c \atop {\displaystyle 3}}
    & {c-e \atop {\displaystyle 5}} 
   \end{array} \right. \]


tikz is probably your best bet, though it is a little heavyweight for what you're after. Perhaps xypic would offer a better solution, and one that isn't quite as intensive as tikz. I'm not sure what you want the thing to look like, but would this be close:

   A \ar[r]^{label} & B

Or something like that?

0

精彩评论

暂无评论...
验证码 换一张
取 消