在图上绘制Dijkstra算法而不将其添加为图像?

在图上绘制Dijkstra算法而不将其添加为图像?

有没有可能不用我糟糕的绘画技巧就能画出下图所示的图画?尤其是这条半透明的背景线(从 s 到 t)?

在此处输入图片描述

我设法做的是:

在此处输入图片描述

代码:

   \documentclass[12pt]{article}
   \usepackage{tikz}

   \begin{document}

   \begin{center}
   \begin{tikzpicture}[scale=0.2]
   \tikzstyle{every node}+=[inner sep=0pt]
   \draw [black] (8.9,-31.3) circle (3);
   \draw [black] (24.7,-14.8) circle (3);
   \draw [black] (23.9,-49.9) circle (3);
   \draw [black] (48.7,-49.9) circle (3);
   \draw [black] (47.5,-14.8) circle (3);
   \draw [black] (62.2,-30.2) circle (3);
   \draw [black] (10.97,-29.13) -- (22.63,-16.97);
   \fill [black] (22.63,-16.97) -- (21.71,-17.2) -- (22.43,-17.89);
   \draw (17.33,-24.52) node [right] {$2$};
   \draw [black] (11.595,-32.613) arc (60.58823:17.18076:24.968);
   \fill [black] (23.19,-46.99) -- (23.43,-46.08) -- (22.47,-46.37);
   \draw (19.33,-37.26) node [right] {$8$};
   \draw [black] (21.094,-48.846) arc (-114.6653:-167.56571:21.027);
   \fill [black] (9.34,-34.27) -- (9.02,-35.15) -- (10,-34.94);
   \draw (12.94,-44.36) node [left] {$8$};
   \draw [black] (45.7,-49.9) -- (26.9,-49.9);
   \fill [black] (26.9,-49.9) -- (27.7,-50.4) -- (27.7,-49.4);
   \draw (36.3,-49.4) node [above] {$1$};
   \draw [black] (27.7,-14.8) -- (44.5,-14.8);
   \fill [black] (44.5,-14.8) -- (43.7,-14.3) -- (43.7,-15.3);
   \draw (36.1,-15.3) node [below] {$6$};
   \draw [black] (8.446,-28.337) arc (-175.32366:-318.38669:21.341);
   \fill [black] (8.45,-28.34) -- (8.88,-27.5) -- (7.88,-27.58);
   \draw (20.36,-6.46) node [above] {$1$};
   \draw [black] (49.57,-16.97) -- (60.13,-28.03);
   \fill [black] (60.13,-28.03) -- (59.94,-27.11) -- (59.21,-27.8);
   \draw (55.38,-21.03) node [right] {$2$};
   \draw [black] (50.4,-47.43) -- (60.5,-32.67);
   \fill [black] (60.5,-32.67) -- (59.64,-33.05) -- (60.46,-33.62);
   \draw (56.05,-41.4) node [right] {$7$};
   \draw [black] (47.496,-47.153) arc (-158.28996:-197.7939:43.716);
   \fill [black] (47.5,-47.15) -- (47.66,-46.22) -- (46.74,-46.59);
   \draw (43.87,-32.49) node [left] {$5$};
   \draw [black] (48.633,-17.577) arc (20.35262:-16.43648:46.729);
   \fill [black] (48.63,-17.58) -- (48.44,-18.5) -- (49.38,-18.15);
   \draw (52.07,-32.22) node [right] {$4$};
   \draw [black] (26.39,-17.28) -- (47.01,-47.42);
   \fill [black] (47.01,-47.42) -- (46.97,-46.48) -- (46.14,-47.05);
   \draw (37.3,-31) node [right] {$3$};
   \draw [black] (24.63,-17.8) -- (23.97,-46.9);
   \fill [black] (23.97,-46.9) -- (24.49,-46.11) -- (23.49,-46.09);
   \draw (23.77,-32.35) node [left] {$5$};
   \end{tikzpicture}
   \end{center}

   \end{document}

答案1

一个pstricks办法:

\documentclass[border=5pt, svgnames]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pst-node}
\usepackage{auto-pst-pdf} 
%% To compile with pdflatex -shell-escape (TeX Live, MacTeX) %% 
%% or pdflatex --enable-write18 (MiKTeX)

\begin{document}

\psset{arrowinset=0.12, arrows =->, shortput = nab}
$ \psset{mnode=Circle, radius = 0.25cm}\psset{fillstyle = solid, fillcolor = Gainsboro!60}
  \def\pscolhooki{\psset{fillstyle = solid, fillcolor = SteelBlue}}
  \begin{psmatrix}[colsep=1.5cm, rowsep = 1cm]
    & [name = t] 2 & [name = v] ∞ \\
    [name = s]\textcolor{white}{0} & & & [name = x] ∞ \\
    & [name = u]8 & [name = w]∞
    %%% labels and arrows
    \psset{labelsep = 2pt}
    \foreach \T/\W in {v/6,w/3,u/5}{\ncline{t}{\T}\naput{\W}}
    \ncline{v}{x}²
    \ncline{w}{x}₇
    \ncline{w}{u}₁
    %%
    \psset{fillstyle = none, arcangle = -20}
    {\psset{border = 2pt, bordercolor = Gainsboro}
      \ncarc{s}{t}₂
      \ncarc{s}{u}₈}
    \ncarc{u}{s}⁸
    \ncarc[nodesep= 0pt]{v}{w}\nbput{5}
    \ncarc{w}{v}\nbput{4}
    \ncarc[arcangle = -65]{v}{s}\nbput[npos = 0.7]{1}
    % \nccurve{linecolor = red}{v}{s}
    \psset{labelsep = 1em}
    \foreach \L in {s, t, u}{\uput[l](\L){\L\strut}}
    \foreach \L in {v, w, x}{\uput[r](\L){\L}}
  \end{psmatrix} $

\end{document} 

在此处输入图片描述

答案2

编辑:

现在我发现我忽略了所需图像已包含在状态节点中,并且它们是彩色的。我现在已经添加了它。

像这样:

在此处输入图片描述

\documentclass[12pt, tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta, automata, positioning, quotes}

\begin{document}
    \begin{tikzpicture}[
node distance = 22mm and 24mm,
every state/.append style = {inner sep=0pt, fill=gray!10,
                             minimum size=7mm},
every edge/.style = {draw, -Triangle, bend angle=15},
                auto=right,
                        ]
\node (s1) [state,fill=gray!50]         {0};
\node (s2) [state, above right=of s1]   {2};
\node (s3) [state, right=of s2]         {$\infty$};
\node (s4) [state, below right=of s3]   {$\infty$};
\node (s5) [state, below  left=of s4]   {$\infty$};
\node (s6) [state, left=of s5]          {8};
%
\draw[gray!30, line width=5pt]
        (s1) to                     (s2)
        (s1) to [bend right=15]     (s6);
%
\draw   (s1) edge ["2"]             (s2)
        (s1) edge [bend right,"8"]  (s6)
        (s2) edge ["6"]             (s3)
        (s2) edge ["3"]             (s5)
        (s2) edge ["5"]             (s6)
        (s3) edge [out=135, in=90,looseness=1.5, "1"]  (s1)
        (s3) edge ["2"]             (s4)
        (s3) edge [bend right,"5"]  (s5)
        (s5) edge [bend right,"4"]  (s3)
        (s5) edge ["7"]             (s4)
        (s5) edge ["1"]             (s6)
        (s6) edge [bend right,"8"]  (s1);
   \end{tikzpicture}
\end{document}

如您所见,上述解决方案已tikz按照您在代码示例中使用的方式完成。

相关内容