A friend wants help in implementing an algorithm for finding the shortest path between two nodes š¢ and š£ in a directed graph (possibly containing negative edge weights). He/she proposes the following:
- Add a large constant to each weight such that all weights become positive
- Run Dijkstra’s algorithm for the shortest path from š¢ to š£
This method is not correct as it may not always work for all cases. A counterexample for when this method does not work:
Assume we have a start point “š¢” and an end point "š£" with "š" vertices/nodes.
Path 1:
−3 8
š¢ → š → š£
Length of path 1: −3 + 8 = 5
Path 2:
−3 3 4
š¢ → š → š → š£
Length of path 2: −3 + 3 + 4 = 4
We can see that path 2 is the shorter path. Now, we add the largest constant (in both cases −3) in both the paths!
Path 1:
0 11
š¢ → š → š£
Length of path 1: 0 + 11 = 11
Path 2:
0 6 7
š¢ → š → š → š£
Length of path 2: 0 + 6 + 7 = 13
Now, in this case, when we add a positive large constant in both paths, path 1 is the shorter path. Therefore, we disprove the correctness of the given algorithm using a simple counter example!
Problem 12.2
Implemented in “OMP.cpp”. Execute make to run.
Problem 12.3 a.
The problem given to us can be represented as a graph problem.
i. Consider our board B.
ii. B has coordinates B[x][y] that represents the position of the player. iii. Every coordinate B[x][y] (position) in the board is a node.
iv. All edges are 1 as the distance moved is 1 (in all directions form the current position)
v. The vertex š = {0, 1, 2, .… , š2 – 1} vi. Edges of vertex š are neighboring nodes, i.e. up, down, left, right.