Skip to content

Commit

Permalink
Tuned crefs in ch10
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxinyu95 committed Apr 19, 2023
1 parent d0fcb49 commit fdcba55
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
26 changes: 13 additions & 13 deletions datastruct/heap/other-heaps/kheap-en.tex
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ \subsubsection{Binomial tree}

\begin{enumerate}
\item $B_0$ has only one node;
\item $B_n$ is formed by two $B_{n-1}$ trees, the one with the greater root element is the left most sub-tree of the other, as shown in figure \cref{fig:link-bitree}.
\item $B_n$ is formed by two $B_{n-1}$ trees, the one with the greater root element is the left most sub-tree of the other, as shown in \cref{fig:link-bitree}.
\end{enumerate}

\begin{figure}[htbp]
Expand All @@ -74,7 +74,7 @@ \subsubsection{Binomial tree}
\label{fig:link-bitree}
\end{figure}

Figure \cref{fig:bitree-forms} gives examples of $B_0$ to $B_4$.
\Cref{fig:bitree-forms} gives examples of $B_0$ to $B_4$.

\begin{figure}[htbp]
\centering
Expand All @@ -98,7 +98,7 @@ \subsubsection{Binomial tree}
\item Every tree has unique rank. i.e. any two trees have different ranks.
\end{enumerate}

From the 2nd rule, for a binomial heap of $n$ elements, convert $n$ to its binary format $(a_m ... a_1, a_0)_2$, where $a_0$ is the least significant bit (LSB) and $a_m$ is the most significant bit (MSB). If if $a_i=0$, there is no tree of rank $i$; if $a_i = 1$, there is a tree of rank $i$. For example, consider a binomial heap of 5 elements. As 5 is 101 in binary, there are 2 binomial trees, one is $B_0$, the other is $B_2$. The binomial heap in figure \cref{fig:bheap2} has 19 elements, 19 is $(10011)_2$. There is a $B_0$, a $B_1$, and a $B_4$.
From the 2nd rule, for a binomial heap of $n$ elements, convert $n$ to its binary format $(a_m ... a_1, a_0)_2$, where $a_0$ is the least significant bit (LSB) and $a_m$ is the most significant bit (MSB). If if $a_i=0$, there is no tree of rank $i$; if $a_i = 1$, there is a tree of rank $i$. For example, consider a binomial heap of 5 elements. As 5 is 101 in binary, there are 2 binomial trees, one is $B_0$, the other is $B_2$. The binomial heap in \cref{fig:bheap2} has 19 elements, 19 is $(10011)_2$. There is a $B_0$, a $B_1$, and a $B_4$.

\begin{figure}[htbp]
\centering
Expand All @@ -117,7 +117,7 @@ \subsubsection{Binomial tree}
\end{Haskell}

\index{left child, right sibling}
There is a method called `left-child, right-sibling'\cite{CLRS}, that can reuse the binary tree data structure to define multi-ways tree. Every node has the left and right part. the left references to the first sub-tree; the right references to its sibling. All siblings form a list as shown in figure \cref{fig:lcrs}. Alternatively, we can use an array or a list to represent the sub-trees.
There is a method called `left-child, right-sibling'\cite{CLRS}, that can reuse the binary tree data structure to define multi-ways tree. Every node has the left and right part. the left references to the first sub-tree; the right references to its sibling. All siblings form a list as shown in \cref{fig:lcrs}. Alternatively, we can use an array or a list to represent the sub-trees.

\begin{figure}[htbp]
\centering
Expand All @@ -129,7 +129,7 @@ \subsubsection{Binomial tree}
\subsection{Link}
\index{Binomial Heap!Link}

To link two $B_n$ trees to a $B_{n+1}$ tree, we compare the two root elements, choose the smaller one as the root, and put the other tree ahead of other sub-trees as shown in figure \cref{fig:link-xy}.
To link two $B_n$ trees to a $B_{n+1}$ tree, we compare the two root elements, choose the smaller one as the root, and put the other tree ahead of other sub-trees as shown in \cref{fig:link-xy}.

