6graph tut&ans

20
Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of Algorithms Helena Wong, 2001 Heaps and Priority Queues 1. For an array of input numbers: 1,13,9,5,12,8,7,4,0,6,2,15 a.) Illustrate the operation of BUILD-MAX-HEAP on these data. b.) For the resultant heap A obtained in (a), illustrate the operation of HEAP-EXTRACT-MAX. c.) For the resultant heap A obtained in (a), illustrate the operation of MAX-HEAP-INSERT(A,10). 2. Write pseudocode for the procedures HEAP-MINIMUM, HEAP-EXTRACT-MIN, HEAP-DECREASE-KEY, and MIN-HEAP-INSERT that implement a min-priority queue with a min-heap. 3. Why do we want the loop index i in line 2 of BUILD-MAX-HEAP to decrease from n/2 down to 1 rather than increase from 1 to n/2 ? Answer: When MAX-HEAPIFY is called in the loop, it is assumed that the binary trees rooted at LEFT(i) and RIGHT(i) are max-heaps. To make sure this assumption holds in each iteration, we must handle the nodes from bottom to top, ie. want the loop index i in line 2 of BUILD-MAX-HEAP to decrease from n/2 down to 1 rather than increase from 1 to n/2 . 4. Argue the correctness of HEAP-INCREASE-HEY using the following loop invariant: At the start of each iteration of the while loop, the array A[1 .. n] satisfies the max-heap property, except that there may be one violation: A[i] may be larger than A[PARENT(i)] HEAP-INCREASE-KEY(A,i,key) A[i] = key While i > 1 and A[PARENT(i)] < A[i] Do exchange A[i] and A[PARENT(i)] i = PARENT(i) Answer: The given loop invariant is not sufficient. We modify it as: At the start of each iteration of the while loop, the array A[1 .. n] satisfies the max-heap property, except that there may be one violation: A[i] may be larger than A[PARENT(i)]. In addition, the parent of A[i] is greater than any child of A[i]. Initialization: Before this routine is executed, it is supposed to be a max-heap. So, any parent of A[i] >= A[i] >= any child of A[i]. After line 1, A[i] is increased to the value of “key”, that may be larger than the parent of A[i] (but still, any parent of A[i] >= any child of A[i]). Then the iteration begins. Hence, before the first iteration, the loop invariant holds. 15 13 9 8 7 5 12 6 2 4 0 1 15 13 9 8 7 5 12 6 2 6 6 2 2 4 0 4 4 0 0 1 1

Upload: murtaza-bhatia

Post on 02-Dec-2014

428 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 6Graph Tut&Ans

Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of AlgorithmsHelena Wong, 2001

Heaps and Priority Queues

1. For an array of input numbers: 1,13,9,5,12,8,7,4,0,6,2,15a.) Illustrate the operation of BUILD-MAX-HEAP on these data.b.) For the resultant heap A obtained in (a), illustrate the operation of HEAP-EXTRACT-MAX.c.) For the resultant heap A obtained in (a), illustrate the operation of MAX-HEAP-INSERT(A,10).

2. Write pseudocode for the procedures HEAP-MINIMUM, HEAP-EXTRACT-MIN, HEAP-DECREASE-KEY, and MIN-HEAP-INSERT that implement a min-priority queue with a min-heap.

3. Why do we want the loop index i in line 2 of BUILD-MAX-HEAP to decrease from n/2 down to 1 rather than increase from 1 to n/2 ?

Answer:When MAX-HEAPIFY is called in the loop, it is assumed that the binary trees rooted at LEFT(i) and RIGHT(i) are max-heaps. To make sure this assumption holds in each iteration, we must handle the nodes from bottom to top, ie. want the loop index i in line 2 of BUILD-MAX-HEAP to decrease from n/2 down to 1 rather than increase from 1 to n/2 .

4. Argue the correctness of HEAP-INCREASE-HEY using the following loop invariant:At the start of each iteration of the while loop, the array A[1 .. n] satisfies the max-heap property, except that there may be one violation: A[i] may be larger than A[PARENT(i)]

