Find the maximum number of valid combinations for elements from two arrays

There are two arrays of length n ( a and b ) consisting of integers> 2.

At each turn, I want to remove an integer from each array ( a[i] and b[j] ), given that a certain condition about them is true (for example, that they are not compatible). (If the condition is wrong, I will try to remove another combination)

In the end, I want to find the maximum number of turns, I can achieve this (until there is a possible combination for deletion that matches the condition). Let me call this the optimal number of turns.

I tried to solve this using a search algorithm and PriorityQueue using Python:

 def search(n, a, b): q = queue.PriorityQueue() encountered = set() encountered.add((tuple(a), tuple(b))) q.put((number_of_coprime_combinations(a, b), a, b)) while q: cost, a, b = q.get() combs = not_coprime_combinations(a, b) if not combs: return n - len(a) for a, b in combs: if not (tuple(a), tuple(b)) in encountered: q.put((number_of_coprime_combinations(a, b), a, b)) encountered.add((tuple(a), tuple(b))) 

number_of_coprime_combinations(a, b) returns the number of possible co-prime combinations given by arrays a and b . This is used as the cost of a given state of two arrays.

 def number_of_coprime_combinations(a, b): n = 0 for idx_a, x in enumerate(a): for idx_b, y in enumerate(b): if is_coprime(x, y): n += 1 return n 

not_coprime_combinations(a, b) returns a list of possible states where the combination of incompatibilities has been removed from a and b :

 def not_coprime_combinations(a, b): l = [] for idx_a, x in enumerate(a): for idx_b, y in enumerate(b): if not is_coprime(x, y): u, v = a[:], b[:] del(u[idx_a]) del(v[idx_b]) l.append((u, v)) return l >>> not_coprime_combinations([2,3],[5,6]) [([3], [5]), ([2], [5])] 

The problem is that this solution is extremely inefficient for large arrays of large integers. Therefore, I am wondering if there is a better solution to this problem.

Example:

 n = 4 a = [2, 5, 6, 7] b = [4, 9, 10, 12] 

Can be deleted:

 (2, 4) (5, 10) (6, 9) 

This will lead to an optimal solution:

 a = [7] b = [12] 

But if you delete:

 (6, 12) (2, 10) 

could go to a non-optimal solution:

 a = [5, 7] b = [4, 9] 

The algorithm should always find the optimal number of revolutions (in this example 3).

+4
source share
3 answers

As far as I can tell, to solve this problem:

  • We construct a bipartite graph G such that for each Ai and Bj, if GCD (Ai, Bj)> 1, G has an edge (Ai, Bj).

  • Find the maximum match for G

  • Power matching is the solution.

I do not see how this could be solved faster.

+3
source

I know where you got this problem. And the solution to this problem is wrong, because its O (n ^ 2) and greedy. n <= 10 x 5.2> a, b <10 ^ 9 from the array

I think you should find some trick in this problem. And the whole algorithm for maximum matches in bipartite graphs will be TL.

+1
source

Assuming that:

  • The is_coprime_pair(pair) function is defined, takes a list of length 2, and returns True for a pair of primes
  • a and b are iterable to combine
  import itertools  
     not_coprimes = itertools.filterfalse (is_coprime_pair, itertools.product (a, b))

not_coprimes will contain all pairs that do not contain two primes

0
source

Source: https://habr.com/ru/post/1490281/


All Articles