# Data Structures and Algorithms with Elixir

**URL:** <https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685>\
**Category:** Backend Chat/Discussions\
**Tags:** algorithms, elixir, data-structures\
**Created:** [26 November 2021 23:21 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685 "2021-11-26T23:21:33Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [10 March 2022 04:21 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/22 "2022-03-10T04:21:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jaeyson](https://forum.devtalk.com/user_avatar/forum.devtalk.com/jaeyson/32/6395_2.png) [@jaeyson](https://forum.devtalk.com/u/jaeyson)\
**Post date:** [10 March 2022 09:32 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/23 "2022-03-10T09:32:49Z")

</div>

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

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

```

massive thanks! 🙏 🙏 🙏

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [10 March 2022 13:43 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/24 "2022-03-10T13:43:29Z")

</div>

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

> [@jaeyson](#):
>
> vice with the repo: as a complete beginner, where should i begin with? im seeing:

**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 🙂

---

<div class="post-metadata">

**Author:** ![jaeyson](https://forum.devtalk.com/user_avatar/forum.devtalk.com/jaeyson/32/6395_2.png) [@jaeyson](https://forum.devtalk.com/u/jaeyson)\
**Post date:** [11 March 2022 06:56 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/25 "2022-03-11T06:56:03Z")

</div>

> [@mafinar](#):
>
> Please let me know if this helped

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

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [11 March 2022 07:20 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/26 "2022-03-11T07:20:49Z")

</div>

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](https://github.com/code-shoily/ex_algo/blob/main/lib/ex_algo/search/binary_search.ex)

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [29 March 2022 00:20 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/27 "2022-03-29T00:20:09Z")

</div>

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](https://github.com/sabiwara/aja) this offers so much learning opportunities for me 😃

---

<div class="post-metadata">

**Author:** ![Jack234](https://forum.devtalk.com/user_avatar/forum.devtalk.com/jack234/32/4118_2.png) [@Jack234](https://forum.devtalk.com/u/Jack234)\
**Post date:** [20 April 2022 16:54 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/28 "2022-04-20T16:54:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [21 April 2022 00:06 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/29 "2022-04-21T00:06:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [11 November 2022 06:58 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/30 "2022-11-11T06:58:51Z")

</div>

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](https://github.com/code-shoily/ex_algo/blob/main/lib/ex_algo/dynamic_programming/subset_sum.ex)

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.

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [1 June 2024 18:07 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/31 "2024-06-01T18:07:17Z")

</div>

Coming back to this one! Starting with Graph algorithms.

---

<div class="post-metadata">

**Author:** ![AntonRich](https://forum.devtalk.com/user_avatar/forum.devtalk.com/antonrich/32/3165_2.png) [@AntonRich](https://forum.devtalk.com/u/AntonRich)\
**Post date:** [13 November 2024 09:59 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/32 "2024-11-13T09:59:23Z")

</div>

I feel like it’s hard for me to dive into algorithms with Elixir right off the bat, because I no prior experience with algorithms in imperative languages.

Would you say that I can dive straight into algorithms with Elixir or should I learn them with some imperative language first?

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [14 November 2024 02:14 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/33 "2024-11-14T02:14:00Z")

</div>

Not all “classic” anlgorithms are well implementable with Elixir. Circular LinkedList or Doubly Linked List would be hard to implement if you go for the text book implementation. You can have bidirectionally traverseable data structure but it will likely be with Lenses. Binary Search won’t look idiomatic as you would be using with Erlang arrays.

A good idea could be to look up references on Purely Functional Data Structures and use Elixir for those.

[Previous page](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685.md?page=1)