HEAP-INCREASE-KEY(A,i,key)A[i] = keyWhile i > 1 and A[PARENT(i)] < A[i]

Do exchange A[i] and A[PARENT(i)]i = PARENT(i)

Answer:The given loop invariant is not sufficient. We modify it as:

At the start of each iteration of the while loop, the array A[1 .. n] satisfies the max-heap property, except that there may be one violation: A[i] may be larger than A[PARENT(i)]. In addition, the parent of A[i] is greater than any child of A[i].

Initialization: Before this routine is executed, it is supposed to be a max-heap. So, any parent of A[i] >= A[i] >= any child of A[i]. After line 1, A[i] is increased to the value of “key”, that may be larger than the parent of A[i] (but still, any parent of A[i] >= any child of A[i]). Then the iteration begins. Hence, before the first iteration, the loop invariant holds.

Maintenance: At the beginning of each iteration, let D be the value of A[i] and P be the value of the parent of A[i]. Suppose the loop invariant is true before each iteration, P is greater than any child of A[i]. In the iteration, if P < D, then they are exchanged, and i is changed to keep the index of the parent, ie. “Node i” means the node storing the value of D, but that has already been moved upwards. And, P is now moved lower and has new children that are smaller than P. Only that the relationship between node i and its parent becomes unknown. So, after the iteration, the loop invariant holds for the new value of i.

Termination: The iteration terminates when case 1: i=1 (ie. A[i] is the root, that has no parent) or case 2: parent of A[i] >= A[i]. In both cases, the truth of the loop invariant imples that A[1..n] satisfies the max-heap property.

15

13 9

8 75 12

6 24 0 1

15

13 9

8 75 12

6 266 224 044 00 11

Page 2: 6Graph Tut&Ans

Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of AlgorithmsHelena Wong, 2001

Data structures for Graph Problems5. Given an adjacency-matrix representation of a directed graph, how long does it take to compute

the out-degree of every vertex? How long does it take to compute the in-degrees?

Answer:For a directed graph G=(V,E), the adjacency-matrix M is a |V|x|V| matrix such that each cell m[i][j] is 1 if there is an edge (vi,vj) in E, and m[i][j] = 0 otherwise.To compute the out-degree of a vertex vk, we count for all o=1 to |V| such that m[k][o]=1 [O(V)]To compute the in-degree of a vertex vk, we count for all i=1 to |V| such that m[i][k]=1 [O(V)]

Given an adjacency-list representation of a directed graph, how long does it take to compute the out-degree of every vertex? How long does it take to compute the in-degrees?

Answer:For a directed graph G=(V,E), the adjacency-list representation involves |V| link lists such that list i points to linked nodes storing the vertices vj where (vi,vj) is an edge in E. To compute the out-degree of a vertex vk, we count the linked nodes of list k. [O(V)]To compute the in-degree of a vertex vk, we count for each linked nodes in all the |V| lists such that the linked node is vk . [O(E)]

6. Give an adjacency-list representation for a complete binary tree on 7 vertices, assume that the vertices are numbered from 1 to 7 as in a binary heap.Give an adjacency-matrix representation.

Breadth First Search

7. Show the distance and pred values that result from running breadth-first search on the following graph, using vertex 3 as the source.

1 2 3

4 5 6

1 2 3

4 5 6

Answer:

30/null

6/null

1/null

2/null

4/null

5 /null

30/null

6/null

1/null

2/null

4/null

5 /null

3Q 3Q

30/null

61/3

1/null

2/null

4/null

5 1/3

30/null

61/3

1/null

2/null

4/null

5 1/3

65Q 65Q

30/null

61/3

1/null

2/null

42/5

5 1/3

30/null

61/3

1/null

2/null

42/5

5 1/3

46Q 46Q

30/null

61/3

1/null

23/4

42/5

5 1/3

30/null

61/3

1/null

23/4

42/5

5 1/3

2Q 2Q

30/null

61/3

1/null

