Simple Commented Javascript DFS Solution
Intuition\nWe are performing cycle prevention not cycle detection\n\n# Approach\nFor each edge [a,b] that is addded to the graph / adjacency list\nPerform a DFS
# Intuition\nWe are performing **cycle prevention** not cycle detection\n\n# Approach\nFor each edge `[a,b]` that is addded to the graph / adjacency list\nPerform a DFS to check if you can traverse from `b` to `a`\n\n# Complexity\nwhere e is the number of edges\n- Time complexity:\n$$O(e^2)$$ -->  For each edge that is traversed in the graph\n\n- Space complexity:\n$$O(e)$$ --> The adjacency list contains all the edges and the DFS call stack will traverse each edge\n\n# Code\n```\n/**\n * @param {number[][]} edges\n * @return {number[]}\n\n Intuition to solve this problem is that we need to build the graph and then check if there is a cycle\n */\nvar findRedundantConnection = function(edges) {\n\n    const adjacencyList = {}\n    const dfs = (node, target, prev) =>{\n        if (node === target) return true \n        for (let subnode of adjacencyList[node]){\n            if (subnode !== prev && dfs(subnode, target, node)) return true\n        }\n        return false\n    }\n\n    //  Build graph one edge at a time\n    for (let edge of edges){\n        const [a,b] = edge\n        if (!adjacencyList[a]) adjacencyList[a] = []\n        if (!adjacencyList[b]) adjacencyList[b] = []\n\n        adjacencyList[a].push(b)\n        adjacencyList[b].push(a)\n\n        //  Traverse the graph and check for a cycle for each new edge\n        if (dfs(b,a,a)) return [a,b]\n    }\n};\n```
['Depth-First Search', 'Graph', 'JavaScript']