Docsity

Solutions to Exercises 10.2-7, 10.3-4, 10.4-6, 11.2-1, 11.2-4, 11.1, and 11-2 - Prof. Jose, Assignments of Electrical and Electronics Engineering

Toggle sidebar
bg1
Solution to Exercise 10.2-7
A -time non-recursive procedure to reverse a singly linked list of n elements with constant
storage is the following:
()nΘ
LIST-REVERSE (L)
p ← head[L]
if
pNIL=
return error “empty list”
q ← next[p]
r ← NIL
while
qNIL≠
do next[p] ← r
r ← p
p ← q
q ← next[p]
next[p] ← r
head[L] ← p
Solution to Exercise 10.3-4
Suppose we have a multi-array representation of length n such that the nodes of the doubly linked
list with m nodes are compacted in the first m index entries. We organize the remaining n-m
entries into a stack representing the free space. The top of the stack is top[free], which is initially
equal to 1 before the creation of the doubly linked list. Allocating a node is straightforward.
However when a node is to be returned to free space, we need to recompact the linked list so that
the nodes reside in the first indexed entries, and all the pointers are adjusted appropriately. We do
this by moving the node that appears right before the top of the stack free into the location freed
and adjust the pointers to that node.
Allocate-Object( )
If top[free]>n then error “out of space”
else {x=top[free];
top[free]=top[free]+1;
return x;
}
pf3
pf4
pf5

Partial preview of the text

Download Solutions to Exercises 10.2-7, 10.3-4, 10.4-6, 11.2-1, 11.2-4, 11.1, and 11-2 - Prof. Jose and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

Solution to Exercise 10.2-

A -time non-recursive procedure to reverse a singly linked list of n elements with constant

storage is the following:

Θ ( ) n

LIST-REVERSE (L)

p ← head [ L ]

if p = NIL

return error “empty list”

q ← next [ p ]

r ← NIL

while q ≠ NIL

do next[p] ← r r ← p p ← q q ← next[p]

next[p] ← r

head[L] ← p

Solution to Exercise 10.3-

Suppose we have a multi-array representation of length n such that the nodes of the doubly linked list with m nodes are compacted in the first m index entries. We organize the remaining n-m entries into a stack representing the free space. The top of the stack is top[free] , which is initially equal to 1 before the creation of the doubly linked list. Allocating a node is straightforward. However when a node is to be returned to free space, we need to recompact the linked list so that the nodes reside in the first indexed entries, and all the pointers are adjusted appropriately. We do this by moving the node that appears right before the top of the stack free into the location freed and adjust the pointers to that node.

Allocate-Object( )

If top[free]>n then error “out of space” else { x=top[free] ; top[free]=top[free]+1 ; return x ; }

Free-Object(x)

y=top[free]-1 ; if ( y=x ) then { top[free]=y ; return } else {if prev[y] is not NIL then next[prv[y]]=x ; else head[L]=x ; if next[y] is not NIL then prev[next[y]]=x ; next[x]=next[y]; prev[x]=prev[y]; key[x]=key[y]; top[free]=y; }

Solution to Exercise 10.4-

If only two pointers and a Boolean value can be used in each node, we will use one pointer as before to point to the leftmost child left[x] , and the other pointer either to point to a sibling unless it is the rightmost child in which case it points to the parent. To distinguish between these two cases, we use the Boolean value (say 0 when the pointer is used for a sibling and 1 when it points to a parent). It is clear that for any node we can explore all its children in time linear in the number of children and we can reach the parent by following the sibling pointer until we reach a node with the Boolean value set to 1 in which case we can reach the parent immediately.

Solution to Exercise 11.2-

For each pair of keys k , l , where k ≠ l , define the indicator random variable

X kl = I { h ( k ) = h ( l )}. Since we assume simple uniform hashing, Pr { X kl = 1 } = Pr { h ( k ) = h ( l )} =

1 / m , and so E[ X kl ] = 1 / m.

Now define the random variable Y to be the total number of collisions, so that.

The expected number of collisions is

Y = (^) ∑ k ≠ lX kl

[ ]

[ ] ( exp )

kl k l

kl k l

E Y E X

E X linearity of ectation

n

m

n n

m

n n

m

≠

≠

∑

∑

