Docsity

Dijkstra Algorithm - Advanced Data Structures and Algorithms - Solved Exam, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
- 1 -
1. (15 points) Show that the greedy method for the minimum spanning tree maintains the
color invariant during an application of the blue rule.
Color invariant. There exists a minimum spanning tree T that includes all the blue edges and
none of the red ones.
Let {u,v} be the edge selected by an application of the blue rule and let T be the minimum spanning
tree that satisfies the invariant before the application of the blue rule. If {u,v} is in T, then clearly T
still satisfies the invariant after the application of the blue rule. Assume then that {u,v} is not in T.
Let (X,Xโ€™) be the cut used by the blue rule to select edge {u,v}. Since T is a spanning tree, the path
from u to v in T must include at least one edge {x,y} that crosses the cut.
Since none of the edges that cross the cut before the application of the blue rule is blue, {x,y} must be
uncolored (it canโ€™t be red, since T includes no red edges}.
Since {u,v} was selected to have minimum cost among all uncolored edges crossing the cut, the cost
of {u,v} can be no larger than the cost of {x,y}.
If we remove {x,y} from T, we split T into two parts, one containing u and one containing v. If we
then add the edge {u,v} we get a new spanning tree that costs no more than T and which satisfies the
color invariant.
CS 541 โ€“ Algorithms and Programs
Problem Set 2 Solutions
J
onathan Turne
r
Exam 2 - 10/28/03
CS 541 โ€“ Algorithms and Programs
Final Exam Solutions
J
onathan Turne
r
12
/
17
/
03
pf3
pf4
pf5
pf8

Partial preview of the text

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

1. (15 points) Show that the greedy method for the minimum spanning tree maintains the

color invariant during an application of the blue rule.

Color invariant. There exists a minimum spanning tree T that includes all the blue edges and

none of the red ones.

Let {u,v} be the edge selected by an application of the blue rule and let T be the minimum spanning

tree that satisfies the invariant before the application of the blue rule. If {u,v} is in T, then clearly T

still satisfies the invariant after the application of the blue rule. Assume then that {u,v} is not in T.

Let (X,Xโ€™) be the cut used by the blue rule to select edge {u,v}. Since T is a spanning tree, the path

from u to v in T must include at least one edge {x,y} that crosses the cut.

Since none of the edges that cross the cut before the application of the blue rule is blue, {x,y} must be

uncolored (it canโ€™t be red, since T includes no red edges}.

Since {u,v} was selected to have minimum cost among all uncolored edges crossing the cut, the cost

of {u,v} can be no larger than the cost of {x,y}.

If we remove {x,y} from T, we split T into two parts, one containing u and one containing v. If we

then add the edge {u,v} we get a new spanning tree that costs no more than T and which satisfies the

color invariant.

CS 541 โ€“ Algorithms and Programs

Problem Set 2 Solutions

Jonathan Turner Exam 2 - 10/28/

CS 541 โ€“ Algorithms and Programs

Final Exam Solutions

Jonathan Turner 12/17/

2. (15 points) The chart below shows the running times for the deletemin operation and the

changekey operation in Dijkstraโ€™s algorithm using d -heaps with varying values of d. The

running times are shown for a random graph with 5,000 vertices and about 100,000 edges.

Which curve is for deletemin and which is for changekey? Explain why.

The top curve must be the one for deletemin, since the running time of deletemin is O(d logd n) and

this increases as the heap parameter increases, while the running time per call of changekey is

O(log d n) since the key values are always reduced, which drops as the heap parameter increases.

If the number of calls to changekey is 10% of the worst-case number and the number of calls

to deletemin equals the worst-case number when d =2, what is the ratio between the average

running time of one call to deletemin to the average running time of one call to changekey.

In the worst-case we can have 100,000 calls to changekey and 5,000 calls to deletemin. In this case,

there are only 10,000 calls to changekey and 5,000 calls to deletemin. If the running times were

equal, we would expect the value on the chart for changekey to be twice as large as the value for

deletemin. In fact, the value on the chart for changekey is slightly more than one fifth as large as the

value for deletemin. So it appears that each call to deletemin takes nearly ten times as long as each

call to changekey when d=2.

0

2

4

6

8

10

2 4 6 8 10 12 14 16 18 20 heap parameter ( d )

time (s)

4. (20 points) To check if a graph has a negative cycle, we can add an additional source vertex,

with a zero cost edge to each of the original vertices and then run the breadth-first scanning

algorithm. This takes O ( mn ) time. We can potentially speed up the algorithm for graphs

