6 ms·
A linear solution for this problem is possible under the assumptions that the graph is directed and acyclic. The directed nature of the graph in this particular
by sjroot 8y ago
A linear solution for this problem is possible under the assumptions that the graph is directed and acyclic. The directed nature of the graph in this particular example is very clear, and because an individual doesn’t forward a message more than once, there are “basically” no cycles.
Had to Google what a Hamiltonian graph was though. I’ve been listening to the musical too much.
- pmiller2 8y agoThe stated problem does not produce an acyclic graph. For instance, if A has B in their contacts, then B is not forbidden from having A in their contacts. In fact, by ensuring that for all A, if A has B as a contact, then B has A as a contact, we can eliminate directedness from the graph and, via isomorphism, pretend we are operating on an undirected graph. The requirement that no actor forward a message twice is merely equivalent to finding a simple path through the graph. It does not “basically” mean there are no cycles.
- sjroot 8y agoI see what you mean, but it just boils down to how you interpret the problem. Specifically, if you focus on the actual forwarding paths instead of the contact lists, the tree structure that enables the linear solution becomes more clear. As I’m guessing was intended, this problem is particularly well-suited for engineering interviews because you can start with an easy example (the DAG one could derive in this specific interview) then ask the applicant how the solution would change given the additional constraints you’ve described.
- pmiller2 8y agoThat’s simply not correct. Do an example. What graph do you draw? That’s the underlying graph that has to be acyclic. Where is this supposed tree structure coming from? Try a dense graph, like with n=5, and every actor having at least 3 contacts.