Tikz 算法有几个阶段

Tikz 算法有几个阶段

我想画出两个算法,每个算法包含三个阶段,如附图所示。在我之前的帖子中(调整 tikz 中拟合节点的大小),我有一个包含两个阶段的算法,它比我想在这篇文章中获得的两个算法更容易控制。我试过了,但无法解决,从第二阶段到第三阶段,我不知道我该怎么做。因此,我再次请求您的帮助。非常感谢!我已经定义了输入、流程和决策节点,我从上一篇文章中重用了它们。

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{chains,
                fit,
                positioning,
                shapes}

\begin{document}
    \begin{tikzpicture}[
     node distance = 8mm and 12mm,
     start chain = A going right,
     start chain = B going below,
     base/.style = {draw, align=center, 
                    inner sep=2mm, on chain=A, join=by arr},
startstop/.style = {base, rounded rectangle},
      io/.style = {base, text width=2cm, trapezium, trapezium stretches body,
                    trapezium left angle=75, trapezium right angle=105},
  process/.style = {base, text width=2cm, minimum height=1cm},
 decision/.style = {base, text width=2cm, diamond, aspect=2, inner xsep=-4mm},
      arr/.style = {-stealth}
                        ]
\node   [startstop] {Start};                        % A-1
\node   [io] {S: an input data};
\node   [process] {A};
\node   [process] {B}; % A-4
    \node (f1) [draw, rounded corners, dashed, 
                fit=(A-2.bottom left corner) (A-2.top right corner) (A-4),
                label={The first phase}
                ]
                {};
%  
    \begin{scope}[base/.append style={on chain=B}] 
\node   [process, right=of A-4] {C}; % B-1
\node   [decision]      {Are clusters stable?};               % B-2
    \end{scope}
%
\draw[arr]  (A-4) -- (B-1);
%
    \node (f2) [draw, rounded corners, dashed, fit=(B-1)(B-2), label={[rotate=-90]right: The second phase}] {};
    \end{tikzpicture}
\end{document}

以下是我想要获得的两个流程图: 模型 1 模型 2

答案1

我认为chains这不是最佳选择。此答案定义了简单样式ab和,分别将节点放置在前一个节点的上方、下方、左侧和右侧。在我看来,这使得创建此类图表变得容易得多lr

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{fit,
                positioning,
                shapes}
