如何用 tkz-graph 绘制锦标赛有向图?

如何用 tkz-graph 绘制锦标赛有向图?

请问,我想用 tikz 绘制一个 n 阶锦标赛有向图(例如 n=6)。但我不知道如何在第二个循环中排除重复的顶点。这是我的代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{tkz-graph}
\usetikzlibrary{graphs,graphs.standard}

    \begin{document}
            \begin{tikzpicture}
            \SetGraphUnit{2}
            \renewcommand*{\VertexLineColor}{white}
            \renewcommand*{\VertexLightFillColor}{red}
            \renewcommand*{\VertexLineWidth}{1pt}
            \GraphInit[vstyle=Welsh]
            \Vertices{circle}{1,2,3,4,5,6}
            \AddVertexColor{blue}{5,1}
            \SetUpEdge[style={->,very thick},color=red]
            \foreach \v in {1,...,5}{
              \foreach \vv in {\v,...,6}{\Edge(\v)(\vv)};  
            };

        \end{tikzpicture}
    \end{document}

在此处输入图片描述

答案1

使用\ifthenelse建築。

输出

在此处输入图片描述

代码

\documentclass[12pt,tikz]{standalone}
\usepackage{tkz-graph}
\begin{document}
\begin{tikzpicture}
  \SetGraphUnit{2}
  \renewcommand*{\VertexLineColor}{white}
  \renewcommand*{\VertexLightFillColor}{red}
  \renewcommand*{\VertexLineWidth}{1pt}
  \GraphInit[vstyle=Welsh]
  \Vertices{circle}{1,2,3,4,5,6}
  \AddVertexColor{blue}{5,1}
  \SetUpEdge[style={->,very thick},color=red]
  \foreach \v in {1,...,5}
  {
    \foreach \vv in {\v,...,6}
    {
      \ifthenelse{\v=\vv}%
      {}% the empty `then` clause
      {%
        \Edge(\v)(\vv)% the `else` clause
      }  
    }
  }
\end{tikzpicture}
\end{document}

根据请求

在此处输入图片描述

变量采用法语名称,以表示对阿兰神秘tkz-graph手册的感谢。

\documentclass[12pt,tikz]{standalone}
\usepackage{tkz-graph}
\begin{document}
\begin{tikzpicture}
  \SetGraphUnit{2}
  \renewcommand*{\VertexLineColor}{white}
  \renewcommand*{\VertexLightFillColor}{red}
  \renewcommand*{\VertexLineWidth}{1pt}
  \GraphInit[vstyle=Welsh]
  \Vertices{circle}{A,B,C,D,E,F}
  \AddVertexColor{blue}{E,A}
  \SetUpEdge[style={->,very thick},color=red]
  \foreach \lettre/\nombre in {A/1,B/2,C/3,D/4,E/5}
  {
    \foreach \llettre/\nnombre in {A/1,B/2,C/3,D/4,E/5,F/6}
    {
      \ifthenelse{\nombre<\nnombre}
      {%
        \Edge(\lettre)(\llettre)
      }  
      {}
    }
  }
\end{tikzpicture}
\end{document}

答案2

\documentclass{article} 
\usepackage{tkz-graph} 
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{tikzpicture}
    \SetGraphUnit{2}
    \renewcommand*{\VertexLineColor}{white}
    \renewcommand*{\VertexLightFillColor}{red}
    \renewcommand*{\VertexLineWidth}{1pt}
    \GraphInit[vstyle=Welsh]
    \Vertices{circle}{1,2,3,4,5,6}
    \AddVertexColor{blue}{5,1}
    \SetUpEdge[style={-{Straight Barb[length=1mm,width=1.2mm]}, thick},color=red]
     \foreach \v [count=\vi from 2] in {1,...,5}{
     \foreach \vv in {\vi,...,6}{\Edge(\v)(\vv)};      
     }; 
\end{tikzpicture}    
\end{document}

给出:

![在此处输入图片描述

附录: 针对您的一条评论中的请求,提供解决方案:

\documentclass{article}
\usepackage{tkz-graph}
\usetikzlibrary{arrows.meta}


\begin{document}
    \begin{tikzpicture}
    \SetGraphUnit{2}
    \renewcommand*{\VertexLineColor}{white}
    \renewcommand*{\VertexLightFillColor}{red}
    \renewcommand*{\VertexLineWidth}{1pt}
    \GraphInit[vstyle=Welsh]
    \SetVertexNoLabel % <-- added
    \Vertices{circle}{1,2,3,4,5,6}
    \SetUpEdge[style={-{Straight Barb[length=1mm,width=1.2mm]}, thick},color=red]
     \foreach \v [count=\vi from 2] in {1,...,5}
     {
     \foreach \vv in {\vi,...,6}{\Edge(\v)(\vv)};
     };
     \SetVertexLabel
    \Vertices{circle}{A,B,C,D,E,F} % <-- added
    \AddVertexColor{blue}{1,5}% <-- moved here
\end{tikzpicture}
\end{document}

![在此处输入图片描述

附录(2): 无需双重绘制顶点的命名解决方案:

\documentclass{article}
\usepackage{tkz-graph}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{tikzpicture}
\SetGraphUnit{2}
\renewcommand*{\VertexLineColor}{white}
\renewcommand*{\VertexLightFillColor}{red}
\renewcommand*{\VertexLineWidth}{1pt}
\GraphInit[vstyle=Welsh]
\Vertices{circle}{A,B,C,D,E,F}
\SetUpEdge[style={-{Straight Barb[length=1mm,width=1.2mm]}, thick},color=red]
 \foreach \v [remember=\v as \vi (initially A)] in {B,...,F}
 {
 \foreach \vv in {\v,...,F}{\Edge(\vi)(\vv)};
 };
\AddVertexColor{blue}{A,E}
    \end{tikzpicture}
\end{document}

答案3

另一种方式,相同的输出。

\documentclass[12pt,tikz]{standalone}
\usepackage{tkz-graph}
\begin{document}
\begin{tikzpicture}
  \SetGraphUnit{2}
  \renewcommand*{\VertexLineColor}{white}
  \renewcommand*{\VertexLightFillColor}{red}
  \renewcommand*{\VertexLineWidth}{1pt}
  \GraphInit[vstyle=Welsh]
  \Vertices{circle}{1,2,3,4,5,6}
  \AddVertexColor{blue}{5,1}
  \SetUpEdge[style={->,very thick},color=red]
  \foreach \v in {1,...,5}
  {
    \foreach \vv in {\number\numexpr\v+1\relax,...,6}
    {
        \Edge(\v)(\vv)
    }
  }
\end{tikzpicture}
\end{document}

相关内容