Awesome
Learn Data Structure and Algorithms by JavaScript
You need to have basic understanding of the JavaScript programming language to proceed with the codes from this repository.
Table of Contents
-
- Linked List
- Stack
- Queue
- Binary Search Tree (BST)
- Heap
- Hash Table
- Disjoint Set Union (Union Find)
- Trie
- Suffix Array
- Segment Tree
- Binary Indexed Tree (BIT)
- Heavy Light Decomposition
-
- Selection Sort
- Bubble Sort
- Insertion Sort
- Merge Sort
- Quick Sort
- Bucket Sort
- Counting Sort
- Heap Sort
- Radix Sort
-
- Graph Representation
- Breadth First Search (BFS)
- Depth First Search (DFS)
- Topological Sort
- Strongly Connected Components (SCC)
- Minimum Spanning Tree (MST)
- All Pairs Shortest Path (Floyd Warshall's Algorithm)
- Single Source Shortest Path Algorithm
- Djkastra's Algorithm
- Bellman Ford Algorithm
- Directed Acyclic Graph
- Bipartite Matching
- Articulation Point, Bridge
- Euler Tour/Path
- Hamiltonian Cycle
- Stable Marriage Problem
- Chinese Postman Problem
- 2-satisfiability
- Flow Algorithms
- Maximum Flow
- Minimum Cut
- Min-Cost Max Flow
- Maximum Bipartite Matching
- Vertex Cover
-
Dynamic Programming
- Rod Cutting
- Maximum Sum (1D, 2D)
- Coin Change
- Longest Common Subsequence
- Longest Increasing Subsequence
- Matrix Multiplication
- Edit Distance (Levenshtein distance)
- 0/1 Knapsack
- Travelling Salesman Problem
- Optimal Binary Search Tree
-
Greedy Algorithms
- Activity Selection/Task Scheduling
- Huffman Coding
- Knapsack Problem (Fractional Knapsack)
-
String Algorithms
- Rabin-Karp Algorithm
- Knuth-Morris-Pratt Algorithm
- Z Algorithm
- Aho-Korasick Algorithm
- Manachers Algorithm
- Boyr-Moore Algorithm
-
Number Theory
- Greatest Common Divisor (GCD)
- Longest Common Multiplier (LCM)
- Euler Totient (Phi)
- Primality Testing
- Prime finding(Sieve of Eratosthenes)
- Prime factorization
- Factorial
- Fibonacci
- Counting, Permutation, combination
- Exponentiation
- Big Mod
- Euclid, Extended euclid
- Josephus Problem
- Farey Sequence
- Catalan numbers
- Burnside's lemma/circular permutation
- Modular inverse
- Probability
- Chinese Remainder Theorem
- Gaussian Elimination method
- Dilworth's Theorem
- Matrix Exponentiation
-
Computational Geometry
- Pick's Theorem
- Convex hull
- Line Intersection
- Point in a polygon
- Area of a polygon
- Line Sweeping
- Polygon intersection
- Closest Pair
-
Game Theory
- Take Away Game
- Nim's Game
- Sprague-grundy Number
-
Others
- BackTracking
- N-Queen's Problem
- Tower of Hanoi Problem
- BackTracking
Introduction
JavaScript is a loosely typed or a dynamic language. That means you don't have to declare the type of a variable ahead of time. The type will get determined automatically while the program is being processed. That also means that you can have the same variable as different types:
var foo = 42; // foo is now a Number
var foo = 'bar'; // foo is now a String
var foo = true; // foo is now a Boolean
Data Types in JavaScript
The latest ECMAScript standard defines seven data types:
-
Six data types that are primitives:
- Boolean (
true
andfalse
) - Null (invalid object or address has the value
null
) - Undefined (a variable that has not been assigned a value has the value
undefined
) - Number (integer and floating point values ranging from -(2<sup>53</sup> -1) to (2<sup>53</sup> -1), +Infinity, -Infinity and NaN(not-a-number) )
- String (Sequence of textual characters. Strings are immutable in JavaScript)
- Symbol (new in ECMAScript 6)
- Boolean (
Arrays
JavaScript Arrays are regular objects for which there is a particular relationship between integer-key-ed properties and the 'length' property. Additionally, arrays inherit from Array.prototype which provides to them a handful of convenient methods to manipulate arrays like indexOf (searching a value in the array) or push (adding an element to the array), etc. This makes arrays a perfect candidate to represent lists or sets.
More details about data types in JavaScript:
- JavaScript data types and data structures - Mozilla Developer Network
- A Beginner’s Guide to JavaScript Variables and Datatypes - SitePoint
- JavaScript Cheat Sheet
- Data types in Javascript
Object Oriented Programming in JavaScript
- Introduction to Object-Oriented JavaScript - Mozilla Developer Network
- OOP In JavaScript: What You NEED to Know - JavaScript is sexy
Big-O Notation and Time Complexity Analysis
Algorithms in plain English: time complexity and Big-O notation Big-O Cheat Sheet Link A beginner's guide to big-O notation
How to Use
The easiest way to run and test the codes from this repository is to use nodejs (I have checked my code using nodejs v6.5.0 in an Windows machine).
Install it in your machine and add it to your environment path so that it is accessible in terminal commands.
Then you can run a JavaScript file like this:
node file.js
ES6/ES2015 implementations
There are ES6 implementations of the Data Structures and Algorithms in their respective folders within a separate folder named es6
.
Couple of things to notice here:
- There are no true private properties available in ES6 yet. So, in classes no workarounds or hacks used to implement private properties. There are several proposals and initiative ongoing to introduce it in ECMAScript. Hoping that would be done soon and we will then add it in our code.
- We tried to implement the data structures and some algorithms in separate modules so that they can be used independently in any other codes. While
import
is indeed part of ES6, it is unfortunately not yet supported by any native environments, Node or browser. Until the native support introduced the easy workaround is to stick with the old CommonJS Module format(Which is supported in NodeJS and as we advised earlier to test our codes in NodeJS, so we are sticking with it). The modules will be exported using CommonJS Module format and imported by 'require'. Though there are other ways likeBabel
,Rollup
etc., but that need some build configurations(And we don't want to make our codes more complicated). And at the end babel will convert the code torequire
andmodule.exports
anyway.
FAQ
-
Why you didn't use prototype methods in Objects?
=> Well, we could've. But before argue on that, you might want to read this nice article. We chose to follow the philosophy described on that article. But still, you can easily rewrite the codes of this repository using 'prototypal methods' for your own use.