\be
link\ (r, x, ts)\ (r, y, ts') = \begin{cases}
Expand Down Expand Up @@ -335,7 +335,7 @@ \subsection{Merge}
\subsubsection{Pop}
\index{Binomial heap!pop}

Although every tree has the minimal element in its root, we don't know which tree holds the overall minimum in the heap. We need locate it from all trees. As there are $O(\lg n)$ trees, it takes $O(\lg n)$ time to find the top element. For pop, we need further remove the top element and maintain heap property. Let the trees be $B_i, B_j, ..., B_p, ..., B_m$ in the heap, and the minimum is in the root of $B_p$. After remove the top, there leave $p$ sub binomial trees with ranks of $p-1, p-2, ..., 0$. We can reverse them to form a new binomial heap $H_p$. The other trees without $B_p$ also form a binomial heap $H' = H - [B_p]$. We merge $H_p$ and $H'$ to get the final result as shown in figure \cref{fig:bheap-del-min}. Below is the definition to access the minimal element in the heap.
Although every tree has the minimal element in its root, we don't know which tree holds the overall minimum in the heap. We need locate it from all trees. As there are $O(\lg n)$ trees, it takes $O(\lg n)$ time to find the top element. For pop, we need further remove the top element and maintain heap property. Let the trees be $B_i, B_j, ..., B_p, ..., B_m$ in the heap, and the minimum is in the root of $B_p$. After remove the top, there leave $p$ sub binomial trees with ranks of $p-1, p-2, ..., 0$. We can reverse them to form a new binomial heap $H_p$. The other trees without $B_p$ also form a binomial heap $H' = H - [B_p]$. We merge $H_p$ and $H'$ to get the final result as shown in \cref{fig:bheap-del-min}. Below is the definition to access the minimal element in the heap.

\begin{figure}[htbp]
\centering
Expand Down Expand Up @@ -563,7 +563,7 @@ \subsubsection{Merge}
\subsubsection{Pop}
\index{Fibonacci Heap!pop} \index{Fibonacci Heap!delete min}

As the link operation is delayed to future during merge, we need `compensate' it during pop. We define it as tree consolidation. Consider another problem: given a list of numbers of $2^m$ ($m$ is natural numbers), for e.g., $L = [2, 1, 1, 4, 8, 1, 1, 2, 4]$, we repeatedly sum the two equal numbers until all numbers are unique. The result is $[8, 16]$. This process is shown in table \cref{tb:num-consolidate}. The first column gives the number we are `scanning'; the second is the middle step, i.e. compare current number and the first number in result list, add them when equal; the last column is the merge result, which inputs to the next step. The consolidation process can be defined with fold:
As the link operation is delayed to future during merge, we need `compensate' it during pop. We define it as tree consolidation. Consider another problem: given a list of numbers of $2^m$ ($m$ is natural numbers), for e.g., $L = [2, 1, 1, 4, 8, 1, 1, 2, 4]$, we repeatedly sum the two equal numbers until all numbers are unique. The result is $[8, 16]$. This process is shown in \cref{tb:num-consolidate}. The first column gives the number we are `scanning'; the second is the middle step, i.e. compare current number and the first number in result list, add them when equal; the last column is the merge result, which inputs to the next step. The consolidation process can be defined with fold:

\begin{table}[htbp]
\centering
Expand Down Expand Up @@ -624,7 +624,7 @@ \subsubsection{Pop}
\end{array}
\ee

Figure \cref{fig:fib-meld-b} gives the consolidation steps. It is similar to number consolidation when compare with table \cref{tb:num-consolidate}. We can use an auxiliary array $A$ to do the consolidation. $A[i]$ stores the tree of rank $i$. We traverse the trees in the heap. If meet another tree of rank $i$, we link them together to obtain a bigger tree of rank $i+1$, clean $A[i]$, and next check whether $A[i + 1]$ is empty or not. If there is a tree of rank $i + 1$, then link them together again. Array $A$ stores the final consolidation result after traverse.
\Cref{fig:fib-meld-b} gives the consolidation steps. It is similar to number consolidation when compare with \cref{tb:num-consolidate}. We can use an auxiliary array $A$ to do the consolidation. $A[i]$ stores the tree of rank $i$. We traverse the trees in the heap. If meet another tree of rank $i$, we link them together to obtain a bigger tree of rank $i+1$, clean $A[i]$, and next check whether $A[i + 1]$ is empty or not. If there is a tree of rank $i + 1$, then link them together again. Array $A$ stores the final consolidation result after traverse.

\captionsetup[subfigure]{labelformat=empty, margin=10pt}
\begin{figure}[htbp]
Expand Down Expand Up @@ -669,7 +669,7 @@ \subsubsection{Pop}
\EndFunction
\end{algorithmic}

It becomes a binomial heap after consolidation. There are $O(\lg n)$ trees. \textproc{Max-Rank}($n$) returns the upper limit of rank $R$ in a heap of $n$ elements. From the binomial tree result, the biggest tree $B_R$ has $2^R$ elements. We have $2^R \leq n < 2^{R+1}$, we estimate the rough upper limit is $R \leq \log_2 n$. We'll give more accurate estimation of $R$ in later section. We need additionally scan all trees, find the minimal root element. We can reuse $min'$ defined in (\cref{eq:extract-min-bitree}) to extract the min-tree.
It becomes a binomial heap after consolidation. There are $O(\lg n)$ trees. \textproc{Max-Rank}($n$) returns the upper limit of rank $R$ in a heap of $n$ elements. From the binomial tree result, the biggest tree $B_R$ has $2^R$ elements. We have $2^R \leq n < 2^{R+1}$, we estimate the rough upper limit is $R \leq \log_2 n$. We'll give more accurate estimation of $R$ in later section. We need additionally scan all trees, find the minimal root element. We can reuse $min'$ defined in \cref{eq:extract-min-bitree} to extract the min-tree.

\be
\begin{array}{rcl}
Expand Down Expand Up @@ -700,7 +700,7 @@ \subsubsection{Pop}
E = m g h
\]

