Self-Adjusting Binary Search Trees. Andrei Pârvu

Similar documents
Splay tree concept Splaying operation: double-rotations Splay insertion Splay search Splay deletion Running time of splay tree operations

CSS 343 Data Structures, Algorithms, and Discrete Math II. Balanced Search Trees. Yusuf Pisan

CSE 100: RED-BLACK TREES

RBT Operations. The basic algorithm for inserting a node into an RBT is:

5 AVL trees: deletion

Topic 23 Red Black Trees

MITOCW 6. AVL Trees, AVL Sort

Balanced Trees. Balanced Trees Tree. 2-3 Tree. 2 Node. Binary search trees are not guaranteed to be balanced given random inserts and deletes

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 17: Heaps and Priority Queues

CSI33 Data Structures

Lecture5: Lossless Compression Techniques

game tree complete all possible moves

From Shared Memory to Message Passing

Adversary Search. Ref: Chapter 5

Random Binary Search Trees. EECS 214, Fall 2017

CS : Data Structures

mywbut.com Two agent games : alpha beta pruning

Permutations. Example 1. Lecture Notes #2 June 28, Will Monroe CS 109 Combinatorics

Module 3 Greedy Strategy

PRIORITY QUEUES AND HEAPS. Lecture 19 CS2110 Spring 2014

LECTURE VI: LOSSLESS COMPRESSION ALGORITHMS DR. OUIEM BCHIR

Module 3 Greedy Strategy

1 Permutations. Example 1. Lecture #2 Sept 26, Chris Piech CS 109 Combinatorics

Coding for Efficiency

Information Theory and Communication Optimal Codes

1 Permutations. 1.1 Example 1. Lisa Yan CS 109 Combinatorics. Lecture Notes #2 June 27, 2018

A Memory Efficient Anti-Collision Protocol to Identify Memoryless RFID Tags

MA/CSSE 473 Day 13. Student Questions. Permutation Generation. HW 6 due Monday, HW 7 next Thursday, Tuesday s exam. Permutation generation

PRIORITY QUEUES AND HEAPS

and 6.855J. Network Simplex Animations

CS/COE 1501

Universal Cycles for Permutations Theory and Applications

COCI 2008/2009 Contest #3, 13 th December 2008 TASK PET KEMIJA CROSS MATRICA BST NAJKRACI

Binary Search Tree (Part 2 The AVL-tree)

Lab 6 This lab can be done with one partner or it may be done alone. It is due in two weeks (Tuesday, May 13)

Data Gathering. Chapter 4. Ad Hoc and Sensor Networks Roger Wattenhofer 4/1

Greedy Algorithms. Kleinberg and Tardos, Chapter 4

AN ALTERNATIVE METHOD FOR ASSOCIATION RULES

Efficient and Compact Representations of Some Non-Canonical Prefix-Free Codes

MITOCW ocw lec11

CSE 21 Practice Final Exam Winter 2016

More on games (Ch )

INF September 25, The deadline is postponed to Tuesday, October 3

Huffman Coding - A Greedy Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

PRIORITY QUEUES AND HEAPS. Slides of Ken Birman, Cornell University

ARTIFICIAL INTELLIGENCE (CS 370D)

Design of Parallel Algorithms. Communication Algorithms

1 This work was partially supported by NSF Grant No. CCR , and by the URI International Engineering Program.

Module 3. Problem Solving using Search- (Two agent) Version 2 CSE IIT, Kharagpur

Solutions to Assignment-2 MOOC-Information Theory

Playing Games. Henry Z. Lo. June 23, We consider writing AI to play games with the following properties:

Will Monroe June 28, with materials by Mehran Sahami and Chris Piech. Combinatorics

UNIVERSITY of PENNSYLVANIA CIS 391/521: Fundamentals of AI Midterm 1, Spring 2010

Computer Science and Software Engineering University of Wisconsin - Platteville. 4. Game Play. CS 3030 Lecture Notes Yan Shi UW-Platteville

Online Frequency Assignment in Wireless Communication Networks

CS188 Spring 2010 Section 3: Game Trees

MA/CSSE 473 Day 14. Permutations wrap-up. Subset generation. (Horner s method) Permutations wrap up Generating subsets of a set

HEAPABLE SEQUENCES AND SUBSEQUENCES

Lecture 14 Instruction Selection: Tree-pattern matching

More on games (Ch )

Dynamic Tag Estimation for Optimizing Tree Slotted Aloha in RFID Networks