\newcounter{nodeno}
\begin{document}
    \begin{tikzpicture}[node font=\sffamily,
    stepnodeno/.code=\stepcounter{nodeno},
    every node/.append style={stepnodeno,
        alias=LN-\number\value{nodeno}},
     a/.style={above=#1 of LN-\the\numexpr\value{nodeno}-1},
     b/.style={below=#1 of LN-\the\numexpr\value{nodeno}-1},
     r/.style={right=#1 of LN-\the\numexpr\value{nodeno}-1},
     l/.style={left=#1 of LN-\the\numexpr\value{nodeno}-1},
     a/.default=2em,b/.default=2em,l/.default=2em,r/.default=2em,
     base/.style = {draw, align=center, 
                    inner sep=2mm%, on chain=A, join=by arr
                    },
startstop/.style = {base, rounded rectangle},
      io/.style = {base, text width=3cm, trapezium, trapezium stretches body,
                    trapezium left angle=75, trapezium right angle=105},
  process/.style = {base, text width=2cm, minimum height=1cm},
 decision/.style = {base, text width=2cm, diamond, aspect=2, inner
        xsep=-5mm,align=center},
      arr/.style = {-stealth},
      task/.style={process,execute at begin node=Do Task\space#1,name=DT#1},
      output/.style={rectangle,path picture={
        \draw ([xshift=-\pgflinewidth/2,yshift=\pgflinewidth/2]path picture bounding box.south east)
        -| ([xshift=\pgflinewidth/2,yshift=-\pgflinewidth/2]path picture bounding box.north west) 
        --
        ([xshift=-1em,yshift=-\pgflinewidth/2]path picture bounding box.north east)
        --
        ([xshift=-\pgflinewidth/2,yshift=-1em]path picture bounding box.north east)
        -- cycle; },minimum width=1.5cm,minimum height=1cm},
      node distance = 2em and 2em]
\node[startstop] (Start) {Start};                        
\node[r=3em,io,minimum height=2cm] (S) {$S$: an input data\\
$x$: parameter};
\node[r=3em,task=1,minimum height=2cm]  {};
\node[r=3em,task=2] {}; 
\node[b,decision] (C1) {Cond\\ 1};
\node[b=4em,task=10] {};
\node[b=4em,task=9] {};
\node[l=5em,task=8] {};
\node[a,task=7,minimum height=1.5cm] {};
\node[a,task=6,minimum width=3.5cm] {};
\node[l,task=5,minimum height=1.5cm] {};
\node[l,task=4] {};
\node[right=of C1,decision] (C2) {Cond\\ 2};
\node[a,output] (out) {};
\node[r,startstop] (End) {End};
\foreach \X in {1,...,4}
{\draw[arr] (LN-\X) -- (LN-\the\numexpr\X+1);}
\foreach \X in {5,...,11}
{\draw[arr] (LN-\the\numexpr\X+1) -- (LN-\X);}
\draw[arr] (C1) -- node[above] (Y1) {Yes}(C2);
\draw[arr] (C2) |- node[left,pos=0.25] (N1) {No}(DT9);
\draw[arr] (C2) -- node[left] (Y2) {Yes}(out);
\draw[arr] (out) -- (End);
\draw[arr] (C1) -| node[above,pos=0.25] (N2) {No}(DT4);
\node (f1) [draw, rounded corners, dashed,inner sep=1ex, 
                fit=(S.bottom left corner) (S.top right corner) (DT1),
                label={[node font=\small\itshape\bfseries]The first phase}
                ]
                {};
\node (f2) [draw, rounded corners, dashed,inner sep=1ex,  
                fit=(DT8) (N2) (DT4) (DT6),
               label={[node font=\small\itshape\bfseries,rotate=90,anchor=south]left:The second phase}
                ]
                {};
\node (f3) [draw, rounded corners, dashed,inner sep=1ex,  
                fit=(DT2) (DT9) (End) (C1.west),
                label={[node font=\small\itshape\bfseries,rotate=-90,anchor=south]right:The third phase}
                ]
                {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

第二张图与之相同,但增加了一些矩阵。

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{fit,
                positioning,
                shapes}
\newcounter{nodeno}
\begin{document}
    \begin{tikzpicture}[node font=\sffamily,
    stepnodeno/.code=\stepcounter{nodeno},
    every node/.append style={stepnodeno,
        alias=LN-\number\value{nodeno}},
     a/.style={above=#1 of LN-\the\numexpr\value{nodeno}-1},
     b/.style={below=#1 of LN-\the\numexpr\value{nodeno}-1},
     r/.style={right=#1 of LN-\the\numexpr\value{nodeno}-1},
     l/.style={left=#1 of LN-\the\numexpr\value{nodeno}-1},
     a/.default=2em,b/.default=2em,l/.default=2em,r/.default=2em,
     base/.style = {draw, align=center, 
                    inner sep=2mm%, on chain=A, join=by arr
                    },
startstop/.style = {base, rounded rectangle},
      io/.style = {base, text width=3cm, trapezium, trapezium stretches body,
                    trapezium left angle=75, trapezium right angle=105},
  process/.style = {base, text width=2cm, minimum height=1cm},
 decision/.style = {base, text width=2cm, diamond, aspect=2, inner
        xsep=-5mm,align=center},
      arr/.style = {-stealth},
      task/.style={process,execute at begin node=Do Task\space#1,name=DT#1},
      output/.style={rectangle,path picture={
        \draw ([xshift=-\pgflinewidth/2,yshift=\pgflinewidth/2]path picture bounding box.south east)
        -| ([xshift=\pgflinewidth/2,yshift=-\pgflinewidth/2]path picture bounding box.north west) 
        --
        ([xshift=-1em,yshift=-\pgflinewidth/2]path picture bounding box.north east)
        --
        ([xshift=-\pgflinewidth/2,yshift=-1em]path picture bounding box.north east)
        -- cycle; },minimum width=1.5cm,minimum height=1cm},
      node distance = 2em and 2em]
\node[startstop] (Start) {Start};                        
\node[a=1em,io,minimum height=2cm] (S) {$S$: an input data\\
$x$: parameter};
\node[r=3em,task=1,minimum height=2cm]  {};
\matrix[r] (mat1){
\node[task=2,minimum width=16em]{}; \\[1ex]
\node[task=3,minimum width=16em]{}; \\
};
\node[right=3em of mat1,task=4] {}; 
\node[b,decision] (C1) {Cond\\ 1};
\node[b=7em,task=13] {};
\node[b=5em,task=12] {};
\node[l=6em,task=11] {};
\node[a,task=10] {};
\matrix[a,column sep=1ex](mat2){
\node[task=8,minimum width=6em,minimum height=3em]{}; &
\node[task=9,minimum width=6em,minimum height=3em]{}; \\
};
\node[above=of mat2,task=7,minimum width=3.5cm] {};
\node[l,task=6,minimum height=1.5cm] {};
\node[l,task=5] {};
\node[right=of C1,decision] (C2) {Cond\\ 2};
\node[a,output] (out) {};
\node[a,startstop] (End) {End};
%
\begin{scope}[arr]
 \foreach \X in {1,2,7,19,20}
  {\draw (LN-\X) -- (LN-\the\numexpr\X+1);}
 \foreach \X in {8,9,10,11,16,17}
  {\draw (LN-\the\numexpr\X+1) -- (LN-\X);}
 \foreach \X in {2,3}
 {\draw (DT1.east) -- ++ (1em,0) |- (DT\X);
 \draw (DT\X.east) -- ++ (1em,0) coordinate(aux1) |- (DT4);} 
 \foreach \X in {8,9}
 {\draw (DT7.south) -- ++ (0,-1em) -| (DT\X);
 \draw (DT\X.south) -- ++ (0,-1em) -| (DT10);
 } 
 \draw[arr] (C1) -- node[above] (Y1) {Yes}(C2);
 \draw[arr] (C2) |- node[left,pos=0.25] (N1) {No}(DT12);
 \draw[arr] (C1) -| node[above,pos=0.25] (N2) {No}(DT5);
\end{scope}
%
\node (f1) [draw, rounded corners, dashed,inner sep=1ex, 
                 fit=(S.bottom left corner) (S.top right corner) (mat1) (aux1),
                 label={[node font=\small\itshape\bfseries]The first phase}
                 ]
                 {};
\node (f2) [draw, rounded corners, dashed,inner sep=1ex,  
                fit=(DT5) (DT7) (DT11) (DT9),
               label={[node font=\small\itshape\bfseries,rotate=90,anchor=south]left:The second phase}
                ]
                {};
\node (f3) [draw, rounded corners, dashed,inner sep=1ex,  
                fit=(DT4) (DT12) (C1.west) (C2.east),
                label={[node font=\small\itshape\bfseries,rotate=-90,anchor=south]right:The third phase}
                ]
                {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容