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

graph algorithm - Advanced Data Structures and Algorithms - Solved Exam, Exams of Data Structures and Algorithms

Main points of this past exam are: Algorithms, Programs, Implementing, Graph Algorithm, Deletemin Operations, Algorithm, Primary Data Structures, Deletemin Operations, Lowest Asymptoti, Implemented

Typology: Exams

2012/2013

Uploaded on 03/23/2013

sardai
sardai 🇮🇳

4

(10)

117 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
- 1 -
1. (15 points) Suppose you’re implementing a graph algorithm that uses a heap as one of its
primary data structures. The algorithm does at least n insert and n deletemin operations and
at most m2/3 insert and m2/3 deletemin operations, when m2/3>n. It also does at most m
changekey operations, all of which reduce the key value. If you implemented this algorithm
using d-heaps, what value of d would you use to get the lowest asymptotic running time.
(Hint: break this into two cases; one for smaller values of m, one for larger values.)
For m<n3/2, we have m2/3<n, so the best choice is the same as for Prim’s algorithm. So I would use
d=2+m/n in this case.
For m>n3/2, the running time is O((m+m2/3)logdn + m2/3d logdn). The asymptotic running time is
dominated by the larger of the two terms, so we can minimize it by choosing a value of d that makes
the two terms roughly equal. Choosing d=m1/3 will work.
What is the resulting asymptotic running time?
O(m log2+m/nn) for m<n3/2, O(m) for m
n3/2.
What is the resulting asymptotic running time if you used a Fibonacci heap, instead of a d-
heap?
O(m + n log n) for m<n3/2, O(m+m2/3 log n)=O(m) for m
n3/2.
Which type of heap would you choose for this application? Why?
Fibonacci heaps have the best overall asymptotic complexity, but they only have an advantage for
m<n log n. On the other hand, they use considerably more space than d-heaps (5 values per stored
item vs. 3) and are more complex to implement. So, I would most likely use a d-heap, unless I knew
that the input graphs were going to be very sparse and that the running time of the heap operations
dominated the overall running time.
CS 541 – Algorithms and Programs
Problem Set 2 Solutions
J
onathan Turne
r
Exam 2
- 10/28/03
CSE 542 – Algorithms and Programs
Final Exam Solutions
J
onathan Turne
r
12/22/05
pf3
pf4
pf5
pf8

Partial preview of the text

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

  1. (15 points) Suppose you’re implementing a graph algorithm that uses a heap as one of its primary data structures. The algorithm does at least n insert and n deletemin operations and at most m2/3^ insert and m2/3^ deletemin operations, when m2/3^ >n. It also does at most m changekey operations, all of which reduce the key value. If you implemented this algorithm using d-heaps, what value of d would you use to get the lowest asymptotic running time. (Hint: break this into two cases; one for smaller values of m, one for larger values.)

For m<n 3/2^ , we have m2/3^ <n, so the best choice is the same as for Prim’s algorithm. So I would use d=2+m/n in this case.

For m>n 3/2^ , the running time is O((m+m2/3^ )log (^) d n + m2/3^ d log (^) d n). The asymptotic running time is dominated by the larger of the two terms, so we can minimize it by choosing a value of d that makes the two terms roughly equal. Choosing d=m 1 /^3 will work.

What is the resulting asymptotic running time?

O(m log 2+m/nn) for m<n 3/2^ , O(m) for m ≥n 3/^.

What is the resulting asymptotic running time if you used a Fibonacci heap, instead of a d- heap?

O(m + n log n) for m<n 3/2^ , O(m+m^2 /^3 log n)=O(m) for m ≥n 3/^.

Which type of heap would you choose for this application? Why?

Fibonacci heaps have the best overall asymptotic complexity, but they only have an advantage for m<n log n. On the other hand, they use considerably more space than d-heaps (5 values per stored item vs. 3) and are more complex to implement. So, I would most likely use a d-heap, unless I knew that the input graphs were going to be very sparse and that the running time of the heap operations dominated the overall running time.

CS 541 – Algorithms and Programs

Problem Set 2 Solutions

Jonathan Turner Exam 2 - 10/28/

CSE 542 – Algorithms and Programs

Final Exam Solutions

Jonathan Turner 12/22/

  1. (15 points) In the Fibonacci heaps data structure, a cut between a vertex u and its parent v causes a cascading cut at v if v has already lost a child since it last became a child of some other vertex. Suppose we change this, so that a cascading cut is performed at v only if v has already lost two children. How does this change alter the lemma shown below (this lemma is from the analysis of the running time of Fibonacci heaps)? Explain your answer.

Lemma. Let x be any node in an F-heap. Let y 1 ,... ,y (^) r be the children of x, in order of time in which they were linked to x (earliest to latest). Then, rank(y (^) i) ≥ i−2 for all i.

The inequality in the lemma becomes rank(y (^) i) ≥ i− 3. Since y (^) i had the same rank as x when it became a

child of x and x must have had at least i −1 children at that time, yi must have had rank of at least i − 1

when it became a child of x. Since it still is a child of x, it can have lost at most two children since that

time, so its rank must be at least i −3.