2/null

42/5

5 1/3

30/null

61/3

1/null

2/null

42/5

5 1/3

4Q 4Q

30/null

61/3

1/null

23/4

42/5

5 1/3

30/null

61/3

1/null

23/4

42/5

5 1/3

Q Q

Page 3: 6Graph Tut&Ans

Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of AlgorithmsHelena Wong, 2001

8. What is the running time of BFS if its input graph is represented by an adjacency matrix and the algorithm is modified to handle this form of input?

Answer: [O(V2)]

9. Argue that in a breadth-first search, the value u.distance is independent of the order in which the vertices in each adjacency list are given. Using the following graph as an example, show that the breadth-first tree computed by BFS can depend on the ordering within adjacency lists.

Answer:“u.distance is independent of the order in which the vertices in each adjacency list are given”In Breadth-First Search, we explore the edges of a graph to reach every vertex from a given source vertex, with “shortest paths”. u.distance is the distance of the shortest path of vertex u, hence it is independent of the order in which the vertices in each adjacency list are given.In the example, if we take “t” as the source vertex, then from t we will go to u and x. Suppose u is in front of x in t’s adjacency list (this means u is queued before x) , then we’ll go to y through u (y’s parent node will be u). However, if x is in front of u in t’s adjacency list (this means x is queued before u), then we’ll go to y through x (y’s parent node will be x). Hence the breadth-first tree will be different depending on the order of u and x. But note that y.distance will still be the same.

Depth First Search and Topological Sorting

10. Show how depth-first search works on the following graph. Assume that the For loops of the DFS procedure considers the vertices in alphabetical order, and assume that each adjacency list is ordered alphabetically. Show the discovery and finishing times for each vertex.Show the ordering of vertices produced by TOPOLOGICAL-SORT when it is run on this graph.

v

s

w

q

t

x

z

r

u

y

Answer:

v

s

w

q

t

x

z

r

u

y

1/16

8/15

9/12

10/11

13/14

17/20

18/19

2/7

3/6

4/5

v

s

w

q

t

x

z

r

u

y

1/16

8/15

9/12

10/11

13/14

17/20

18/19

2/7

3/6

4/5

Ordering of vertices: r,u,q,t,y,x,z,s,v,wNote that this graph is not acyclic: ie. It contains cycles, hence the “topological sorting” is questionable.

Page 4: 6Graph Tut&Ans

Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of AlgorithmsHelena Wong, 2001

11. Explain how a vertex u of a directed graph can end up in a depth-first tree containing only u, even though u has both incoming and outgoing edges in G.

Answer:

ua

zSuppose the above graph is to be traversed by depth-first search, and the vertices are examined according to alphabetical order. We firstly examine a. Since it has no outgoing edges, we mark a with start time and finishing time as 1/2, ie. a is a tree with only one node. Then we examine u. Since u has only one outgoing edge that directs to an already examined node (a), we can’t traverse further from u. Hence we mark u with start time and finishing time as 3/4, ie. u is a tree with only one node. In this example, although u has both incoming and outgoing edges, but it ends up as a depth-first tree containing only u. This is because (1) all its outgoing edges point to vertices that have been examined before u is examined, and (2) all its incoming edges are pointed from vertices that are examined only after u is examined.

Minimum Spanning Trees

12. Run the Kruskal’s algorithm on the following graph:

Answer: Step Edge Considered Connected componentsInitialization -- {1} {2} {3} {4} {5} {6} {7}1 {1,2} {1,2} {3} {4} {5} {6} {7}2 {2,3} {1,2,3} {4} {5} {6} {7}3 {4,5} {1,2,3} {4,5} {6} {7}4 {6,7} {1,2,3} {4,5} {6,7}5 {1,4} {1,2,3,4,5} {6,7}6 {2,5} Rejected7 {4,7} {1,2,3,4,5,6,7}

7

1 2 3

4 5 6

4

4 6 4 5 6

3 8

3

21

7

7

1 2 3

4 5 6

4

4 6 4 5 6

