
Algorithms and Data Structures Explained and Implemented in JavaScript - deckermann
https://github.com/trekhleb/javascript-algorithms
======
eximius
The documentation and code quality is all good.

The implementation choices leave some things to be desired. The Queue and
Stack implementations are Linked Lists instead of array backed, the hash table
is closed instead of (the only barely more complicated) open Robin hood hash
table scheme, the union-find/disjoint-set implementation doesn't have path
compression or rank unions.

Overall very good, but it could be Great (tm) with just a little bit of work.

~~~
pitaj
Wait are linked lists not better for first-in first-out data structures? I
thought arrays were not preferred in those cases because shift/unshift are
highly inefficient.

Am I wrong? I thought array-based queues resulted in O(n) queue/dequeue. Now
for stacks, which are last-in first-out, it makes sense to use an array.

~~~
deathanatos
> _I thought array-based queues resulted in O(n) queue /dequeue._

Not necessarily. A circular buffer[1] can be used as a queue with O(1)
queue/dequeue. C++ implementations (gcc?), IIRC, uses an interesting array-of-
arrays approach; it also has O(1) queue/dequeue. I'm not sure why the array-
of-arrays approach is better than a circular buffer, though.

Array based designs can result is less allocations, and maybe less overhead.
For example, if you have a circular buffer with space for 16 items, it only
needs to allocate space if you need more room, whereas a linked list queue
would allocate for each and every item placed into it. Linked lists also
require space for the pointer to the next link, for each link in the list.
(And, if you keep them, back pointers, though these aren't necessary for just
a queue.) Arrays might have some unused slack space, however.

