]> mj.ucw.cz Git - saga.git/commitdiff
Reformulate the trick for k-permutations.
authorMartin Mares <mj@ucw.cz>
Fri, 22 Feb 2008 13:31:29 +0000 (14:31 +0100)
committerMartin Mares <mj@ucw.cz>
Fri, 22 Feb 2008 13:31:29 +0000 (14:31 +0100)
rank.tex

index 2a62859c1b0e53fe342ba7904db39fd1e4bc8188..da67a9fa2e4738f06da1bcabbbe35df156ca3119 100644 (file)
--- a/rank.tex
+++ b/rank.tex
@@ -250,28 +250,27 @@ To calculate $R_H(x)$, we can again use the vector operation \<Rank> from Algori
 this time on the vector~$\bf h$.
 
 The only operation we cannot translate directly is unranking in~$A$. We will
-therefore define an~auxiliary vector~$\bf b$ of the same size as~$\bf h$,
-such that $b_i=h_i-i$ (this is the number of elements of~$A$ smaller
-than~$h_i$). Now, if we want to find the $r$-th smallest element of~$A$, we find
-the largest $i$ such that $b_i<r$ (the rank of~$r$ in~$\bf b$) and we return
-$h_i+1+r-b_i$. This works because there is no hole in~$A$ between the element
-$h_i+1$, which has rank~$b_i$, and the desired element of~rank~$r$.
+therefore define an~auxiliary vector~$\bf r$ of the same size as~$\bf h$
+containing the ranks of the holes: $r_i=R_A(h_i)=h_i-R_H(h_i)=h_i-i$.
+To find the $j$-th smallest element of~$A$, we locate the interval between
+holes to which this element belongs: the interval is bordered from below by
+a~hole~$h_i$ such that $i$ is the largest index satisfying~$r_i \le j$.
+In other words, $i=\<Rank>(r,j+1)-1$. Finding the right element in the interval
+is then easy: $R^{-1}_A(j) = h_i + 1 + j - r_i$.
 
 \example
-If $A=\{2,5,6\}$ and $n=8$, then ${\bf h}=(1,3,4,7,8)$ and ${\bf b}
-= (0,1,1,3,3)$. When we want to calculate $R^{-1}_A(2)$, we find $i=3$ and we
-get $g_3+1+2-b_3 = 4+1+2-1 = 6$.
+If $A=\{2,5,6\}$ and $n=8$, then ${\bf h}=(1,3,4,7,8)$ and ${\bf r}
+= (0,1,1,3,3)$. When we want to calculate $R^{-1}_A(2)$, we find $i=2$ and
+the wanted element is $h_2+1+2-r_2 = 4+1+2-1 = 6$.
 
 \para
-The vector~$\bf b$ can be updated in constant time whenever an~element is
+The vector~$\bf r$ can be updated in constant time whenever an~element is
 inserted to~$\bf h$. It is sufficient to shift the fields apart (we know
-that the position of the new element in~$\bf b$ is the same as in~$\bf h$),
+that the position of the new element in~$\bf r$ is the same as in~$\bf h$),
 insert the new value using masking operations and decrease all higher fields
 by one in parallel by using a~single subtraction. Updates after deletions
 from~$\bf h$ are analogous.
 
-\FIXME{State the algorithms properly.}
-
 We have replaced all operations on~$A$ by the corresponding operations on the
 modified data structure, each of which works again in constant time. Therefore
 we have just proven the following theorem, which brings this section to
@@ -282,10 +281,11 @@ When we order the $k$-per\-mu\-ta\-tions on the set~$[n]$ lexicographically, bot
 ranking and unranking can be performed on the RAM in time~$\O(k)$.
 
 \proof
-We modify algorithms \ref{rankalg} and \ref{unrankalg} as described in this section.
-The modified data structure performs all required operations on the set~$A$ in
-constant time. The depth of the recursion is $\O(k)$ and we spend $\O(1)$ time
-at every level. The time bound follows.
-\qed
+We modify algorithms \ref{rankalg} and \ref{unrankalg} for $k$-permutations as
+shown at the beginning of this section. We use the vectors $\bf h$ and~$\bf r$
+described above as an~implicit representation of the set~$A$. The modified
+algorithm uses recursion $k$~levels deep and as each operation on~$A$ can be
+performed in~$\O(1)$ time using $\bf h$ and~$\bf r$, every level takes only
+constant time. The time bound follows. \qed
 
 \endpart