Analysis of Approximate Quickselect and Related Problems

Similar documents
Chunksort: A Generalized Partial Sort Algorithm

Data selection. Lower complexity bound for sorting

On Partial Sorting. Conrado Martínez. Univ. Politècnica de Catalunya, Spain

Data Structures and Algorithms

Analysis of Algorithms CMPSC 565

Partial Quicksort. Conrado Martínez

Fundamental Algorithms

Fundamental Algorithms

Review Of Topics. Review: Induction

Data Structures and Algorithm Analysis (CSC317) Randomized Algorithms (part 3)

Divide-and-conquer. Curs 2015

Search Algorithms. Analysis of Algorithms. John Reif, Ph.D. Prepared by

Week 5: Quicksort, Lower bound, Greedy

Lecture 4. Quicksort

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

COMP 250: Quicksort. Carlos G. Oliver. February 13, Carlos G. Oliver COMP 250: Quicksort February 13, / 21

Analysis of Algorithms. Randomizing Quicksort

Algorithms And Programming I. Lecture 5 Quicksort

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

CS361 Homework #3 Solutions

Kartsuba s Algorithm and Linear Time Selection

Introduction to Randomized Algorithms: Quick Sort and Quick Selection

= main diagonal, in the order in which their corresponding eigenvectors appear as columns of E.

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Ancestor Problem for Branching Trees

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Expected heights in heaps. Jeannette M. de Graaf and Walter A. Kosters

Lecture #8: We now take up the concept of dynamic programming again using examples.

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

R ij = 2. Using all of these facts together, you can solve problem number 9.

Chapter 1: Systems of linear equations and matrices. Section 1.1: Introduction to systems of linear equations

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

COMP Analysis of Algorithms & Data Structures

Forty years of tree enumeration

Data Structures and Algorithms CSE 465

Advanced Analysis of Algorithms - Midterm (Solutions)

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Direct Methods for Solving Linear Systems. Matrix Factorization

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Partition and Select

Binary Search Trees. Motivation

Searching with Dice A survey on randomized data structures

Rank, Trace, Determinant, Transpose an Inverse of a Matrix Let A be an n n square matrix: A = a11 a1 a1n a1 a an a n1 a n a nn nn where is the jth col

5. DIVIDE AND CONQUER I

Design and Analysis of Algorithms

Fundamental Algorithms

Fast Sorting and Selection. A Lower Bound for Worst Case

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Asymptotic distribution of two-protected nodes in ternary search trees

CSE 4502/5717: Big Data Analytics

A General Lower Bound on the I/O-Complexity of Comparison-based Algorithms

Quick Sort Notes , Spring 2010

Chapter 4 - MATRIX ALGEBRA. ... a 2j... a 2n. a i1 a i2... a ij... a in

Lecture 7: Dynamic Programming I: Optimal BSTs

Algorithm Analysis Divide and Conquer. Chung-Ang University, Jaesung Lee

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Bin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then

1 Multiply Eq. E i by λ 0: (λe i ) (E i ) 2 Multiply Eq. E j by λ and add to Eq. E i : (E i + λe j ) (E i )

CSE 591 Homework 3 Sample Solutions. Problem 1

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Summary of Chapters 7-9

Neville s Method. MATH 375 Numerical Analysis. J. Robert Buchanan. Fall Department of Mathematics. J. Robert Buchanan Neville s Method

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Pattern Popularity in 132-Avoiding Permutations

The divide-and-conquer strategy solves a problem by: 1. Breaking it into subproblems that are themselves smaller instances of the same type of problem

Direct Methods for Solving Linear Systems. Simon Fraser University Surrey Campus MACM 316 Spring 2005 Instructor: Ha Le

CSCI 3110 Assignment 6 Solutions

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

Everything You Always Wanted to Know about Quicksort, but Were Afraid to Ask. Marianne Durand

Column Generation. i = 1,, 255;

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

1 Divide and Conquer (September 3)

The Quicksort algorithm and related topics