MITOCW R11. Principles of Algorithm Design

MITOCW R9. Rolling Hashes, Amortized Analysis

From ProbLog to ProLogic

16.410/413 Principles of Autonomy and Decision Making

Problem A. Worst Locations

Heapable Sequences and Subseqeuences

Twin Binary Sequences: A Nonredundant Representation for General Nonslicing Floorplan

Generalized Game Trees

Past questions from the last 6 years of exams for programming 101 with answers.

CS-E4800 Artificial Intelligence

Informatica Universiteit van Amsterdam. Performance optimization of Rush Hour board generation. Jelle van Dijk. June 8, Bachelor Informatica

CHAPTER 5 PAPR REDUCTION USING HUFFMAN AND ADAPTIVE HUFFMAN CODES

TIME- OPTIMAL CONVERGECAST IN SENSOR NETWORKS WITH MULTIPLE CHANNELS

DATA STRUCTURE TREES UNIT II

MITOCW watch?v=c6ewvbncxsc

Recap from previous lectures. Information Retrieval. Recap from previous lectures. Topics for Today. Dictionaries & Tolerant Retrieval.

Midterm. CS440, Fall 2003

CS188 Spring 2010 Section 3: Game Trees

Recap from previous lecture. Information Retrieval. Topics for Today. Recall: Basic structure of an Inverted index. Dictionaries & Tolerant Retrieval

Parsimony II Search Algorithms

(Lec19) Geometric Data Structures for Layouts

Lectures: Feb 27 + Mar 1 + Mar 3, 2017

COMP9414: Artificial Intelligence Adversarial Search

Lecture 14. Questions? Friday, February 10 CS 430 Artificial Intelligence - Lecture 14 1

RFID Systems, an Introduction Sistemi Wireless, a.a. 2013/2014

DECISION TREE TUTORIAL

Set 4: Game-Playing. ICS 271 Fall 2017 Kalev Kask

Printing: You may print to the printer at any time during the test.

COS 226 Algorithms and Data Structures Fall Midterm Exam

Backtracking. Chapter Introduction

Adversarial Search 1

Introduction to Source Coding

COMP 2804 solutions Assignment 4

COS 226 Algorithms and Data Structures Fall Midterm Exam

GENOMIC REARRANGEMENT ALGORITHMS

A Factor Graph Based Dynamic Spectrum Allocation Approach for Cognitive Network

Game Tree Search. CSC384: Introduction to Artificial Intelligence. Generalizing Search Problem. General Games. What makes something a game?

Wednesday, February 1, 2017

A Level Computer Science H446/02 Algorithms and programming. Practice paper - Set 1. Time allowed: 2 hours 30 minutes

Transcription:

Self-Adjusting Binary Search Trees Andrei Pârvu Andrei Pârvu 13-05-2015 1

Motivation Andrei Pârvu 13-05-2015 2

Motivation: Find Andrei Pârvu 13-05-2015 3

Motivation: Insert Andrei Pârvu 13-05-2015 4

Motivation: Delete Andrei Pârvu 13-05-2015 5

Goal: O(M logn) time complexity (N elements, M operations) Andrei Pârvu 13-05-2015 6

What is a Binary Search Tree? Binary tree :) Andrei Pârvu 13-05-2015 7

What is a Binary Search Tree? Binary tree :) Each node stores an element (key) Andrei Pârvu 13-05-2015 8

What is a Binary Search Tree? Binary tree :) Each node stores an element (key) Key of a node: is bigger than the keys of the left subtree is smaller than the keys of the right subtree Andrei Pârvu 13-05-2015 9

Example Andrei Pârvu 13-05-2015 10

Operations: find element walk recursively down the tree Andrei Pârvu 13-05-2015 11

Operations: find element walk recursively down the tree if element equals with node key, stop Andrei Pârvu 13-05-2015 12

Operations: find element walk recursively down the tree if element equals with node key, stop else go to left child if element < than node key go to right child if element > than node key Andrei Pârvu 13-05-2015 13

Operations: insert element same algorithm as find add element as leaf Andrei Pârvu 13-05-2015 14

Example: insert element Andrei Pârvu 13-05-2015 15

