Escolar Documentos
Profissional Documentos
Cultura Documentos
January 6, 2010
1
0 2.15 id=2
(o) tw=2.15
sw=0.20
id=3 id=5
The linear time algorithm uses just tw=0.60 tw=0.95
sw=0.60 sw=0.90
layouts the events’ probabilities as sub-
intervals in a continuous segment, and
samples a random number uniformly in
id=6
that segment. See figure 1. The length tw=0.05
of each sub-interval is proportional to the sw=0.05
2
also all the ancestors of nodes s ∈ S. versatile header only library which also in-
The set K represents all the nodes cludes binary tree algorithms, see [2].
whose cumulative weight has to be It is straightforward to adapt a client
updated. The size of set K is pro- data-structure to be used with an intrusive
portional to the height of tree and tree. Further, as long as a tree is a partic-
thus both building it and updating the ular kind of directed graph, it is also pos-
cumulative probability weight values sible to adapt such data structure to work
should have O(log n) complexity. with boost.graph algorithms, and then just
use the topological sort there...
3. Compute a topological sort Ksorted =
κ1 , κ2 , . . . κ|K| of the set K such that
leaf nodes be first1 ; References
4. For each node in Ksorted , in that order, [1] T.H. Cormen, C.E. Leiserson, R.L.
the cumulative weight is re-computed Rivest, and C. Stein. Introduction to al-
summing the self-weight with the cu- gorithms. The MIT press, 2001.
mulative weight of the left and right
subtree. [2] Ion Gaztanaga and Olaf Krzikalla.
Boost.Intrusive. Boost.
Step 3 ensures that cumulative weights be
updated in the right order.
Sampling from the given probabilty pairs
is done following the tree structure. Algo-
rithm 1 shows the detailed steps.
3
Data: root_node pointer to the root of the red-black gree
Data: fact a real number from some (uniform) randomness source
begin
s = fast * (root_node -> tree_prb_weight) ;
current_node = root_node ;
if current_node -> left is not null then
a = current_node -> left -> tree_prb_weight ;
else
a=0;
b = current_node->self_prb_weight + left_start;
while s ≥ a and s < b do
if s < a then
current_node = current_node -> left ;
else
s=s−a;
current_node = current_node -> right ;
if current_node -> left is not null then
a = current_node -> left -> tree_prb_weight ;
else
a=0;
b = current_node->self_prb_weight ;
return current_node -> id
end