✅✅ Cycle detection || C++ solution || Easy to understand 🔥🔥| DFS
Intuition\nCheck if adding the edge in a graph is  causing to form a cycle.\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Crea
# Intuition\nCheck if adding the edge in a graph is  causing to form a cycle.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Create adjcency list for given graph\n2. Start adding edges and every time check its forming a cycle after addition of that edge.\n3. if yes then return that edge.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n * (V + E))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g.  -->\n\n# Code\n```\nclass Solution {\npublic:\n    bool dfs(int src, int parent, vector<int> adj[], vector<bool>& vis) {\n        vis[src] = true;\n        for (auto adjnode : adj[src]) {\n            if (!vis[adjnode]) {\n                if (dfs(adjnode, src, adj, vis))\n                    return true;\n            } else if (adjnode != parent)\n                return true;\n        }\n        return false;\n    }\n\n    vector<int> findRedundantConnection(vector<vector<int>>& edges) {\n        int n = edges.size();\n        vector<int> adj[n + 1];\n        for (int i = 0; i < n; i++) {\n            adj[edges[i][0]].push_back(edges[i][1]);\n            adj[edges[i][1]].push_back(edges[i][0]);\n            vector<bool> vis(n + 1, false);\n            if (dfs(edges[i][0], -1, adj, vis)) {\n                return edges[i];\n            }\n        }\n        return {};\n    }\n};\n```\n![leet_cat.png](https://assets.leetcode.com/users/images/6bfdbc5f-61bd-4739-a551-cfd78642d04b_1707335784.3728864.png)\n\n
['Depth-First Search', 'Breadth-First Search', 'C++']