Dynamic Trees - Advanced Data Structures and Algorithms - Solved Exam, Exams of Data Structures and Algorithms

Main points of this past exam are: Advanced Data Structures, Dynamic Trees Data Structure, Virtual Tree, Tarjan Calls, Individual Weight, Dynamic Trees, Analysis, Total Weight Function, Correctness, Balanced Binary

Typology: Exams

2012/2013

Uploaded on 03/23/2013

sardai
sardai 🇮🇳

4

(10)

108 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
- 1 -
1. (10 points) The figure below represents one of the trees in a dynamic trees data structure.
Show the underlying representation of this tree (what Tarjan calls the virtual tree). Be sure
to show the cost of each vertex using the differential form. Arrange each of the binary search
trees so that the label of the node at the root of the BST comes first alphabetically, among all
nodes in the BST.
In the analysis of dynamic trees, we defined an individual weight function on the vertices and
a total weight function. What is the individual weight of c? What is the total weight of e?
iw(c)=6, tw(e)=4
CS 541 – Algorithms and Programs
Problem Set 2 Solutions
J
onathan Turne
r
Exam 2
- 10/28/03
CS 542 – Advanced Data Structures and Algorithms
Exam 3 Solutions
J
onathan Turne
r
12/6/05
3
4
3
2
2
5
1
32
6
3
c
e
b
h
j
a
i
g
k
f
d
3
4
3
2
2
5
1
32
6
3
c
e
b
h
j
a
i
g
k
f
d
0,3
0,2
0,3 g
k
d
f
i
e
j
hb
a
c
0,5
2,1
0,2
0,0
0,0
3,0
1,2
0,0
cost, min
0,3
0,2
0,3 g
k
d
f
i
e
j
hb
a
c
0,5
2,1
0,2
0,0
0,0
3,0
1,2
0,0
cost, min
pf3
pf4
pf5

Partial preview of the text

Download Dynamic Trees - Advanced Data Structures and Algorithms - Solved Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

  1. (10 points) The figure below represents one of the trees in a dynamic trees data structure. Show the underlying representation of this tree (what Tarjan calls the virtual tree). Be sure to show the cost of each vertex using the differential form. Arrange each of the binary search trees so that the label of the node at the root of the BST comes first alphabetically, among all nodes in the BST.

In the analysis of dynamic trees, we defined an individual weight function on the vertices and a total weight function. What is the individual weight of c? What is the total weight of e?

iw(c)=6, tw(e)=

CS 541 – Algorithms and Programs

Problem Set 2 Solutions

Jonathan Turner Exam 2 - 10/28/

CS 542 – Advanced Data Structures and Algorithms

Exam 3 Solutions

Jonathan Turner 12/6/

c

e

b

h

j

a

i

g

k

f

3 d

c

e

b

h

j

a

i

g

k

f

d 0,

0,3 g

k

d

i f

e

j

h (^) b

a

c

∆cost, ∆min

0,3 g

k

d

i f

e

j

h (^) b

a

c

