Dynamic trees (Steator and Tarjan 83)

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

1 Finger search trees. 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2) : Assumes that.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Dynamic Trees Goal: maintain a forest of rooted trees with costs on vertices.  Each tree has a root, every edge directed towards the root. Operations.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
1 Minimize average access time Items have weights: Item i has weight w i Let W =  w i be the total weight of the items Want the search to heavy items.
1 Dynamic trees (Steator and Tarjan 83). 2 Operations that we do on the trees Maketree(v) w = findroot(v) (v,c) = mincost(v) addcost(v,c) link(v,w,r(v,w))
1 Splay trees (Sleator, Tarjan 1983). 2 Motivation Assume you know the frequencies p 1, p 2, …. What is the best static tree ? You can find it in O(nlog(n))
1 Dynamic trees (Steator and Tarjan 83). 2 Operations that we do on the trees Maketree(v) w = findroot(v) (v,c) = mincost(v) addcost(v,c) link(v,w,r(v,w))
Binary Trees Chapter 6.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Splay Trees and B-Trees
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
1 Binomial heaps, Fibonacci heaps, and applications.
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Starting at Binary Trees
1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
1 Biased 2-b trees (Bent, Sleator, Tarjan 1980). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
1 Fat heaps (K & Tarjan 96). 2 Goal Want to achieve the performance of Fibonnaci heaps but on the worst case. Why ? Theoretical curiosity and some applications.
SPLAY TREE The basic idea of the splay tree is that every time a node is accessed, it is pushed to the root by a series of tree rotations. This series.
Self-Adjusting Search trees
Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick
Binary search tree. Removing a node
Binomial heaps, Fibonacci heaps, and applications
Splay Trees Binary search trees.
Finger search trees.
Splay Trees.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Splay Trees Binary search trees.
Lecture 7 Algorithm Analysis
Lecture 25 Splay Tree Chapter 10 of textbook
Binary Trees, Binary Search Trees
Data Structures Lecture 4 AVL and WAVL Trees Haim Kaplan and Uri Zwick
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Randomized Algorithms: Data Structures
Efficient implementation of Dinic’s algorithm for maximum flow
Lecture 9 Algorithm Analysis
Splay Trees In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there are no such rules. Search, insert, and delete.
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Red-Black Trees.
Lecture 7 Algorithm Analysis
CMSC 341 Splay Trees.
Red-Black Trees 1/16/2019 1:56 PM Splay Trees v z Splay Trees.
Splay trees (Sleator, Tarjan 1983)
Lecture 7 Algorithm Analysis
CMSC 341 Splay Trees.
Binary Trees, Binary Search Trees
Binomial heaps, Fibonacci heaps, and applications
Binary SearchTrees [CLRS] – Chap 12.
Red Black Trees (Guibas Sedgewick 78)
Fibonacci Heaps.
Richard Anderson Spring 2016
Binomial heaps, Fibonacci heaps, and applications
CMSC 341 Splay Trees.
CMSC 341 Splay Trees.
Binary Trees, Binary Search Trees
Splay Trees Binary search trees.
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

Dynamic trees (Steator and Tarjan 83)

Operations that we do on the trees Maketree(v) w = findroot(v) (v,c) = mincost(v) addcost(v,c) link(v,w,r(v,w)) cut(v) findcost(v)

Definitions We represent each tree T by a virtual tree V. The virtual tree is a binary tree with middle children. left middle right Think of V as partitioned into solid subtrees connected by dashed edges What is the relation between V and T ?

Actual tree a b c g d j i f l e h k p q m n o r s t u v w

Path decomposition Partition T into disjoint paths a b c g d j i f l e k p q m n o r s t u v w

Virtual trees (cont) Each path in T corresponds to a solid subtree in V f l b q i c a p j h The parent of a vertex x in T is the successor of x (in symmetric order) in its solid subtree or the parent of the solid subtree if x is the last in symmetric order in this subtree r g e k m d o n v w t u s

Virtual trees (cont) f a l b b q c c i a g d j i f l p e j h h r g e k m d m n o o n v r s t w t u u s v w

Virtual trees (representation) Each vertex points to p(x) to its left son l(x) and to its right son r(x). A vertex can easily decide if it is a left child a right child or a middle child. Each solid subtree functions like a splay tree.

Simple case -- paths Assume for a moment that each tree T in the forest is a path so each virtual tree is a simple splay tree. b a c e d f d b e a c f

Findroot(v) Splay at v, then follow right pointers until you reach the last vertex w on the right path. Return w and splay at w.

