• Stars
    star
    2,187
  • Rank 21,100 (Top 0.5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐ŸŒŠ Pretty, common and useful algorithms with modern JS and beautiful tests

Pretty algorithms

travis

Common useful algorithms written in modern, pretty and easy-to-understand Javascript along with real-world usage examples. Implementations are in standard ES6 Javascript with the addition of Typescript type annotations for better clarity.

All the algorithms are also tested using Jest with the help of custom beautiful snapshots.

Jest

Note

The purpose of this repository is to show algorithms written using declarative and intuitive code as much as possible. It's not meant to be used as production. Clarity and simplicity is favored over performance.

If you need something absolutely performant in production try checking felipernb/algorithms.js with low-level optimisations.

Content

Miscellaneous

Search

Sort

Usage

You can play around with the code cloning the repo and running the following commands:

npm install
npm test

Play around with the source code, the tests and learn algorithms! You can also run the following command to put tests in watch mode and auto-run with changes. Jest CLI output is awesome!

npm run test -- --watch

Jest

Real-world usage

Some real-world usage of the algorithms to show when they can be applied.

Activity selection ๐Ÿ“†

The activity selection problem is the selection of non-conflicting activities to perform within a given time frame, given a set of activities each marked by a start time (s[i]) and finish time (f[i]).

A classic application of this problem is in scheduling a room for multiple competing events, each having its own time requirements (start and end time), and many more arise within the framework of operations research. Source

Change making ๐Ÿ’ฐ

The change-making problem addresses the question of finding the minimum number of coins (of certain denominations) that add up to a given amount of money.

The change-making problem is a knapsack type problem, and has applications wider than just currency. It can be used whenever there is the need to calculate the minimum set of items to add up to a value.

An application of change-making problem can be found in computing the ways one can make a nine dart finish in a game of darts. Source

Huffman coding ๐Ÿ”ก

A Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression.

The Huffman coding is often used as a "back-end" to other compression methods. DEFLATE (PKZIP's algorithm) and multimedia codecs such as JPEG and MP3 have a front-end model and quantization followed by the use of prefix codes. Source

Longest common subsequence ๐Ÿ“

The longest common subsequence (LCS) problem is the problem of finding the longest subsequence common to two sequences. It differs from problems of finding common substrings since subsequences are not required to occupy consecutive positions within the original sequences.

The longest common subsequence problem is the basis of data comparison programs such as the diff utility and has applications in bioinformatics (sequences of DNA).
It is also widely used by revision control systems such as Git for reconciling multiple changes made to a revision-controlled collection of files. Source

The LCS problem is also the base of the edit-distance problem, which quantifies how dissimilar two strings are to one another.
An application of the latter could avoid some security problems, as happened to NPM: crossenv malware on the npm registry.

Maximum subarray ๐Ÿ“ˆ

Find the contiguous subarray within an array of numbers which has the largest sum.

The Maximum subarray problem is useful to find the range of maximum in a period of events, such as the best moment to buy and sell stock assets knowing their value in a period of time. Another example:

You have a list of income details for each month of the company ACME for a year of 2016โ€ฆyou have to find out in which part of year the company earns more income and how much it earns. Source

This algorithm also has been applied to radio telescope images acquired for the Australian square kilometer array pathfinder project. This paper provides an overview of the maximum subarray algorithm and shows how this can be utilized for optical and radio telescope applications. Source

Priority-queues ๐ŸŽข

A priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority.

A priority queue can be used for Bandwidth management, Discrete event simulation, Dijkstra's algorithm and so on. Open the source for more details.

Rod cutting ๐Ÿ’ธ

Find the best way to cut a rod of length n, assuming that each length has a price. Find best set of cuts to get maximum price.

The Rod cutting problem can be used to maximize a profit whenever the resources can be divided into minor quantities and sold separately. Useful, isn't it?

Binary search (tree) ๐ŸŒณ

Binary search is a search algorithm that finds the position of a target value within a sorted collection.

Some applications of Binary search:

  • Fast autocomplete/dictionary. Source

  • Set and Map implementation of many standard language libraries. Source

Sorting ๐Ÿค”

A sorting algorithm is an algorithm that puts elements of a list in a certain order

Many computer scientists consider sorting to be the most fundamental problem in the study of algorithms. There are several reasons:

  • Sometimes an application inherently needs to sort information. For example, in order to prepare customer statements, banks need to sort checks by check number.

  • Algorithms often use sorting as a key subroutine. For example, a program that renders graphical objects which are layered on top of each other might have to sort the objects according to an โ€œaboveโ€ relation so that it can draw these objects from bottom to top. We shall see numerous algorithms in this text that use sorting as a subroutine.

  • We can draw from among a wide variety of sorting algorithms, and they employ a rich set of techniques. In fact, many important techniques used throughout algorithm design appear in the body of sorting algorithms that have been developed over the years. In this way, sorting is also a problem of historical interest.

Source.

Check instead this article about pros and cons of each sorting algorithms.

TODO

  • Add more real-world examples
  • Add benchmarks

More Repositories

1

ng-animate

๐ŸŒ™ A collection of cool, reusable and flexible animations for Angular 14+
TypeScript
721
star
2

react-tiny-dom

๐Ÿ™ A minimal implementation of react-dom using react-reconciler
JavaScript
465
star
3

sinergia

โš’๏ธ Cooperative expensive tasks at 60fps via ES6 generators
JavaScript
162
star
4

react-kanban

๐Ÿ—“ Kanban board built with React.js and Redux
JavaScript
115
star
5

rx-polling

๐Ÿ“ฌ RxJS-based polling library with exponential backoff
TypeScript
66
star
6

gmail-smart-compose

A study implementation of Gmail Smart Compose trained with Keras and used in browser with Tensorflow.js
Jupyter Notebook
27
star
7

talks

Collection of my public talks
JavaScript
11
star
8

wealth

Personal financial management app in pure Javascript
JavaScript
9
star
9

billy

๐Ÿ“  Angular 2+, RxJS and Redux Invoice Application
TypeScript
9
star
10

fedra-thesis

Next-generation system for Computing Continuum via WebAssembly (WASM)
WebAssembly
7
star
11

ygo

๐Ÿƒ YuGiOh! PWA built with Web Components and hyperHTML
CSS
6
star
12

lab

Personal experiments with technologies
TeX
4
star
13

blog

Chronicles of a young developer's journey
MDX
3
star
14

gulp-react-browserify

๐Ÿ’ฅ Boilerplate for React.js development with Gulp and Browserify
JavaScript
3
star
15

gh-issues-for-comments

๐Ÿˆ Automatically create Github issues to use as blog comments
JavaScript
3
star
16

inkmark

Progetto P2
C++
2
star
17

libromastro

A webapp to keep track of your stock operations
TypeScript
2
star
18

fsm-editor

Finite State Machine editor
TypeScript
2
star
19

metalsmith-gh-comments

Metalsmith plugin for Github issues as blog comments
JavaScript
2
star
20

daisyhub

The favourite hub for Animal Crossing turnip exchange
TypeScript
2
star
21

telegram-nope-bot

A Telegram bot which alerts about NOPE value on tickers
TypeScript
2
star
22

webpack-react-boilerplate

Essential Webpack Boilerplate for React.js development with readable and minimal configuration
JavaScript
1
star
23

continuum

TeX
1
star
24

network-color

An educational game for anyone interested in Computer Science
TypeScript
1
star
25

brokerapi-rs

V2.16 Open Service Broker API compliant brokers in Rust
Rust
1
star
26

chattina

An MVC Chat, built in pure Javascript.
JavaScript
1
star
27

pwa

CSS
1
star
28

currency

๐Ÿ’ฐCurrency Exchange in React, Redux & Redux-Saga
JavaScript
1
star
29

jiayihu.github.io

Personal Website
JavaScript
1
star