# Find the intersection of elements of powerset one by one

**URL:** https://discourse.julialang.org/t/find-the-intersection-of-elements-of-powerset-one-by-one/72763
**Category:** Performance
**Tags:** question, performance, set
**Created:** [December 8, 2021, 11:57am UTC](https://discourse.julialang.org/t/find-the-intersection-of-elements-of-powerset-one-by-one/72763 "2021-12-08T11:57:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![XLVII](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xlvii/32/37099_2.png) [@XLVII](https://discourse.julialang.org/u/XLVII)
#### Post date: [December 8, 2021, 11:57am UTC](https://discourse.julialang.org/t/find-the-intersection-of-elements-of-powerset-one-by-one/72763/1 "2021-12-08T11:57:53Z")

</div>

```
Let S = { trigon, tetragon , pentagon , ... ,... } is a set of polygons

```

How to calculate intersection of subsets of (every element actually) `Powerset(S)`

```
Powerset(S) =[{} , {trigon} , {tetragon} , {pentagon} , {trigon, tetragon} , {trigon,pentagon} ,......]

for i in Powerset(S)
  find intersect(i)

```

I need really fast algorithm, is there any solution?  
intersection funtion is so expansive and therefore I want minimize this

Are dynamic programming solutions suitable for this?

[find every node intersection](https://i.stack.imgur.com/PQYSu.png)

## intersect( a, b,c ) = intersect( intersect ( a,b), c)

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [December 8, 2021, 2:37pm UTC](https://discourse.julialang.org/t/find-the-intersection-of-elements-of-powerset-one-by-one/72763/2 "2021-12-08T14:37:29Z")

</div>

Dynamic programming will help a lot here, but the much better method is to not do this in the first place. This operation inherently has exponential time relative to the number of polygons.

---

<div class="post-metadata">

### Author: ![XLVII](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xlvii/32/37099_2.png) [@XLVII](https://discourse.julialang.org/u/XLVII)
#### Post date: [December 8, 2021, 3:00pm UTC](https://discourse.julialang.org/t/find-the-intersection-of-elements-of-powerset-one-by-one/72763/3 "2021-12-08T15:00:32Z")

</div>

Can you say something about time complexity?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [December 8, 2021, 3:02pm UTC](https://discourse.julialang.org/t/find-the-intersection-of-elements-of-powerset-one-by-one/72763/4 "2021-12-08T15:02:57Z")

</div>

`S` is a set with `2^n` elements in it. You want to allocate an object for each of those. That’s a ton of time if n isn’t very small (less than 15 or so)
