Write a function named isDominatingSet
that checks whether a given set of vertexes represents a dominating set for a given graph.
For a given graph G, a dominating set is a set of vertexes D where every vertex in G either is an element of D or is adjacent (a neighbor) to an element of D.
Another way of phrasing this is that a given set of vertexes D is a dominating set of G if there does not exist any vertex in G outside of D that cannot reach a vertex in D by traveling across a single edge.
Your function accepts two parameters: a reference to a BasicGraph
, and a reference to a Set
of strings representing vertex names. You should return true if the given set is a dominating set for the given graph, and false if not.
You may assume that every string in the given set corresponds to the name of a vertex found in the graph.
You may also assume that the graph is undirected, in other words, that the edges are bidirectional between neighboring vertexes.
If the given graph and set are both empty, you should return true
.
For example, in the graph pictured below, {V3, V6, V9} is a dominating set, because every vertex in the graph either is one of those vertexes or a neighbor of one of those three vertexes.
So the call of isDominatingSet(graph, {"V3", "V6", "V9"})
would return true
.
Other example dominating sets would be {V0, V4, V6} and {V2, V4, V5, V7} and {V2, V4, V5, V8, V9}.
Constraints:
You may define private helper functions if so desired, and you may construct auxiliary collections as needed to solve this problem.
You should not modify the structure of the graph (e.g., add or remove vertexes or edges), but you may wish to modify the state variables inside individual vertexes/edges (e.g., cost, visited, color, etc.).