-2

I am looking for graphs of big-o notation terms below;

  • O(1) is a constant time complexity
  • O(n) is a linear time complexity
  • O(log_2(n)) is a logarithmic time complexity (I mean log basis 2)
  • O(n log_2(n))
  • O(n^2) is a quadratic time complexity
  • O(n^3) cubic time complexity
  • O(2^n) exponential time complexity

Could you help me to represent these big O notations using pgfplots?

I have tried this code snipped for log_2(n). To tell the truth, I'm not so sure.


\documentclass[border=50pt]{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}[trim axis left]
\begin{axis}
[
    axis lines = left,
    xlabel = \(n\),
    ylabel = {\(\log_{2}(n)\)},
]
[domain=0:10,
  samples=1000,
  enlarge x limits=false,
  grid=both,
  no markers,
  axis equal,
  legend pos=outer north east,
legend style={draw=none},]
\addplot +[thick] {ln(x)/ln(2)};
\addlegendentry{$\log_{2}(n)$};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

hsnclk
  • 77
  • 2
  • 12

1 Answers1

1

You were almost there ... The only missing parts were other functions and proper axes configuration. Here is my solution. You might need to think about the ranges. I am not entirely sure they are correct but did not bother to check. I changed a way you to make the graph itself centred; I think overlay works better.

enter image description here

\documentclass[margin=50pt]{standalone}
\usepackage{pgfplots}

\pgfplotsset{width=12cm,compat=1.18}


\begin{document}

\begin{tikzpicture}
  \begin{axis}
    [
    axis lines = left,
    xmin=1, xmax=8.1, ymin=1, ymax=9,
    domain=0:8, samples=100, no markers, thick, grid=both,
    xlabel = \(n\), ylabel = {\(f(n)\)},
    label style = {overlay},       % Has he same effect as
    ticklabel style = {overlay},   % trim axis [left|right]
    legend entries = {
      $1$,
      $\log_{5}n$,
      $\log_{2}n$,
      $n$,
      $n\log_{2}n$,
      $2^{n}$,
      $n^{2}$,
      $n^{3}$,
    },
    every axis/.style = {font=\footnotesize},
    label style = {font=\normalsize},
    ]
    \addplot+ {1};
    \addplot+ {1 + ln(x)/ln(5)};
    \addplot+ {1 + ln(x)/ln(2)};
    \addplot+ {x};
    \addplot+ {1 + x*ln(x)/ln(2)};
    \addplot+ {2^x-1};
    \addplot+ {x^2};
    \addplot+ {x^3};
  \end{axis}
\end{tikzpicture}

\end{document}
Celdor
  • 2,437
  • 2
  • 23
  • 44