Git Product home page Git Product logo

algorithms.js's Introduction

algorithms.js

Build Status Coverage Status Dependency Status devDependency Status Inline docs npm

Atwood's Law applied to CS101.

Classic algorithms and data structures implemented in JavaScript, you know... FOR SCIENCE.

Installing

npm install --save algorithms

Contents

Data Structures

require('algorithms/data_structures');
// or
require('algorithms').DataStructures;
  • AVLTree
  • BST
  • DisjointSetForest
  • FenwickTree
  • Graph
  • HashTable
  • Heap
    • MaxHeap
    • MinHeap
  • LinkedList
  • PriorityQueue
  • Queue
  • Set (HashSet)
  • Stack
  • Treap

Geometry algorithms

require('algorithms/geometry');
// or
require('algorithms').Geometry;
  • BezierCurve

Graph algorithms

require('algorithms/graph');
// or
require('algorithms').Graph;
  • breadthFirstSearch
  • depthFirstSearch
  • eulerPath
  • topologicalSort
Shortest path
  • bellmanFord
  • bfsShortestPath
  • dijkstra
  • floydWarshall
  • SPFA (Shortest Path Faster Algorithm)
Minimum spanning tree
  • prim
  • kruskal

Math algorithms

require('algorithms/math');
// or
require('algorithms').Math;
  • collatzConjecture
  • extendedEuclidean
  • fastPower
  • fibonacci
  • findDivisors
  • fisherYates
  • gcd (Greatest common divisor)
  • greatestDifference
  • lcm (Least common multiple)
  • newtonSqrt
  • nextPermutation
  • powerSet
  • reservoirSampling
  • shannonEntropy

Search algorithms

require('algorithms/search');
// or
require('algorithms').Search;
  • bfs (breadth-first search for binary trees)
  • binarySearch
  • dfs (depth-first search for binary trees)
  • inOrder (default)
  • postOrder
  • preOrder

Sorting algorithms

require('algorithms/sorting');
// or
require('algorithms').Sorting;
  • bubbleSort
  • countingSort
  • heapSort
  • insertionSort
  • quicksort
  • radixSort
  • selectionSort
  • shellSort
  • shortBubbleSort

String algorithms

require('algorithms/string');
// or
require('algorithms').String;
  • hamming
  • huffman
  • decode
  • encode
  • knuthMorrisPratt
  • levenshtein
  • longestCommonSubsequence
  • longestCommonSubstring
  • rabinKarp

Contributing

This project uses Google JavaScript Style Guide which can be a bit strict, but is really helpful in order to have more readable and less error-prone code

algorithms.js's People

Contributors

aaron-goshine avatar amilajack avatar anaran avatar argonlaser avatar barmidrol avatar brunorb avatar cthesky avatar eush77 avatar evandroeisinger avatar felipernb avatar filipefalcaos avatar gauravmittal1995 avatar geakstr avatar gibatronic avatar guliash avatar gyj963 avatar juanplopes avatar lamnguyen2187 avatar liamgriffiths avatar lifebeyondfife avatar matheusdallrosa avatar prakash-jha-dev avatar robertlread avatar rrrene avatar scompo avatar shuding avatar tayllan avatar xuefeng-zhu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

algorithms.js's Issues

Iterators for various data structures

I noticed that a lot of the data structures(BST, HashTable, Set, etc) don't have iterators defined. Were they intentionally left out? If not then I wouldn't mind going through and adding them.

sonar-scanner fails for PR (Travis-CI)

Everything passes and it fails on sonar-scanner, resulting in a negative overall result for PR.

Even for basic variable change PR #127 , sonar-scanner failed. There must be some issue with it.

This is the log from Travis-CI build.
The command "make" exited with 0.
0.11s$ sonar-scanner
sonar-scanner: command not found
The command "sonar-scanner" exited with 127.

prototype property issues.

var graph = graphFromEdges(false, [
[1, 2],
[1, 5]
]);

graph.edge("1","2")
1

graph.edge("1","constructor")
function Object() { [native code] }

hasOwnProperty can help here.

Well... No Geometry?

I am finding some libraries about computational geometry, especially Bezier-curve related.

Including these algorithms will be helpful.

Modular architecture

A user should be able to load just some "module" of the library, like just the data_structures or just the sorting algorithms

More Efficient Fibonacci?

