











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
AUBS Algorithms and programming assignment 7 with exercises and solutions
Typology: Exercises
Uploaded on 05/14/2023
10 documents
1 / 19
This page cannot be seen from the preview
Don't miss anything!












CMPS 214 Problem Set 7
Note: Starting from Problem 4 in this problem set are all DP problems.
// use the information in p to reconstruct the shortest path: path = [t] current = t while current != s: current = p[current] add current to the front of the path return path, d[t] Step through Dijkstra_st_path(G; s; t) on the graph G shown below. Complete the table below (on the next page) to show what the arrays d and p are at each step of the algorithm, and indicate what path is returned and what its cost is. If it is helpful, the LATEXcode for the table is reproduced at the end of the PSET.
s
u
v
t
[We are expecting: The following things:
The table below lled out The shortest path and its cost that the algorithm returns.
No justication is required.]
Solution:
d[s] d[u] d[v ] d[t] p[s] p[u] p[v ] p[t] When entering the rst while loop for the rst time, the state is:
0 1 1 1 None None None None
Immediately after the rst ele- ment of D is added, the state is:
0 3 1 9 None s None s
Immediately after the second ele- ment of D is added, the state is:
0 3 5 8 None s u u
Immediately after the third ele- ment of D is added, the state is:
0 3 5 6 None s u v
Immediately after the fourth ele- ment of D is added, the state is:
0 3 5 6 None s u v
Shortest path would be: s; u; v ; t with a cost of d[t] = 6
[We are expecting: A list of vertices from G that are visited by the Dijkstra's algorithm in the order of when they are rst marked sure.]
Solution:
S; B; E; T
(b) Even if a graph contains no negative cycles (but still contains negative edges) we might still be in trouble. Please draw a graph G, which contains both positive and negative edges but does not contain negative cycles, and specify some source s 2 V where Dijkstra(G, s) does not correctly compute the shortest paths from s. [We are expecting: A graph G and a brief explanation on the shortest path that is not correctly computed.]
Solution: In order to come up with such an example, remember a core assumption of the standard Dijkstra's algorithm. Once you mark a vertex as "sure", you know you have found the shortest path from the vertex to it. This, however, is not necessarily the case when the graph we are dealing with includes negative edges. In other words, in some cases, we may have to undo marking some vertices as "sure". Take a look at the example below:
The shortest path from vertex A to vertex D is clearly A; C; B; D. However, Dijk- stra's algorithm with A as the source applied on this graph works as follows: First, A is selected, it relaxes the distances of its neighbors (d[D] becomes 1, d[B] be- comes 2, d[C] becomes 4), and we set A to "sure". Then we pick D, which is the "unsure" vertex with the smallest d, and we set it to "sure". Then, we pick B, d[B] + 3 is not smaller than d[D] so d[D] is unchanged, then we set B to sure. Finally, we pick C; C relaxes d[B] because d[C] 100 is smaller than d[B]. Since all vertices were marked as "sure", we terminate.
One way to x the algorithm above would be to "unset" the "sure" ag of B after
It should be easy to see that Negative-Dijkstra does not do its intended job. The reason why that is the case is that it punishes shortest paths with a lot of edges.
Consider the graph G above. The shortest path from A to C is clearly A; D; E; C. However, once we apply Negative-Dijkstra, the shortest path from A to C becomes A; B; C.