Gonzo RI
Gonzo RI

Reputation: 71

Graph Abstract Data Type: Search method.

I am implementing a Graph for a school assignment with an adjacency list. I am having troubles with the search method. I would like to ask you the following question:

If you're searching a vertex that is not in the Graph, should it be an error? Which is the best criteria to determine if a possible result is an error or not?

Thanks, Gonzalo from Argentina (Hope you understand my english.)

Upvotes: 0

Views: 126

Answers (1)

mariosangiorgio
mariosangiorgio

Reputation: 5543

A solution is to throw a NoSuchElementException if the element is not there.

An alternative solution could be to use something similar to Scala's Option or Haskell's Maybe.

Another possible solution is to introduce a pair of methods boolean contains(VertexID id) and Vertex get(VertexID id). The class should use the query method contains to search for the presence for the element and the getter only if he knows that the element is there. Of course get should throw a NoSuchElementException if it is invoked for a missing element.

Upvotes: 2

Related Questions