This preview shows pages 1–15. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: ' n "i . hmgto Unuemts mSt Lows Engineering A Fast Implementation
of Dynamic Trees Jon Turner
Computer Science & Engineering
Washington University www.ar1.wustl.edu/~jst :1, Mshlngton UnwersuymStlouis Engineering Representing Path Sets as Search Trees .A path set can be represented by a set of search trees
>> each node on a path corresponds to a node in a search tree I No key is used
>> search tree structure just defines the order of nodes in the path
>> so, to find the tail of a path, follow right pointers from tree root to a node with no right child 9 (D o G 0 _two alternative
binary search trees $Wash1ngton UnwersuymStlouls Engineering Differential Costs IThe node costs are represented implicitly by two fields
added to each tree node I Let cost(x) be cost of x and let mincost(x) be the
minimum cost of any descendant of X in the tree. I Each node has fields Acost(x) and Amin(x) defined as
Acost(x) = cost(x) — mincost(x) mincost(x) — mincost(p(x)) if x has a parent Amin(x) = { I mincost(x)=sum of Amin fields of nodes on the path
from X to the root, so cost(x)=mincost(x)+Acost(x)
I This representation makes it possible to change cost of all nodes in subtree by the same amount in 0(1) time 3 mincost(x) otherwise :1, Mshmgton UnlverSItymStlouls Engineering Example of Path Set Representation mincost(d) =Amin(a) +Amin(c)+Amin(d) = 5
cost(d) =mincost(d) +Acost(d) = 5 :1, Mshmgton UniverSItymStlouis Engineering Review Questions 1. Suppose we represent the path on page 4 using the right—hand binary search tree
on page 2. Draw the tree, labeling each node with its cost and its mincost. Now,
draw a second version of the tree, labeling each node with its Amin and Acost values. . Given a binary search tree where each node is labeled by its cost and mincost
values, describe an algorithm to find the last (rightmost) node that has the
minimum cost, among all nodes in the tree. . Given a node x in a search tree with Amin and Acost values, describe an algorithm
to compute the cost and mincost for X. Describe an algorithm for increasing the cost
of all nodes in a tree with root r by some value D. E Washington University in St. Louis Engineering Updating Costs During Rotations I Rotation operations must update differential cost fields
in order to maintain actual cost values Acost, Amin Acost’(z) =Acost(z) +Amin(z)
Amin’(z) =Amin(x)
Acost’(x)=Acost(x)— Amin’(x)
Amin’(x)=min{Acost(x), Amin(a)*,
Amin(b)+Amin(z)*} Amin’(a) = Amin(a)Amin’(x)
Amin’(b) = Amin(b)—Amin’(x)
+Amin(z)
Amin’(c) = Amin(c) + Amin(z)
*drop term if a is missing
Tdrop term if b is missing :1, Washmgton UniverSItymStlouis Engineering Implementing Path Set Ops path function findpath(node v) return splay(v); end;
node function findtall(path p) _ time bounded
do right(p) 7: null => p := right(p) ad; by number of return splay(p); procedure addpathcost(path p, real X)
Amin(p) := Amin(p) + x; end; time [path, path] function split(item /', path 5)
Path 51,52} time bounded
SPIaYU); by number of
51,52 == Ieft(i),ri9ht(i); p(sl),p(52) == null; splay steps
Ieft(i), right(i) := null;
Amin(sl) := Amin(sl) + Amin(i); Amin(sz) := Amin(sz) + Amin(i);
Amin(i) := Amin(i) + Acost(i); Acost(i) := 0;
return [51,52]; end; $Msh1ngton UanerSIthStJDUIb Engineering
[node, real] function findpathcost(path p);
do right(p)¢null and Amin(right(p))=0 3
p := right(p)
 (right(p)=nu or Amin(r/'ght(p))>0) and Acost(p)>0 2
p := /eft(p)
od; time bounded
p := splay(p); by number of
return [p, Amin(p)]; splay steps
end; path function join(path r,v,q); real temp' _ , constant
/eft(v),r/ght(v) := W; p(r),p(q) == V;
temp := Amin(V)} Amin(v) := min{Am/n(r), Amin(v), Amin(q)};
Acost(v) := temp — Amin(v);
Amin(r) := Amin(r) — Amin(v); Amin(q) := Amin(q) — Amin(v);
return v;
end; E \X/ashington University in St. Louis Revisiting Expose Engineering path function expose(node v);
path p; p := null; do v e null => I
[IN] := splice(p,v); od,
succ(p) := null;
return p; end; [path, node] function splice(path p, node v);
path q,r; node w;
w := succ(findpath(v)); [w] := splitm; g: :> Q:
fi, if q¢nul 2 succ(q) . ,I x :> p
return [join(p,v,r),w g 0
time bounded by A number of splay steps 9 , —
E Washington University in St. Louis Engineering Analysis of Dynamic Trees l Theorem 5.2. A sequence of m dynamic tree ops on a
set of trees with n nodes can be done in 0(m log n) time
Proof. Running time is proportional to m plus the number of
splices (which we already know is O(m log n)) plus number of
splay steps >> account for the splay steps using a credit—based analysis
Types of trees >> define an actual tree to be the abstract tree implemented by the
dynamic trees data structure >> define a virtual tree to be the linked set of binary search trees that
implements an actual tree >> define a solid tree to be one of the binary search trees in a virtual tree E \X/ashington University in St. Louis Engineering Node weights >> define the individual weight, iw(v) of a node v to
be 1 plus the number of actual tree descendants
of v in subtrees linked to v by a dashed edge >> define the total weight, tw(v) of a node v to be
the sum of the individual weights of the
descendants of v in its solid tree >> note: tw(v) is the number of descendants tW(V)=# “Odes here .
of v in the virtual tree containing v;
consequently, tw(v)sn for all nodes Let rank(v) = [lg tw(v)J and define the
following credit invariant every node x contains rank(x) credits For each tree op, use one credit per
splay step plus any additional credits
needed to maintain the credit invariant Lemma 4.2, which bounds the number of credits needed to perform
a splay operation while maintaining a credit invariant, holds for this
credit invariant, as well as for the simpler invariant used earlier iw(v)=# nodes here 11 II :1, Washmgton UniverSItymStlouis Engineering steps in an expose at a vertex X in a virtual tree with root v Now, consider the number of credits used to account for splay §
>> by Lemma 4.2, the number is at most 3(rank(v)—rank(x)) plus 1 per splice
By the earlier analysis, the total number of exposes is &
at most 2m, and the number of splices is O(m log n) >> so total number of credits used by expose ops is O(m log n) To complete the analysis, we must determine the number of credits used by the tree ops, in addition to
those used by the exposes >> the only operation that requires additional credits is the link operation,
which joins three paths into one >>the root node in the tree representing the new path acquires O(og n)
credits in this operation Combining the various estimates, the total number of credits used
(and thus the running time) is O(m log n) I 1’) E \X/ashington University in St. Louis Exercises .Suppose that the tree shown below represents one path in a pathset data
structure, where the numbers labeling the nodes are the Acost and
Amin values. —
Engineering List the vertices in the order in which
they appear on the path. a,g,d,b,f,c,e,h What are the costs of vertices a, b, e
and h? 1, 4, 5and8 Show how the tree changes
following a splay at node b. Show the updated Acost and
Amin values. 9 3,1
0 1,0 9 2,2
0 0 0,5
0,0 I? E Washington University in St. Louis 2.The diagram below shows a dynamic
tree represented as a collection of
paths that are linked together. The
nodes with a heavy outline are the
canonical nodes for their paths. Show the corresponding virtual
tree that implements this actual
tree. For each node, include its
Acost and Amin values. —
Engineering Then, show how the virtual tree
changes after an expose at node p. The two trees are
shown below,
with each vertex labeled with its Acost and Amin values. 1/! E Washington University in St. Louis .The diagram below shows a virtual tree
that implements an actual tree in the
dynamic trees data structure. for each
node in the tree with root d, list its
individual weight and its total weight, as defined in the analysis of the dynamic
trees data structure. —
Engineering Nodes p and 0 each have an individual
weight of 1. Nodes i,j and d all have an
individual weight of 2, while node a has
an individual weight of 7. Node 0 has a
total weight of 1, node p has 2, node i
has 2, node j has 6, node a has 7 and
node d has 15. What is the rank of every node in the
entire virtual tree? The leaves all have a rank of 0. Nodes p,i
and n all have a rank of 1. Nodes j, a and
b all have a rank of2 and node d has a
rank of 3. How many credits are needed to satisfy
the credit invariant? 3+3x2+3=12 I: ...
View
Full
Document
This note was uploaded on 03/08/2012 for the course CSE 542 taught by Professor Jonturner during the Spring '12 term at Washington State University .
 Spring '12
 JonTurner

Click to edit the document details