[1]:
[https://en.wikipedia.org/wiki/Circular_buffer](https://en.wikipedia.org/wiki/Circular_buffer)

~~~
jayd16
Resizing a fully contiguous circle buffer would cause a copy every element as
well forcing you to make a single contiguous memory section. Array of arrays
just needs to resize the top level array.

~~~
manwe150
But the copy only has to move n items, but was constructed with m items, where
n < m (and usually n << m). Where n is the max size of the queue and m is the
total number of items that will ever be enqueued.

On the other hand, an array of arrays (no recursion) doesn’t change the big-O
complexity cost, just the constant multiplier. That should definitely improve
performance of the uncommon operation (the copy), but hypothetically might
slow down the actual queuing operations (and drastically reduce throughout)

But maybe you meant the array of arrays to be recursive? That seems like it
would alter the big-O (from n+m to log(n)+m). But typically m>>n, so the net
result is the same.

------
teachrdan
This repo is beautiful. The README is detailed and clear, and the contents
seem pretty exhaustive. As a native JS guy I'm especially excited to check out
the implementation of non-tree graphs!

Having said all that: The algorithms I've had to tackle at work tend to be
fairly trivial or total one-offs involving scheduling events in human time.

How much algorithmic work do you other JS folks end up doing?

~~~
chatmasta
> How much algorithmic work do you other JS folks end up doing?

I recently built a questionnaire/form editor/builder which relies heavily on
tree structures. I ended up writing a lot of tree walking code. It was the
first time I've ever needed to implement interview-like questions in an actual
project.

I make heavy use of functional programming in Javascript, which lends itself
to really clean implementations of algorithms like tree walking, e.g.:
[https://hastebin.com/qapawepavi.js](https://hastebin.com/qapawepavi.js)

~~~
Scarbutt
As someone new to JS I find vanilla JS tedious for FP, what library(ies) do
you use to ease this?

~~~
chatmasta
I have to admit I’m far from a FP guru, very little experience with true
functional languages like Haskell etc. For me it’s more a question of style
and applying functional paradigms when possible, eg treating functions as
first class citizens, making heavy use of .bind() and passing functions as
arguments. I use ES6 via Babel and actually find it to be quite expressive and
powerful when used correctly. I just try to keep my functions very small with
tight separation of concerns. There are lots of libraries to make JS “more
functional,” as the sibling comment alludes to, but I find vanilla JS (or at
least ES6) already has many of the features necessary for functional
programming, as long as you make the conscious choice to use it that way.

~~~
crooked-v
It requires use of Babel, but you might find the function bind operator an
interesting alternative to .bind().

[https://babeljs.io/docs/plugins/transform-function-
bind/](https://babeljs.io/docs/plugins/transform-function-bind/)

~~~
chatmasta
That’s very cool! But I’d be hesitant to introduce something non-standard into
the codebase. Sure the toolchain can handle it but I’d be worried other devs
won’t immediately recognize the syntax.

------
superkarolis
Had the same idea and got sad you got there faster. But then I looked at the
code and realized you did it better than I would have done it so good job!

~~~
Rapzid
Me too! I wanted to refresh myself on implementations and expose the JS
community to some stuff they may not have been exposed to... TypeScript is
still fair game!

------
chatmasta
I like it! Something I've been thinking about recently is using ES6 proxies to
visualize algorithms like this. Lay out the arrays and objects as blocks on a
canvas, and highlight the blocks as the algorithm reads / writes to the
corresponding slots of the arrays / objects.

~~~
pjmlp
This kind of visualizations?

[https://visualgo.net/en](https://visualgo.net/en)

[https://www.cs.usfca.edu/~galles/visualization/Algorithms.ht...](https://www.cs.usfca.edu/~galles/visualization/Algorithms.html)

~~~
jcmoscon
[https://visualgo.net/en](https://visualgo.net/en) is super cool! Thanks for
posting it

------
slathrop
This is a terrific idea! The code I reviewed so far looks very well written.
Thanks for this... folks looking to interview for a software developer
position (especially at Google or with any group that emphasizes fundamentals)
would do well to get acquainted with this repo!

------
evacchi
I would be interested in hearing feedback on this book [1]. I bought it for a
friend who's getting started with programming, coming from a different field.
It seems a good book overall, but I haven't had the time to take a very deep
look at it.

[1] [https://www.packtpub.com/web-development/learning-
javascript...](https://www.packtpub.com/web-development/learning-javascript-
data-structures-and-algorithms-third-edition)

------
crescentfresh
Tangential, but is there a name for a Trie that isn't for string
searching/matching? I've had a need for this in the past but each node needed
to store a non-character value. This was to facilitate a sort-of path finding
where the input was matched against a known set of paths.

Every library I looked at had implementations revolving around nodes that
stored chars (you know, for auto-complete scenarios) but nowhere in the
definition of a Trie is it bound to such an implementation (so I thought).

~~~
mattnewton
I think that is just the common use case.

It shouldn’t be too hard to roll your own here, it’s just a set of tree
traversal functions.

~~~
crescentfresh
Oh I did, was just surprised every formal language had one tied to string
searching/matching, including this demo.

------
ethanpil
I'd love to see something like this for every language... similar to the to do
app which has become the de facto standard to compare web frameworks..

------
52-6F-62
If anybody wants to build it into a library I threw together an index file and
included the rollup run to allow you to import everything as one library or as
separate modules:

[https://gist.github.com/robertfairley/11ba23640578650671dbde...](https://gist.github.com/robertfairley/11ba23640578650671dbde4df1f4737f)

------
dmayle
Along similar lines, does anyone know of something similar that includes
multigraphs? (Graphs with parallel edges).

There's a python library called networkx, but it is so heavily object-oriented
(multi-inheritance) as to make it impossible to get any benefit from reading
the code.

------
52-6F-62
Damn I’d just started doing something similar on the side. Looks like I’ll
have to rethink my efforts. :)

Nicely done

------
djhworld
Seems like a good reference manual to use before attending an interview at
Google/Amazon/Facebook etc

------
amelius
They don't explain lock-free data-structures?

------
tommerbomb
Bookmarked! Well done.

------
sciencesama
is there a similar thing with python too ?

~~~
Kagerjay
[https://www.geeksforgeeks.org](https://www.geeksforgeeks.org)

~~~
partycoder
I have mixed feelings about that site.

There are people posting verbatim interview questions, which is a breach of
interview NDA.

------
jonjojr
Gold!

