如何仅通过给出顶点集和边集在 tikz 中绘制图形?

如何仅通过给出顶点集和边集在 tikz 中绘制图形?

我陷入以下困境:

我需要绘制一个具有顶点集 V 和边集 E 的图 G。G 的顶点集为 1,2,3,4,5,....35,36。以下给出邻接标准:

1 is adjacent to the vertices  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

2 is adjacent to the vertices  1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35

3 is adjacent to the vertices  1 2 4 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 28 29 31 32 34 35

4 is adjacent to the vertices  1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35

5 is adjacent to the vertices  1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 21 22 23 24 26 27 28 29 31 32 33 
34 36

6 is adjacent to the vertices  1 5 7 11 13 17 19 23 25 29 31 35

7 is adjacent to the vertices  1 2 3 4 5 6 8 9 10 11 12 13 15 16 17 18 19 20 22 23 24 25 26 27 29 30 31 32 33 34 36

8 is adjacent to the vertices  1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35

9 is adjacent to the vertices  1 2 4 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 28 29 31 32 34 35

10 is adjacent to the vertices  1 3 7 9 11 13 17 19 21 23 27 29 31 33

11 is adjacent to the vertices  1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 21 23 24 25 26 27 28 29 30 31 32 34 35 36

12 is adjacent to the vertices  1 5 7 11 13 17 19 23 25 29 31 35

13 is adjacent to the vertices  1 2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 27 28 29 30 31 32 33 34 35 36

14 is adjacent to the vertices  1 3 5 9 11 13 15 17 19 23 25 27 29 31 33

15 is adjacent to the vertices  1 2 4 7 8 11 13 14 16 17 19 22 23 26 28 29 31 32 34

16 is adjacent to the vertices  1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35

17 is adjacent to the vertices  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 35 36

18 is adjacent to the vertices  1 5 7 11 13 17 19 23 25 29 31 35

19 is adjacent to the vertices  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

20 is adjacent to the vertices  1 3 7 9 11 13 17 19 21 23 27 29 31 33

21 is adjacent to the vertices  1 2 4 5 8 10 11 13 16 17 19 20 22 23 25 26 29 31 32 34

22 is adjacent to the vertices  1 3 5 7 9 13 15 17 19 21 23 25 27 29 31 35

23 is adjacent to the vertices  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26 27 28 29 30 31 32 33 34 35 36

24 is adjacent to the vertices  1 5 7 11 13 17 19 23 25 29 31 35

25 is adjacent to the vertices  1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 21 22 23 24 26 27 28 29 31 32 33 34 36

26 is adjacent to the vertices  1 3 5 7 9 11 15 17 19 21 23 25 27 29 31 33 35

27 is adjacent to the vertices  1 2 4 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 28 29 31 32 34 35

28 is adjacent to the vertices  1 3 5 9 11 13 15 17 19 23 25 27 29 31 33

29 is adjacent to the vertices  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 30 31 32 33 34 35 36

30 is adjacent to the vertices  1 7 11 13 17 19 23 29 31

31 is adjacent to the vertices  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 32 33 34 35 36

32 is adjacent to the vertices  1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35

33 is adjacent to the vertices  1 2 4 5 7 8 10 13 14 16 17 19 20 23 25 26 28 29 31 32 34 35

34 is adjacent to the vertices  1 3 5 7 9 11 13 15 19 21 23 25 27 29 31 33 35

35 is adjacent to the vertices  1 2 3 4 6 8 9 11 12 13 16 17 18 19 22 23 24 26 27 29 31 32 33 34 36

36 is adjacent to the vertices  1 5 7 11 13 17 19 23 25 29 31 35

我知道如何通过手动指定坐标来绘制图形。可以使用\node\draw 是否可以在不手动输入坐标的情况下绘制图形? 是否可以让 tikz 自动选择坐标并绘制图形?是否可以使用某个循环,只需指定顶点和边,图形就会绘制出来?

由于它是一个很大的图,因此手动指定坐标非常困难。

请帮助别人。

答案1

正如所提到的Torbjørn T.在评论中,TiZ 有一个graphdrawing库,可用于自动计算图形布局。

通过\usetikzlibrary{graphs,graphdrawing}启用图形绘图库。然后,您可以使用 选择多个图形绘图库\usegdlibrary

图形是通过给出顶点及其边来绘制的\graph。有几种方法可以做到这一点,但一种简单的方法(特别是如果图形数据是计算机生成的)是给出没有连接边的顶点,v并列出具有邻居的顶点的所有邻居v--u_1, v--u_2, ..., v--u_n

您必须从您加载的图形绘图库中选择一个布局。(库列表可在PGF 手册,第四部分)。

可以用绘制自循环v--[loop]v,在我的测试中v--v这还不够(参见使用 tikz-graphdrawing 库进行自循环)。

您需要使用 LuaLaTeX 编译该文档,因为 graphdrawing 库依赖于 Lua 组件。(基础图是由 igraph R 包为我最近的一次研讨会演讲生成的,我只是在这个例子中添加了一个新节点 10 和 1 处的自循环。)

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{fontspec}

\usepackage{tikz}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{circular,force,layered,routing}

\tikzset{
  graphs/simpleer/.style={
    nodes={draw,circle, blue, left color=blue!20, text=black, inner sep=1pt},
    node distance=2.5cm, nodes={minimum size=2em}
  },
  every loop/.style={},
}