Mincost(v) With every vertex x we record cost(x) = the cost of the edge (x,p(x)) We also record with each vertex x mincost(x) = minimum of cost(y) over all descendants y of x. b a c e d f 1 3 2 7 4 7,1 d 3,1 4,4 b e 2,2 1,1 ,  a c f

Mincost(v) Splay at v and use mincost values to search for the minimum Notice: we need to update mincost values as we do rotations. y x x C A y B C A B

Addcost(v,c) Rather than storing cost(x) and mincost(x) we will store cost(x) = cost(x) - cost(p(x)) min(x) = cost(x) - mincost(x) b a c e d f 1 3 2 7 4 Addcost(v,c) : Splay at v, cost(v) += c cost(left(v)) -= c similarly update min 7,7,6 d 3,-4,2 4,-3,0 b e 1,-2,0 2,-1,0 , , 0 a c f

Addcost(v,c) (cont) Notice that now we have to update cost(x) and min(x) through rotations w v v C A w b b B C A B cost’(v) = cost(v) + cost(w) cost’(w) = -cost(v) cost’(b) = cost(v) + cost(b)

Addcost(v,c) (cont) Update min: w v v C A w b b B C A B min’(w) = max{0, min(b) - cost’(b), min(c) - cost(c)} min’(v) = max{0, min(a) - cost(a), min’(w) - cost’(w)}

Link(v,w,c), cut(v) Translate directly into catenation and split of splay trees if we talk about paths. Lets do the general case now.

The general case Each solid subtree of a virtual tree is a splay tree. We represent costs essentially as before. cost(x) = cost(x) - cost(p(x)) or cost(x) is x is a root of a solid subtree min(x) = cost(x) - mincost(x) (where mincost is the minimum cost within the subtree)

Splicing Want to change the path decomposition such that v and the root are on the same path. Let w be the root of a solid subtree and v a middle child of w w w ==> right right u v v u Want to make v the left child of w. It requires: cost’(v) =  cost(v) -  cost(w) cost’(u) =  cost(u) +  cost(w) min’(w) = max{0, min(v) - cost’(v), min(right(w))- cost(right(w))}

Splicing (cont) What is the effect on the path decomposition of the real tree ? w w ==> right right u v v u a a ==> b b w w u u v v

Splaying the virtual tree Let x be the vertex in which we splay. We do 3 passes: 1) Walk from x to the root and splay within each solid subtree w v x After the first pass the path from x to the root consists entirely of dashed edges 2) Walk from x to the root and splice at each proper ancestor of x. Now x and the root are in the same solid subtree 3) Splay at x Now x is the root of the entire virtual tree.

Dynamic tree operations w = findroot(v) : Splay at v, follow right pointers until reaching the last node w, splay at w, and return w. (v,c) = mincost(v) : Splay at v and use cost and min to follow pointer to the smallest node after v on its path (its in the right subtree of v). Let w be this node, splay at w. addcost(v,c) : Splay at v, increase cost(v) by c and decrease cost(left(v)) by c, update min(v) link(v,w,r(v,w)) : Splay at v and splay at w and make v a middle child of w cut(v) : Splay at v, break the link between v and right(v), set cost(right(v)) += cost(v)

Dynamic tree (analysis) It suffices to analyze the amortized time of splay. An extension of the access lemma. Recall: Assign weight 1 to each node. The size of a node is the total number of descendants it has in the virtual tree. Rank is the log of the size. Change: Potential is c+1 times the sum of the ranks for some constant c. The access lemma then shows that the amortized cost of the splay is 3(r(root)-r(x)) + 1 - 2c L/2  where L is the length of the splay path. This is at most clogn +1 - c(L-1). pass 1 takes 3clogn + k pass 2 takes k pass 3 takes 3clogn + 1 - c(k-1) k=#dashed edges on the path For an appropriate c this is O(log n)

Proof of the access lemma (cont) (3) zig y x ==> x C A y B C A B amortized time(zig) = 1 +  = 1 + r’(x) + r’(y) - r(x) - r(y)  1 + r’(x) - r(x)  1 + 3(r’(x) - r(x))

Proof of the access lemma (cont) (1) zig - zig z y x A B C D x ==> A y B z C D amortized time(zig) = 1 +  = 2 + r’(x) + r’(y) + r’(z) - r(x) - r(y) - r(z) = 2 + r’(y) + r’(z) - r(x) - r(y)  2 + r’(x) + r’(z) - 2r(x)  2r’(x) - r(x) - r’(z) + r’(x) + r’(z) - 2r(x) = 3(r’(x) - r(x))

Proof of the access lemma (cont) (2) zig - zag z x ==> y D y z A B C D x A B C Similar. (do at home)