Nice to see another person doing CS101 to keep themselves sharp. There are a couple of cute O(ln(n)) Fibonacci calculators to try:

Release 1.0

In order to have a 1.0 release we need:

  1. Add self-balancing BSTs (red and black, avl, ...)
  2. Add iterators (forEach) for all the data structures
  3. Have a modular structure to allow someone to load just the sorting algorithms, for example, in a simple way

Add to Bower

Adding the library to Bower would make it easier to use on front end projects than npm.

Bower is a package manager for the web. It offers a generic, unopinionated solution to the problem of front-end package management, while exposing the package dependency model via an API that can be consumed by a more opinionated build stack.

http://bower.io/#defining-a-package

Is there real need for >> 1 and << 1 everywhere?

Can't modern javascript compilers optimize this kind of instructions? I know this is kind of bikeshedding, but I think using shifts instead of multiplications only sacrifices readability of the code.

[Bug] PriorityQueue: Inserting previously extracted element does not work

Thanks for providing this module.

I've used the PriorityQueue a bit and noticed something that looks like a bug to me. It seems like if the same element is inserted again after have being extracted once alrady, is not possible.

See this minimal example:

const PriorityQueue = require("algorithms/data_structures").PriorityQueue;

const q = new PriorityQueue();

// Inserting and extracting one time works as expected:
q.insert("a", 1);
console.log(q.isEmpty());
console.log(q.extract());
console.log(q.isEmpty());

// Inserting 'a' again does not work, queue will be empty and nothing to extract:
q.insert("a", 1);
console.log(q.isEmpty());
console.log(q.extract());

The outoupt of this is:

false
a
true
true
undefined

while the expected output is:

false
a
true
false
a

My system:

  • node v19.2.0.
  • macOS 13.0.1

Find elements on a LinkedList

I'm looking for a good way to find elements on a LinkedList (academic purpose), using a functional aproach.

The forEach iterate over all list applying a function, it's useful for some cases, discussed at #88.
When the scenario is only to search and return an element, the forEach can be used with a workaround:

var a;
list.forEach(function (e) {
        if (key === e) {
          a = e;
          return;
        };
    });

and it will perform over all elements. I want to stop at first match, something like

var a = list.find(function(e /*[, key [,comparator_func]]*/){...})  // return an element or null

The iterator pattern can be used, maybe just refactor some code...
What do you think about this?

Simpler Binary Heaps?

Here's a simpler implementation of a Binary Heap; feel free to introduce a comparator into the constructor...

function PriorityQ(elems) {
// Implements a binary heap priority queue
    var q = [null];
    this.push = function (h) {
        var i, j;
        for (i = q.length; (j = (i / 2) | 0) && q[j] > h; q[i] = q[j], i = j);
        q[i] = h;
        return this;
    };
    this.pop = function () {
        var i, j, r, t;
        for (r = q[1]; q.length > 1 && r == q[1]; q[i] = t) {
            for (i = 1, t = q.pop(); (j = i * 2) < q.length;) {
                if (j < q.length && q[j] > q[j + 1]) j++;
                if (t <= q[j]) break;
                q[i] = q[i = j];
            }
        }
        return r;
    };
    if (Object.prototype.toString.call(elems) === '[object Array]')
        elems.forEach(this.push);
}

Should not binary search algorithm return the position in the array?

In its current form the binary search algorithm is not really useful, because in most cases it is not only the presence of a value that matters but also the position in the array.

I suggest that the binary search interface should mimic the standard Array#indexOf, in which case the algorithm could be considered as a more efficient alternative to indexOf for sorted arrays.

SPFA & negative cycle.

graph = new Graph(false)
graph.addEdge(1,2,-1)
graph.addEdge(2,3,-2)
graph.addEdge(3,4,0)
graph.addEdge(4,1,1)

SPFA(graph, "2")

The algorithm is going to infinite loop, because on every iteration we can improve shortest path.

Looks like algorithm should remember visited edges and break if it trying to visit some edge in the second time.

graph coloring and building an hierarchical topology

Hello guys!
Would be that interesting for you ?
The idea is: from usual parent -> children relations we build an hierarchied topology and calculate the x, y coords for the nodes to be shown on layout with specific width, height and make a general connections between nodes: connections to the same node from different nodes could be replaced with one common bus, etc.
I am going to implement it for the project in samsung.

Update wiki

For 1.0 it would be nice to have all algos and data structures documented properly on the wiki

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.