The divide-and-conquer strategy solves a problem by: Chapter 2. Divide-and-conquer algorithms. Multiplication

Searching. Sorting. Lambdas

Standard paths in another composition poset

ICS 6N Computational Linear Algebra Matrix Algebra

Definition 2.3. We define addition and multiplication of matrices as follows.

Data Structure. Mohsen Arab. January 13, Yazd University. Mohsen Arab (Yazd University ) Data Structure January 13, / 86

Advanced Analysis of Algorithms - Homework I (Solutions)

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

ANALYTICAL MATHEMATICS FOR APPLICATIONS 2018 LECTURE NOTES 3

Today s Outline. CS 362, Lecture 4. Annihilator Method. Lookup Table. Annihilators for recurrences with non-homogeneous terms Transformations

Chapter 3 Interpolation and Polynomial Approximation

1 Basic Definitions. 2 Proof By Contradiction. 3 Exchange Argument

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version)

Outline. Computer Science 331. Cost of Binary Search Tree Operations. Bounds on Height: Worst- and Average-Case

MATH 341 MIDTERM 2. (a) [5 pts] Demonstrate that A and B are row equivalent by providing a sequence of row operations leading from A to B.

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

Selection and Adversary Arguments. COMP 215 Lecture 19

The Divide-and-Conquer Design Paradigm

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

Computing Connected Components Given a graph G = (V; E) compute the connected components of G. Connected-Components(G) 1 for each vertex v 2 V [G] 2 d

University of the Virgin Islands, St. Thomas January 14, 2015 Algorithms and Programming for High Schoolers. Lecture 5

Transcription:

Analysis of Approximate Quickselect and Related Problems Conrado Martínez Univ. Politècnica Catalunya Joint work with A. Panholzer and H. Prodinger LIP6, Paris, April 2009

Introduction Quickselect finds the kth smallest element out of n given elements with average cost (n) Approximate Quickselect selects, out of n given elements, an element whose rank k fails within a prespecified rank [i::j] We analyze the exact number of passes (recursive calls) and number of key comparisons made by AQS, for arbitrary i, j and n Asymptotic estimates follow easily from the exact results

Introduction Quickselect finds the kth smallest element out of n given elements with average cost (n) Approximate Quickselect selects, out of n given elements, an element whose rank k fails within a prespecified rank [i::j] We analyze the exact number of passes (recursive calls) and number of key comparisons made by AQS, for arbitrary i, j and n Asymptotic estimates follow easily from the exact results

Introduction Quickselect finds the kth smallest element out of n given elements with average cost (n) Approximate Quickselect selects, out of n given elements, an element whose rank k fails within a prespecified rank [i::j] We analyze the exact number of passes (recursive calls) and number of key comparisons made by AQS, for arbitrary i, j and n Asymptotic estimates follow easily from the exact results

Introduction Quickselect finds the kth smallest element out of n given elements with average cost (n) Approximate Quickselect selects, out of n given elements, an element whose rank k fails within a prespecified rank [i::j] We analyze the exact number of passes (recursive calls) and number of key comparisons made by AQS, for arbitrary i, j and n Asymptotic estimates follow easily from the exact results

Introduction The techniques we use to analyze Approximate Quickselect prove useful to analyze other problems as well 1 The number of moves in which the ith element gets involved when selecting the jth smallest element out of n 2 The number of common ancestors of the nodes of rank i and j in a random binary search tree (BST) of size n 3 The size of the smallest subtree containing the nodes i and j in a random BST of size n 4 The distance (number of edges) between the nodes i and j in a random BST of size n We analyze also Approximate Multiple Quickselect, an algorithm to find q elements with ranks failing in prespecified ranges [i 1 ::j 1 ]; [i 2 ::j 2 ]; : : : ; [i q ::j q ]

