Get all possible combinations of 3 values from n possible elements


Vektor88

I'm trying to get a list of all possible combinations of 3 elements from a list of 30 items. I tried using the following code but it fails to throw OutOfMemoryError. Is there a more efficient alternative than this?

val items = sqlContext.table(SOURCE_DB + "." + SOURCE_TABLE).
  select("item_id").distinct.cache

val items.take(1) // Compute cache before join

val itemCombinations = items.select($"item_id".alias("id_A")).
  join(
    items.select($"item_id".alias("id_B")), $"id_A".lt($"id_B")).
    join(
      items.select($"item_id".alias("id_C")), $"id_B".lt($"id_C"))
Masger

This approach seems to work, but may incur considerable overhead at the query execution level. Assuming n is a fairly small number, we can do this directly using the Scala implementation:

val localItems = items.collect
val combinations = localItems.combinations(3)

The result is iteratorthat one element can be consumed at a time without significant memory overhead.

Spark version (edited)

Given the desire to make this version of Spark, the query planner can be avoided (assuming there is a problem) by dropping down to the RDD level. This is basically the same as the join in the question:

val items = sqlContext.table(SOURCE_DB + "." + SOURCE_TABLE).select("item_id").rdd

val combinations = items.cartesian(items).filter{case(x,y) => x<y}.cartesian(items).filter{case ((x,y),z) => y<z}

Run the equivalent code on your local machine:

val data = List.fill(1000)(scala.util.Random.nextInt(999))
val rdd = sparkContext.parallelize(data)
val combinations = rdd.cartesian(rdd).filter{case(x,y) => x<y}.cartesian(rdd).filter{case ((x,y),z) => y<z}

combinations.count
// res5: Long = 165623528

Related


How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations in 3 different sets

Selkuk I'm trying to write a python program that iterates over a list of all possible numbers for 3 different sets and prints out all possible numbers. The numerical range is given at the bottom. No 2 sets of numbers should be repeated, each order should be di

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

get all possible combinations of k elements from a list

Tobias Herman I need a function that does the same thing as itertools.combinations(iterable, r) in python So far I came up with this: {-| forward application -} x -: f = f x infixl 0 -: {-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -} combinatio

Get all possible combinations of k elements from a list

Tobias Herman I need a function that does the same thing as in pythonitertools.combinations(iterable, r) So far I have come up with this: {-| forward application -} x -: f = f x infixl 0 -: {-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -} combin

Get all possible combinations in 3 different sets

Selkuk I'm trying to write a python program that iterates through a list of all possible numbers for 3 different sets and prints out all possible numbers. The numerical range is given at the bottom. No 2 sets of numbers should be repeated, each order should be

Get all possible combinations of values in a list - Python

Luca F. I have a list ['a', 'bill', 'smith']and I want to write a python code to get all possible combinations of applying a specific criterion. To be more precise, I want to get the combination of these three elements in the list plus the first letter of each

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

Get all possible combinations in 3 different sets

Selkuk I'm trying to write a python program that iterates over a list of all possible numbers for 3 different sets and prints out all possible numbers. The numerical range is given at the bottom. No 2 sets of numbers should be repeated, each order should be di

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of values

george here I want to write a code to show all possibilities of output. The code below shows a combination with a variable with no repeats, so for an iteration of 2, it's not included ('yes', 'yes') or ('no', 'no'). I would like to add repeatable values to the

Get all possible combinations from 3 categories in MySQL

Erik Andershed I have 3 categories (cate1, cate2, cate3) and there are 14 rows in the database. I like to get all possible combinations. database: id category 1 cate1 2 cate2 3 cate3 6 cate1 7 cate2 8 cate3 9 cate1 10 cate2 11 cat