图算法

图算法

我必须制作一个图上算法的动画,我必须制作类似 bfs 的东西,从一个顶点开始,然后增加其他顶点的值。然后我们取另一个顶点并再次开始,最后所有顶点的值将相等。我尝试了一些东西来解决这个问题,但我不知道如何做到这一点。这是我写的:

    \documentclass[hyperref={pdfpagelabels=false}]{beamer}
\usepackage{lmodern}

\usepackage[utf8]{inputenc} 
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}    
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}

\tikzstyle{vertex}=[circle,fill=black!25,minimum size=15pt,inner sep=0pt]
\tikzstyle{selected vertex} = [vertex, fill=red!24]
\tikzstyle{blue vertex} = [vertex, fill=blue!24]
\tikzstyle{edge} = [draw,thick,-]
\tikzstyle{weight} = [font=\big]
\tikzstyle{selected edge} = [draw,line width=5pt,-,red!50]
\tikzstyle{ignored edge} = [draw,line width=5pt,-,black!20]

\usefonttheme{professionalfonts}
\beamertemplatenavigationsymbolsempty
\setbeamertemplate{navigation symbols}{}%

\begin{document}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\def \n {10}
\def \a {1}
\def \b {2}
\def \c {3}
\def \d {4}
\def \e {5}
\def \f {6}
\def \g {7}
\def \h {8}
\def \i {9}
\def \j {10}

\begin{frame}
    \begin{figure}
        \begin{tikzpicture}[->,scale=1.8, auto,swap]

            \foreach \pos/\name/\z in {{(0,0)/a/\a}, {(1,-1)/b/\b}, {(1.5,-2)/c/\c},
                                    {(1.3,-2.5)/d/\d}, {(0.6,-2.5)/e/\e}, {(2,-2.66)/f/\f}, 
                                    {(1.3,-3.5)/g/\g},{(0.6,-3.5)/h/\h},{(2,-3.33)/i/\i},
                                    {(1.9,-1.3)/j/\j}}
                \node[vertex] (\name) at \pos {\z};


            \foreach \source/ \dest /\pos in {a/b/bend right, b/c/bend left, c/d/, d/e/bend right,
                                             d/f/bend left, e/h/bend right, f/i/bend right, h/g/bend right,                                                  i/g/bend left, c/j/bend right, c/j/bend left}
                \path (\source) edge [\pos] node {} (\dest);
           %there must be an animation

        \end{tikzpicture}
    \end{figure}
\end{frame}
\end{document}

答案1

一种选择是使用键值方法(通过keyval包)来更改节点的值。您只需使用覆盖规范和命令\mylistvalues来更新每张幻灯片所需的值;例如,

\mylistvalues{va=1,vb=2,vc=3,vd=4,ve=5,vf=6,vg=7,vh=8,vi=9,vj=10}

为所有节点分配初始值并

  \mylistvalues<5->{va=10,vb=9,vc=8,vd=7}

从第五张幻灯片开始,更改前四个顶点的值。

以下是使用此方法制作的动画:

在此处输入图片描述

代码:

\documentclass[hyperref={pdfpagelabels=false}]{beamer}
\usepackage{lmodern}

\usepackage[utf8]{inputenc} 
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}    
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}
\usepackage{keyval}

\makeatletter
\define@key{myani}{va}{\def\Va{#1}}
\define@key{myani}{vb}{\def\Vb{#1}}
\define@key{myani}{vc}{\def\Vc{#1}}
\define@key{myani}{vd}{\def\Vd{#1}}
\define@key{myani}{ve}{\def\Ve{#1}}
\define@key{myani}{vf}{\def\Vf{#1}}
\define@key{myani}{vg}{\def\Vg{#1}}
\define@key{myani}{vh}{\def\Vh{#1}}
\define@key{myani}{vi}{\def\Vi{#1}}
\define@key{myani}{vj}{\def\Vj{#1}}
\newcommand<>\mylistvalues[1]{\only#2{\setkeys{myani}{#1}}}
\makeatother

\tikzstyle{vertex}=[circle,fill=black!25,minimum size=15pt,inner sep=0pt]
\tikzstyle{selected vertex} = [vertex, fill=red!24]
\tikzstyle{blue vertex} = [vertex, fill=blue!24]
\tikzstyle{edge} = [draw,thick,-]
\tikzstyle{weight} = [font=\big]
\tikzstyle{selected edge} = [draw,line width=5pt,-,red!50]
\tikzstyle{ignored edge} = [draw,line width=5pt,-,black!20]

\usefonttheme{professionalfonts}
\beamertemplatenavigationsymbolsempty
\setbeamertemplate{navigation symbols}{}%

\def\n{10}
% Initialize the values
\mylistvalues{va=1,vb=2,vc=3,vd=4,ve=5,vf=6,vg=7,vh=8,vi=9,vj=10}

\begin{document}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\begin{frame}
    \begin{figure}
        \begin{tikzpicture}[->,scale=1.8, auto,swap]
            \foreach \pos/\name/\z in {{(0,0)/a/\Va}, {(1,-1)/b/\Vb}, {(1.5,-2)/c/\Vc},
                                    {(1.3,-2.5)/d/\Vd}, {(0.6,-2.5)/e/\Ve}, {(2,-2.66)/f/\Vf}, 
                                    {(1.3,-3.5)/g/\Vg},{(0.6,-3.5)/h/\Vh},{(2,-3.33)/i/\Vi},
                                    {(1.9,-1.3)/j/\Vj}}
{
  \mylistvalues<2->{va=2,vb=3,vc=4,vd=5,ve=6,vf=7,vg=8,vh=9,vi=10}
  \mylistvalues<3->{va=3,vb=4,vc=5,vd=6,ve=7,vf=9,vg=9,vh=10}
  \mylistvalues<4->{va=4,vb=5,vc=6,vd=7,ve=8,vf=9,vg=10}
  \mylistvalues<5->{va=5,vb=6,vc=7,vd=8,ve=9,vf=10}
  \mylistvalues<6->{va=6,vb=7,vc=8,vd=9,ve=10}
  \mylistvalues<7->{va=7,vb=8,vc=9,vd=10}
  \mylistvalues<8->{va=8,vb=9,vc=10}
  \mylistvalues<9->{va=9,vb=10}
  \mylistvalues<10>{va=10}

  \node<1->[vertex] (\name) at \pos {\z};
}
            \foreach \source/ \dest /\pos in {a/b/bend right, b/c/bend left, c/d/, d/e/bend right,
                                             d/f/bend left, e/h/bend right, f/i/bend right, h/g/bend right,                                                  i/g/bend left, c/j/bend right, c/j/bend left}
                \path (\source) edge [\pos] node {} (\dest);

        \end{tikzpicture}
    \end{figure}
\end{frame}

\end{document}

顺便说一句,\tikzstyle您应该改用更方便的\tikzset语法,而不是旧的语​​法。

相关内容