Sie sind auf Seite 1von 4

Assignment 1 CPSC 469

Edward Kimo Hon


9/10/14

A.1.2 Two Terrible Data Structures


A.1.2.1 Sorted Array

For each element i, the cost of insertion is determined by the number of elements
> i which have already been inserted.

Thus, element n will always cost 0

because no element can be greater than it. Rather than calculate the cost of
each permutation, we will use linearity of expectations to nd the expected cost
of inserting elements
know there are

(n i + 1)!

ni

1...n

and sum the expectations. For each element i, we

elements greater than it. Including that element, there are

permutations of how they can be arranged, with each being equally

likely since we choose the elements at random. Of these permutations, it is clear


that all permutations where i is inserted before the others incurs a cost of 0 for
i's insertion. We nd this fraction of the total number of cases to be

(n i)!
1
=
(n i + 1)!
ni+1
1
1
ni+1 of the cases incur a cost of 1, ni+1 cases incur a cost of 2,
and so on up until n i. The expectation is therefore:
Similarly,

E[i] = (

2
ni
1
1
+
+. . .+
)=
(1+2+. . .+ni)
ni+1 ni+1
ni+1
ni+1
=

1
(n i)(n i + 1)
ni

=
ni+1
2
2

Which we need to sum over all elements i to get our nal expectation

E[Cost] =

n
X
ni
i=1

n1
1X
n (n 1)
1 (n 1)(n 1 + 1)
=
ni=
2 i=0
2
2
4

A.1.2.2 Doubly Linked List

We will again use linearity of expectations by summing the expected cost of


inserting the 1st to nth elements.

To insert the kth element, it is clear that

there are k places it can go in between and around the existing k-1 elements.

It is equally clear that the header pointer can be on any of the k-1 existing
elements.

k (k 1)

We therefore know that there are a total of

possible

combinations we have to think about.

There are

(k 1)
2 (k 1)

There are

elements that each have 2 spots that are 1 move away

(k 3) elements that have 2

spots that are 2 moves away, and

elements that have 1 spot that is 2 moves away

2 (k 3) + 2 1

...

And so on, at each step i we have 2 more elements that can only nd 1 slot
that is i moves away

This yields our formula for the # of possibilities for i moves

2 (k 2i + 1) + 2 (i 1) = (2k 2i)
To get the weighted sum, we now multiply each number of possibilities by
the weight of the moves and sum from 1 to the maximum number of moves
possible (k-1)

k1
X

i (2k 2i) =

i=1

k (k 1) (k + 1)
3

And the expectation of inserting the kth element is just that weighted sum
divided by the total number of possibilities

E[k] =

k(k1)(k+1)
3

k (k 1)

k+1
3

And our total expectation can be found by summing

E[k] from 1 to n.

We'll

take the 1st and 2nd cases separately, since we know that the rst one will be
free (by denition) and the second one is necessarily 1.

E[Cost] = 0+1+

n
X
k+1

k=3

= 1+

k=3

k=3

1X
1X
1
k+
1 = 1+
3
3
3



n(n + 1)
1
1 2
3 + (n 2) =
n + 3n 4
2
3
6

A.1.3 Parallel Graph Coloring


We will approach this problem in a casewise fashion. For each edge, there are 4
cases:
1. The edge was monochromatic after step 1, and monochromatic after step
2
2. The edge was monochromatic after step 1, and not monochromatic after
step 2

3. The edge was not monochromatic after step 1, and monochromatic after
step 2
4. The edge was monochromatic after step 1, and not monochromatic after
step 2
We are most interested in cases 1 and 3 since they produce monochromatic
edges after step 2, which is what we are looking for. We observe that if we take

1
k probability that the
second will be colored the same as the rst. Thus, the probability that an edge
any two vertices and color the rst arbitrarily, there is a

is monochromatic after step 1 is

P (Step1) =

1
k

And clearly the chance an edge is not monochromatic after step 1 is

1
P (Step1) = 1 =
k

k1
k

Now we analyze step 2. If an edge is monochromatic, both vertices will be


rerolled in step 2. By the same logic as previously, we observe that the chance
that both are the same color (that the edge is monochromatic) is

P (Step2|Step1) =

1
k

The case where an edge is not monochromatic after step 1 but is after step
2 is more complicated. We propose 3 cases
1. Neither vertex gets rerolled
2. One vertex gets rerolled
3. Both vertices get rerolled
We can ignore case 1 since this will clearly not produce a monochromatic edge.
A vertex will get rerolled if any one of its

r1

neighboring vertices is the same

color. The chance that any neighbor is the same color can be calculated by the
following:


P (Reroll) = 1 P (N oM onoEdges) = 1

k1
k

r1

Thus the chance that Case 2 occurs multiplied by the chance that the reroll
makes it the same as the unrolled vertex is equal to


2 1

k1
k

r1 ! 

k1
k

r1 !  
1
k

And the chance that Case 3 occurs multipled by the chance that the reroll
creates a monochromatic edge is


1

k1
k

r1 !2  
1
k

Adding everything up by the law of total probability, we have

P (M ono) = P (Step2|Step1)P (Step1) + P (Step2|Step1)P (Step1)


r1 ! 
r1  

r1 !2   

  
1 k1
1
k1
k1
1
k1
1
+ 2 1
+ 1
=
k k
k
k
k
k
k
k
The expected value can be found by simply summing over all m edges
which all have

P (M ono) to
X

e E,

be monochromatic after both steps

P (M ono) = m P (M ono)

eE

r1 ! 
r1


k1
k1
1

+
E[M onochromaticEdges] = m
+ 2 1
k2
k
k
m
=
k


1

k1
k

2r1 !

k1
k

r1 !2 
k

Das könnte Ihnen auch gefallen