Alex Rivera | Logout

Finding maximum number k such that for all combinations of k pairs, we have k different elements in each combination

Asked 2012-05-10T12:41:50.487
13

We are given N pairs. Each pair contains two numbers. We have to find maximum number K such that if we take any combination of J (1<=J<=K) pairs from the given N pairs, we have at least J different numbers in all those selected J pairs. We can have more than one pair same.

For example, consider the pairs (1,2) (1,2) (1,2) (7,8) (9,10) For this case K = 2, because for K > 2, if we select three pairs of (1,2), we have only two different numbers i.e 1 and 2.

Checking for each possible combination starting from one will take a very large amount of time. What would be an efficient algorithm for solving the problem?

Edit
Report

1 Answer

0

Create a graph with one vertex for each number and one edge for each pair.

If this graph is a chain or a tree, we have the number of "numbers", equal to number of "pairs" plus one, After removing any number of edges from this graph, we never get less vertexes than edges.

Now add a single cycle to this chain/tree. There is equal number of vertexes and edges. After removing any number of edges from this graph, again we never get less vertexes than edges.

Now add any number of disconnected components, each should not contain more than one cycle. Once again, we never get less vertexes than edges after removing any number of edges.

Now add a second cycle to any of disconnected components. After removing all other components. at last we have more edges than vertexes (more pairs than numbers).

All this leads to the conclusion that K+1 is exactly the number of edges in the smallest possible subgraph, consisting of two cycles and, possibly, a chain, connecting these cycles.

Algorithm:

For each connected component, find the shortest cycle going through every node with Floyd-Warshall algorithm.

Then for each non-overlapping pair of cycles (in single component), use Dijkstra’s algorithm, starting from any node with at least 3 edges in one cycle, to find shortest path to other cycle; and compute a sum of lengths of both cycles and a shortest path, connecting them. For each overlapping pair of cycles, just compute the number of their edges.

Now find the minimum length of all these subgraphs. And subtract 1.

The above algorithm computes K if there is at least one double-cycle component in the graph. If there are no such components, K = N.

answered 2012-05-10T18:24:51.480

Your Answer