Search code examples
pythondrawinglatexgraph-drawingsorting-network

Drawing sorting networks


What's a good way to draw sorting networks that look like those at the bottom of this post? A python package or LaTeX typsetting package would be much appreciated.

Sorting network with groupings Simple sorting network


Solution

  • It is very possible that some more specific LaTeX packages exist to draw such networks, but you can start from having a look at TikZ package (manual). This is how I (quickly) reproduced the simpler of the two images in your question using TikZ:

    \documentclass[tikz,border=5mm]{standalone}
    
    \begin{document}
    
    \begin{tikzpicture}
    %\fill [gray!15] (1.5,1.5) -- (2.5,1.5) -- (2.5,2.5) -- (1.5,2.5) -- cycle;
    \foreach \a in {1,...,4}
      \draw[thick] (0,\a) -- ++(5,0);
    \foreach \x in {{1,2},{1,4},{2,1},{2,3},{3,1},{3,2},{3,3},{3,4},{4,2},{4,3}}
      \filldraw (\x) circle (1.5pt);
    \draw[thick] (1,2) -- (1,4);
    \draw[thick] (2,1) -- (2,3);
    \draw[thick] (3,1) -- (3,2);
    \draw[thick] (3,3) -- (3,4);
    \draw[thick] (4,2) -- (4,3);
    \end{tikzpicture}
    
    \end{document}
    

    enter image description here

    The commented line (beginning with %) would give you a sample of light-gray rectangle on the background as in the first image of yours.