One way to solve the problem 411 in clojure

Post on 17-Aug-2015

142 views 0 download

Tags:

Transcript of One way to solve the problem 411 in clojure

clj-syd pep-411"It was nice to meet you at the first clj-syd hack night"

clj-syd pep-411"It was nice to meet you at the first clj-syd hack night"

aka "One way to solve the Problem 411 in Clojure"

(hack-night (nth clj-syd 10))

Problem 411

Problem 411 outline

Generate2D Grid

of Stations

Calculatelongest

uphill path

Problem 411 longest uphill path

longestpath = 5

Problem 411 longest uphill path

n x n

Problem 411 longest uphill path

n=22

Problem 411 station generation

(x, y) = (2i mod n, 3i mod n)i = [0..2n]

Problem 411 station generation

n = 2244 stations (2n)11 distinct stations

Problem 411 30 instances

k = [1..30] n = k5

Problem 411 30 instances

k = [1..30] n = k5

Σ solve(n)

Problem 411 30 instances

k = [1..30] n = k5

Σ solve(n)

Problem 411 n = k5

Dynamic Programming Approach

Naïve Algorithm generate stations

basei mod n(Modular Exponentiation)

Naïve Algorithm generate stations

(x, y) = (2i mod n, 3i mod n)i = [0..2n]

Naïve Algorithm generate stations

Naïve Algorithm generate stations

Naïve Algorithm dynamic programming

Naïve Algorithm dynamic programming

max

Naïve Algorithm dynamic programming

1+max*subproblem

Naïve Algorithm dynamic programming

1+max*subproblem

Naïve Algorithm dynamic programming

0

Naïve Algorithm correct (but slow)

Naïve Algorithm correct (but slow)

Naïve Algorithm correct (but slow)

Improvements:Station Generation

Station Generation avoid repetitions

Station Generation avoid repetitions

Station Generation avoid repetitions

Station Generation avoid repetitions

Station Generation improve modpow

Station Generation improve modpow

Improvements:Station Generation

Improvements:Station Generation

Data Structure

QuadTree

QuadTree

Point QuadTree

*QuadTree

SortedMap ofSortedSets DS

SortedMap of SortedSet group by Y coord

SortedMap of SortedSet group by Y coord

y=1

SortedMap of SortedSet group by Y coord

y=3

SortedMap of SortedSet group by Y coord

y=15

SortedMap of SortedSet group by Y coord

SortedMap of SortedSet group by Y coord

SortedMap of SortedSet group by Y coord

SortedMap of SortedSet group by Y coord

SortedMap of SortedSet group by Y coord

SortedMap of SortedSet group by Y coord

SortedMap ofSortedSets DS

Now what ?!?!?

SortedMap of SortedSet now what?!?!

same problem but 1D only... any leverage?

SortedMap of SortedSet Patience Sorting

SortedMap of SortedSet Patience Sorting

SortedMap of SortedSet Patience Sorting

SortedMap of SortedSet now what?!?!

.....

SortedMap of SortedSet now what?!?!

SortedMap of SortedSet now what?!?!

WTF ??!?!?!

SortedMap of SortedSet Patience Sorting

SortedMap of SortedSet Patience Sorting

SortedMap of SortedSet Patience Sorting

SortedMap of SortedSet Patience Sorting

SortedMap of SortedSet Patience Sorting

SortedMap of SortedSet Patience Sorting

SortedMap of SortedSet Patience Sorting

SortedMap of SortedSet Patience Sorting

SortedMap of SortedSet Patience Sorting

[1 3 5 8 10][1 4 5 8 10][1 4 6 8 10]

...

SortedMap ofSortedSets DS

Patience Algorithm ?

SortedMap of SortedSet Patience

SortedMap of SortedSet Patience

3

SortedMap of SortedSet Patience

4

SortedMap of SortedSet Patience Sorting

SortedMap of SortedSet Patience

4

[1 10 12] --> [1 2 12] --> [1 2 12 20]

SortedMap of SortedSet Patience

4

[1 10 12] --> [1 2 12] --> [1 2 12 20]

SortedMap of SortedSet Patience

4

[1 2 12 20] --> [1 2 8 20] --> [1 2 8 14]

SortedMap of SortedSet Patience

4

[1 2 8 14] --> [1 2 4 14] --> [ ? ? ? ]

SortedMap of SortedSet Patience

5

[1 2 8 14] --> [1 2 4 14] --> [1 2 4 14 18]

SortedMap of SortedSet Patience

5

[1 2 8 14] --> [1 2 4 14] --> [1 2 4 14 18]

SortedMap of SortedSet Patience

5

[1 2 4 14 18] --> [1 2 4 6 18] --> [1 2 4 6 16]

Patience AlgorithmPython to the Rescue

Patience AlgorithmPython to the Rescue

SortedMap of SortedSet Py to the rescue

SortedMap of SortedSet Py to the rescue

SortedMap of SortedSet Py to the rescue

SortedMap of SortedSet Py to the rescue

SortedMap of SortedSet Py to the rescue

SortedMap of SortedSet Py to the rescue

http://hg.python.org/cpython/file/2.7/Lib/bisect.py

SortedMap of SortedSet Py to the rescue

SortedMap of SortedSet Py to the rescue

SortedMap of SortedSet Py to the rescue

SortedMap of SortedSet Py to the rescue

SortedMap of SortedSet Py to the rescue

SortedMap of SortedSet Py to the rescue

SortedMap of SortedSet Py to the rescue

LIS in 1D is easy...What about our 2D DS?

SortedMap of SortedSet LIS on our DS

[1 2 4 6 16]

SortedMap of SortedSet LIS on our DS

[1 2 4 6 16]

SortedMap of SortedSet LIS on our DS

[1 2 4 6 16]

SortedMap of SortedSet LIS on our DS

[1 2 4 6 16]

SortedMap of SortedSet LIS on our DS

[1 2 4 6 16]

DS as a SortedSet

DS as a SortedSet Full Scale?

k=30 --> n=305 --> 24,300,000

DS as a SortedSet Full Scale?

k=30 --> n=305 --> 24,300,000

Everything --> "mod n"

DS as a SortedSet Full Scale?

k=30 --> n=305 --> 24,300,000

DS as a SortedSet XY combined

DS as a SortedSet XY combined

DS as a SortedSet XY combined

DS as a SortedSet Stations-Y

Here be Dragons

Here be Dragons

Optimisations innermost loops

"If a tree falls in a forest and no one is around to hear it, does it make a sound?"

Optimisations innermost loops

map - pmap - r/map

*map you multicore, uh?

map

pmap

r/map

*map you multicore, uh?

*map you multicore, uh?

what's next?

What's next? chunked-seq

http://goo.gl/Peym4

What's next? chunked-seq

What's next? 1) measure 2) optimise

What's next? 1) measure 2) optimise

Questions?

Twitter @filippovitale BitBucket: filippovitale

GitHub: filippovitale

Thank you!

Twitter @filippovitale BitBucket / GitHub: filippovitale

f in