This is a problem to find Euler Path of the graph. As this is an undirected graph, the Euler Path exists iff:
- All graph vertices have even degree. Or,
- All graph vertices have even degree, except two vertices have odd degree. And these two vertices will be start / end of the Euler Path.
This problem explicitly stated that the Euler Path existing, so no need to do the test to find out if the path exists or not. Instead, the job is to find out the exact path with the constraint that vertices should be sequenced in ascending order. There are a couple of well studied algorithm can solve the Euler Path finding problem, however for this test data set I find that we can just use DFS - which is not strictly a Euler Path finding solution because the DFS solution here alone can’t decide if an edge we are going to remove is a ‘bridge edge’ (which, if removed will never makes us traverse back to the graph). I think the solution here is not sound to cover any type of Euler Path, but is just lucky enough to pass the existing test data set.
|