Drawing the Powerset of a Set using Latex and TIKZ (Hasse Diagram)

Today, I will show how to draw the powerset of a set using Latex and TIKZ, to produce some nice figures that can be used in paper written using Latex. The code that is shown below is adapted from code from StackOverflow.

First, we will draw the powerset of the set {a,b} as a Hasse diagram:

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

\begin{tikzpicture}
    \matrix (A) [matrix of nodes, row sep=1.2cm]
    { 
        & $\{a,b\}$ \\  
	$\{a\}$ &  & $\{b\}$\\
        & $\emptyset$ \\
    };
    \draw (A-1-2)--(A-2-1);
    \draw (A-1-2)--(A-2-3);
    \draw (A-2-1)--(A-3-2);
    \draw (A-2-3)--(A-3-2);
\end{tikzpicture}
\end{document} 

Then, I will show how to draw the powerset of the set {a,b,c}:

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

\begin{tikzpicture}
    \matrix (A) [matrix of nodes, row sep=1.2cm]
    {
        $\{a,b\}$ & $\{a,c\}$ & $\{b,c\}$ \\
        $\{a\}$ & $\{b\}$ & $\{c\}$ \\
        & $\emptyset$ \\
    };
    \path (A-1-1)--(A-1-2) node[above=1.2cm] (link) {$\{a,b,c\}$};
    
    \foreach \i in {1,...,3}
    \draw (link.south) -- (A-1-\i.north);
    \foreach \i/\j in {1/2, 3/2, 2/1, 1/1, 3/3, 2/3}
    \draw (A-1-\i.south)--(A-2-\j.north);
    \foreach \i/\j in {1/2, 2/2, 3/2}
    \draw (A-2-\i.south)--(A-3-\j.north);
\end{tikzpicture}

\end{document} 

Then, I will show how to draw the powerset of the set {a,b,c,d}:

\begin{tikzpicture}
    \matrix (A) [matrix of nodes, row sep=1.5cm]
    { 
       ~ &  ~ & $\{a,b,c,d\}$ \\  

	~ &  $\{a,b,c\}$ & $\{a,b,d\}$  & $\{a,c,d\}$ & $\{b,c,d\}$\\
	$\{a,b\}$ & $\{a,c\}$  & $\{a,d\}$ & $\{b,c\}$ & $\{b,d\}$ & $\{c,d\}$\\
	~ &  $\{a\}$ & $\{b\}$  & $\{c\}$ & $\{d\}$\\
      ~ & ~ &  $\emptyset$ \\
    };
    \draw (A-1-3.south)--(A-2-2.north);
    \draw (A-1-3.south)--(A-2-3.north);
    \draw (A-1-3.south)--(A-2-4.north);
    \draw (A-1-3.south)--(A-2-5.north);

    \draw (A-2-2.south)--(A-3-1.north);
    \draw (A-2-2.south)--(A-3-2.north);
    \draw (A-2-2.south)--(A-3-4.north);

    \draw (A-2-3.south)--(A-3-1.north);
    \draw (A-2-3.south)--(A-3-3.north);
    \draw (A-2-3.south)--(A-3-5.north);

    \draw (A-2-4.south)--(A-3-2.north);
    \draw (A-2-4.south)--(A-3-3.north);
    \draw (A-2-4.south)--(A-3-6.north);

    \draw (A-2-5.south)--(A-3-4.north);
    \draw (A-2-5.south)--(A-3-5.north);
    \draw (A-2-5.south)--(A-3-6.north);

    \draw (A-3-1.south)--(A-4-2.north);
    \draw (A-3-1.south)--(A-4-3.north);

    \draw (A-3-2.south)--(A-4-2.north);
    \draw (A-3-2.south)--(A-4-4.north);

    \draw (A-3-3.south)--(A-4-2.north);
    \draw (A-3-3.south)--(A-4-5.north);

    \draw (A-3-4.south)--(A-4-3.north);
    \draw (A-3-4.south)--(A-4-4.north);

    \draw (A-3-5.south)--(A-4-3.north);
    \draw (A-3-5.south)--(A-4-5.north);

    \draw (A-3-6.south)--(A-4-4.north);
    \draw (A-3-6.south)--(A-4-5.north);

    \draw (A-4-2.south)--(A-5-3.north);
    \draw (A-4-3.south)--(A-5-3.north);
    \draw (A-4-4.south)--(A-5-3.north);
    \draw (A-4-5.south)--(A-5-3.north);
\end{tikzpicture}

That is all for today. Hope this will be useful.

Note that I had also written a blog post about how to draw the powerset of a set using Java and GraphViz instead of using Latex.

This entry was posted in Latex and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.