3 8

3

21

7

Page 5: 6Graph Tut&Ans

Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of AlgorithmsHelena Wong, 2001

13. Suppose that the graph G=(V,E) is represented as an adjacency matrix. Give a simple implementation of Prim’s algorithm for this case that runs in O(V2) time.

Answer:

Single-Source Shortest Paths

14. Run the Bellman-Ford algorithm on the following directed graph, using vertex z as the source.

0

5

6

8

79

-4

-37s

-2

2

t x

y z

Answer:

0

5

6

8

79

-4

-37s

-2

2

t x

y z

0

5

6

8

79

-4

-37s

-2

2

t x

y z

2

7

0

5

6

8

79

-4

-37s

-2

2

t x

y z

2

7

0

5

6

8

79

-4

-37s

-2

2

t x

y z

2

5

9

7

0

5

6

8

79

-4

-37s

-2

2

t x

y z

2

5

9

7

0

5

6

8

79

-4

-37s

-2

2

t x

y z

2

5

9

6

0

5

6

8

79

-4

-37s

-2

2

t x

y z

2

5

9

6

0

5

6

8

79

-4

-37s

-2

2

t x

y z

2

4

9

6

0

5

6

8

79

-4

-37s

-2

2

t x

y z

2

4

9

6

0

5

6

8

79

-4

-37s

-2

2

t x

y z

MST-PRIM(G,r) /* G = (V,E) */1 for i=1 to |V|2 dist[i] = 3 pred[i] = 4 dist[r] = 05 Create a min-priority queue Q for indexes of vertices according

to values of dist6 While Q is not empty7 i = EXTRACT-MIN(Q)8 For j = 1 to |V|9 if m[i][j]=110 if j exists in Q and weight of (i,j) < dist[j]11 pred[j] = i12 dist[j] = weight of (i,j)

Page 6: 6Graph Tut&Ans

Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of AlgorithmsHelena Wong, 2001

15. The basic Bellman-Ford algorithm is to relax the whole set of edges |V|-1 times. This is carried out in the |V|-1 iterations of the For loop. A more efficient version of the Bellman-Ford algorithm is to keep track of whether there is any changes to the d values in each iteration, and to stop when there is no change in one iteration. Implement this version by changing some codes of the basic Bellman-Ford algorithm and the relaxation routine.

Answer: RELAX(u,v) 1 if v.d > u.d + w(u,v)2 v.d = u.d + w(u,v)3 v.pred = u4 changes = true

Bellman-Ford Algorithm(G,s) /*G=(V,E)*/1 INITIALIZE-SINGLE-SOURCE(G,s)2 changes = true3 Iterations_done=0;4 while changes = true and Iterations_done<=|V|-15 change = false6 for each edge (u,v) in E7 RELAX(u,v)8 Iterations_done++;9 for each edge (u,v) in E10 if v.d > u.d + w(u,v)11 return false12 return true

16. Run DAG-SHORTEST-PATHS on the following graph, using vertex s as the source.

Answer:

0 5 2 7 -1 -25 2 7 -1 -2

3 24

6 1

s 0 5 2 7 -1 -25 2 7 -1 -2

3 24

6 1

s

0 5 3 5 2 7 -1 -25 2 7 -1 -2

3 24

6 1

s 0 5 3 5 2 7 -1 -25 2 7 -1 -2

3 24

6 1

s

0 5 3 11 5 2 7 -1 -25 2 7 -1 -2

3 24

6 1

s 0 5 3 11 5 2 7 -1 -25 2 7 -1 -2

3 24

6 1

s

0 5 3 10 7 55 2 7 -1 -25 2 7 -1 -2

3 24

6 1

s 0 5 3 10 7 55 2 7 -1 -25 2 7 -1 -2

3 24

6 1

s

0 5 3 10 7 55 2 7 -1 -25 2 7 -1 -2

3 24

6 1

s 0 5 3 10 7 55 2 7 -1 -25 2 7 -1 -2

3 24

6 1

s

