[Python] EASY to Understand Try All Subsets with Comments
\nclass Solution(object):\n    def countSubgraphsForEachDiameter(self, n, edges):\n        """\n        :type n: int\n        :type edges: List[List[int]]\n    
```\nclass Solution(object):\n    def countSubgraphsForEachDiameter(self, n, edges):\n        """\n        :type n: int\n        :type edges: List[List[int]]\n        :rtype: List[int]\n        """\n        import collections\n        g = collections.defaultdict(set)\n\t\t# build graph\n        for e in edges:\n            g[e[0]].add(e[1])\n            g[e[1]].add(e[0])\n\t\t\n\t\t# fetch all subsets\n        def allset(s):\n            x = len(s)\n            res = []\n            for i in range(1 << x):\n                res.append(set(s[j] for j in range(x) if (i & (1 << j))))\n            return res\n\t\t\n\t\t# function to find max distance, return value is max distance from cur node to any leaf\n        def dfs(cur, d, par):\n            if not cur or cur not in d:\n                return 0\n            self.visited.add(cur)\n            res = 0\n            ds = [dfs(nei, d, cur) + 1 for nei in g[cur] if nei != cur and nei != par and nei in d]\n            if ds:\n\t\t\t\t# update return value first\n                res = max(res, max(ds))\n\t\t\t# update global max\n            self.max_d = max(self.max_d, res)\n            if len(ds) >= 2:\n                ds.sort()\n\t\t\t\t# global max could also be two child paths connected by cur node\n                self.max_d = max(self.max_d, ds[-1] + ds[-2])\n            return res\n\n        res = [0 for i in range(1, n)]\n        for d in allset([i for i in range(1, n + 1)]):\n            self.max_d = 0\n            self.visited = set()\n            for k in d:\n                self.visited.add(k)\n                dfs(k, d, None)\n                break\n\t\t\t# subset need to be connected\n            if self.max_d >= 1 and len(self.visited) == len(d):\n                res[self.max_d - 1] += 1\n        return res\n            \n```
[]