\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\tikzstyle{vertex}=[auto=left,circle,fill=black!25,minimum size=20pt,inner sep=0pt]
\begin{document}
\begin{tikzpicture}
\node[vertex] (n1) at (1,4) {s};
\node[vertex] (n2) at (3,2) {};
\node[vertex] (n3) at (3,3) {};
\node[vertex] (n4) at (3,4) {};
\node[vertex] (n5) at (3,6) {};
\node[vertex] (n6) at (9,4) {t};
\node[vertex] (n7) at (7,6) {};
\node[vertex] (n8) at (7,4) {};
\node[vertex] (n9) at (7,3) {};
\node[vertex] (n10) at (7,2) {};
\foreach \from/\to in {n1/n2,n1/n3,n1/n4,n1/n5,
n6/n7,n6/n8,n6/n9,n6/n10}
\draw[densely dotted] (\from) -- (\to);
\foreach \from/\to in {n2/n10,n3/n9,n4/n8,n5/n7}
\draw(\from) -- (\to);
\end{tikzpicture}
\end{document}
我正在尝试在节点(n4,n5)
和之间获取垂直点(n6,n7)
。我尝试了很多方法,但仍然不行。
我也尝试向图中添加边权重。我不断找到以矩阵形式编写的链接。
我需要添加表示树上的左节点和右节点的文本。如何在树上的所需位置添加文本?
答案1
要放置任何字符,例如
\dots
,您可以使用\path
并通过放置所需的字符node
并使用sloped
选项以便字符沿路径旋转:\path (n4) -- (n5) node [red, font=\Huge, midway, sloped] {$\dots$}; \path (n6) -- (n7) node [red, font=\Huge, midway, sloped] {$\dots$};
如果您对这些不满意,可以添加小实心圆圈。
left
可以通过/选项添加和定位位于开始和结束处的节点,right
以便将文本放置在节点特定点的左侧/右侧:\node [left ] at (n1.west) {start}; \node [right] at (n6.east) {end};
要在每个边缘上方添加文本,您可以向循环添加另一个选项
\foreach
并通过节点放置文本:node [midway, above, orange] {$\weight$}
代码:
\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\tikzstyle{vertex}=[auto=left,circle,fill=black!25,minimum size=20pt,inner sep=0pt]
\begin{document}
\begin{tikzpicture}
\node[vertex] (n1) at (1,4) {s};
\node[vertex] (n2) at (3,2) {$n_2$};
\node[vertex] (n3) at (3,3) {$n_3$};
\node[vertex] (n4) at (3,4) {$n_4$};
\node[vertex] (n5) at (3,6) {$n_5$};
\node[vertex] (n6) at (9,4) {t};
\node[vertex] (n7) at (7,6) {$n_7$};
\node[vertex] (n8) at (7,4) {$n_8$};
\node[vertex] (n9) at (7,3) {$n_9$};
\node[vertex] (n10) at (7,2) {$n_{10}$};
\foreach \from/\to/\weight in {n1/n2/a, n1/n3/b, n1/n4/c, n1/n5/d,
n6/n7/e, n6/n8/f, n6/n9/g, n6/n10/h}
\draw[densely dotted] (\from) -- (\to) node [midway, above, orange] {$\weight$};
\foreach \from/\to/\weight in {n2/n10/i, n3/n9/j, n4/n8/k, n5/n7/l}
\draw(\from) -- (\to) node [midway, above, orange] {$\weight$};;
% These are for dotted lines
%\draw [red, dotted, ultra thick] (n4) -- (n5);
%\draw [blue,dotted, ultra thick] (n6) -- (n7);
\path (n4) -- (n5) node [red, font=\Huge, midway, sloped] {$\dots$};
\path (n6) -- (n7) node [red, font=\Huge, midway, sloped] {$\dots$};
\node [left , red] at (n1.west) {start};
\node [right, red] at (n6.east) {end};
\end{tikzpicture}
\end{document}