Amazon Interview Question
Software Engineer / DevelopersTortoise-hair is the best for finding loops in linked lists.
But for Graphs, i think we should do a BFS & check whether the visited node is visited again.
using dfs.. check if the graph has any strongly connected components. If it does, it has a cycle..
we can use a modification of the kruskal's algorithm to find the minimum spanning tree.
We can set every node in the graph as an independent set and sort the edges
for every edge in the list we check if both the ends are in the separate set, if u=yes then we merge
If we encounter an edge whose both ends are already in the same end, then we found the cycle.
For set operations we can use a Union-find data structure
tortoise and hare algorithm to detect circle.
- pansophism December 13, 2010then go reverse to find the beginning node.