# The package to visualize graphs

What could be the packages to use for visualization of graphs? I tried Graphviz, but it's very hard to direct how the graphs should be shown.

How can I make the graphs represented as is shown in the examples (excerpt from Graph Theory and Complex Networks: An Introduction).

• TikZ, PSTricks, Asymptote. See Numbering nodes in a for loop, How can I label TikZ-graphs on four vertices with a loop? and texample.net/tikz/examples/complete-graph. — What do you struggle with? What have your tried? TikZ can do this with \foreach loops and chains and stuff. — Do you have a specific input syntax? – Qrrbrbirlbel Sep 21 '13 at 19:17
• if you use LuaTeX and watn to to graph layout in an automatic way, you might want to take a look at Jannis work (diploma thesis) gezeiten.org/tag/tikz – Ronny Sep 21 '13 at 19:46
• @Qrrbrbirlbel -- It's funny, all three famous packages are shown in three answers. Generally the choice depends on users. – Tawei Sep 22 '13 at 5:33
• @selwyndd21 No surprise there. That’s why I linked the related topics. We already have examples (especially for TikZ) on how to draw such graphics. I’m still interested in how OP wants to create these diagrams (“input syntax”). There is also TikZ’ new Lua Graphdrawing library which could be very helpful (see Ronny’s comment). – Qrrbrbirlbel Sep 22 '13 at 13:02

You can use TikZ. Here's a very minimal example,

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[every node/.style={circle,inner sep=2pt,fill=black}]
\node (A) at (0,0) {};
\node (B) at (1,1) {};
\node (C) at (0,1) {};
\node (D) at (1,0) {};

\draw (A) -- (B)
(A) -- (C)
(A) -- (D)
(B) -- (D);
\end{tikzpicture}
\end{document}


You can use pstricks

\documentclass{article}
\usepackage{pstricks,pst-node}
\begin{document}
\begin{pspicture}[showgrid=false](1,1)
\pnode(0,0){A}
\pnode(1,1){B}
\pnode(0,1){C}
\pnode(1,0){D}

\rput(A){\psdot}
\rput(B){\psdot}
\rput(C){\psdot}
\rput(D){\psdot}

\psline(A)(B)
\psline(A)(C)
\psline(A)(D)
\psline(B)(D)
\end{pspicture}
\end{document}


While this following example could be done a bit more efficiently, it does show that you can make very nicely connected graphs:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
\def\mycircleofnodes{C0}
\foreach \x in {0,30,...,330}
{
\node[circle,fill=black,inner sep=2pt] (C\x) at (\x:3) {} ;
\ifnum\x>0\relax\xdef\mycircleofnodes{\mycircleofnodes,C\x}\fi
}
\foreach \x in {0,30,...,330}
{
\foreach \y in \mycircleofnodes
{
\draw (\y) -- (C\x);
}
}
\end{tikzpicture}

\end{document}


If you don't want to connect every node to each other, then you can do something along the following lines:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
\def\mycircleofnodes{C0}
\foreach \x in {0,1,...,11}
{
\node[circle,fill=black,inner sep=2pt] (C\x) at (\x*30:3) {} ;
\ifnum\x>0\relax\xdef\mycircleofnodes{\mycircleofnodes,C\x}\fi
}

\foreach \x/\y in {0/1,0/2,0/3,0/4,0/5,0/6,0/7,%
2/4,%
5/6,5/7,5/10,%
8/9,8/11}
{
\draw (C\x) -- (C\y);
}

\end{tikzpicture}

\end{document}


Here's (only) the beginning of how to set up something like your second example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}

\def\rowA{0}
\def\rowB{0,1,...,9}
\def\rowC{0,1,...,17}
\def\rowD{0,1,...,13}
\def\rowE{0,1,...,5}
\def\rowF{0}

\begin{document}

\begin{tikzpicture}[x=0.5cm,
y=1.75cm,
every node/.style={circle,
inner sep=2pt,
fill=black}
]

