algorithms and data structures (ws15/16) example solutions ... · algorithms and data structures...

11
Algorithms and Data Structures (WS15/16) Example Solutions for Unit 12-13 Question 2 In this problem, we will not distinguish the key x i and the node which store x i . Let X ij be an indicator random variable where X ij =1 iff x i is an ancestor of x j . Let S k be the size of subtree rooted at x k . We want to find E[S k ]. By definition, S k = n i=1 X ki . From class, we know E[X ki ] = Pr[X ki = 1] = Pr[x k has minimum priority among elements between x i and x k ]= 1 |k-i|+1 . Now we can compute E[S k ] directly. E[S k ] = E[ n X i=1 X ki ] = n X i=1 E[X ki ] = k X i=1 1 k - i +1 + n X i=k+1 1 i - k +1 from class = H k + H n-k - 1= O(log n) 1

Upload: others

Post on 08-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithms and Data Structures (WS15/16) Example Solutions ... · Algorithms and Data Structures (WS15/16) Example Solutions for Unit 12-13 Question 2 Inthisproblem,wewillnotdistinguishthekeyx

Algorithms and Data Structures (WS15/16)Example Solutions for Unit 12-13

Question 2

In this problem, we will not distinguish the key xi and the node which store xi.

Let Xij be an indicator random variable where Xij = 1 iff xi is an ancestor of xj . Let Sk bethe size of subtree rooted at xk. We want to find E[Sk].

By definition, Sk =∑n

i=1Xki . From class, we know E[Xki] = Pr[Xki = 1] = Pr[xk hasminimum priority among elements between xi and xk] =

1|k−i|+1 .

Now we can compute E[Sk] directly.

E[Sk] = E[

n∑i=1

Xki]

=

n∑i=1

E[Xki]

=k∑

i=1

1

k − i+ 1+

n∑i=k+1

1

i− k + 1from class

= Hk +Hn−k − 1 = O(log n)

1

Page 2: Algorithms and Data Structures (WS15/16) Example Solutions ... · Algorithms and Data Structures (WS15/16) Example Solutions for Unit 12-13 Question 2 Inthisproblem,wewillnotdistinguishthekeyx
Page 3: Algorithms and Data Structures (WS15/16) Example Solutions ... · Algorithms and Data Structures (WS15/16) Example Solutions for Unit 12-13 Question 2 Inthisproblem,wewillnotdistinguishthekeyx
Page 4: Algorithms and Data Structures (WS15/16) Example Solutions ... · Algorithms and Data Structures (WS15/16) Example Solutions for Unit 12-13 Question 2 Inthisproblem,wewillnotdistinguishthekeyx
Page 5: Algorithms and Data Structures (WS15/16) Example Solutions ... · Algorithms and Data Structures (WS15/16) Example Solutions for Unit 12-13 Question 2 Inthisproblem,wewillnotdistinguishthekeyx
Page 6: Algorithms and Data Structures (WS15/16) Example Solutions ... · Algorithms and Data Structures (WS15/16) Example Solutions for Unit 12-13 Question 2 Inthisproblem,wewillnotdistinguishthekeyx
Page 7: Algorithms and Data Structures (WS15/16) Example Solutions ... · Algorithms and Data Structures (WS15/16) Example Solutions for Unit 12-13 Question 2 Inthisproblem,wewillnotdistinguishthekeyx
Page 8: Algorithms and Data Structures (WS15/16) Example Solutions ... · Algorithms and Data Structures (WS15/16) Example Solutions for Unit 12-13 Question 2 Inthisproblem,wewillnotdistinguishthekeyx
Page 9: Algorithms and Data Structures (WS15/16) Example Solutions ... · Algorithms and Data Structures (WS15/16) Example Solutions for Unit 12-13 Question 2 Inthisproblem,wewillnotdistinguishthekeyx
Page 10: Algorithms and Data Structures (WS15/16) Example Solutions ... · Algorithms and Data Structures (WS15/16) Example Solutions for Unit 12-13 Question 2 Inthisproblem,wewillnotdistinguishthekeyx
Page 11: Algorithms and Data Structures (WS15/16) Example Solutions ... · Algorithms and Data Structures (WS15/16) Example Solutions for Unit 12-13 Question 2 Inthisproblem,wewillnotdistinguishthekeyx