Given a directed graph, the goal is to combine the node with the nodes it is pointing to and come up with minimum number of these [lets give the name] super nodes.
The catch is once you combine the nodes you can’t use those nodes again. [first node as well as all the combined nodes – that is all the members of one super node]
The greedy approach would be to pick the node with maximum out degree and combine that node with nodes it is pointing to and remove all of them. Do this every time with the nodes which are not removed yet from graph.
The greedy is O(V), but this won’t necessarily output minimum number super nodes.
So what is the best algorithm to do this?
20! is rather large, larger than 2^61. Fortunately there’s a better way to solve small instances: (EDIT) dynamic programming. By saving the optimal solutions to each subproblem, we pay some memory to obtain a very large savings in time.
Here’s some sample code in Python. In implementing the code below in another language, you’ll probably want to number the vertices 0, …, n-1 and implement the sets as bit vectors.
This problem has an NP-hardness reduction from set cover that preserves the objective value. This means that unless P = NP, the best guarantee you can obtain for a polynomial-time algorithm is that it always outputs a solution that is at most
O(log n)times larger than optimal.If
x1, ..., xmare the elements to be covered andS1, ..., Snare the sets, then the goal of set cover is to choose the minimum number of sets whose union is{x1, ..., xm}. Assuming that each element appears in at least one set, make a graph with nodesx1, ..., xm, S1, ..., Sn, Rwhere there are arcs fromRto allSiand for alli, j, an arc fromSitoxjif and only ifxjbelongs toSi. There’s a straightforward correspondence between node closures and set covers: to obtain a node closure from a set cover, remove the vertices corresponding to the sets chosen and thenR; to obtain a set cover from a node closure, take all sets whose vertices were chosen plus sets containing eachxjwhose vertex was chosen.(Note, for set cover, the greedy algorithm achieves the optimal approximation ratio! Something similar might be true for your problem.)