Introduction The techniques we use to analyze Approximate Quickselect prove useful to analyze other problems as well 1 The number of moves in which the ith element gets involved when selecting the jth smallest element out of n 2 The number of common ancestors of the nodes of rank i and j in a random binary search tree (BST) of size n 3 The size of the smallest subtree containing the nodes i and j in a random BST of size n 4 The distance (number of edges) between the nodes i and j in a random BST of size n We analyze also Approximate Multiple Quickselect, an algorithm to find q elements with ranks failing in prespecified ranges [i 1 ::j 1 ]; [i 2 ::j 2 ]; : : : ; [i q ::j q ]

Introduction The techniques we use to analyze Approximate Quickselect prove useful to analyze other problems as well 1 The number of moves in which the ith element gets involved when selecting the jth smallest element out of n 2 The number of common ancestors of the nodes of rank i and j in a random binary search tree (BST) of size n 3 The size of the smallest subtree containing the nodes i and j in a random BST of size n 4 The distance (number of edges) between the nodes i and j in a random BST of size n We analyze also Approximate Multiple Quickselect, an algorithm to find q elements with ranks failing in prespecified ranges [i 1 ::j 1 ]; [i 2 ::j 2 ]; : : : ; [i q ::j q ]

Introduction The techniques we use to analyze Approximate Quickselect prove useful to analyze other problems as well 1 The number of moves in which the ith element gets involved when selecting the jth smallest element out of n 2 The number of common ancestors of the nodes of rank i and j in a random binary search tree (BST) of size n 3 The size of the smallest subtree containing the nodes i and j in a random BST of size n 4 The distance (number of edges) between the nodes i and j in a random BST of size n We analyze also Approximate Multiple Quickselect, an algorithm to find q elements with ranks failing in prespecified ranges [i 1 ::j 1 ]; [i 2 ::j 2 ]; : : : ; [i q ::j q ]

Introduction The techniques we use to analyze Approximate Quickselect prove useful to analyze other problems as well 1 The number of moves in which the ith element gets involved when selecting the jth smallest element out of n 2 The number of common ancestors of the nodes of rank i and j in a random binary search tree (BST) of size n 3 The size of the smallest subtree containing the nodes i and j in a random BST of size n 4 The distance (number of edges) between the nodes i and j in a random BST of size n We analyze also Approximate Multiple Quickselect, an algorithm to find q elements with ranks failing in prespecified ranges [i 1 ::j 1 ]; [i 2 ::j 2 ]; : : : ; [i q ::j q ]

Introduction The techniques we use to analyze Approximate Quickselect prove useful to analyze other problems as well 1 The number of moves in which the ith element gets involved when selecting the jth smallest element out of n 2 The number of common ancestors of the nodes of rank i and j in a random binary search tree (BST) of size n 3 The size of the smallest subtree containing the nodes i and j in a random BST of size n 4 The distance (number of edges) between the nodes i and j in a random BST of size n We analyze also Approximate Multiple Quickselect, an algorithm to find q elements with ranks failing in prespecified ranges [i 1 ::j 1 ]; [i 2 ::j 2 ]; : : : ; [i q ::j q ]

The algorithm Ensure: Array A[l::r ], integers i and j with l i j r Require: Returns a value k, with i k j, A[k ] has rank between i l + 1 and j l + 1 in the array A[l::r ] procedure AQS(A, i, j, l, r) if r l j i then return l end if PARTITION(A, l, r, k) { 8m : (l m < k ) ) A[m] A[k ], and 8m : (k < m r ) ) A[k ] A[m] } if j < k then return AQS(A, i, j, l, k 1) else if i > k then return AQS(A, i, j, k + 1, r) else return k end if end procedure

The number of passes P n;i;j = the average number of recursive calls to AQS to select an element of rank k 2 [i::j] out of n P n;i;j = 1+ 1 n Xi 1 k=1 P n k ;i k ;j k + 1 n nx k=j+1 P k 1;i;j ; for 1 i j n

The number of passes P n;i;j = the average number of recursive calls to AQS to select an element of rank k 2 [i::j] out of n P n;i;j = 1+ 1 n Xi 1 k=1 P n k ;i k ;j k + 1 n The initial recursive call nx k=j+1 P k 1;i;j ; for 1 i j n

