• No se han encontrado resultados

DISCURSOS SOBRE LAS TIC: PROYECTOS INSTITUCIONALES E INNOVACIÓN EDUCATIVA

In line 5 of MARK1 we check to see if Q= RLINK(P) can lead to other unmarked accessible nodes. If so, Q is stacked. The examination

procedure MARK1 (X)

//X is a list node. Mark all nodes accessible from X. It is assumed that MARK(0) = 1 and TAG(0) = 0. ADD and DELETE

perform the standard stack operations//

1 P X; initialize stack

2 loop //follow all paths from P, P is a marked list node//

3 loop //move downwards stacking RLINK's if needed//

4 Q RLINK (P)

5 if TAG(Q) = 1 and MARK(Q) = 0 then call ADD(Q) //stack Q//

6 MARK(Q) 1 //Q may be atomic//

7 P DLINK (P)

//any unmarked nodes accessible from P?//

8 if MARK(P) = 1 or TAG(P) = 0 then exit 9 MARK(P) 1

10 forever

11 MARK(P) 1 //P may be an unmarked atomic node//

12 if stack empty then return //all accessible nodes marked//

13 call DELETE(P) //unstack P//

14 forever 15 end MARK1

of nodes continues with the node at DLINK(P). When we have moved downwards as far as is possible, line 8, we exit from the loop of lines 3-10. At this point we try out one of the alternative moves from the stack, line 13. One may readily verify that MARK1 does indeed mark all previously unmarked nodes which are accessible from X.

In analyzing the computing time of this algorithm we observe that on each iteration (except for the last) of the loop of lines 3-10, at least one previously unmarked node gets marked (line 9). Thus, if the outer

loop, lines 2-14, is iterated r times and the total number of iterations of the inner loop, lines 3-10, is p then at least q = p - r previously unmarked nodes get marked by the algorithm. Let m be the number of new nodes marked. Then m q = p - r. Also, the number of iterations of the loop of lines 2-14 is one plus the number of nodes that get stacked. The only nodes that can be stacked are those previously unmarked (line 5). Once a node is stacked it gets marked (line 6). Hence r m + 1. From this and the knowledge that m p - r, we conclude that p 2m + 1. The computing time of the algorithm is O(p + r).

Substituting for p and r we obtain O(m) as the computing time. The time is linear in the number of new nodes marked! Since any algorithm to mark nodes must spend at least one unit of time on each new node marked, it follows that there is no algorithm with a time less than O(m). Hence MARK1 is optimal to within a constant factor (recall that 2m = O (m) and 10m = O(m)).

Having observed that MARK1 is optimal to within a constant factor you may be tempted to sit back in your arm chair and relish a moment of smugness. There is, unfortunately, a serious flaw with MARK1.

This flaw is sufficiently serious as to make the algorithm of little use in many garbage collection

applications. Garbage collectors are invoked only when we have run out of space. This means that at the time MARK1 is to operate, we do not have an unlimited amount of space available in which to maintain the stack. In some applications each node might have a free field which can be used to maintain a linked stack. In fact, if variable size nodes are in use and storage compaction is to be carried out then such a field will be available (see the compaction algorithm COMPACT). When fixed size nodes are in use, compaction can be efficiently carried out without this additional field and so we will not be able to maintain a linked stack (see exercises for another special case permitting the growth of a linked stack).

Realizing this deficiency in MARK1, let us proceed to another marking algorithm MARK2. MARK2 will not require any additional space in which to maintain a stack. Its computing is also O(m) but the constant factor here is larger than that for MARK1.

Unlike MARK1(X) which does not alter any of the links in the list X, the algorithm MARK2(X) will modify some of these links. However, by the time it finishes its task the list structure will be restored to its original form. Starting from a list node X, MARK2 traces all possible paths made up of DLINK's and RLINK's. Whenever a choice is to be made the DLINK direction is explored first. Instead of maintaining a stack of alternative choices (as was done by MARK1) we now maintain the path taken from X to the node P that is currently being examined. This path is maintained by changing some of the links along the path from X to P.

