Choosing n sets of integers that maximize the number of n-digit number permutations that satisfy an arbitrary condition

时间:2016-04-04 17:54:52

标签: python

Say you have a list of all the n-digit numbers that satisfy a certain arbitrary condition. Now, you want to choose one set of k digits for each of the n digits that could be used to produce the greatest number of n-digit numbers on your list. So the total number of permutations will be k^n.

Alternatively, instead of trying to produce the greatest possible number of permutations that satisfy the condition, how would you maximize the ratio of permutations that satisfy the condition to permutations that don't.

I've been trying to think this problem through, and I just can't think up a solution very easily that isn't a brute-force approach. What am I missing?

EDIT: here's an example of what I mean. Let n=2 and k=4, that is, I want the program to find two lists, each containing 4 digits. By combining one digit from the first list with one digit from the second list, you can create a 2 digit number. For example let list1={0,1,4,7} and list2={0,2,3,8}. The set of two digit numbers I can create with these two lists is {00,02,03,08,10,12,13,18,40,42,43,48,70,72,73,78}.

Now I have an arbitrary condition that is tested by some external function, let's say in this case it's whether the two digit number is prime. For the lists I defined above, the subset of two digit numbers that I can create that satisfy this condition is {02,03,13,43,73}.

I have two separate goals:

  1. Write a program that can determine which digits in list1 and list2 will produce the largest possible set of two digit numbers that satisfy my equation. For example, if I change list2 so it is now {1,3,7,9}, then the set of two-digit primes I can create becomes {01,03,07,11,13,17,19,41,43,47,71,73,79}. So in this case (assuming I'm right), the number is 13.

  2. Write a program that can determine which digits in list1 and list2 will produce the set of two-digit numbers with the largest ratio of elements that satisfy my condition to elements that don't. For the example with the new list2 values, the ratio is 13/16 because 09, 49, and 77 are not prime.

1 个答案:

答案 0 :(得分:0)

编辑:这个答案并不完整,但可能会引导其他人走向正确的方向。

首先,需要列举所有令人满意的值:

任何算法都必须按某种顺序检查n位数字,看它们是否满足条件。假设算法测试数字n1,n2,n3,...

然后你可以想象一个任意条件,只适用于非常接近枚举结束的数字。换句话说,对于任何算法,我们总是可以提出一个任意条件,算法在时间或准确性方面表现得非常差。

Maximum Density Subgraph问题的关系:

这个问题相当于在多部分图中找到最大密度子图。

我们可以将问题实例转换为多部分图,如下所示。图中的每个独立集对应于小数位,并且每个顶点对应于该小数位中的可能值。例如,当n = 2时,第一个独立集合将是0_,1_,2_,...并且第二个独立集合将是_0,_1,_2,...,....边缘对应于令人满意的值。例如,如果所有素数都满足,我们将有边(0_,_ 2),(0_,_ 3),...,(1 _,_ 1)......等。

现在为n个小数位中的每一个选择k个数字等同于从图的每个部分中选择k个节点。我们希望进行此选择以最大化最终图形中保留的边数,或最大化边缘到非边缘的分数。这与尝试在所描述的约束下找到具有最大密度的子图相同。

如链接论文所述,最大密度子图是NP-Hard 。由于图表上的结构限制以及每个部件限制为10个节点,这个问题可能会稍微容易一些,但我不确定。 我觉得这个问题也可能是NP-Hard。