KaLee
BAN USERJust to summarize, as per my understanding: Please add any more points missed
glebtepanov1992 is right...
let a, b, c, d, e, and f are nodes of same graph.
1. a-b-c-d-e
2. a-c-e-f
3. a-b-d-c
4. a-c-g [of, course i didn't list all the possible traversal paths but this should be enough]
Now as Sandeep has poined out, if code always stops at kth hop (to avoid unnecessary traversal larger than k), then we might loose few valid nodes from visiting. As an example lets take k = 4. Somehow, the DFS chooses the 3. as the first path. Everyone knows, how DFS work. that is, when a node is visited, if at all we come across the same node, we wont go beyond that node. With the path considered, code will definitely miss the node g from printing its value.[ Path 4.]
If we dont the limit the number of hops, we end up exploring all the connected nodes from source, which is very inefficient. [as mentioned by eugene]
Question is with 'n' streams which is a finite value, & implicitly means that you can store those many values.. Only problem is that, you cannot store these values all in one go, as they are infinite stream ==> that cannot be contained by your RAM size.... Now if you know, how min-heap works, the first answer is closest one till now
- KaLee June 01, 2016