Question
Implement a function bool chainable(vector
, which takes a set of strings as parameters and returns true
if they
Isn't that similar to the infamous traveling salesman problem?
If you have n
strings, you can construct a graph out of them, where each node corresponds to one string. You construct the edges the following way:
a
and b
are chainable, you introduce an edge a -> b
with weight 1
.a
and b
, you introduce an edge a -> b
with weight n
.Then, all your strings are chainable (without repetition) if and only if you can find an optimal TSP route in the graph whose weight is less than 2n
.
Note: Your problem is actually simpler than TSP, since you always can transform string chaining into TSP, but not necessarily the other way around.