Time complexity of operations if elements are chosen randomly, then O(M logn) most of the time that is not the case :( Andrei Pârvu 13-05-2015 16

Example linear tree Andrei Pârvu 13-05-2015 17

How to make it faster? Andrei Pârvu 13-05-2015 18

Rotations rotate a node to the left or to the right Andrei Pârvu 13-05-2015 19

Rotations rotate a node to the left or to the right maintain the BST invariant Andrei Pârvu 13-05-2015 20

Rotations rotate a node to the left or to the right maintain the BST invariant use them to modify the tree structure and maintain it balanced Andrei Pârvu 13-05-2015 21

Example: rotation to the right Andrei Pârvu 13-05-2015 22

Operations: rotation to the left Andrei Pârvu 13-05-2015 23

How can we use rotations? Andrei Pârvu 13-05-2015 24

Move to root heuristic after accessing an item at node x, rotate edge from x to its parent until x becomes root. Does this improve anything? Andrei Pârvu 13-05-2015 25

Move to root heuristic after accessing an item at node x, rotate edge from x to its parent until x becomes root. Does this improve anything? No, time of access can still be O(n) Andrei Pârvu 13-05-2015 26

Splay tree BST with a restructuring heuristic, called splaying after inserting or finding an element, do pairs of rotations bottom-up Andrei Pârvu 13-05-2015 27

Splay tree BST with a restructuring heuristic, called splaying after inserting or finding an element, do pairs of rotations bottom-up rotations depend on the structure of the path each pair of rotations shall be named a splaying step Andrei Pârvu 13-05-2015 28

Splay tree BST with a restructuring heuristic, called splaying after inserting or finding an element, do pairs of rotations bottom-up rotations depend on the structure of the path each pair of rotations shall be named a splaying step repeat splaying step on x until it is root Andrei Pârvu 13-05-2015 29

Splaying step - case 1: zig if p(x), parent of x, is root of tree, rotate edge joining x with p(x) terminal case Andrei Pârvu 13-05-2015 30

Example: zig Andrei Pârvu 13-05-2015 31

Splaying step - case 2: zig-zig p(x) not the root g(x) parent of p(x) x and p(x) both right-children or both left-children rotate edge joining p(x) with g(x) rotate edge joining p(x) with x Andrei Pârvu 13-05-2015 32

Example: zig-zig Andrei Pârvu 13-05-2015 33

Splaying step - case 3: zig-zag p(x) not the root g(x) parent of p(x) x left child and p(x) right child or vice-versa rotate edge joining x with p(x) rotate edge joining x with g(x) Andrei Pârvu 13-05-2015 34

Example: zig-zag Andrei Pârvu 13-05-2015 35

Example: splaying on a node Andrei Pârvu 13-05-2015 36

Example: splaying on a node (1) Andrei Pârvu 13-05-2015 37

Example: splaying on a node (2) Andrei Pârvu 13-05-2015 38

Example: splaying on a node (3) Andrei Pârvu 13-05-2015 39

Example: splaying on a node (4) Andrei Pârvu 13-05-2015 40

Complexity & Analysis Why is splaying better than move to root heuristic? Andrei Pârvu 13-05-2015 41

Complexity & Analysis Why is splaying better than move to root heuristic? if a node is at depth d on the splaying path, it will be at about d/2 after the splay Andrei Pârvu 13-05-2015 42

Complexity & Analysis Why is splaying better than move to root heuristic? if a node is at depth d on the splaying path, it will be at about d/2 after the splay except the root, its child and the splayed node Andrei Pârvu 13-05-2015 43

Complexity & Analysis II use the potential method Φ(T ) = extra time that can be later consumed on tree T from T to T amortized time = actual_time + Φ(T ) Φ(T ) Andrei Pârvu 13-05-2015 44

Complexity & Analysis II amortized time = actual_time + Φ(T ) Φ(T ) if actual time < amortized time, increase potential if actual time > amortized time, decrease potential Andrei Pârvu 13-05-2015 45

Analysis on M operations t 1 + t 2 +... + t M + (Φ(T 1 ) Φ(T 0 )) + (Φ(T 2 ) Φ(T 1 )) +... + (Φ(T M ) Φ(T M 1 )) = t 1 + t 2 +... + t M + Φ(T M ) Φ(T 0 ). Andrei Pârvu 13-05-2015 46

Potential function size(x) = number of nodes in the subtree rooted at x rank(x) = log 2 (size(x)) Φ(T ) = sum of ranks of nodes in subtree T Andrei Pârvu 13-05-2015 47

Potential function Andrei Pârvu 13-05-2015 48

Potential splaying only x, p(x) and g(x) change rank Φ = rank i (g) rank i 1 (g) + rank i (x) rank i 1 (x) + rank i (p) rank i 1 (p) actual_cost + Φ 3 (rank i (x) rank i 1 (x)) + 1 Andrei Pârvu 13-05-2015 49

Complexity & Analysis III amortized time = actual_cost + Φ 3 (rank i (x) rank i 1 (x)) + 1 total time O(m log(n)) Andrei Pârvu 13-05-2015 50

Analysis Pros: no additional information stored in nodes not that hard to implement Cons: at one point an operation can have O(n) time problems with multithreading Andrei Pârvu 13-05-2015 51

Splitting a splay tree split(i, t): construct and return t 1 and t 2 elements in t 1 smaller than i elements in t 2 greater than i Ideas? Andrei Pârvu 13-05-2015 52

How to split? Andrei Pârvu 13-05-2015 53

Joining two splay trees join(t1, t2): combine t 1 and t 2 into single tree elements in t 1 smaller than elements in t 2 Ideas? Andrei Pârvu 13-05-2015 54

How to join? Andrei Pârvu 13-05-2015 55

Applications: Lexicographic Search Tree Andrei Pârvu 13-05-2015 56

Lexicographic Search Tree store a set S of strings repeated access operations are efficient Andrei Pârvu 13-05-2015 57

Example - Lexicographic Tree Andrei Pârvu 13-05-2015 58

Lexicographic Search Tree II ternary tree symbols in each node two types of edges middle (dashed) left & right nodes in the tree correspond to prefixes of strings: concatenate symbols from which we leave by a dashed edge nodes connected by continuous edges form a binary search tree Andrei Pârvu 13-05-2015 59

Search for bats (1) Andrei Pârvu 13-05-2015 60

Search for bats (2) Andrei Pârvu 13-05-2015 61

Search for bats (3) Andrei Pârvu 13-05-2015 62

Search for bats (4) Andrei Pârvu 13-05-2015 63

Search for bats (5) Andrei Pârvu 13-05-2015 64

Using splaying rotation rearranges left and right child, but not the middle props splay at node x: similar with normal splay tree if node is middle child, continue with p(x) Andrei Pârvu 13-05-2015 65

Using splaying rotation rearranges left and right child, but not the middle props splay at node x: similar with normal splay tree if node is middle child, continue with p(x) after splaying, path from root to x contains only dashed edges Andrei Pârvu 13-05-2015 66

Insert car Andrei Pârvu 13-05-2015 67

Insert car (2) Andrei Pârvu 13-05-2015 68

Insert car (3) Andrei Pârvu 13-05-2015 69

Lex Tree splay Andrei Pârvu 13-05-2015 70

Lex Tree splay Andrei Pârvu 13-05-2015 71

Lex Tree analysis time of access is bounded by σ plus number of right and left edges traversed O( σ + log 2 (N)) Andrei Pârvu 13-05-2015 72

Application: Link-Cut Trees Andrei Pârvu 13-05-2015 73

Link-Cut Trees abstract data structure for maintaining a forest of rooted trees the following operations should be supported find_root(v) cut(v) link(v, w) Andrei Pârvu 13-05-2015 74

Where are self-adjusting BSTs used? Andrei Pârvu 13-05-2015 75

Java (TreeMap, TreeSet) and C++ (set, map) Andrei Pârvu 13-05-2015 76

Java (TreeMap, TreeSet) and C++ (set, map) Linux CFS scheduler, which decides which tasks are executed when Andrei Pârvu 13-05-2015 77

Java (TreeMap, TreeSet) and C++ (set, map) Linux CFS scheduler: decides which tasks are executed when memory allocators Andrei Pârvu 13-05-2015 78

Experiment 1: normal queries Andrei Pârvu 13-05-2015 79

Experiment 2: reduced set of query elements Andrei Pârvu 13-05-2015 80

Take-home message you probably use self-adjusting binary search trees every day :) it is useful to know how they work and how to implement one C++ STL or java.util cannot save you all the time Andrei Pârvu 13-05-2015 81

Andrei Pârvu 13-05-2015 82

Potential function zig-zag only x, p(x) and g(x) change rank Φ = rank (g) rank(g) + rank (x) rank(x) + rank (p) rank(p) = rank (g) rank(x) + rank (p) rank(p) rank (g) + rank (p) 2 rank(x) rank (g) + rank (p) 2 rank(x) + 2 2 [rank (g) + rank (p) 2 rank(x)] + 2 rank (x) rank(p) rank (g) 2 2 (rank (x) rank(x)) 2 Andrei Pârvu 13-05-2015 83