Question: Checking Shortest Paths Shortest path algorithms can be complicated. How can we check that they are correct? Luckily, there are simple and efficient algorithms to
Checking Shortest Paths
Shortest path algorithms can be complicated. How can we check that they are correct?
Luckily, there are simple and efficient algorithms to verify that the output of a single source shortest path algorithm is correct. Your task in this part of the assignment is to implement such an algorithm. You will then use this algorithm to check that your single source shortest path algorithm in part is correct!
Background
For this part all graphs will be directed and weighted, and can possibly have negative weights. We will use the graph class from Week Tutorial with the additions that now the class is templated and we add public member functions to remove edges and check if an edge is present. The template type T represents the type of the edge weights. For us T can be int, MyInteger, or double. All the functions you write will also be templated.
If an algorithm claims that the shortest path in a graph
G from vertex to vertex has length we need to verify two things:
There actually is a path from vertex to vertex of length
There is no path from vertex to vertex of length
Item is relatively simple to verify, but is much more interesting. The way we will verify is through a shortest path tree, which we discuss next.
Shortest Path Trees
In part of this assignment you will write a single source shortest path algorithm that outputs a shortest path tree. A shortest path tree for a graph G with N vertices is a graph with only at most N edges that has all the information to construct a shortest path from the source to any vertex in G The source vertex is the root of the tree, ie all edges are directed away from the source. Technically a shortest path tree is not necessarily a tree, because in the original graph G some vertices may not be reachable from the source. In the shortest path tree such nonreachable vertices will be isolated verticesvertices no incoming or outgoing edges. Precisely, a shortest path tree will have the following properties:
a It is a tree plus some isolated vertices. Any vertex not reachable from the source must be isolated, there are no cycles even ignoring edge directions, and all edges are directed away from the root.
b All edges of the tree are also edges in the original graph, with the same weight. That is the tree is a subgraph of the original graph.
c The length of the unique path from the root of the tree to any other nonisolated vertex is the same as the length of a shortest path in the original graph from the source to that vertex.
You will write functions to verify each of these three properties.
To check a we have the function
template
bool isTreePlusIsolatedconst Graph& G int root;
This function should return false if and only if G contains a cycle when edge directions are ignored, or if there is a nonisolated vertex which is not reachable from the root.
To check b we have the function
template
bool isSubgraphconst Graph& H const Graph& G;
As this function is pretty simple, we generalise it beyond trees to two graphs H and G The function should return true if and only if H is a subgraph of G that is the size of H is at most that of G and every edge of H is also an edge in G with the same weight.
To help verify c we have a function that returns a vector that records the length of the path from the root to every other vertex in the tree.
template
std::vector pathLengthsFromRootconst Graph& tree, int root;
As we are using this to check a "more complicated" shortest path algorithm, you should take advantage of the fact that this is a tree to use a "simple" algorithm hint: depth or breadthfirst search
Taken together, these functions can verify part because they show there is a path in the tree from the source to a given vertex of a certain length, and all edges of the tree are also edges in the original graph. Now let us turn to
No shorter paths
Item is quite interesting. How can we verify there is no shorter path without trying all of them? All the shortest path algorithms we have studied are based on relaxing edges. We maintain a vector bestDistanceTo, where bestDistanceTo.atv is always an upper bound on the length of a shortest path from the source to v We then go over edges in some order and relax them. If there is an edge from u to v we do:
if bestDistanceToatu bestDistanceTov GgetEdgeWeightv u
bestDistanceTo.atu bestDistanceTov GgetEdgeWeightv u
Think about the BellmanFord shortest path algorithm. We initialise bestDistanceTo.atsource and bestDistanceTo.atv infinity for every other vertex v Thus bestDistanceTo.atv starts as an upper bound on the distance from vertex to vertex v We then do n rounds of relaxing every edge, when the graph has n vertice
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