The number of passes P n;i;j = the average number of recursive calls to AQS to select an element of rank k 2 [i::j] out of n P n;i;j = 1+ 1 n Xi 1 k=1 P n k ;i k ;j k + 1 n nx k=j+1 P k 1;i;j ; for 1 i j n If the pivot lands at k < i we continue in the right subarray of n k elements looking for an element with rank in [i k ::j k ]

The number of passes P n;i;j = the average number of recursive calls to AQS to select an element of rank k 2 [i::j] out of n P n;i;j = 1+ 1 n Xi 1 k=1 P n k ;i k ;j k + 1 n nx k=j+1 P k 1;i;j ; for 1 i j n If the pivot lands at k > j we continue in the left subarray of k 1 elements looking for an element with rank in [i::j]

The number of passes P n;i;j = the average number of recursive calls to AQS to select an element of rank k 2 [i::j] out of n P n;i;j = 1+ 1 n Xi 1 k=1 P n k ;i k ;j k + 1 n nx k=j+1 If the pivots lands at k, i k j, we are done P k 1;i;j ; for 1 i j n

The number of comparisons C n;i;j = the average number of key comparisons in AQS to select an element of rank k 2 [i::j] out of n C n;i;j = n 1+ 1 n Xi 1 k=1 C n k ;i k ;j k + 1 n nx k=j+1 C k 1;i;j ; for 1 i j n

The generic trivariate recurrence T n;i;j = generic toll function X n;i;j = T n;i;j + 1 n Xi 1 k=1 X n k ;i k ;j k + 1 n nx k=j+1 X k 1;i;j ; for 1 i j n Example T n;i;j = 1 ) passes T n;i;j = n 1 ) comparisons T n;i;j = n 6 + O(1) ) swaps i = j ) Quickselect

The generic trivariate recurrence Define: X (z; u 1 ; u 2 ) := T (z; u 1 ; u 2 ) := XXX i1 ji nj XXX i1 ji nj The trivariate recurrence translates to @ @z X (z; u 1; u 2 ) = X n;i;jz n u i 1 uj 2 ; T n;i;jz n u i 1 uj 2 : 1 u 1u 2 1 z + X (z; u 1 ; u 2 )+ @ 1 zu 1 u 2 @z T (z; u 1; u with initial condition X (0; u 1 ; u 2 ) = 0.

The generic trivariate recurrence Define: X (z; u 1 ; u 2 ) := T (z; u 1 ; u 2 ) := XXX i1 ji nj XXX i1 ji nj The trivariate recurrence translates to @ @z X (z; u 1; u 2 ) = X n;i;jz n u i 1 uj 2 ; T n;i;jz n u i 1 uj 2 : 1 u 1u 2 1 z + X (z; u 1 ; u 2 )+ @ 1 zu 1 u 2 @z T (z; u 1; u with initial condition X (0; u 1 ; u 2 ) = 0.

The generic trivariate recurrence Lemma X (z; u 1 ; u 2 ) = 1 (1 z)(1 zu 1 u 2 ) Z z 0 (1 t)(1 u 1 u 2 t) @ @t T (t; u 1; u 2 ) dt:

The generic trivariate recurrence Theorem X n;i;j = + + Xi 1 `=1 k=j Xi 1 `=1 Xn 1 k=j n i+` X 1 i+` 2T k ;`;j T n i+`;`;j i+` n i + ` + 1 T k ;i;j k + 1 + T n;i;j i+` (k + 1)(k + 2) Setting i = j we rederive the generic solution for Quickselect-like recurrences by Kuba (2006).

Analyzing AQS Theorem The expected number of passes in AQS is P n;i;j = H j + H n i+1 2H j i+1 + 1: The expected number of comparisons in AQS is C n;i;j = 2(n + 1)H n + 2(j i + 4)H j i+1 2(j + 2)H j 2(n i + 3)H n i+1 + 2n j + i 2: ( ) H n := X 1kn 1 k ; nth harmonic number

Moves in Quickselect Consider the ith smallest element in the array A[1::n]. How many times do we move it around when selecting the jth smallest element in A?

Moves in Quickselect We make a case analysis, comparing with the position k where the pivot lands after a partitioning step: if k < i j or k < j i, Quickselect continues recursively in A[k + 1::n] that does contain ith element if j i < k or i j < k, Quickselect continues in A[1::k 1] that does contain the ith element if i k j or j k i, either Quickselect stops or continues in a subarray not containing ith element

Moves in Quickselect We make a case analysis, comparing with the position k where the pivot lands after a partitioning step: if k < i j or k < j i, Quickselect continues recursively in A[k + 1::n] that does contain ith element if j i < k or i j < k, Quickselect continues in A[1::k 1] that does contain the ith element if i k j or j k i, either Quickselect stops or continues in a subarray not containing ith element

Moves in Quickselect We make a case analysis, comparing with the position k where the pivot lands after a partitioning step: if k < i j or k < j i, Quickselect continues recursively in A[k + 1::n] that does contain ith element if j i < k or i j < k, Quickselect continues in A[1::k 1] that does contain the ith element if i k j or j k i, either Quickselect stops or continues in a subarray not containing ith element

Moves in Quickselect To find the toll function (number of moves where i participates) in a single partitioning step, we also consider three cases: 1 i = k ) the element i is moved (once) 2 i < k ) the element i is moved if it were in A[k ::n] ) prob = (n k + 1)=(n 1) 3 i > k ) the element is moved if it were in A[2::k ] ) prob = (k 1)=(n 1)