∆cost, ∆min

  1. (10 points) The correctness of any data structure operation depends on its maintaining certain essential properties of the data structure. The data portion of the class declaration for the C++ implementation of the balanced binary search tree data structure is shown below. What properties of the data must be maintained by programs that operate on it? List as many properties as you can. class bbsts { int n; // trees defined on nodes {1,...,n} struct node { // tree node structure int lchild, rchild, parent; int rank; keytyp keyfield; } *vec; ... }; n is non-negative and the number of nodes in the vec array is at least n+1.

For 1un,if vec[u].lchildNull then 1vec[u].lchildn. For 1un,if vec[u].rchildNull then 1vec[u].rchildn. For 1un,if vec[u].parentNull then 1vec[u].parentn. For 1un,vec[u].rank >.

For 1un, if vec[u].lchild=vNull then vec[v].parent=u. For 1un, vec[u].rchild=vNull implies that vec[v].parent=u.

The pointers must define a collection of trees. Specifically, this means that for 1un,vec[u].lchildvec[u].rchild and the parent pointers do not define any cycles.

For 1un, if vec[u].lchild=vNull then vec[v].keyfield<vec[u].keyfield. For 1un, if vec[u].rchild=vNull then vec[v].keyfield>vec[u].keyfield.

For 1un, if vec[u].parent=vNull then vec[u].rankvec[v].rankvec[u].rank +1_. If vec[v].parent=w_ ≠ Null then vec[u].rank<vec[w].rank. For 1un, if vec[u].lchild=Null or vec[u].rchild=Null, then vec[u].rank=1.

How does your answer change if t is an ancestor of s , initially? Explain.

In this case, the depth of t can increase by 3 during the splay step that makes t a descendant of s, but then it will decrease. Consequently,

depth t′ ( ) ≤ depth t( ) + 3 − 2 ⎢⎣ depth t( ) / 3⎥⎦

Do you think that it would be worthwhile modifying the implementation of self-adjusting binary search trees to allow the use of these larger splay steps? Why or why not?

It might be, but one would need to try it out and compare the two in order to be sure. The larger splay steps do seem to do a better job of balancing the tree, since they move vertices that are far from the root, closer to the root, than do the “standard” splay steps. On the other hand, there is some extra overhead associated with determining which of the different splay steps is applicable in a given situation. This could completely offset the speedup obtained by improving the balance.

  1. (15 points) The differential cost representation used by the path sets data structure requires that ∆ min and ∆ cost be updated during a rotation operation. Show that the two equations below are correct.

∆cost'(z) = ∆cost(z) + ∆min(z)

∆cost'(z) = cost'(z) − mincost'(z) = cost(z) − mincost(x) = cost(z) − (mincost(z) − ∆min(z)) = ∆cost(z) + ∆min(z)

∆min'(c) = ∆min(c) + ∆min(z)

∆min'(c) = mincost'(c) − mincost'(z) = mincost(c) − mincost(x) = (mincost(c) − mincost(z)) + (mincost(z) − mincost(x)) = ∆min(c) + ∆min(z)

∆cost, ∆min

z

x

b c

a

x

z

a b

c

∆cost′, ∆min′

∆cost'(z) = ∆cost(z) + ∆min(z)

∆cost'(z) = cost'(z) − mincost'(z) = cost(z) − mincost(x) = cost(z) − (mincost(z) − ∆min(z)) = ∆cost(z) + ∆min(z)

∆min'(c) = ∆min(c) + ∆min(z)

∆min'(c) = mincost'(c) − mincost'(z) = mincost(c) − mincost(x) = (mincost(c) − mincost(z)) + (mincost(z) − mincost(x)) = ∆min(c) + ∆min(z)

∆cost, ∆min

z

x

b c

a

x

z

a b

c

∆cost, ∆min^ ∆cost′,^ ∆min′

z

x

b c

a

x

z

a b

c

∆cost, ∆min^ ∆cost′,^ ∆min′

z

x

b c

a

∆cost, ∆min

z

x

b c

a

z

x

b c

aa

x

z

a b

c

∆cost′, ∆min′

x

z

a b

c

x 1,

z

a b

c

∆cost′, ∆min′

  1. (10 points) The figure below shows an intermediate state in the execution of the minimum cost augmenting path algorithm for the min-cost max-flow algorithm, using vertex labels to represent transformed costs.

Draw the residual graph corresponding to this flow, and for each vertex, determine its distance from s using the transformed costs. Write the distances by the vertices and mark the edges that are in the shortest path tree from s. Identify a minimum cost augmenting path in the graph. What is its cost, using the transformed costs?

The path s, c, b, e, t is a minimum cost augmenting path with cost 1.

What is the new value of the vertex label for e when this step is finished? For t?

The new distance label for e is 2. The new distance label for t is 1.

s

b

a

8,2,

3,3,

4,3,

6,−2,

8,6,

3,2,0 5,1,

5,1,0 3,1,

e

d

t

5,−1,

6,3,

7,5,

capacity, cost, flow

− (^1) label

0 − 2

2

1

0

− 1

s c

b

a

8,2,

3,3,

4,3,

6,−2,

8,6,

3,2,0 5,1,

5,1,0 3,1,

e

d

t

5,−1,

6,3,

7,5,

capacity, cost, flow

− (^1) label

0 − 2

2

1

0

− 1

c

s

b

a

8,

3,− 3

4,

3,− 2

8,

3,2 5,

5,1 3,

e

d

t

2,− 1

6,

7,

0

1

0

0

1

0

0

c 3,

3,

s

b

a

8,

3,− 3

4,

3,− 2

8,

3,2 5,

5,1 3,

e

d

t

2,− 1

6,

7,

0

1

0

0

1

0

0

c 3,

3,