0 5 3 10 7 55 2 7 -1 -25 2 7 -1 -2

3 24

6 1

s 0 5 3 10 7 55 2 7 -1 -25 2 7 -1 -2

3 24

6 1

s

0 5 2 7 -1 -25 2 7 -1 -2

3 24

6 1

s

Page 7: 6Graph Tut&Ans

Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of AlgorithmsHelena Wong, 2001

17. Give a simple example of a directed graph with negative-weight edges for which Dijkstra’s algorithm produces incorrect answers.

Answer:

5

10

-6

4

s

5

10

-6

4

s0

5

10

5

10

-6

94

s0

5

10

5

10

-6

94

s0

4

10

5

10

-6

94

s0

4

10

5

10

-6

9

4

s0

5

10

5

10

-6

4

s0

5

10

5

10

-6

4

s0

5

10

5

10

-6

94

s0

5

10

5

10

-6

94

s0

5

10

5

10

-6

94

s

Not a shortest path

The algorithmfinishes here

Page 8: 6Graph Tut&Ans

Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of AlgorithmsHelena Wong, 2001

All-Pairs Shortest Paths

18. Run SLOW-ALL-PAIRS-SHORTEST-PATHS on the following graph, showing the matrices that result for each iteration of the loop.Do the same for FASTER-ALL-PAIRS-SHORTEST-PATHS.

Answer: SLOW-ALL-PAIRS-SHORTEST-PATHS

l(2) 1 2 3 4 5 61 0 6 -1 2 -2 0 2 0 3 3 -3 0 4 -84 -4 10 0 -5 5 8 7 9 0 6 6 5 10 7 0

l(3) 1 2 3 4 5 61 0 6 8 -1 2 -2 0 2 -3 3 -2 -3 0 -1 2 -84 -4 2 0 -5 5 5 7 9 0 6 3 5 10 7 5 0

l(4) 1 2 3 4 5 61 0 6 8 -1 2 -2 0 2 -3 3 -5 -3 0 -1 -3 -84 -4 2 0 -5 5 5 7 9 0 6 3 5 10 7 2 0

l(5) 1 2 3 4 5 61 0 6 8 -1 2 -2 0 2 -3 3 -5 -3 0 -1 -6 -84 -4 2 0 -5 5 5 7 9 0 6 3 5 10 7 2 0

1 2 3

4 5 6

-4

1 2

-8105

7-12

3

01056

075

30-44

-8023

2012

-101

654321w

01056

075

30-44

-8023

2012

-101

654321w

Page 9: 6Graph Tut&Ans

Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of AlgorithmsHelena Wong, 2001

Answer: FASTER-ALL-PAIRS-SHORTEST-PATHS

L(2) 1 2 3 4 5 61 0 6 -1 2 -2 0 2 0 3 3 -3 0 4 -84 -4 10 0 -5 5 8 7 9 0 6 6 5 10 7 0

l(4) 1 2 3 4 5 61 0 6 8 -1 2 -2 0 2 -3 3 -5 -3 0 -1 -3 -84 -4 2 0 -5 5 5 7 9 0 6 3 5 10 7 2 0

l(8) 1 2 3 4 5 61 0 6 8 -1 2 -2 0 2 -3 3 -5 -3 0 -1 -6 -84 -4 2 0 -5 5 5 7 9 0 6 3 5 10 7 2 0

19. Suppose we also wish to compute the vertices on shortest paths in the SLOW-ALL-PAIRS-SHORTEST-PATHS algorithm. Show how to compute the predecessor matrix from the completed matrix L of shortest-path weights in O(n3) time.

Answer: Use the graph in question 18 as example. To compute the predecessor matrix pred[6][6], consider the shortest path for each pair of vertices (i,j). This shortest path should be any one of the followings:1. Use the shortest path from i to vertex 1, then use the edge from 1 to j. => pred[i][j] = 1 2. Use the shortest path from i to vertex 2, then use the edge from 2 to j. => pred[i][j] = 23. Use the shortest path from i to vertex 3, then use the edge from 3 to j. => pred[i][j] = 34. Use the shortest path from i to vertex 4, then use the edge from 4 to j. => pred[i][j] = 45. Use the shortest path from i to vertex 5, then use the edge from 5 to j. => pred[i][j] = 56. Use the shortest path from i to vertex 6, then use the edge from 6 to j. => pred[i][j] = 6

From the completed matrix L, we know the shortest path weight for (i,j), hence we can compute the weights of the paths in the above 6 cases to see which one matches the shortest path weight in L. eg. if case 4 matches the shortest path weigh in L, then pred[i][j] = 4.

COMPUTE-PRED()1 for i= 1 to 62 for j=1 to 63 for k=1 to 64 if L[i][j] = L[i][k]+w[k][j]5 then pred[i][j]=k

Page 10: 6Graph Tut&Ans

Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of AlgorithmsHelena Wong, 2001

20. Run the Floyd-Warshall algorithm on the graph for question 18. Show the matrix D(k) that results for each iteration of the outer loop.

Answer:

D(1) 1 2 3 4 5 6 Pred(1) 1 2 3 4 5 61 0 -1 1 1 2 1 0 2 0 2 2 2 1 3 2 0 -8 3 3 34 -4 0 -5 4 4 1 5 7 0 5 5 6 5 10 0 6 6 6

D(2) 1 2 3 4 5 6 Pred(2) 1 2 3 4 5 61 0 -1 1 1 2 1 0 2 0 2 2 2 1 3 3 2 0 4 2 -8 3 2 3 2 1 34 -4 0 -5 4 4 1 5 8 7 9 0 5 2 5 2 6 6 5 10 7 5 0 6 2 6 6 2 1

D(3) 1 2 3 4 5 6 Pred(3) 1 2 3 4 5 61 0 -1 1 1 2 1 0 2 0 2 2 2 1 3 3 2 0 4 2 -8 3 2 3 2 1 34 -4 0 -5 4 4 1 5 8 7 9 0 5 2 5 2 6 6 5 10 7 5 0 6 2 6 6 2 1

D(4) 1 2 3 4 5 6 Pred(4) 1 2 3 4 5 61 0 -1 1 1 2 -2 0 2 -3 2 4 2 1 3 0 2 0 4 -1 -8 3 4 3 2 1 34 -4 0 -5 4 4 1 5 5 7 9 0 5 4 5 2 6 3 5 10 7 2 0 6 4 6 6 2 1

D(5) 1 2 3 4 5 6 Pred(5) 1 2 3 4 5 61 0 6 8 -1 1 5 2 1 2 -2 0 2 -3 2 4 2 1 3 0 2 0 4 -1 -8 3 4 3 2 1 34 -4 2 0 -5 4 4 5 1 5 5 7 9 0 5 4 5 2 6 3 5 10 7 2 0 6 4 6 6 2 1

D(6) 1 2 3 4 5 6 Pred(6) 1 2 3 4 5 61 0 6 8 -1 1 5 2 1 2 -2 0 2 -3 2 4 2 1 3 -5 -3 0 -1 -6 -8 3 4 6 2 1 34 -4 2 0 -5 4 4 5 1 5 5 7 9 0 5 4 5 2 6 3 5 10 7 2 0 6 4 6 6 2 1

Page 11: 6Graph Tut&Ans

Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of AlgorithmsHelena Wong, 2001

21. How can the output of the Floyd-Warshall algorithm be used to detect the presence of a negative-weight cycle?

Answer:2 ways:

A. Check the main-diagonal entries of the result matrix for a negative value.B. Run the normal FLOYD-WARSHALL algorithm one extra iteration (k=|V|) to see if any of the d

value change. If there are negative cycles, then some shortest-path cost will be cheaper. If there are no such cycles, then no d-values will change because the algorithm gives the correct shortest paths.

22. Use Johnson’s algorithm to find the shortest paths between all pairs of vertices in the graph for question 18. Show the values of w’ and computed by the algorithm.

Answer:Step 1: Add a new vertex {s} and the 0-weight edges:

Step 2: Run BELLMAN-FORD(G,s)

1

2

3

4

5

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

1

2

3

4

5

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

01

02

03

0

4

0

5

0

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

01

02

03

0

4

0

5

0

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

1 2 3

4 5 6

-4

1 2

-8105

7-12

3

s

0

0

0

0

0

0

Page 12: 6Graph Tut&Ans

Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of AlgorithmsHelena Wong, 2001

-41

02

03

0

4

-1

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

-41

02

03

0

4

-1

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

-41

-32

03

0

4

-5

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

-41

-32

03

0

4

-5

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

-41

-32

03

-1

4

-5

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

-41

-32

03

-1

4

-5

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

-51

-32

03

-1

4

-5

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

-51

-32

03

-1

4

-5

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

-51

-32

03

-1

4

-6

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

-51

-32

03

-1

4

-6

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

Since no edge can relax any more vertex, we conclude that this graph doesn’t have negative-weight cycle.

Page 13: 6Graph Tut&Ans

Tutorial Exercise (Graph Algorithms) CS3381 Design and Analysis of AlgorithmsHelena Wong, 2001

Step 3: For each edge, compute new weights w’(u,v) = w(u,v) + (s,u) - (s,v)

-51

-32

03

-1

4

-6

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

-51

-32

03

-1

4

-6

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

-5

1-3

20

3

-1

4

-6

5

-8

6

--4+(-1)-(-5)

= 0

1+(-3)-(-5)= 3

2+0-(-3)= 5

-8+(0)-(-8)=0

10+(-8)-(0)= 2

5+(-8)-(-3)= 0

7+(-6)-(-3)= 4

-1+(-5)-(-6)=0

2+(-3)-(-1)=0

3+(-1)-(-6)=8

0s

0

0

0

0

0

0

-5

1-3

20

3

-1

4

-6

5

-8

6

--4+(-1)-(-5)

= 0

1+(-3)-(-5)= 3

2+0-(-3)= 5

-8+(0)-(-8)=0

10+(-8)-(0)= 2

5+(-8)-(-3)= 0

7+(-6)-(-3)= 4

-1+(-5)-(-6)=0

2+(-3)-(-1)=0

3+(-1)-(-6)=8

0s

0

0

0

0

0

0

Step 4: For each vertex u,

Run DIJKSTRA(G,u) using w’ to compute ’(u,v) for all v in VThen for each vertex v in V, compute (u,v)= ’(u,v) + (s,v) - (s,u)

1

2

3

4

5

6

0

3 5

02

04

0

0

8

1

2

3

4

5

6

0

3 5

02

04

0

0

8

Step 4.1: Run DIJKSTRA(G,v1). Result:

01

42

3

4

4

0

5

6

0

3 5

02

04

0

0

8

01

42

3

4

4

0

5

6

0

3 5

02

04

0

0

8

Compute (v1,v2)= ’(v1,v2) + (s,v2) - (s,v1)(v1,v4)= ’(v1,v4) + (s,v4) - (s,v1)(v1,v5)= ’(v1,v5) + (s,v5) - (s,v1)

-51

-32

03

-1

4

-6

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

-51

-32

03

-1

4

-6

5

-8

6

-4

1 2

-81057-12

3

0s

0

0

0

0

0

0

0

1

4+(-3)-(-5)=6

2

3

4+(-1)-(-5)=8

4

0+(-6)-(-5)=-1

5

6

0

3 5

02

0

4

0

0

8

0

1

4+(-3)-(-5)=6

2

3

4+(-1)-(-5)=8

4

0+(-6)-(-5)=-1

5

6

0

3 5

02

0

4

0

0

8

Step 4.2: Run DIJKSTRA(G,v2) …Step 4.3: Run DIJKSTRA(G,v3) …Step 4.4: Run DIJKSTRA(G,v4) …Step 4.5: Run DIJKSTRA(G,v5) …Step 4.6: Run DIJKSTRA(G,v6) …