Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Algorithms and Programs CS 541: Solving Dynamic Trees and Binary Search Tree Problems, Exams of Data Structures and Algorithms

Solutions to problem sets and exams for the algorithms and programs (cs 541) course at the university level. The problems involve implementing dynamic trees, understanding the admissible path algorithm, and modifying self-adjusting binary search trees. Students can use this document as a reference for studying and preparing exams, quizzes, or assignments related to these topics.

Typology: Exams

2012/2013

Uploaded on 03/23/2013

sardai
sardai 🇮🇳

4

(10)

117 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
- 1 -
1. (10 points) The figure below shows an implementation of the dynamic trees data structure
with several “virtual trees.” Show the corresponding set of “actual trees” (showing the
vertex costs).
Assuming these trees are used to implement the admissible path algorithm, what is the
residual capacity of the edge from j to d?
2
If the next step in the admissible path algorithm adds an edge from a to j and if this edge
has a residual capacity of 5, what is the residual capacity of the next augmenting path?
1
CS 541 – Algorithms and Programs
Problem Set 2 Solutions
J
onathan Turne
r
Exam 2 - 10/28/03
CS 541 – Algorithms and Programs
Exam 3 Solutions
J
onathan Turne
r
12
/
4
/
03
aj
c
f
e
s
h
g
t
d
b0,2
0,2
0,3
0,98
0,1
0,1
0,2
0,5
0,99 0,3
0,100
aj
c
f
e
s
h
g
t
d
b0,2
0,2
0,3
0,98
0,1
0,1
0,2
0,5
0,99 0,3
0,100
a
e
b
4
f
t
s
h
g
j
d
100
3
2
100
100
1
52
23
c
a
e
b
4
f
t
s
h
g
j
d
100
3
2
100
100
1
52
23
c
pf3
pf4
pf5

Partial preview of the text

Download Algorithms and Programs CS 541: Solving Dynamic Trees and Binary Search Tree Problems and more Exams Data Structures and Algorithms in PDF only on Docsity!

  1. (10 points) The figure below shows an implementation of the dynamic trees data structure

with several “virtual trees.” Show the corresponding set of “actual trees” (showing the vertex costs).

Assuming these trees are used to implement the admissible path algorithm, what is the residual capacity of the edge from j to d?

If the next step in the admissible path algorithm adds an edge from a to j and if this edge has a residual capacity of 5, what is the residual capacity of the next augmenting path?

CS 541 – Algorithms and Programs

Problem Set 2 Solutions

Jonathan Turner Exam 2 - 10/28/

CS 541 – Algorithms and Programs

Exam 3 Solutions

Jonathan Turner 12/4/

a j

c

f

e

s

h

t g

b 0,2 d

a j

c

f

e

s

h

t g

b 0,2 d

a

e

b

f

t

s

h

g

j

d

c

a

e

b

f

t

s

h

g

j

d

c

  1. (10 points) The figure below shows an intermediate state in the execution of the highest

label preflow-push algorithm. Show the state of the algorithm after each of the next two steps in which a vertex becomes balanced. You need not re-draw the entire residual graph, but do show all edges, vertices and associated values that are modified by each step.

s

d f

b e

t

5

(^3 )

2

3

6

1

1 2

c

2

3

2

8,

1,

9,

2,

(^70)

2,

5

3

6

1

residual capacity

distance label, excess

s

d f

b e

t

5

(^3 )

2

3

6

1

1 2

c

2

3

2

8,

1,

9,

2,

(^70)

2,

5

3

6

1

residual capacity

distance label, excess

s

d f

b e

t

5

(^3 )

2

3

6

3

(^1 )

c

2

3

2

8,

1, 1

9,

3 , 0

(^70)

2, 2

3

3

6

1

s

d f

b e

t

5

(^3 )

2

3

6

3

(^1 )

c

2

3

2

8,

1, 3

9,

3,

(^70)

2, 0

3

3

6

1

s

d f

b e

t

5

(^3 )

2

3

6

3

(^1 )

c

2

3

2

8,

1, 1

9,

3 , 0

