Golang DFS with very detailed comments [with runnable tests and ASCII graphs]
Inspired by: https://leetcode.com/problems/critical-connections-in-a-network/discuss/410345/Python-(98-Time-100-Memory)-clean-solution-with-explanaions-for-conf
Inspired by: https://leetcode.com/problems/critical-connections-in-a-network/discuss/410345/Python-(98-Time-100-Memory)-clean-solution-with-explanaions-for-confused-people-like-me\n\n[Runnable tests and visualized testcases here](https://play.golang.org/p/AewLt_WO2wM)\n\n```go\nfunc criticalConnections(n int, connections [][]int) [][]int {\n\tgraph := make([][]int, n)\n\n\tfor _, c := range connections {\n\t\tgraph[c[0]] = append(graph[c[0]], c[1])\n\t\tgraph[c[1]] = append(graph[c[1]], c[0])\n\t}\n\n\treturn dfs(0, -1, 0, graph, make([]*int, n))\n}\n\nfunc dfs(cur, prev, depth int, graph [][]int, visited []*int) (res [][]int) {\n\t// marking node as visited and saving depth\n\tvisited[cur] = &depth\n\n\t// go over neighbors\n\tfor _, n := range graph[cur] {\n\t\t// do not check the node where we came from\n\t\tif n == prev {\n\t\t\tcontinue\n\t\t}\n\n\t\t// if we didn\'t see this node before, go check it\n\t\tif visited[n] == nil {\n\t\t\tres = append(res, dfs(n, cur, depth+1, graph, visited)...)\n\t\t}\n\n\t\t/* if there is "smaller" depth value\n\t\t   for the neighbor, this means, that they are conected\n\t\t   in more than one path, see example:\n\n\t\t   depth:    graph:       visited depth:\n\t\t\t 0:       0              0: 0\n\t\t\t\t\t  | \\            1: 0\n\t\t\t 1:       1 - 2          2: 0\n\t\t\t\t\t  |              3: 2\n\t\t\t 2:       3\n\n\t\t\t DFS will go: 0 -> 1 -> 2\n\t\t\t\t\t\t\t   1 -> 3\n\n\t\t\tWe can see, that 0, 1, 2 has "visited depth" of "0".\n\t\t\tThis is because, we are storing the minimal depth\n\t\t\tof the neigbor, but excluding the one we came from.\n\t\t\tSo even we got to 2 from 0, but it was still able\n\t\t\tto reach 0 in different path, because it\'s "visited depth"\n\t\t\tis the same as 0 it means that they are connected in more\n\t\t\tthan one way.\n\t\t*/\n\t\tif *visited[cur] > *visited[n] {\n\t\t\tvisited[cur] = visited[n]\n\t\t}\n\n\t\t// taking into account previous comment, we know\n\t\t// that neigbors connected in more then one way has\n\t\t// the same "visited depth". So if even that after\n\t\t// exploring all nodes, we see that neigbors "visited depth"\n\t\t// is bigger than current "deepth", this MUST be the Critical Link\n\t\tif *visited[n] > depth {\n\t\t\tres = append(res, []int{cur, n})\n\t\t}\n\t}\n\n\treturn res\n}\n```
['Go']