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 Answers2

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

percusse
  • 157,807
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

Alain Matthes
  • 95,075