Data Structures and Algorithms with Elixir

Awesome - I shall look forward to it :003:

3 Likes

Understood Lavenshtein Distance, going to implement it now. My gut feeling is that with pattern matching, this algorithm will be a breeze to reason with through Elixir.

3 Likes

Hi @mafinar :wave: hope you’re ok, asking your advice with the repo: as a complete beginner, where should i begin with? im seeing:

ex_algo/
├── list/
├── number/
├── queue/
├── set/
├── sort/
├── stack/
└── tree/

massive thanks! :pray: :pray: :pray:

3 Likes

Hello @jaeyson thanks for asking, I’m doing great. Hope you are too.

tl;dr All are stand-alone so you can just click on any link from README and start, but I’ll start with sort and search the terms on Wikipedia and mentally map the pseudocode/data-flow with the Elixir code (following test cases to double check my understanding).

And beyond that tl;dr…

Interesting questions. So far, every algorithm is stand-alone (i.e. no module aliases another, yet.)

So normally, when I was introduced with data structures and algorithms for the first time, I started with searching and sorting, Stack, then Queue and then LinkedList before all hell breaking lose. And when LinkedList, I meant linear linked list not double, circular, xor etc.

So I’d suggest maybe start with sort/ then stack/ and then list/? queue is interesting because of the way it deals with FIFO with zipper.

So normally, a data structure implemented here should first be looked at their respective .ex file, there are doctests to follow, but you can also click on the README links for tests. Some of the tests have Property Tests to give you a “meta” understanding of that data structure. For example, “if I have list of integer and I push them all into a stack and then pop them all out to another list, then they will be inverse of each other” is a statement that is true to any stack. Those tests also give some idea.

I DO have plans to add external links to these data structures so that once you’re looking into say, Catalan Numbers (inside number/) then you can go through all the links that explain it, and also an independent README inside all folders for a detailed discussion of it. But I kept that for future since I want to cover some algorithms first.

Please let me know if this helped, and feel free to suggest more properties or clarification and bugs when you encounter :slight_smile:

4 Likes

super helpful and greatly appreciated :pray: :pray: :pray:, i’ll follow your advice!

3 Likes

Today it was Binary Search. Unfortunately I used Elixir lists for binary search for today’s version, therefore making this an O(n) attempt, equal or worse than linear search. I will try out an array version later, even that’s not O(1) (Nothing is in immutable world?). This is just for showing the mechanism of binary search without any of its benefits gained in a language with real arrays.

Here’s the link: Binary Search

4 Likes

Going to do a deep dive on the internals of Vector as implemented here: GitHub - sabiwara/aja: Extension of the Elixir standard library focused on data stuctures, data manipulation and performance this offers so much learning opportunities for me :smiley:

3 Likes

I’ve read this “Discrete and combinatorial mathematics” at the beginning of this year, hard reading, very hard)

3 Likes

I don’t feel the challenge perhaps because i took two courses where i consulted this book (at one it was the main textbook) in the university and the instructor was amazing.

I love that book and that topic. Brings back a lot of fond memories.

2 Likes

Took me longer than usual to implement this one. I could do this quicker with say, Dart. I find it difficult to elegantly implement 2D data structures with Elixir.

So here is the initial implementation of subset sum algorithm using dynamic programming. This one only says True/False as to whether a subset exists in a list that sums to a target as an example, given [1, 2, 3, 4] and target being 5 it will be true as 5 can be computed by sums of [2, 3] and [4, 1]. However, given [1, 3, 5], target of 7 should be false as no subset exists that would sum to 7

Here’s the initial implementation: ex_algo/subset_sum.ex at main · code-shoily/ex_algo · GitHub

I will attempt at a function that returns all the subsets that sum up to said target tomorrow (or tonight if I don’t get sleepy). Backtracking should be fun!

Also, this was supposed to be part of my Advent of Code reconnection through solving Year 2015/24 but felt like moving all algorithm related things into this repository instead.

2 Likes