Let S (^) k be the smallest possible number of descendants that a node of rank k has, in our modified version of Fibonacci heaps. Give a recursive lower bound on S (^) k. That is, give an inequality of the form S (^) k ≥ f(S 0 ,S 1 ,... , S (^) k− 1 ) where f is some function of the S (^) i’s for i<k.

Clearly S 0 =1, S 1 =2 and S 2 =3. For k>2, we can use the modified lemma to conclude that S (^) k ≥ 3 + S 0 + S 1 +" + Sk − 3. Note that the difference between the bounds for S (^) k and for Sk − 1 is Sk − 3.

Use this to give a lower bound on the smallest number of descendants that a node with rank 7 can have.

From the above, we have S 3 ≥3+S 0 = 4, S 4 ≥ 4+S 1 =6, S 5 ≥6+S 2 = 9, S 6 ≥9+S 3 ≥13, S 7 ≥13+S 4 ≥19.

  1. (10 points). The figure below shows an intermediate state in the execution of Dijkstra’s algorithm. The bold edges in the graph are the edges defined by the parent pointers, and the numbers next to the vertices are the current distance values. Fill in the blanks (as appropriate) in the arrays that implement the d-heap (assume d=2).

Show how the heap content changes after the next iteration.

a

c

b

1

3

2

7

7

e h

4

2

0

d

g

f

8

j

i

k

2

4

3

8

(^7 )

3 1

2

2 3

5

4

9

1

3

7

a

c

b

1

3

2

7

7

e h

4

2

0

d

g

f

8

j

i

k

2

4

3

8

(^7 )

3 1

2

2 3

5

4

9

1

3

7

h d

b

c

h

key

a

b

c

d e f g

h i j k l

h^ −^1 2 3 1 4

h d

b

c

h

key

a

b

c

d e f g

h i j k l

h^ −^1 2 3 1 4

h b

f

c

h

key

a

b

c d e

f g

h i j k l

h^ −^1 1 3 2 4

h b

f

c

h

key

a

b

c d e

f g

h i j k l

h^ −^1 1 3 2 4

  1. (15 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 the node with the smallest key from among those vertices with costs less than or equal to 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 costBound, int dmSum) { // Return the index of the leftmost node in the subtree with // root t that has cost <= costBound. The variable dmSum is // the sum of the Dmin values for the proper ancestors of t. // Return Null, if there is no node with cost less than costbound.

int mincost;

if (t == Null) return Null; mincost = dmSum + Dmin(t); if (mincost > costBound) return Null;

int y = search(left(t), costBound, mincost); if (y != Null) return y; if (mincost + Dcost(t) <= costBound) return t; return search(right(t),costBound,mincost); }

  1. (10 points) Given a bipartite, graph with edge weights.

Draw a picture of the min-cost flow graph that can be used to find a maximum matching in this graph.

Identify a minimum cost augmenting path in the flow graph. What is its cost? Draw the residual graph that results from saturating this path.

The path s-c-x-t is a minimum cost augmenting path with cost -8.

Identify a minimum cost augmenting path in the residual graph. What is its cost?

The path s-b-x-c-z-t is a minimum cost augmenting path, with cost -5.

What is the matching corresponding to the flow that exists after flow is added to this augmenting path?

{b,x}, {c,z}

a

c

b

x

z

y

a

c

b

x

z

y

a

c

b

− 1 − 3

− 2 − 7

− 6 − 3 − 4

s t

0

0

0

0

0

0

x

z

y

− 8

edge capacities=

a

c

b

− 1 − 3

− 2 − 7

− 6 − 3 − 4

s t

0

0

0

0

0

0

x

z

y

− 8

edge capacities=

a

c

b

s t

x

z

y

a

c

b

s t

x

z

y

  1. (10 points) The figure below show a possible intermediate stage in the execution of the Edmonds-Karp algorithm for finding a maximum size matching. The states of the vertices are indicated by the plus and minus signs (for even and odd), the arrows represent the parent pointers and the edges adjacent to certain vertices correspond to their bridge values. The partition data structure is shown at right and the queue of edges to be processed is at the top. In the diagram at left, draw a closed curve around the sets of vertices that have been condensed into a single vertex in the current shrunken graph. If any of these sets contain subsets, corresponding to smaller blossoms, circle the vertices in those smaller blossoms, as well.

What happens when the next edge in the queue is processed by the algorithm? Explain.

Nothing. The edge ae joins two vertices that are contained in the same vertex in the current shrunken graph, so the algorithm just discards this edge.

When the next edge, [i,h] is processed, an augmenting path is found. What is that augmenting path?

i,h,q,p,j,b,g,a,d,e,c,r

b

h

k

d e

a j

c

i

p

r

ga

ag

qp

q

pq

l g

n m s

ln

k l r c

n

b (^) d b g

a

q e p h

j

i

s (^) m

queue : ae, ih, la, kd, ln

+ b

h

k

d e

a j

c

i

p

r

ga

ag

qp

q

pq

l g

n m s

ln

k l r c

n

b (^) d b g

a

q e p h

j

i

s (^) m

queue : ae, ih, la, kd, ln

+ b

h

k

d e

a j

c

i

p

r

ga

ag

qp

q

pq

l g

n m s

ln

k l r c

n

b (^) d b g

a

q e p h

j

b (^) d b g

a

q e p h

j

i

s (^) m

queue : ae, ih, la, kd, ln