Moves in Quickselect M n;i;j:= The expected number of moves of the ith element when selecting the jth smallest out of n M n;i;j = 1 n Xi 1 k=1 M n k ;i k ;j k + 1 n nx k=j+1 M k 1;i;j + (i 1)(i 2) 2n(n 1) + (n i)(n i + 1) 2n(n 1) + 1 n ; 1 i < j n Analogous recurrences for i = j and j < i, all three solved using the theorem for trivariate recurrences

Moves in Quickselect M n;i;j:= The expected number of moves of the ith element when selecting the jth smallest out of n M n;i;j = 1 n Xi 1 k=1 M n k ;i k ;j k + 1 n nx k=j+1 M k 1;i;j + (i 1)(i 2) 2n(n 1) + (n i)(n i + 1) 2n(n 1) + 1 n ; 1 i < j n Analogous recurrences for i = j and j < i, all three solved using the theorem for trivariate recurrences

Binary search trees A n;i;j:= average # of common ancestors of nodes i and j S n;i;j:= average size of smallest subtree containing nodes i and j D n;i;j:= average distance (# of edges) between nodes i and j

Binary search trees A n;i;j:= average # of common ancestors of nodes i and j S n;i;j:= average size of smallest subtree containing nodes i and j D n;i;j:= average distance (# of edges) between nodes i and j

Binary search trees A n;i;j:= average # of common ancestors of nodes i and j S n;i;j:= average size of smallest subtree containing nodes i and j D n;i;j:= average distance (# of edges) between nodes i and j

Binary search trees 15 5 16 2 10 1 4 8 13 3 6 9 12 14 7 11 Example A 16;8;12 = 3 (nodes 15, 5, 10) S 16;8;12 = 9 (subtree rooted at 10) D 16;8;12 = 3

Binary search trees if T = (L; R) is random binary search tree (BST) of size n > 0, then 1 Any element has identical probability of being the root, thus PrfjLj = k 1 j jt j = ng = 1 n ; 1 k n 2 L and R are independent random BSTs of sizes k 1 and n k, 1 k n

Binary search trees if T = (L; R) is random binary search tree (BST) of size n > 0, then 1 Any element has identical probability of being the root, thus PrfjLj = k 1 j jt j = ng = 1 n ; 1 k n 2 L and R are independent random BSTs of sizes k 1 and n k, 1 k n