a. Since we assume uniform hashing, we can use the same observation as is used in Corollary 11.7: that inserting a key entails an unsuccessful search followed by placing the key into the first empty slot found. As in the proof of Theorem 11.6, if we let X be the random variable denoting

the number of probes in an unsuccessful search, then Pr { X ≥ i } ≤ α i −^1. Since n ≤ m /2, we have

α ≤ 1 /2. Letting i = k + 1, we have Pr { X > k } = Pr { X ≥ k + 1 } ≤ (1/ 2)( k^ +1) 1^ −^ = 2 − k.

b. Substituting k = 2 lg n into the statement of part (a) yields that the probability that the i th

insertion requires more than k = 2 lg n probes is at most 2 −2lg^ n =(2lg n^ )−^2 = n −^2 =1/ n^2.

c. Let the event A be X > 2 lg n , and for i = 1 , 2 , … , n , let the event A i be X i > 2 lg n. In part (b),

we showed that Pr { A i } ≤ for i = 1 , 2 , … , n. From how we defined these events, A = A 1 ∪ A 2

∪ · · · ∪ A n. Using Boole’s inequality, (C.18), we have

1/ n^2

1 2

2

Pr {A} Pr {A } + Pr {A }+ +Pr {A }

n

n

1/n

L n

k

k

d. We use the definition of expectation and break the sum into two parts:

{ }

{ } { }

{ } { }

{ } { }

1 2lg

1 2lg 1 2lg

1 2lg 1 2lg

1 2lg 1

[ ] Pr

Pr Pr

2 lg Pr Pr

2 lg Pr Pr

n

k n (^) n

k k n n (^) n

k k n n (^) n

k k n

E X k X k

k X k k X k

n X k n X

n X k n X

= ⎡⎢ ⎤⎥

= = (^) ⎡⎢ ⎤⎥+ ⎡⎢ ⎤⎥

= = (^) ⎡⎢ ⎤⎥+ ⎡⎢ ⎤⎥

= = (^) ⎡⎢ ⎤⎥+

∑

∑ ∑

∑ ∑

∑ ∑

Since X takes on exactly one value, we have that

{ } { } and

2lg

1

Pr Pr 2 lg 1

n

k

X k X n

⎡⎢ ⎤⎥

=

∑ =^ =^ ≤^ ⎡⎢^ ⎤⎥ ≤ {^ }^ {^ } 2lg 1

Pr Pr 2 lg 1/

n

k n

X k X n

= ⎡⎢ ⎤⎥+

∑ =^ ≤^ >^ ≤ n^ ,^ by

part (c). Therefore,

[ ] 2 lg 1 (1/ )

2 lg 1

(lg )

E X n n n

n

O n

Solution to Problem 11-

a. A particular key is hashed to a particular slot with probability 1 / n. Suppose we select a specific

set of k keys. The probability that these k keys are inserted into the slot in question and that all other keys are inserted elsewhere is

k n

n n

−

k

Since there are ways to choose our k keys, we get

n

k

k n k k

n

Q

n n k

−

⎛ ⎞ ⎛ ⎞ ⎛^ ⎞

b. For i = 1 , 2 , … , n , let X i be a random variable denoting the number of keys that hash to slot i ,

and let A i be the event that X i = k , i.e., that exactly k keys hash to slot i. From part (a), we have Pr

{ A } = Qk. Then,

Pr

Pr max

k

i n i

P M k

X k

≤ ≤

= Pr {there exists i such that X i = k and that X i ≤ k for i = 1 , 2 , … , n }

≤ Pr {there exists i such that X i = k } = Pr { A 1 ∪ A 2 ∪ · · · ∪ An }

≤ Pr { A 1 } + Pr { A 2 }+· · ·+Pr { A n } (by inequality (C.18)) = nQ k.

c. We start by showing two facts. First, 1 − 1 / n < 1, which implies (1 − 1/ n ) n^ − k < 1. Second, n! /( n

− k )! = n ·( n − 1 )·( n − 2 ) · · ·( n − k + 1 ). Using these facts, along with the simplification

of equation (3.17), we have

< n^ k

k! >( k / e ) k

k n k k

n k k

k

k k k

n

Q

n n k n k

n

n

n k n k

n n k n

k

e

k k e

k

−

−

= ⎛^ ⎞ ⎛^ − ⎞