TL; DR I need to find all ways to go through an undirected cyclic, possibly unconnected graph to find all groups of connected nodes that meet certain criteria.

There is a diagram of nodes, where each node is assigned a value and the edges represent compatible nodes.

Compatible nodes can be grouped together, but only if each node is compatible with all others. For example the following diagram, shown as adjunct list:

```
0: 2, 3
1:
2: 0, 4, 6, 7
3: 0, 4, 6, 7
4: 2, 3, 5, 6
5: 4, 7
6: 2, 3, 4, 6
7: 2, 3, 5, 6
```

Nodes {4, 5} can together form a group, as well as nodes {3, 4, 6}, while Node 1 can only form a single set of elements.

Now that each node contains a value, you will find n sets with the highest combined value. For the above example, if the nodes have the following values:

```
0: 4
1: 6
2: 4
3: 5
4: 6
5: 3
6: 3
7: 5
```

The solution for n = 2 are the sets {3, 4, 6} and {2, 7} with a combined value of 23.

The approach I've started is to use a modified version of DFS that only populates the stack with the nodes that are compatible with all the nodes in the current set. If there are no such nodes, we create a new set and continue with the nodes that have not yet been visited.

Pseudocode:

```
// As long as we have unvisited nodes
while (visit.contains (false))
{
// Take the first unattached knot and put it on a pile
stack.push (find_first (visited, wrong));
// Modified DFS
while (! stack.empty ())
{
Node n = stack.pop ();
// add the node to the set (this is always either the first or the compatible node)
temp_set.add (n);
// Find all compatible nodes by finding the intersection of the node's node score list
// already in set
compatibleNodes = find_all_compatible_nodes (set);
// Add the first compatible unvisited node we find to the stack
for (c: compatibleNodes)
{
if (! visited[c])
{
stack.push (c);
Visited[c] = true;
break;
}
}
}
// If we no longer have compatible nodes for this group, we'll add them to the candidate solution and start with a new one
CandidateSolution.add (temp_set);
temp_set.clear ();
}
// Once we have visited all the nodes, we have a candidate solution where two sets with the highest score are the possible answer
```

This works now for a single possible solution, but no different pass paths are examined. Essentially, a tree is created in which each branch represents a set and in which all chart nodes are contained. I have to find all sorts of trees that can be built to find the tree with the highest score.

I think I understand what I have to do next. Start with the last solution, go back to the point where an alternative path could have been taken, and build a new solution from there. Then continue until I return to the first knot and no more nodes to visit. But I can not deal with the implementation. Any help appreciated.