Common ancestors Suppose that the root of the BST is the kth element. if i j < k the number of common ancestors is 1 + the number of common ancestors in a random of BST of size k 1 if k < i j the number of common ancestors is 1 + the number of common ancestors in a random of BST of size n 1 k (of the nodes of ranks i k and j k!) Lemma if i k j, then there is only one common ancestor (k) A n;i;j = P n;i;j = passes in AQS

Common ancestors Suppose that the root of the BST is the kth element. if i j < k the number of common ancestors is 1 + the number of common ancestors in a random of BST of size k 1 if k < i j the number of common ancestors is 1 + the number of common ancestors in a random of BST of size n 1 k (of the nodes of ranks i k and j k!) Lemma if i k j, then there is only one common ancestor (k) A n;i;j = P n;i;j = passes in AQS

Common ancestors Suppose that the root of the BST is the kth element. if i j < k the number of common ancestors is 1 + the number of common ancestors in a random of BST of size k 1 if k < i j the number of common ancestors is 1 + the number of common ancestors in a random of BST of size n 1 k (of the nodes of ranks i k and j k!) Lemma if i k j, then there is only one common ancestor (k) A n;i;j = P n;i;j = passes in AQS

Common ancestors Suppose that the root of the BST is the kth element. if i j < k the number of common ancestors is 1 + the number of common ancestors in a random of BST of size k 1 if k < i j the number of common ancestors is 1 + the number of common ancestors in a random of BST of size n 1 k (of the nodes of ranks i k and j k!) Lemma if i k j, then there is only one common ancestor (k) A n;i;j = P n;i;j = passes in AQS

Size of subtree of LCA The recurrence for S n;i;j follows the usual pattern: S 1 n;i;j = n 1 = n Xi 1 k=1 Xi 1 k=1 S n k ;i k ;j k + 1 n S n k ;i k ;j k + 1 n nx k=j+1 nx k=j+1 S k 1;i;j + 1 n jx k=i n S k 1;i;j + j i + 1

Size of subtree of LCA The recurrence for S n;i;j follows the usual pattern: S 1 n;i;j = n 1 = n Xi 1 k=1 Xi 1 k=1 S n k ;i k ;j k + 1 n S n k ;i k ;j k + 1 n nx k=j+1 nx k=j+1 S k 1;i;j + 1 n jx k=i n S k 1;i;j + j i + 1 The toll function is n only when the pivot k satisfies i k j

Size of subtree of LCA Apply the theorem for trivariate recurrences with T n;i;j := j i + 1 Theorem S n;i;j = (j i + 1) H j + H n i+1 2H j i+1 + 1 = (j i + 1) A n;i;j (j i + 1) log j + log(n i + 1) 2 log(j i + 1) + 1

Distance The recurrence for D n;i;j: D n;i;j = 1 n + 1 n Xi 1 k=1 D n k ;i k ;j k + 1 n jx k=i nx D k 1;i;j k=j+1 (A k 1;i;i + A n k ;j k ;j k + 2)

Distance The recurrence for D n;i;j: D n;i;j = 1 n + 1 n Xi 1 k=1 D n k ;i k ;j k + 1 n jx k=i nx D k 1;i;j k=j+1 (A k 1;i;i + A n k ;j k ;j k + 2) If k is the LCA of i and j, the distance between i and j is the depth of i in the left subtree (A k 1;i;i), plus the depth of j (the (j k )th element) in the right subtree (A n k ;j k ;j k), plus 2

Distance Since we know A n;i;j, we can obtain the toll function for distances 1 n jx k=i (A k 1;i;i + A n k ;j k ;j k + 2) = j i + 1 n H i + H n+1 j + 2H j+1 i 2

Distance The last step is to apply the theorem of trivariate recurrences with the toll function above (quite laboriously!) Theorem D n;i;j = 4H j+1 i (H j H i ) (H n+1 i H n+1 j ) 3