\begin{document}
  \begin{figure}
    \centering
    \begin{tikzpicture}
      \graph[simpleer, simple necklace layout]{
        1--3, 1--[loop]1,
        2--6, 5--6,
        3--7, 4--7, 5--7, 6--7,
        1--8, 6--8, 7--8,
        5--9, 6--9,
        10;
      };
    \end{tikzpicture}
    \caption{Erdős--Rényi  graph for~\(n=9\), \(p_{9}=0.3\)
      plus one loop at~\(1\)
      and an unconnected vertex~\(10\)
      drawn in \texttt{simple necklace layout}}
  \end{figure}

  \begin{figure}
    \centering
    \begin{tikzpicture}
      \graph[simpleer, layered layout]{
        1--3, 1--[loop]1,
        2--6, 5--6,
        3--7, 4--7, 5--7, 6--7,
        1--8, 6--8, 7--8,
        5--9, 6--9,
        10;
      };
    \end{tikzpicture}
    \caption{Erdős--Rényi  graph for~\(n=9\), \(p_{9}=0.3\)
      plus one loop at~\(1\)
      and an unconnected vertex~\(10\)
      drawn in \texttt{layered layout}}
  \end{figure}

  \begin{figure}
    \centering
    \begin{tikzpicture}
      \graph[simpleer, spring layout]{
        1--3, 1--[loop]1,
        2--6, 5--6,
        3--7, 4--7, 5--7, 6--7,
        1--8, 6--8, 7--8,
        5--9, 6--9,
        10;
      };
    \end{tikzpicture}
    \caption{Erdős--Rényi  graph for~\(n=9\), \(p_{9}=0.3\)
      plus one loop at~\(1\)
      and an unconnected vertex~\(10\)
      drawn in \texttt{spring layout}}
  \end{figure}
\end{document}

图 1:圆形图


以下是输入图表的方法(对我来说,它看起来有点太拥挤,无法很好地绘制,但这可能是个人喜好问题)。

如果你有每个边的邻居列表,那么v -- {u_1,u_2,...,u_n},循环也必须单独列出。

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{fontspec}

\usepackage{tikz}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{circular}

\tikzset{
  graphs/simpleer/.style={
    nodes={draw,circle, blue, left color=blue!20, text=black, inner sep=1pt},
    nodes={minimum size=2em}
  },
  every loop/.style={},
}

\begin{document}
\begin{tikzpicture}
\graph[simpleer, simple necklace layout]{
  1 -- {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36},
  2 -- {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35},
  3 -- {1,2,4,5,7,8,10,11,13,14,16,17,19,20,22,23,25,26,28,29,31,32,34,35},
  4 -- {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35},
  5 -- {1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19,21,22,23,24,26,27,28,29,31,32,33,34,36},
  6 -- {1,5,7,11,13,17,19,23,25,29,31,35},
  7 -- {1,2,3,4,5,6,8,9,10,11,12,13,15,16,17,18,19,20,22,23,24,25,26,27,29,30,31,32,33,34,36},
  8 -- {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35},
  9 -- {1,2,4,5,7,8,10,11,13,14,16,17,19,20,22,23,25,26,28,29,31,32,34,35},
  10 -- {1,3,7,9,11,13,17,19,21,23,27,29,31,33},
  11 -- {1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,31,32,34,35,36},
  12 -- {1,5,7,11,13,17,19,23,25,29,31,35},
  13 -- {1,2,3,4,5,6,7,8,9,10,11,12,14,15,16,17,18,19,20,21,22,23,24,25,27,28,29,30,31,32,33,34,35,36},
  14 -- {1,3,5,9,11,13,15,17,19,23,25,27,29,31,33},
  15 -- {1,2,4,7,8,11,13,14,16,17,19,22,23,26,28,29,31,32,34},
  16 -- {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35},
  17 -- {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,35,36},
  18 -- {1,5,7,11,13,17,19,23,25,29,31,35},
  19 -- {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36},
  20 -- {1,3,7,9,11,13,17,19,21,23,27,29,31,33},
  21 -- {1,2,4,5,8,10,11,13,16,17,19,20,22,23,25,26,29,31,32,34},
  22 -- {1,3,5,7,9,13,15,17,19,21,23,25,27,29,31,35},
  23 -- {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,36},
  24 -- {1,5,7,11,13,17,19,23,25,29,31,35},
  25 -- {1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19,21,22,23,24,26,27,28,29,31,32,33,34,36},
  26 -- {1,3,5,7,9,11,15,17,19,21,23,25,27,29,31,33,35},
  27 -- {1,2,4,5,7,8,10,11,13,14,16,17,19,20,22,23,25,26,28,29,31,32,34,35},
  28 -- {1,3,5,9,11,13,15,17,19,23,25,27,29,31,33},
  29 -- {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,30,31,32,33,34,35,36},
  30 -- {1,7,11,13,17,19,23,29,31},
  31 -- {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,32,33,34,35,36},
  32 -- {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35},
  33 -- {1,2,4,5,7,8,10,13,14,16,17,19,20,23,25,26,28,29,31,32,34,35},
  34 -- {1,3,5,7,9,11,13,15,19,21,23,25,27,29,31,33,35},
  35 -- {1,2,3,4,6,8,9,11,12,13,16,17,18,19,22,23,24,26,27,29,31,32,33,34,36},
  36 -- {1,5,7,11,13,17,19,23,25,29,31,35},
  % loops are given separately
  1--[loop]1,
};
\end{tikzpicture}
\end{document}

图表上的完整内容

相关内容