(^70)

2, 2

3

3

6

1

s

d f

b e

t

5

(^3 )

2

3

6

3

(^1 )

c

2

3

2

8,

1, 3

9,

3,

(^70)

2, 0

3

3

6

1

  1. (20 points) Consider a binary search tree in which each vertex has an associated key and a

cost. The vertices are ordered by the keys in the usual way (so the keys of the vertices in the left subtree of a given vertex x are strictly less than the key of x , and so forth). The costs are represented using the differential representation we used for representing path sets. Complete the recursive function search, shown below, so that it returns a least-cost vertex from among those vertices with keys smaller than a given bound. The structure of the tree nodes is shown below also. class twoWayTrees { int n; // trees defined on items {1,...,n} struct node { int k, Dc, Dm; // key and differential cost fields int lc, rc; // indices of left and right children } *vec;

... } #define left(x) (vec[x].lc) // you may assume similar declarations // for right, key, Dcost, Dmin int search(int t, int bound) { // Return the index of a least cost node from among those nodes // in the subtree with root t that have keys less than bound. // If there is more than one such node, pick the one with // the smallest key. Return Null, if there is no least cost node // with key less than bound. if (t == Null) return Null; else if (left(t) != Null && Dmin(left(t)) == 0) return search(left(t),bound); else if (key(t) < bound && Dcost(t) == 0) return t; else if (right(t) != Null && Dmin(right(t)) == 0) return search(right(t),bound); else return Null; }

  1. (15 points) How many augmenting paths does the min-cost, augmenting path algorithm

find in the network shown below (the pairs on each edge are the capacity and the cost). Explain.

The algorithm finds 54 distinct augmenting paths in this graph. Initially, it finds 9 paths of the form s,a 1 ,x (^) i,y (^) j,d 1 ,t with capacity 1 and cost 0. At this point the edge from s to a 1 is saturated, as is the edge from d 1 to t. Next, it finds 9 paths of the form s,b 1 ,y (^) i,x (^) j,c 1 ,t with cost 2.Then, it finds 9 paths of the form s,2 2 ,a 1 ,x (^) i,y (^) j,d 1 ,d 2 ,t with cost 4. And so forth. It continues in this fashion, always just adding one unit of flow for each augmenting path, since all paths pass through the central bipartite subgraph.

Explain how this example can be generalized to show that the min-cost, augmenting path algorithm can take time Ω( n^5 ).

To generalize the example, make the a, b, c, and d chains all k vertices long, with edge costs of 0,2,4,... from s to each of the a vertices and from each of the d vertices to t. Make the edge costs from s to each of the b vertices and from each of the c vertices to t, equal to 1,3,5,... Make the capacities of all the edges incident to s and t k 2. Expand the central bipartite subgraph so that it has 2k vertices and edges from the x vertices to the y vertices with capacity 1 and cost 0. Assign infinite capacity and 0 cost to all other edges. In this graph, the min-cost augmenting path algorithm with find 2k 3 augmenting paths. Since it uses Dijkstra’s algorithm to find each augmenting path and since the graph has more

than k 2 edges, it will take Ω (k 2 ) time to find each augmenting path. This gives a total of Ω (k 5 ) time to

find the minimum-cost maximum flow, which is Ω (n^5 ), since n=6k+2.

a 3 a 2 a 1

b 3 b 2 b 1

x 1 x 2 x 3

y 1 y 2 y 3

d 1 d 2 d 3

s t

∞,0 c 1 c 2 c 3

∞,

∞,

∞,

∞,

∞,

∞,

∞,

1,

∞,

∞,

9, 9,

9,4 9,

9,

9,

9,2 9,

9,1 9,

9,

9,

a 3 a 2 a 1

b 3 b 2 b 1

x 1 x 2 x 3

y 1 y 2 y 3

d 1 d 2 d 3

s t

∞,0 c 1 c 2 c 3

∞,

∞,

∞,

∞,

∞,

∞,

∞,

1,

∞,

∞,

9, 9,

9,4 9,

9,

9,

9,2 9,

9,1 9,

9,

9,