Skip to content

Commit

Permalink
#128 4.4-4
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtask committed Mar 28, 2024
1 parent 27ae375 commit 4affd3f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
37 changes: 36 additions & 1 deletion chapter4/sections/4/4.tex
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
\workinprogress % TODO
We'll use the recursion-tree method to guess an upper bound on $T(n)$.
Let's assume that $0<\alpha\le1/2$, because the case when $1/2\le\alpha<1$ is symmetric, and as we'll see, the choice of $\alpha$ doesn't affect the order of growth of an upper bound on $T(n)$.
\refFigure{4.4-4} shows such a tree.
\begin{figure}[htb]
\input{4.tikz}
\caption{A recursion tree for the recurrence $T(n)=T(\alpha n)+T((1-\alpha)n)+cn$, where $0<\alpha\le1/2$.} \label{fig:4.4-4}
\end{figure}

Let $n_0>0$ be the implicit threshold constant such that $T(n)=\Theta(1)$ for $0<n<n_0$, and let $c$ represent the upper-bound constant hidden by the $\Theta(n)$ term for $n\ge n_0$.
The root-to-leaf path formed by right-going edges is the longest, where the node at depth $i$ represents a subproblem of size $(1-\alpha)^in$.
Let $h$ be the height of the tree.
At depth $h$ the rightmost node is a leaf, so $(1-\alpha)^hn<n_0\le(1-\alpha)^{h-1}n$, which gives $h=\lfloor\log_{1/(1-\alpha)}(n/n_0)\rfloor+1$, and thus $h=\Theta(\lg n)$.
The recursion divides the problem into two subproblems, whose sizes sum up to the size of the problem.
Since the cost incurred by a problem of size $m$ is $cm$, the sum of costs incurred by the subproblems is also $cm$.
Therefore, the total cost of all nodes at any depth is bound by $cn$.
Summing the costs of internal nodes across each level, we have at most $cn$ per level times $h=\Theta(\lg n)$ for a total cost of $O(n\lg n)$ for all internal nodes.

The number of leaves of the tree is described by the following recurrence:
\[
L(n) =
\ccases{
1 & \text{if $n<n_0$}, \\
L(\alpha n)+L((1-\alpha)n) & \text{if $n\ge n_0$}.
}
\]
We can apply the substitution method to show it has solution $L(n)=O(n)$ for any $0<\alpha<1$.
Using the inductive hypothesis $L(n)\le dn$ for some constant $d>0$, and assuming that the inductive hypothesis holds for all values less than $n$, we have
\begin{align*}
L(n) &= L(\alpha n)+L((1-\alpha)n) \\
&\le d\alpha n+d(1-\alpha)n \\
&= dn,
\end{align*}
which holds for any $d>0$ and any $0<\alpha<1$.
Picking $d=1$ suffices to handle the base case $L(1)=1$ for $0<n<n_0$.

Returning to recurrence $T(n)$, we obtain its upper bound of $O(n\lg n)+O(n)=O(n\lg n)$.
64 changes: 64 additions & 0 deletions chapter4/sections/4/4.tikz
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
\begin{tikzpicture}[
level/.style = {level distance=3\vertexsize, sibling distance=16\vertexsize/2^#1},
level 3/.style = {edge from parent/.style = {transition edge={solid}{very densely dashed}{0.7}}},
level arrow/.style = {->, thick, blue},
total node/.style = {align=right, text width=10mm},
]

\node (root) {$cn$}
child {node (0) {$c\alpha n$}
child {node[text depth=0.5ex, text height=1.5ex] (00) {$c\alpha^2n$}
child {node (000) {}}
child {node (001) {}}
}
child {node[text depth=0.5ex, text height=1.5ex] (01) {$c\alpha(1-\alpha)n$}
child {node (010) {}}
child {node (011) {}}
}
}
child {node (1) {$c(1-\alpha)n$}
child {node[text depth=0.5ex, text height=1.5ex] (10) {$c\alpha(1-\alpha)n$}
child {node (100) {}}
child {node (101) {}}
}
child {node[text depth=0.5ex, text height=1.5ex] (11) {$c(1-\alpha)^2n$}
child {node (110) {}}
child {node (111) {}}
}
};
\node[below=\vertexsize of 000] (leaf 0) {$\Theta(1)$};
\node[below=2\vertexsize of 001] (leaf 1) {$\Theta(1)$};
\node[below=1.75\vertexsize of 010] (leaf 2) {$\Theta(1)$};
\node[below=2.75\vertexsize of 011] (leaf 3) {$\Theta(1)$};
\node[below=2.5\vertexsize of 100] (leaf 4) {$\Theta(1)$};
\node[below=3.5\vertexsize of 101] (leaf 5) {$\Theta(1)$};
\node[below=3.25\vertexsize of 110] (leaf 6) {$\Theta(1)$};
\node[below=4.25\vertexsize of 111] (leaf 7) {$\Theta(1)$};
\foreach \x in {0, ..., 7} {
\draw[very densely dashed] (leaf \x.north) -- +(0mm, 2.5mm);
}

\node[total node, right=of leaf 7] (level h total) {$\Theta(1)$};
\node[total node] at (level h total |- 11) (level 2 total) {$cn$};
\node[total node] at (level h total |- 1) (level 1 total) {$cn$};
\node[total node] at (level h total |- root) (level 0 total) {$cn$};

\path (level 2 total.east) -- (level h total.east) node[midway, xshift=-5mm, font=\bfseries] (total ellipsis) {$\vdots$};
\node[font=\bfseries] at (total ellipsis -| root) {$\vdots$};

\draw[level arrow, shorten <=2mm] (root) -- (level 0 total);
\draw[level arrow, shorten <=2mm] (1) -- (level 1 total);
\draw[level arrow, shorten <=2mm] (11) -- (level 2 total);
\draw[level arrow, shorten <=2mm] (leaf 7) -- (level h total);

\draw[orange, decorate, decoration={brace, amplitude=8pt, mirror}]
(leaf 0.south west |- leaf 7.south east) -- (leaf 7.south east) node[midway, yshift=-5mm, text=black] (leaves total) {$O(n)$};

\coordinate (total line end) at (leaves total -| level h total.east);
\draw (1 |- total line end) -- (total line end) node[at end, anchor=east, yshift=-4mm] {Total: $O(n\lg n)$};

\coordinate (bottom) at (leaf 0.west |- level h total);
\coordinate[left=14mm of bottom] (bottom shifted);
\draw[<->, orange] (bottom shifted) -- (bottom shifted |- root) node[pos=0.5, fill=white, text=black] {$\lfloor\log_{1/(1-\alpha)}(n/n_0)\rfloor+1$};

\end{tikzpicture}

0 comments on commit 4affd3f

Please sign in to comment.