As shown in figure \cref{fig:potential-energy}, consider some process, that moves an object of mass $m$ up and down, and finally stops at height $h'$. Let the friction resistance be $W_f$, the process works the following power:
As shown in \cref{fig:potential-energy}, consider some process, that moves an object of mass $m$ up and down, and finally stops at height $h'$. Let the friction resistance be $W_f$, the process works the following power:

\[
W = m g (h' - h) + W_f
Expand Down Expand Up @@ -734,7 +734,7 @@ \subsubsection{Pop}
\subsection{Increase priority}
\index{Fibonacci Heap!decrease key}

We can use heap to manage tasks with priority. When need prioritize a task, we decrease the corresponding element, making it close to the heap top. Some graph algorithms, like the minimum spanning tree and Dijkstra's algorithm rely on this heap operation\cite{CLRS} meet amortized constant time. Let $x$ be a node in the heap $H$, we need decrease its value to $k$. As shown in figure \cref{fig:cut-fib-tree}, if the element in $x$ is less than the one in its parent $y$, we cut $x$ off $y$, the add it the heap (forest). Although it ensures the parent still holds the minimum in the tree, it is not binomial tree any more. The performance drops when loss too many sub-trees. We add another rule to address this problem: {\em If a node losses its second sub-tree, it is immediately cut from parent, and added to the heap (forest).}
We can use heap to manage tasks with priority. When need prioritize a task, we decrease the corresponding element, making it close to the heap top. Some graph algorithms, like the minimum spanning tree and Dijkstra's algorithm rely on this heap operation\cite{CLRS} meet amortized constant time. Let $x$ be a node in the heap $H$, we need decrease its value to $k$. As shown in \cref{fig:cut-fib-tree}, if the element in $x$ is less than the one in its parent $y$, we cut $x$ off $y$, the add it the heap (forest). Although it ensures the parent still holds the minimum in the tree, it is not binomial tree any more. The performance drops when loss too many sub-trees. We add another rule to address this problem: {\em If a node losses its second sub-tree, it is immediately cut from parent, and added to the heap (forest).}

\begin{figure}[htbp]
\centering
Expand Down Expand Up @@ -942,7 +942,7 @@ \subsection{Merge, insert, and top}
\EndFunction
\end{algorithmic}

Similar to Fibonacci heap, we implement insert with merge as (\cref{eq:fib-insert}). We access the top element from the root: $top\ (x, ts) = x$. Both operations are bound to constant time.
Similar to Fibonacci heap, we implement insert with merge as \cref{eq:fib-insert}. We access the top element from the root: $top\ (x, ts) = x$. Both operations are bound to constant time.

\subsection{Increase priority}
\index{Pairing heap!decrease key}
Expand Down Expand Up @@ -971,7 +971,7 @@ \subsection{Pop}
pop\ (x, ts) = \textit{consolidate}\ ts
\ee

We firstly merge every two sub-trees from left to right, then merge these paired results from right to left to a tree. This explains the why we name it `paring heap'. Figure \cref{fig:merge-pairs} and \cref{fig:merge-right} show the paired merge.
We firstly merge every two sub-trees from left to right, then merge these paired results from right to left to a tree. This explains the why we name it `paring heap'. \Cref{fig:merge-pairs,fig:merge-right} show the paired merge.

\begin{figure}[htbp]
\centering
Expand Down
Loading

0 comments on commit fdcba55

Please sign in to comment.