with small negative cycles (cycles with just a few edges) by checking to see if the parent

pointers form a cycle at the end of each pass. The subroutine shown below checks to see if

the parent pointers form a cycle. What is its running time? Why?

int cycle(int n, int p[]) { // Return 1 if the parent pointers, p, form a cycle. Else return 0. int u, v; int count; int *mark = new int[n+1]; // mark[u]=1 once we confirm that u // has a null ancestor, else mark[u]= for (u = 1; u <= n; u++) mark[u] = (p[u] == Null? 1 : 0); for (u = 1; u <= n; u++) { count = 0; for (v = u; mark[v] == 0; v = p[v]) if (count++ == n) return 1; for (v = u; mark[v] == 0; v = p[v]) mark[v] = 1; } return 0; }

The running time is O(n). Since mark[u] is initialized to 1 for all vertices with a null parent pointer,

the inner loops are guaranteed to terminate if u is not part of a cycle. Let k x be the number of

iterations of each of the inner loops when the value of the loop variable is x. If x is part of a cycle, then

the count variable ensures termination after at most n iterations of the first inner loop. If x is not

part of a cycle, then k x mark bits are set during the iteration of the outer loop when u=x. Once all n

mark bits are set no further inner loop iterations can be performed. Hence, the overall running time

is O(n).

Suppose we run this subroutine after each pass of the breadth-first scanning algorithm and

terminate early if a cycle is found. What is the running time of this modified algorithm if a

cycle is formed in the parent pointers after k passes? Why?

O(k(m+n)), since each pass takes O(m) time and each cycle check takes O(n) time.

Suppose we run the breadth-first scanning algorithm on the graph shown below (after

adding the extra source vertex and connecting edges), in order to detect negative cycles. In

the worst case, how many passes must be executed by the algorithm before the negative

cycle is reflected by the parent pointers? Why?

It will take 5 passes. In the worst-case, the queue will contain vertices a, b, c, d in that order at the

end of the first pass, all at distance zero with parent s. At the end of the second pass, a, b and c will

have distance โˆ’ 1 with p(a)=b, p(b)=c and p(c)=d. At the end of the third pass, a and b will have distance โˆ’ 2 with no change in the parent pointers. At the end of the fourth pass, a will have distance โˆ’ 3 with no change in the parent pointers. Finally, at the end of the fifth pass, b will have distance โˆ’ 3

and p(b)=a, forming a cycle in the parent pointers.

a b c d

โˆ’ 1

0

โˆ’ 1 โˆ’ 1 a b c d

โˆ’ 1

0

โˆ’ 1 โˆ’ 1

5. (15 points) In the two-phase algorithm for finding maximum flows in unit networks, we use

breadth-first search to find augmenting paths once d ( s ) exceeds ( n โˆ’2) 1/2^. We have shown

that this allows the algorithm to complete in O ( mn 1/2^ ) time. If we do not switch to breadth-

first search in the second phase, the algorithm may not complete in O ( mn 1/2^ ) time.

Now, consider a version of the admissible path algorithm in which exact distance labels are

recomputed after every n relabelings and the current augmenting path search is restarted at

the source vertex. This version of the algorithm can be faster than the original algorithm in

practice, since it can avoid large numbers of relabeling steps. Show that if this algorithm is

run on a unit network that d ( s ) exceeds ( n โˆ’2) 1/2^ after O ( mn 1/2^ ) time (you may use the fact

that the first phase of the two phase algorithm completes in O ( mn 1/2^ ) time).

Since the first phase of the two phase algorithm uses the original admissible path algorithm, it

suffices to show that the time required for recomputing the exact distance labels is O(mn 1/2^ ). Since

none of the distance labels exceeds (n โˆ’ 2) 1/2^ during the first phase, there are O(n 3/2^ ) relabelings

during the first phase and hence O(n 1/2^ ) recomputations of exact labels. Since each recomputation

takes O(m) time, the additional time spent on recomputations is O(mn 1/2^ ).

Show that the modified admissible path algorithm completes the computation of a

maximum flow in O ( mn 1/2^ ) time, after d ( s ) exceeds ( n โˆ’2) 1/2^ (you may use facts proved in the

original analysis of the two phase algorithm, without re-deriving them).

As was shown in the original analysis of the two phase algorithm, the difference between the current

flow and the maximum flow is O(n 1/2^ ) after d(s)>(n โˆ’ 2) 1/2^. This means that O(n 1/2^ ) new augmenting

paths remain to be found. After each recomputation of the vertex labels, the next augmenting path

must be found before the next individual vertex relabeling occurs. This means that there are O(n 1/2^ )

recomputations needed to complete the max flow computation, so the time spent on these

recomputations is O(mn 1/2^ ). This also implies that the time spent relabeling individual vertices is

O(n3/2^ ).

s

b

a

5,2,

e

d

t

capacity, cost, flow

c

s

b

a

e

d

t

residual capacity, cost

0,3 excess, label

c

s

b

a

5,2,

e

d

t

capacity, cost, flow

c

s

b

a

e

d

t

residual capacity, cost

0,3 excess, label

c

7. (15 points) The figure at right

shows an intermediate state in

the execution of the scaling

algorithm (with edge cost

transformation) for the min-cost,

max flow problem. In particular,

the figure shows the state of the

algorithm at the end of the first

scaling phase.

Show the state of the algorithm

just before the first augmenting

path search of the second scaling

phase (that is, after edges have

been added and flows adjusted

to eliminate violations of the

labeling condition).

s

b

a

5,

4,โˆ’ 2

5,-

5,

6 ,โˆ’ 1

e

d

t

4,โˆ’ 1

6,

5,โˆ’ 3

2 ,

-5,

โˆ’โˆ’โˆ’โˆ’ 3 ,

โˆ’โˆ’ โˆ’โˆ’ 2 ,

3 ,

3,

2,

c

3, โˆ’โˆ’โˆ’โˆ’ 1

2,

3,

2,

2,

s

b

a

5,2,

3,1, 3

5,2,

5,2,

2,0, 2

3,2,0^ 5,1,

6,1, 6 2,1,

e

d

t

4,1,

6,3,

c 7,3,

s

b

a

5,

4,โˆ’ 2

5,-

5,

6 ,โˆ’ 1

e

d

t

4,โˆ’ 1

6,

5,โˆ’ 3

2 ,

-5,

โˆ’โˆ’โˆ’โˆ’ 3 ,

โˆ’โˆ’ โˆ’โˆ’ 2 ,

3 ,

3,

2,

c

3, โˆ’โˆ’โˆ’โˆ’ 1

2,

3,

2,

2,

s

b

a

5,

4,โˆ’ 2

5,-

5,

6 ,โˆ’ 1

e

d

t

4,โˆ’ 1

6,

5,โˆ’ 3

2 ,

-5,

โˆ’โˆ’โˆ’โˆ’ 3 ,

โˆ’โˆ’ โˆ’โˆ’ 2 ,

3 ,

3,

2,

c

3, โˆ’โˆ’โˆ’โˆ’ 1

2,

3,

2,

2,

s

b

a

5,2,

3,1, 3

5,2,

5,2,

2,0, 2

3,2,0^ 5,1,

6,1, 6 2,1,

e

d

t

4,1,

6,3,

s c 7,3,

b

a

5,2,

3,1, 3

5,2,

5,2,

2,0, 2

3,2,0^ 5,1,

6,1, 6 2,1,

e

d

t

4,1,

6,3,

c 7,3,

8. (10 points) The figure below shows an intermediate stage in the execution of the Edmonds-

Karp algorithm for finding a maximum size matching. The matching edges are not shown

explicitly, but parent pointers are shown and the partition data structure is shown.

Draw circles around the vertex sets which form blossoms.

Mark the bridges of the blossoms with a B.

On the diagram, mark even vertices with a plus sign and odd vertices with a minus sign.

Indicate which edges are in the current matching, by marking them with an M.

k

a

l g

d

m

n i q 0

0 0 0 0

(^0 )

1

b c e h j^ p

r

0 0 0 0 0 0

1

b

l g h

k

d e

a

n (^) m q

j

c

i

p

r

k

a

l g

d

m

n i q 0

0 0 0 0

(^0 )

1

b c e h j^ p

r

0 0 0 0 0 0

1

k

a

l g

d

m

n i q 0

0 0 0 0

(^0 )

1

b c e h j^ p

r

0 0 0 0 0 0

1

b c e h j^ p

r

0 0 0 0 0 0

1

b

l g h

k

d e

a

n (^) m q

j

c

i

p

r

b

l g h

k

d e

a

n (^) m q

j

c

i

p

r

b

l g h

k

d e

a

n (^) m q

j

c

i

p

r

  • (^) +

โˆ’

โˆ’

โˆ’

โˆ’

  • โˆ’

B

B

โˆ’

โˆ’

b

l g h

k

d e

a

n (^) m q

j

c

i

p

r

  • (^) +

โˆ’

โˆ’

โˆ’

โˆ’

  • โˆ’

B

B

โˆ’

โˆ’