# Create a command to draw edges in a bipartite graph

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:

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.

-

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}


-
thanks, you have been very clear. I understood the error I did in reading the list :) – Claudio Fiandrino May 4 '12 at 12:18
@ClaudioFiandrino You're very welcome. Good idea by the way. But Altermundus' tkz-berge package is a killer :) – percusse May 4 '12 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 – Claudio Fiandrino May 4 '12 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 May 5 '12 at 19:00

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
\end{scope}
\Edges(b2,a0,b0,a1,b2,a3,b1,a2)
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}
\begin{scope}[rotate=90]
\SetVertexNoLabel
\end{scope}
\Edges(b2,a0,b0,a1,b2,a3,b1,a2)
\end{tikzpicture}

\begin{tikzpicture}
\begin{scope}[rotate=90]
\GraphInit[vstyle=Simple]
\end{scope}
\Edges(b2,a0,b0,a1,b2,a3,b1,a2)
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}
\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}


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

\begin{document}

￼\begin{tikzpicture}

I really like tkz-graph and tkz-berge! – Gonzalo Medina May 4 '12 at 12:12