\foreach \x in \rowA { \node (A\x) at (\x-0.5,2)  {}; }
\foreach \x in \rowB { \node (B\x) at (\x-5,1)    {}; }
\foreach \x in \rowC { \node (C\x) at (\x-9,0)    {}; }
\foreach \x in \rowD { \node (D\x) at (\x-7,-1)   {}; }
\foreach \x in \rowE { \node (E\x) at (\x-3,-2)   {}; }
\foreach \x in \rowF { \node (F\x) at (\x-0.5,-3) {}; }

\foreach \x    in {4,8,9}                    { \draw (A0)  -- (B\x); }
\foreach \x/\y in {0/2,1/7,1/8,1/17}         { \draw (B\x) -- (C\y); }

\foreach \x/\y in {0/0,0/7,0/8,0/10,1/4,1/8} { \draw (E\x) -- (D\y); }
\foreach \x/\y in {0/0,0/1,0/2,0/3,0/4,0/5}  { \draw (F\x) -- (E\y); }

\end{tikzpicture}

\end{document}


Be advised! Both pstricks and tikz have their own learning curves. They both have ample documentation. The documentation for pstricks is spread over multiple pdf files, which at times can lead to frustration when you don't know where to look for the documentation. tikz has an immense and very comprehensive manual (though knowing which libraries are necessary can be a bit frustrating at times).

Asymptote provides a lot of convenient programming means to handle data structures. Here is one way to draw circular visualisation of graphs:

% visg.tex :
\documentclass{article}
\usepackage[inline]{asymptote}
\begin{asydef}
struct circGraph{
real r;
int n;

pair node(int i){
return rotate(90+i*360/n)*(r,0);
}

real lineW;
pen linePen;
pen[] linePens;

pen nodePenO=invisible+3*linewidth(linePen);
pen nodePenA=white;
pen nodePenB=orange;

real labelOff;

void drawLines(){
int lineCount;
lineCount=0;
for(int j=0;j<n;++j){
draw(node(i)--node(j),linePens[lineCount]);
++lineCount;
};
}
}
}

void drawNodes(){
for(int i=0;i<n;++i){
pair p=node(i);
label(string(i),labelOff*p);
}
}

void operator init(
,real r=1
,pen[] linePens={lightred,darkgreen,blue}
,real lineW=0.6bp
,pen nodePenA=white
,pen nodePenB=orange
,real labelOff=1.08
){
this.r        = r;
this.linePens = copy(linePens);
this.lineW    = lineW;
this.nodePenA = nodePenA;
this.nodePenB = nodePenB;
this.nodePenO=invisible+3*lineW;
this.labelOff=labelOff;

this.linePens.cyclic=true;
drawLines();
drawNodes();
}
}
\end{asydef}
\usepackage{lmodern}
\begin{document}
\begin{figure}
\begin{asy}
size(300);
defaultpen(fontsize(10pt));

"00000000000000000001100000000011100000000000000000",
"00000000000000000010000010000001101000000000000000",
"00000000000010100100000101100000001000000000000000",
"00000000000001010000100000000000000000000000000000",
"00000000000010000010000001100000000000000000000000",
"00000000000010000000000100000010100000000000000000",
"00000000000001000100000010100000000000000000000000",
"00000000000000001001000000000100001000000000000000",
"00000000000000000000110001100100000110000000000000",
"00000000000001101000100011010000000000000000000000",
"00000000000000000000000000000000001000000000000000",
"00000000000000000000000000000001000000000000000000",
"00000000000000000000000000000000000000100010100000",
"00000000000000000000000000000000000000000001001000",
"00000000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000000010100010000",
"00000000000000000000000000000000000000100000000000",
"00000000000000000000000000000000000000010000100010",
"00000000000000000000000000000000000000000100000000",
"00000000000000000000000000000000000001000100000010",
"00000000000000000000000000000000000000000000000100",
"00000000000000000000000000000000000000000010000000",
"00000000000000000000000000000000000000010000000000",
"00000000000000000000000000000000000000000100010000",
"00000000000000000000000000000000000000000001000000",
"00000000000000000000000000000000000000100000001000",
"00000000000000000000000000000000000000011010000100",
"00000000000000000000000000000000000000000000000001",
"00000000000000000000000000000000000000100001000000",
"00000000000000000000000000000000000000010000000001",
"00000000000000000000000000000000000000100001000000",
"00000000000000000000000000000000000000100000001000",
"00000000000000000000000000000000000000000100000000",
"00000000000000000000000000000000000001010000000010",
"00000000000000000000000000000000000000001000011000",
"00000000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000001000000000010",
"00000000000000000000000000000000000000010000000000",
"00000000000000000000000000000000000000010000000000",
};

