7

In a document I need to insert several bipartite graphs, each one with different number of nodes and edge composition.

My idea was to create a command with two arguments:

  • the first one for the number of nodes;
  • the second one with the list of connections.

The call to this command is:

\bipgraph{5}{{1,2},{4,3},{2,4,1}}

in which there are 5 nodes and connections are built as:

  • the first node on the left side is connected to {1,2}, the first and the second node of the right side;

  • the second node on the left side is connected to {4,3}, the fourth and the third node of the right side.

This is my MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\newcommand{\bipgraph}[2]{%
    \begin{tikzpicture}[every node/.style={circle,draw}]
    \foreach \xitem in {1,2,...,#1}
    {%
    % first set
    \node at (0,\xitem) (a\xitem) {};
    % second set
    \node at (2,\xitem) (b\xitem) {};   
    }%

    % connections
    \foreach \dritem [count=\yi] in {#2}
    {% 
    \foreach \tritem in {\yi}
    \path (a\yi) edge (b\tritem);
    }
    \end{tikzpicture}  
}

\begin{document}
\bipgraph{5}{{1,2},{4,3},{2,4,1}}
\end{document}

which gives as result:

enter image description here

I think the problem arises when I read the list of connections in the second argument of \bipgraph, but I don't know how to fix it.

2 Answers 2

9

Removing a set of braces and replacing one of the variables solve the problem.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\newcommand{\bipgraph}[2]{%
    \begin{tikzpicture}[every node/.style={circle,draw}]
    \foreach \xitem in {1,2,...,#1}
    {%
    % first set
    \node at (0,\xitem) (a\xitem) {};
    % second set
    \node at (2,\xitem) (b\xitem) {};   
    }%

    % connections
    \foreach \x [count=\xi] in {#2}
    {% 
    \foreach \tritem in \x % <-- Here no braces to make it a foreach list also not \xi but \x
    \draw(a\xi) -- (b\tritem);
    }
    \end{tikzpicture}  
}

\begin{document}
\bipgraph{5}{{1,2},{4,3},{2,4,1}}
\end{document}

enter image description here

4
  • thanks, you have been very clear. I understood the error I did in reading the list :) Commented May 4, 2012 at 12:18
  • @ClaudioFiandrino You're very welcome. Good idea by the way. But Altermundus' tkz-berge package is a killer :)
    – percusse
    Commented May 4, 2012 at 13:25
  • I started seen the tkz collection and yes.. it is a killer collection :). Anyway I learnt from my error applying your trick in quite different environment Commented May 4, 2012 at 15:02
  • @ClaudioFiandrino That's quite nice. Next stop is to get the same effect by using pgfkeys to make the input scheme more natural :)
    – percusse
    Commented May 5, 2012 at 19:00
8

Only for information, you can draw all these kind of graphs with tkz-berge. There are many possibilities to draw the edges, here I show you two methods. The first very simple : \Edges(b2,a0,b0,a1,b2,a3,b1,a2) you get a chain of edges. The names of vertices are determined by a prefix a, b etc.. (it's an option) and a number (indice).

\Edges is a macro from tkz-graph. tkz-berge is useful for classic graphs (see the last example) and new tools are given to draw specific egdes like \EdgeFromOneToSel you draw from one vertex through a selection of vertices. There are also \EdgeFromOneToAll, EdgeIdentity etc.

\documentclass[11pt]{scrartcl}
\usepackage{tkz-berge}

\begin{document}
\begin{tikzpicture}
   \begin{scope}[rotate=90]
       \SetVertexMath
       \grEmptyLadder[RA=2,RB=4]{5}   
   \end{scope}
    \Edges(b2,a0,b0,a1,b2,a3,b1,a2)
\end{tikzpicture} 
\hspace{1cm}
\begin{tikzpicture}
   \begin{scope}[rotate=90]
       \SetVertexNoLabel  
       \grEmptyLadder[RA=2,RB=4]{5}   
   \end{scope}
    \Edges(b2,a0,b0,a1,b2,a3,b1,a2)
\end{tikzpicture} 

\begin{tikzpicture}
   \begin{scope}[rotate=90]
      \GraphInit[vstyle=Simple]
       \grEmptyLadder[RA=2,RB=4]{5}   
   \end{scope}
    \Edges(b2,a0,b0,a1,b2,a3,b1,a2)
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}
  \GraphInit[vstyle=Shade]
  \grPath[form=1,RA=2]{5}
  \grPath[form=1,prefix=b,RA=2,RS=4]{5}
  \EdgeFromOneToSel{a}{b}{0}{0,3}
  \EdgeFromOneToSel{a}{b}{1}{0,3}
  \EdgeFromOneToSel{a}{b}{3}{3}
  \EdgeFromOneToSel{a}{b}{4}{2,3}    
\end{tikzpicture}
\end{document} 

enter image description here

\documentclass[11pt]{scrartcl}
\usepackage{tkz-berge}

\begin{document}

\begin{tikzpicture}
 \GraphInit[vstyle=Shade] 
  \SetVertexNoLabel 
   \grCompleteBipartite[RA=4,RB=3,RS=6]{3}{5} 
   \end{tikzpicture}
\end{document}

enter image description here

3
  • I really like tkz-graph and tkz-berge! Commented May 4, 2012 at 12:12
  • @Altermundus: thanks for the information. I didn't know those packages, but the first impression is very positive. I will document myself on that. Commented May 4, 2012 at 12:22
  • I'm surprised nobody linked to his website or his Gallery of named Graphs. They are natural places to follow up.
    – DJP
    Commented May 4, 2012 at 22:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .