This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: ECE608, Homework #11 Solution (1) CLR 24.13 The proof of Lemma 24.2 shows that for every v , d [ v ] has attained its final value after length (any shortestweight path to v ) iterations of BellmanFord . Thus after m passes, BellmanFord can terminate. We don’t know m in advance, so we can’t make the algorithm loop exactly m times and then terminate. But if we just make the algorithm stop when nothing changes any more, it will stop after m +1 iterations (i.e., after one iteration without a change), unless there is a negative weight cycle. Hence, we should also make sure that the number of iterations does not exceed  V [ G ]  − 1. BELLMANFORD( M + 1)( G,w,s ) 1. INITIALIZESINGLESOURCE ( G,s ) 2. changes ← True 3. iteration ← 1 4. while ( changes = True ) ∧ ( iteration ≤  V [ G ]  − 1) 5. do changes ← False 6. iteration ← iteration + 1 7. for each edge ( u,v ) ∈ E [ G ] 8. do RelaxM ( u,v,w ) 9. for each edge ( u,v ) ∈ E [ G ] 10. do if d [ v ] > d [ u ] + w ( u,v ) 11. then return FALSE 12. return TRUE RelaxM ( u,v,w ) 1. if d [ v ] > d [ u ] + w ( u,v ) 2. then d [ v ] ← d [ u ] + w ( u,v ) 3. π [ v ] ← u 4. changes ← True (2) CLR 24.14 Change line 7 of the BELLMANFORD algorithm, as given in the CLR text, to: then d [ v ] ← −∞ (3) CLR 24.24 Consider a node v in a directed acyclic graph G . The paths in G starting from v must go from any outgoing edge from v . Let < V,u ) be one of such edges, then the paths starting from v and containing ( v,u ) can either stop at u or continue from u 1 to some other vertices. Since G is a directed acyclic graph, any edge can only appear at most once in any path. We can thus conclude that the number of paths starting from v and containing ( v,u ) equals the number of paths starting from u plus one. We give the algorithm for calculating the total number of paths in G below. DAGCOUNTPATHS( G ) 1. topologically sort the vertices of G 2. totalcount ← 3. for every vertex v in G , taken in reverse topologically sorted order 4. do count [ v ] ← 5. for each u ∈ Adj [ v ] 6. do count [ v ] ← count [ v ] + count [ u ] + 1 7. totalcount ← totalcount + count [ v ] The running time of this algorithm is Θ( V + E ). (4) CLR 24.32 Consider the graph below: a26a25 a27a24 a26a25 a27a24 a26a25 a27a24 a26a25 a27a24 a45 a45 a54 a64 a64 a64 a64 a64 a64 a82 a82 1 37 4 2 s t u x The predecessor subgraph returned by Dijkstra ’s algorithm will look like this: 2 a26a25 a27a24 a26a25 a27a24 a26a25 a27a24 a26a25 a27a24 a54 a64 a64 a64 a64 a64 a64 a82 a827 4 2 s t u x The correct answer should use the path s → x → t → u instead of s → u , since δ ( s,u ) = 0....
View
Full
Document
This note was uploaded on 01/10/2012 for the course ECE 565 taught by Professor Pai during the Fall '11 term at Purdue.
 Fall '11
 PAI

Click to edit the document details