There should not be much that is confusing in these tasks.
id
to the graph? Probably not.AMGraph
, you may assume that there is a maximum number of vertices and that the ids will be in the range [1, maxVertices]. You should not, however, assume that vertices will be added with ids in that order (1 to maxVertices). You should use a matrix to get the benefits of this representation. Using a Map
or a List
of List
s really defaults to a representation that is closer to an adjacency list.Vertex
implement clone
correctly.getEdge
: What should one do if there is no edge between a given pair of vertices?
null
but this is problematic because a client may have an unguarded use of the return value.RuntimeException
(unchecked) but it is quite possible that someone asks for an edge when it does not exist. Throwing a RuntimeException
is not a good choice.try
and catch
block. Moreover, we should try to avoid the overhead of exception processing when possible. Throwing a checked exception also weakens the spec.getEdge
should be called only when edge
returns true
. Adding the precondition weakens the specification but the impact on the client is less significant (the check is easy) and the overhead is restricted. The best option may well be to weaken the specification in the interface and implement to this weakened spec. (The goal of leaving these methods somewhat underdetermined at the start is to allow people to think about whether a strong spec is always the best option.)shortestPath
: What should one do if there is no path between a given pair of vertices? What should one do if the source and sink are the same? What should one do if one or both vertices are not in the graph? The discussion is somewhat similar to getEdge
.
pathLength
should behave correctly for a list with one vertex.pathLength
such that an empty list implies no path ⇒ a path of infinite length (maybe Integer.MAX_VALUE
) with a suitable spec for the postcondition.minimumSpanningComponents
k = 1
, you should return a minimum spanning tree.k
, you are essentially making a small modification to the MST algorithm.k
. If we believe that a client may not be able to verify this requirement (because it is a computationally-expensive verification) then we can throw a checked exception. Both these approaches weaken the specification and so the spec in the interface needs to be updated.getCenter
: Do not use the Marvel dataset for your own tests unless you are using a small subset. The Marvel dataset can be used separately to test the performance of your algorithm but your test suite needs to be efficient to run (should not take much time). The test suite for Task 1-4 should not take more than 3 minutes. You should pick tests suitably. Small tests are enough to achieve coverage and (in almost all cases) find bugs.clone()
for Link
.ImGraph
to help with this task. (You should not need to do so for Tasks 1-4.)