+ rc(v) = \big\lvert
+ \left\lbrace
+ (v_1, v_2) \in A \big/
+ v_1 \in Live \thickspace set \cup Root \thickspace set
+ \wedge v_2 = v
+ \right\rbrace
+ \big\rvert
+
+El *mutator* entonces debe actualizar este contador cada vez que el grafo
+de conectividad cambia, es decir, cada vez que se agrega, modifica
+o elimina una arista del grafo (o visto de una forma más cercana al código,
+cada vez que se agrega, modifica o elimina un puntero).
+
+Esta invariante es fundamental para el conteo de referencias, porque se
+asume que si el contador es 0 entonces el *mutator* no tiene ninguna
+referencia a la celda y por lo tanto es *basura*:
+
+.. math::
+
+ rc(v) = 0 \Rightarrow v \in Basura
+
+Para mantener esta invariante el *mutator*, cada vez que cambia un puntero
+debe decrementar en 1 el contador de la celda a la que apuntaba
+antiguamente e incrementar en 1 el contador de la celda a la que apunta
+luego de la modificación. Esto asegura que la invariante se mantenga
+durante toda la ejecución del programa. Si al momento de decrementar un
+contador éste queda en 0, la celda asociada debe liberarse de forma de
+poder ser reciclada. Esto implica que si esta celda almacena punteros, los
+contadores de las celdas apuntadas deben ser decrementados también, porque
+solo deben almacenarse en el contador las aristas del *live set* para
+mantener la invariante. De esto puede resultar que otros contadores de
+referencia queden en 0 y más celdas sean liberadas. Por lo tanto,
+teóricamente la complejidad de eliminar una referencia puede ser
+:math:`O(\lvert Live \thickspace set \rvert)` en el peor caso.
+
+Las primitivas implementadas para este tipo de recolector son las
+siguientes (acompañadas de una implementación básica)::
+
+ new()
+ cell = alloc()
+ if cell is null
+ throw out_of_memory
+ cell.rc = 1
+ return cell
+
+ del(cell)
+ cell.rc = cell.rc - 1
+ if cell.rc is 0
+ for child* in cell.children
+ del(*child)
+ free(cell)
+
+ update(ref*, cell)
+ cell.rc = cell.rc + 1
+ del(*ref)
+ *ref = cell
+
+
+Ejemplo
+^^^^^^^
+
+A continuación se presenta un ejemplo gráfico para facilitar la comprensión
+del algoritmo.
+
+Por simplicidad se asumen celdas de tamaño fijo con dos punteros, ``left``
+y ``right`` (estructura clásica para representar un árbol binario) y se
+muestra el contador de referencias abajo del nombre de cada celda. Se parte
+con una pequeña estructura ya construída y se muestra primero lo que ocurre
+al eliminar la referencia :math:`r0 \to h1`:
+
+.. TODO: figure
+
+.. raw:: latex
+
+ \begin{figure}[htp]
+ \centering
+ \subfigure[TODO]{
+
+.. digraph:: g3_1
+
+ margin = 0;
+ ratio = fill;
+ size = "3.0,3.8";
+ edge [ color = gray40 ];
+ node [
+ shape = record,
+ width = 0,
+ height = 0,
+ style = filled,
+ fillcolor = gray25,
+ fontcolor = white
+ ];
+
+ subgraph cluster_all {
+
+ root [
+ label = "root\nset|<r0> r0\n*|<r1> r1",
+ style = filled,
+ fillcolor = gray96,
+ fontcolor = black,
+ ];
+
+ h1 [ label = "h1\n1|<l> left|<r> right" ];
+ h2 [ label = "h2\n2|<l> left|<r> right" ];
+ h3 [ label = "h3\n2|<l> left|<r> right" ];
+ h4 [ label = "h4\n1|<l> left|<r> right" ];
+ h5 [ label = "h5\n1|<l> left|<r> right" ];
+ h6 [ label = "h6\n1|<l> left|<r> right" ];
+
+ root:r0 -> h1 [ style = bold, color = black ];
+ root:r1 -> h3;
+ h1:l -> h2;
+ h1:r -> h3;
+ h2:l -> h4;
+ h2:r -> h5;
+ h3:l -> h2;
+ h3:r -> h6;
+
+ }
+
+.. raw:: latex
+
+ }
+ \subfigure[TODO]{
+
+.. digraph:: g3_2
+
+ margin = 0;
+ ratio = fill;
+ size = "3.0,3.8";
+ edge [ color = gray40 ];
+ node [
+ shape = record,
+ width = 0,
+ height = 0,
+ style = filled,
+ fillcolor = gray25,
+ fontcolor = white
+ ];
+
+ subgraph cluster_all {
+
+ root [
+ label = "root\nset|<r0> r0\n*|<r1> r1",
+ style = filled,
+ fillcolor = gray96,
+ fontcolor = black,
+ ];
+
+ subgraph marked {
+ node [ fillcolor = white, fontcolor = black ];
+ h1;
+ };
+
+ h1 [ label = "h1\n0|<l> left|<r> right" ];
+ h2 [ label = "h2\n2|<l> left|<r> right" ];
+ h3 [ label = "h3\n2|<l> left|<r> right" ];
+ h4 [ label = "h4\n1|<l> left|<r> right" ];
+ h5 [ label = "h5\n1|<l> left|<r> right" ];
+ h6 [ label = "h6\n1|<l> left|<r> right" ];
+
+ root:r0 -> h1 [ style = invis ];
+ root:r1 -> h3;
+ h1:l -> h2 [ style = bold, color = black ];
+ h1:r -> h3;
+ h2:l -> h4;
+ h2:r -> h5;
+ h3:l -> h2;
+ h3:r -> h6;
+
+ }
+
+.. raw:: latex
+
+ }
+ \caption{TODO, part 1}
+ \end{figure}
+
+ \begin{figure}[htp]
+ \centering
+ \subfigure[TODO]{
+
+.. digraph:: g3_3
+
+ margin = 0;
+ ratio = fill;
+ size = "3.0,3.8";
+ edge [ color = gray40 ];
+ node [
+ shape = record,
+ width = 0,
+ height = 0,
+ style = filled,
+ fillcolor = gray25,
+ fontcolor = white
+ ];
+
+ subgraph cluster_all {
+
+ root [
+ label = "root\nset|<r0> r0\n*|<r1> r1",
+ style = filled,
+ fillcolor = gray96,
+ fontcolor = black,
+ ];
+
+ subgraph marked {
+ node [ fillcolor = white, fontcolor = black ];
+ h1;
+ };
+
+ h1 [ label = "h1\n0|<l> left|<r> right" ];
+ h2 [ label = "h2\n1|<l> left|<r> right" ];
+ h3 [ label = "h3\n2|<l> left|<r> right" ];
+ h4 [ label = "h4\n1|<l> left|<r> right" ];
+ h5 [ label = "h5\n1|<l> left|<r> right" ];
+ h6 [ label = "h6\n1|<l> left|<r> right" ];
+
+ root:r0 -> h1 [ style = invis ];
+ root:r1 -> h3;
+ h1:l -> h2 [ style = invis ];
+ h1:r -> h3 [ style = bold, color = black ];
+ h2:l -> h4;
+ h2:r -> h5;
+ h3:l -> h2;
+ h3:r -> h6;
+
+ }
+
+.. raw:: latex
+
+ }
+ \subfigure[TODO]{
+
+.. digraph:: g3_4
+
+ margin = 0;
+ ratio = fill;
+ size = "3.0,3.8";
+ edge [ color = gray40 ];
+ node [
+ shape = record,
+ width = 0,
+ height = 0,
+ style = filled,
+ fillcolor = gray25,
+ fontcolor = white
+ ];
+
+ subgraph cluster_all {
+
+ root [
+ label = "root\nset|<r0> r0|<r1> r1",
+ style = filled,
+ fillcolor = gray96,
+ fontcolor = black,
+ ];
+
+ subgraph marked {
+ node [ fillcolor = white, fontcolor = black ];
+ h1;
+ };
+
+ h1 [ label = "h1\n0|<l> left|<r> right" ];
+ h2 [ label = "h2\n1|<l> left|<r> right" ];
+ h3 [ label = "h3\n1|<l> left|<r> right" ];
+ h4 [ label = "h4\n1|<l> left|<r> right" ];
+ h5 [ label = "h5\n1|<l> left|<r> right" ];
+ h6 [ label = "h6\n1|<l> left|<r> right" ];
+
+ root:r0 -> h1 [ style = invis ];
+ root:r1 -> h3;
+ h1:l -> h2 [ style = invis ];
+ h1:r -> h3 [ style = invis ];
+ h2:l -> h4;
+ h2:r -> h5;
+ h3:l -> h2;
+ h3:r -> h6;
+
+ }
+
+.. raw:: latex
+
+ }
+ \caption{TODO, parte 2}
+ \end{figure}
+
+Como se observa, la celda ``h1`` queda con el contador en 0, por lo tanto
+pasa a ser *basura*. Los contadores de las celdas a las que apuntaban
+``h1.left`` y ``h1.right`` son decrementados, pero no se detecta más basura
+porque sigue existiendo un camino del *root set* a aquellas celdas, y por
+transitividad a todas aquellas celdas apuntadas por éstas. Es por esto que
+no es necesario seguir recorriendo el grafo de conectividad.
+
+A continuación se muestra como opera el algoritmo al cambiar a donde apunta
+``h3.left``, haciéndola apuntar a ``h5``. Es decir, que pasa luego de la
+operación ``update(h3.left, h5)`` (por simplicidad no se muestra más la
+celda ``h1`` que ahora es *basura*):
+
+.. TODO: figure
+
+.. raw:: latex
+
+ \begin{figure}[htp]
+ \centering
+ \subfigure[TODO]{
+
+.. digraph:: g4_1
+
+ margin = 0;
+ ratio = fill;
+ size = "3.0,3.8";
+ edge [ color = gray40 ];
+ node [
+ shape = record,
+ width = 0,
+ height = 0,
+ style = filled,
+ fillcolor = gray25,
+ fontcolor = white
+ ];
+
+ subgraph cluster_all {
+
+ root [
+ label = "root\nset|<r0> r0|<r1> r1",
+ style = filled,
+ fillcolor = gray96,
+ fontcolor = black,
+ ];
+
+ subgraph marked {
+ node [ fillcolor = white, fontcolor = black ];
+ };
+
+ h2 [ label = "h2\n1|<l> left|<r> right" ];
+ h3 [ label = "h3\n1|<l> left\n*|<r> right" ];
+ h4 [ label = "h4\n1|<l> left|<r> right" ];
+ h5 [ label = "h5\n1|<l> left|<r> right" ];
+ h6 [ label = "h6\n1|<l> left|<r> right" ];
+
+ root:r1 -> h3;
+ h2:l -> h4;
+ h2:r -> h5;
+ h3:l -> h2 [ style = bold, color = black ];
+ h3:r -> h6;
+
+ }
+
+.. raw:: latex
+
+ }
+ \subfigure[TODO]{
+
+.. digraph:: g4_2
+
+ margin = 0;
+ ratio = fill;
+ size = "3.0,3.8";
+ edge [ color = gray40 ];
+ node [
+ shape = record,
+ width = 0,
+ height = 0,
+ style = filled,
+ fillcolor = gray25,
+ fontcolor = white
+ ];
+
+ subgraph cluster_all {
+
+ root [
+ label = "root\nset|<r0> r0|<r1> r1",
+ style = filled,
+ fillcolor = gray96,
+ fontcolor = black,
+ ];
+
+ subgraph marked {
+ node [ fillcolor = white, fontcolor = black ];
+ };
+
+ h2 [ label = "h2\n1|<l> left|<r> right" ];
+ h3 [ label = "h3\n1|<l> left\n*|<r> right" ];
+ h4 [ label = "h4\n1|<l> left|<r> right" ];
+ h5 [ label = "h5\n2|<l> left|<r> right" ];
+ h6 [ label = "h6\n1|<l> left|<r> right" ];
+
+ root:r1 -> h3;
+ h2:l -> h4;
+ h2:r -> h5;
+ h3:l -> h2 [ style = bold, color = black ];
+ h3:l -> h5 [ style = dotted, color = black ];
+ h3:r -> h6;
+
+ }
+
+.. raw:: latex
+
+ }
+ \caption{TODO, part 1}
+ \end{figure}
+
+ \begin{figure}[htp]
+ \centering
+ \subfigure[TODO]{
+
+.. digraph:: g4_3
+
+ margin = 0;
+ ratio = fill;
+ size = "3.0,3.8";
+ edge [ color = gray40 ];
+ node [
+ shape = record,
+ width = 0,
+ height = 0,
+ style = filled,
+ fillcolor = gray25,
+ fontcolor = white
+ ];
+
+ subgraph cluster_all {
+
+ root [
+ label = "root\nset|<r0> r0|<r1> r1",
+ style = filled,
+ fillcolor = gray96,
+ fontcolor = black,
+ ];
+
+ subgraph marked {
+ node [ fillcolor = white, fontcolor = black ];
+ h2;
+ };
+
+ h2 [ label = "h2\n0|<l> left|<r> right" ];
+ h3 [ label = "h3\n1|<l> left\n*|<r> right" ];
+ h4 [ label = "h4\n1|<l> left|<r> right" ];
+ h5 [ label = "h5\n2|<l> left|<r> right" ];
+ h6 [ label = "h6\n1|<l> left|<r> right" ];
+
+ root:r1 -> h3;
+ h2:l -> h4 [ style = bold, color = black ];
+ h2:r -> h5;
+ h3:l -> h2 [ style = invis ];
+ h3:l -> h5 [ style = dotted, color = black ];
+ h3:r -> h6;
+
+ }
+
+.. raw:: latex
+
+ }
+ \subfigure[TODO]{
+
+.. digraph:: g4_4
+
+ margin = 0;
+ ratio = fill;
+ size = "3.0,3.8";
+ edge [ color = gray40 ];
+ node [
+ shape = record,
+ width = 0,
+ height = 0,
+ style = filled,
+ fillcolor = gray25,
+ fontcolor = white
+ ];
+
+ subgraph cluster_all {
+
+ root [
+ label = "root\nset|<r0> r0|<r1> r1",
+ style = filled,
+ fillcolor = gray96,
+ fontcolor = black,
+ ];
+
+ subgraph marked {
+ node [ fillcolor = white, fontcolor = black ];
+ h2; h4;
+ };
+
+ h2 [ label = "h2\n0|<l> left|<r> right" ];
+ h3 [ label = "h3\n1|<l> left\n*|<r> right" ];
+ h4 [ label = "h4\n0|<l> left|<r> right" ];
+ h5 [ label = "h5\n2|<l> left|<r> right" ];
+ h6 [ label = "h6\n1|<l> left|<r> right" ];
+
+ root:r1 -> h3;
+ h2:l -> h4 [ style = invis ];
+ h2:r -> h5 [ style = bold, color = black ];
+ h3:l -> h2 [ style = invis ];
+ h3:l -> h5 [ style = dotted, color = black ];
+ h3:r -> h6;
+
+ }
+
+.. raw:: latex
+
+ }
+ \caption{TODO, part 2}
+ \end{figure}
+
+ \begin{figure}[htp]
+ \centering
+ \subfigure[TODO]{
+
+.. digraph:: g4_5
+
+ margin = 0;
+ ratio = fill;
+ size = "3.0,3.8";
+ edge [ color = gray40 ];
+ node [
+ shape = record,
+ width = 0,
+ height = 0,
+ style = filled,
+ fillcolor = gray25,
+ fontcolor = white
+ ];
+
+ subgraph cluster_all {
+
+ root [
+ label = "root\nset|<r0> r0|<r1> r1",
+ style = filled,
+ fillcolor = gray96,
+ fontcolor = black,
+ ];
+
+ subgraph marked {
+ node [ fillcolor = white, fontcolor = black ];
+ h2; h4;
+ };
+
+ h2 [ label = "h2\n0|<l> left|<r> right" ];
+ h3 [ label = "h3\n1|<l> left\n*|<r> right" ];
+ h4 [ label = "h4\n0|<l> left|<r> right" ];
+ h5 [ label = "h5\n1|<l> left|<r> right" ];
+ h6 [ label = "h6\n1|<l> left|<r> right" ];
+
+ root:r1 -> h3;
+ h2:l -> h4 [ style = invis ];
+ h2:r -> h5 [ style = invis ];
+ h3:l -> h5 [ style = bold, color = black ];
+ h3:l -> h2 [ style = invis ];
+ h3:r -> h6;
+
+ }
+
+.. raw:: latex
+
+ }
+ \subfigure[TODO]{
+
+.. digraph:: g4_6
+
+ margin = 0;
+ ratio = fill;
+ size = "3.0,3.8";
+ edge [ color = gray40 ];
+ node [
+ shape = record,
+ width = 0,
+ height = 0,
+ style = filled,
+ fillcolor = gray25,
+ fontcolor = white
+ ];
+
+ subgraph cluster_all {
+
+ root [
+ label = "root\nset|<r0> r0|<r1> r1",
+ style = filled,
+ fillcolor = gray96,
+ fontcolor = black,
+ ];
+
+ subgraph marked {
+ node [ fillcolor = white, fontcolor = black ];
+ h2; h4;
+ };
+
+ h2 [ label = "h2\n0|<l> left|<r> right" ];
+ h3 [ label = "h3\n1|<l> left|<r> right" ];
+ h4 [ label = "h4\n0|<l> left|<r> right" ];
+ h5 [ label = "h5\n1|<l> left|<r> right" ];
+ h6 [ label = "h6\n1|<l> left|<r> right" ];
+
+ root:r1 -> h3;
+ h2:l -> h4 [ style = invis ];
+ h2:r -> h5 [ style = invis ];
+ h3:l -> h5;
+ h3:l -> h2 [ style = invis ];
+ h3:r -> h6;
+
+ }
+
+.. raw:: latex
+
+ }
+ \caption{TODO, parte 3}
+ \end{figure}
+
+.. _ref_gc_cycles:
+
+TODO El problema de los ciclos