shipout(bbox(Fill(paleyellow)));

\end{asy}
\end{figure}
%
\begin{figure}
\begin{asy}
size(200);
defaultpen(fontsize(10pt));

"00000000110000000001110000",
"00000001000001000000110100",
"01010010000010110000000100",
"00101000010000000000000000",
"01000001000000110000000000",
"01000000000010000001010000",
"00100010000001010000000000",
"00000100100000000010000100",
"00000000011000110010000011",
"00110100010001101000000000",
"00000000000000000000000100",
};

currentpen=olive;
,nodePenA=yellow
,nodePenB=brown
,labelOff=1.2);
shipout(bbox(Fill(paleblue)));

\end{asy}
\end{figure}

\end{document}
%
% Process:
% pdflatex visg.tex
% asy asy visg-*.asy
% pdflatex visg.tex

• Asymptote and g.kov both rock! +1 – user11232 Sep 22 '13 at 1:01
• An off-topic question: Is it easy to find the intersection points of two arbitrary curves in Aysmptote? For example, curves passing through (1,2)(2,4)(4,-1)... and (0,-2)(2,3)(4,3)..., respectively. – kiss my armpit Sep 22 '13 at 5:11
• @PGFTricks: size(300); guide p=(1,2)..(2,4)..(4,-1); guide q=(0,-2)..(2,3)..(4,3); draw(p,red); draw(q,green); dot(intersectionpoint(p,q),UnFill); – g.kov Sep 22 '13 at 5:24
• @PGFTricks: You might also find it useful to use functions real[][] intersections, pair[] intersectionpoints and path subpath (among the others), see asymptote.pdf. – g.kov Sep 22 '13 at 6:21

Just for fun with PSTricks.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node,pst-plot}

\psset{showpoints}

\def\Graph#1{%
\begin{pspicture}(-2,-2)(2,2)
\curvepnodes[plotpoints=\numexpr#1+1]{0}{360}{2 t PtoC}{P}
\multido{\ix=0+1,\itemp=1+1}{\Pnodecount}{%
\multido{\iy=\itemp+1}{\numexpr\Pnodecount-1-\ix}{\psline(P\ix)(P\iy)}}
\end{pspicture}}

\begin{document}
\multido{\i=3+1}{10}{\Graph{\i}}
\end{document}


## Remarks:

Pnodecount is the last index that is equal to plotpoints minus 1. As the first point and the last point are on the same radial line in the case of a full circular domain, the last point must be excluded. Be careful with off-by-one error when specifying the loop range.

The key plotpoints is defined in pst-plot and the macro \curvepnodes is defined in pst-node. When we use \curvepnodes with plotpoints, we have to load both packages. If you forget to load pst-plot (as I did often), the \curvepnodes[plotpoints=...]... will not compile. I don't know whether it should be regarded a bad design pattern applied to packages with cross-linking.

I answered a different question here but most of the answer is relevant to your question. You want to use tikz in combination with the tkz-graph, and tkz-berge packages. The beautiful results are in the PDF "Gallery of Named Graphs"; they are comparable with the examples you have given. The graph packages are due to Alain Matthes of the Altermundus site. The computer algebra system Sage supports tikz and Altermundus' packages as well as LaTeX.