Detect cycle using BFS,DFS,Union-find and Union-find by Rank