Java - subsets with bit masking - diameters with DFS
```\nclass Solution {\n    \n    private boolean[] beingVisited;\n    private List> graph;\n    private Set> subsets = new HashSet<>();\n    \n    public int[] 
```\nclass Solution {\n    \n    private boolean[] beingVisited;\n    private List<Set<Integer>> graph;\n    private Set<Set<Integer>> subsets = new HashSet<>();\n    \n    public int[] countSubgraphsForEachDiameter(int n, int[][] edges) {\n        this.beingVisited = new boolean[n];\n        int[] counts = new int[n-1];\n        counts[0] = edges.length;\n        \n        initializeGraph(n, edges);\n        initializeSubsets(n);\n        \n        for(Set<Integer> subset : this.subsets) {\n            //find diameter of graph formed by vertices in subset\n            counts[diameter(subset)-1]++;\n        }\n        return counts;\n    }\n\n    private void initializeSubsets(int n) {\n        for(int i = (1 << n); i < (1 << n + 1); i++) {\n            String bitmask = Integer.toBinaryString(i).substring(1); \n            \n            Set<Integer> subset = new HashSet<>();\n            for(int j=0; j<bitmask.length(); j++) {\n                if(bitmask.charAt(j)==\'1\'){\n                    subset.add(j+1);\n                }\n            }\n\n            if(subset.size() <= 2) continue;\n            \n            //check validity of subset - vertices must have n-1 edges among them\n            int edgeCount = 0;\n            for(Integer node : subset) {\n                for(int adjacent : graph.get(node-1)) {\n                    if(subset.contains(adjacent)) {\n                        edgeCount++;\n                    }\n                }\n            }\n            \n            if((edgeCount/2) >= subset.size() - 1) {\n                this.subsets.add(subset);\n            }  \n        }\n    }\n    \n    private int diameter(Set<Integer> subset) {\n        //find farthest vertex from any vertex\n        FarthestNode farthestNode = farthestNode(subset.iterator().next(), subset, 0);\n        //get diameter of path from vertex found in previous step\n        return farthestNode(farthestNode.node, subset, 0).distance;\n    }\n    \n    private FarthestNode farthestNode(int node, Set<Integer> subset, int distance) {\n        beingVisited[node-1] = true;\n        FarthestNode farthestNode = new FarthestNode(node, distance);\n        \n        for(Integer adjacent : graph.get(node-1)) {\n            \n            if(!beingVisited[adjacent-1] && subset.contains(adjacent)) {\n                \n                FarthestNode otherFarthestNode = farthestNode(adjacent, subset, distance + 1);\n                \n                if(otherFarthestNode.distance > farthestNode.distance) {\n                    farthestNode = otherFarthestNode;\n                }\n            }\n        }\n        beingVisited[node-1] = false;\n        return farthestNode;\n    }\n    \n    private void initializeGraph(int n, int[][] edges) {\n        this.graph = new ArrayList<>();\n        for(int i=0; i<n; i++){\n            graph.add(new HashSet<>());\n        }\n        for(int[] edge : edges) {\n            graph.get(edge[0]-1).add(edge[1]);\n            graph.get(edge[1]-1).add(edge[0]);\n        }\n    }\n    \n    private static class FarthestNode {\n        int node;\n        int distance;\n        FarthestNode(int node, int distance) {\n            this.node = node;\n            this.distance = distance;\n        }\n    }\n}
[]