2

I am stuck at the following:

I need to draw a graph G with vertex set V and edge set E. The set of vertices of G is 1,2,3,4,5,....35,36. The following gives the adjacency criterion:

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

I know how to draw the graph by manually specifying the co-ordinates. It can be done using \node and \draw Is it possible to draw the graph without manually entering the co-ordinates? Is it possible to make tikz automatically choose the co-ordinates and draw the graph?is it possible to use some loop where I just specify the vertices and edges and the graph gets drawn?

Since its a big graph its very difficult to manually specify the coordinates.

Please help someone.

Charlotte
  • 963
  • TikZ does have features for automatic positioning of graphs, though I have no idea how to apply your criteria. See chapter 19 Specifying graphs, and Part IV Graph drawing of the manual. – Torbjørn T. Jul 04 '20 at 09:56
  • @TorbjørnT.; is it possible to draw a graph in tikz just by specifying the vertices and edges? – Charlotte Jul 04 '20 at 10:45
  • I do believe that is what is described in the manual, yes. But there are various algorithms for the positioning. While waiting for answers, I suggest looking at the manual, or searching this site for examples. – Torbjørn T. Jul 04 '20 at 10:49
  • How do I get the manual? Do you have the link? can you share it pleass – Charlotte Jul 04 '20 at 11:10
  • Texdoc.net(http://texdoc.net/texmf-dist/doc/generic/pgf/pgfmanual.pdf), or go to ctan.org. You may also already have it on your computer, as part of the TeX distribution you're using. To find it, open a terminal/command prompt, type texdoc pgf and hit enter. – Torbjørn T. Jul 04 '20 at 11:14
  • @TorbjørnT.; thanks for the manual , i just checked it but I could not find how to draw a graph given its vertex and edge set , do you mind sharing the code on how to do it – Charlotte Jul 04 '20 at 11:20
  • I'm not really familiar with this kind of stuff, so I can't just "share the code". What about the example on page 420 in the manual (start of chapter 28)? Beyond that, and searching, I suggest waiting a bit to see if other people will answer, I probably can't. (I may not even have understood your question fully.) – Torbjørn T. Jul 04 '20 at 11:29
  • This looks like another question you've posted here. I've answered that and have shown you how to relabel the vertices as well. – DJP Jul 04 '20 at 14:09

1 Answers1

4

As mentioned by Torbjørn T. in the comments, TikZ has a graphdrawing library that can be used to automatically calculate the graph layout.

With \usetikzlibrary{graphs,graphdrawing} you enable the graph drawing library. Then you can select several graph drawing libraries with \usegdlibrary.

A graph is drawn by giving vertices and their edges in \graph. There are several ways to do that, but a simple method (especially if the graph data is computer generated) is to give vertices without connecting edges as v and list all neighbours of a vertex that has neighbours in v--u_1, v--u_2, ..., v--u_n form.

You have to select a layout from one of the graph drawing libraries you loaded. (A list of libraries can be found in the PGF manual, Part IV).

A self-loop can be drawn with v--[loop]v, in my tests v--v was not enough (see Self-loop with tikz-graphdrawing library).

You will need to compile the document with LuaLaTeX, since the graphdrawing library relies on Lua components. (The base graph was generated by the igraph R package for a recent seminar talk of mine, I just added a new node 10 and the self-loop at 1 for this example.)

% !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}

Figure 1: circular graph


Here's how you could input your graph (it looks a bit too crowded to be drawn nicely to me, but that may be a matter of taste).

If you have lists of neighbours for each edge, say v -- {u_1,u_2,...,u_n},, again loops have to be listed separately.

% !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}

full on graph

moewe
  • 175,683
  • Thanks a lot for the answer but how to compile in LuaLatex? I never used it, is it anything that needs to be installed separately – Charlotte Jul 04 '20 at 12:05
  • @Math_Freak Most editors will have a button to run LuaLaTeX. If you normally compile from the command line, just run lualatex <document> instead of pdflatex <document>. How do you compile your document at the moment? What editor do you use? Note that some changes may be needed if you switch from (pdf)LaTeX to LuaLaTeX, see https://tex.stackexchange.com/q/28642/35864 (in many cases it is enough to ensure the document is UTF-8 encoded, to remove inputenc and fontenc from the preamble and change the font setup to fontspec (which allows you to load system fonts)). – moewe Jul 04 '20 at 12:07
  • I use TexStudio and MikTex. Where will I write lualatex document? In TexStudio I just click the double play button(green symbol with two arrows) and it gets complied – Charlotte Jul 04 '20 at 12:08
  • @Math_Freak Tools > Commands > LuaLaTeX runs LuaLaTeX on the document. Then you can inspect it by clicking on "View"/F7. (It may also be enough to add a magic command to the top of the document, see the edit.) – moewe Jul 04 '20 at 12:11
  • Is it possible to draw the graph without the necklace ?? – Charlotte Jul 04 '20 at 12:12
  • @Math_Freak You can select a different layout method. See §§30-35 of the manual for different layout options. In my first example I also show layered layout and spring layout. (For Erdős–Rényi graphs the simple necklace layout works quite well I think, but your mileage may vary.) – moewe Jul 04 '20 at 12:15
  • For layered layout I got the following error message Package pgfkeys Error: I do not know the key '/tikz/graphs/layered layout' and I am going to ignore it. Perhaps you misspelled it. \graph[simpleer,layered layout]; Can you please help ? – Charlotte Jul 04 '20 at 12:20
  • @Math_Freak If you want to use layered layout you need to list layered in \usegdlibrary. You'll at least need \usegdlibrary{layered}. In general if you want to use a particular layout you always need to load the corresponding graph drawing library under which it is listed/documented in the manual. – moewe Jul 04 '20 at 14:18
  • thanks a lot for the clarification – Charlotte Jul 04 '20 at 14:19