Consider the example list of figure 4.29(a). Initially, all nodes except node A are unmarked and only node E is atomic. From node A we can either move down to node B or right to node I. MARK2 will always move down when faced with such an alternative. We shall use P to point to the node currently being examined and T to point to the node preceding P in the path from X to P. The path T to X will be maintained as a chain comprised of the nodes on this T- X path. If we advance from node P to node Q then either Q = RLINK(P) or Q = DLINK(P) and Q will become the node currently being examined . The node preceding Q on the X - Q path is P and so the path list must be updated to represent the path from P to X. This is simply done by adding node P to the T - X path already constructed. Nodes will be linked onto this path either through their DLINK or RLINK field. Only list nodes will be placed onto this path chain. When node P is being added to the path chain, P is linked to T via its DLINK field if Q

= DLINK(P). When Q = RLINK(P), P is linked to T via its RLINK field. In order to be able to

determine whether a node on the T- X path list is linked through its DLINK or RLINK field we make use of the tag field. Notice that since the T- X path list will contain only list nodes, the tag on all these nodes will be one. When the DLINK field is used for linking, this tag will be changed to zero. Thus, for nodes on the T - X path we have:

Figure 4.29 Example List for MARK2

A B C D F G F D C B H B A I J I A (e) Path taken by P

Figure 4.29 Example List for MARK2 (contd.)

The tag will be reset to 1 when the node gets off the T - X path list.

Figure 4.29(b) shows the T - X path list when node P is being examined. Nodes A, B and C have a tag of zero indicating that linking on these nodes is via the DLINK field. This also implies that in the original list structure, B = DLINK(A), C = DLINK(B) and D = P = DLINK(C). Thus, the link information destroyed while creating the T- X path list is present in the path list. Nodes B, C, and D have already been marked by the algorithm. In exploring P we first attempt to move down to Q = DLINK(P) = E. E is an atomic node so it gets marked and we then attempt to move right from P. Now, Q = RLINK(P) = F.

This is an unmarked list node. So, we add P to the path list and proceed to explore Q. Since P is linked to Q by its RLINK field, the linking of P onto the T - X path is made throught its RLINK field. Figure 4.29(c) shows the list structure at the time node G is being examined. Node G is a deadend. We cannot move further either down or right. At this time we move backwards on the X-T path resetting links and tags until we reach a node whose RLINK has not yet been examined. The marking continues from this node. Because nodes are removed from the T - X path list in the reverse order in which they were added to it, this list behaves as a stack. The remaining details of MARK2 are spelled out in the formal

SPARKS algorithm. The same driver as for MARK1 is assumed.

procedure MARK 2(X)

//same function as MARK1//

//it is assumed that MARK(0) = 1 and TAG(0) = 0//

1 P X; T 0 //initialize T - X path list//

2 repeat

3 Q DLINK(P) //go down list//

4 case

5 :MARK(Q) = 0 and TAG(Q) = 1: //Q is an unexamined list node//

6 MARK(Q) 1; TAG(P) 0

7 DLINK(P) T; T P //add Pto T - X path list//

8 P Q //move down to explore Q//

9 :else: //Q is an atom or already examined//

10 MARK(Q) 1

11 L1: Q RLINK(P) //move right of P//

12 case

13 :MARK(Q) = 0 and TAG(Q) = 1: //explore Q further//

14 MARK(Q) 1

15 RLINK (P) T; T P

16 P Q

17 :else: MARK(Q) 1 //Q is not to be explored;

back up on path list//

18 while T 0 do //while path list not empty//

19 Q T

20 if TAG(Q) = 0 //link to P through DLINK//

21 then [T < DLINK(Q); DLINK (Q) P 22 TAG(Q) 1; P Q; go to L1]

//P is node to right of Q//

23 T RLINK(Q); RLINK(Q) P 24 P Q

25 end 26 end 27 end

28 until T= 0 29 end MARK 2

Documento similar