Giter VIP home page Giter VIP logo

javascript-algorithms's Introduction

About

This repository contains JavaScript implementations of famous computer science algorithms.

API reference with usage examples available here.

Development

To install all dev dependencies

Call:

npm install

To setup repository with documentation

npm run doc

This will build the documentation and open it in your browser.

To update .html files with documentation

Just run npm run doc again.

To run tests

Call:

npm run test

This will execute all *.spec.js files.

To deploy documentation site

npm run deploy

This requires you to have commit access to your Git remote.

Contributions

Fork the repo and make required changes. Afterwards, push your changes in branch. The name will be according to the changes you did. Initiate the pull request.

Make sure your editor makes validations according to the .jshintrc in the root directory of the repository.

Before pushing to the repository, run:

npm run build

If the build is not successful, fix your code in order for the tests and jshint validation to run successfully. Then create a pull request.

Contributors

mgechev AndriiHeonia Jakehp lygstate mik-laj krzysztof-grzybek
mgechev AndriiHeonia Jakehp lygstate mik-laj krzysztof-grzybek
pvoznenko jettcalleja filipefalcaos kdamball lekkas infusion
pvoznenko jettcalleja filipefalcaos kdamball lekkas infusion
deniskyashif brunohadlich designeng Microfed Nirajkashyap pkerpedjiev
deniskyashif brunohadlich designeng Microfed Nirajkashyap pkerpedjiev
duffman85 Xuefeng-Zhu emyarod alexjoverm amilajack BorislavBorisov22
duffman85 Xuefeng-Zhu emyarod alexjoverm amilajack BorislavBorisov22
brunob15 BryanChan777 ysharplanguage jurassix fisenkodv contra
brunob15 BryanChan777 ysharplanguage jurassix fisenkodv contra
liesislukas marrcelo wnvko millerrach xiedezhuo DengYiping
liesislukas marrcelo wnvko millerrach xiedezhuo DengYiping
fanixk wlx199x shaunak1111
fanixk wlx199x shaunak1111

License

The code in this repository is distributed under the terms of the MIT license.

javascript-algorithms's People

Contributors

alexjoverm avatar amilajack avatar andriiheonia avatar bernhardfritz avatar borislavborisov22 avatar brunob15 avatar brunohadlich avatar bryanchan777 avatar deniskyashif avatar dependabot[bot] avatar designeng avatar duffman85 avatar emyarod avatar filipefalcaos avatar infusion avatar jakehp avatar jettcalleja avatar jurassix avatar kdamball avatar krzysztof-grzybek avatar lekkas avatar lygstate avatar mgechev avatar microfed avatar mik-laj avatar nirajkashyap avatar pkerpedjiev avatar pvoznenko avatar xuefeng-zhu avatar ysharplanguage 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

javascript-algorithms's Issues

Can't clone repo as instructed by readme.md

Running the command git clone [email protected]:mgechev/javascript-algorithms.git javascript-algorithms-docs from readme.md results in the following output-

git clone [email protected]:mgechev/javascript-algorithms.git javascript-algorithms-docs
Cloning into 'javascript-algorithms-docs'...
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I tried changing the git clone URL to https://github.com/mgechev/javascript-algorithms.git and it worked.
Should I send a pull request to change the clone URL?

topological-sort DAG exception

What wrong with my code? I have an exception "The graph is not a DAG".

var topsort = require('../src/graphs/others/topological-sort').topologicalSort;
var graph = [[0, 1, 0, 0, 1],
             [0, 0, 0, 0, 0],
             [1, 1, 0, 1, 1],
             [0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0]];
var vertices = topsort(graph);

But there is a graph I want to represent:
image001

and it looks directed and acyclic.

Version without solutions?

Hey, I'm putting together a coding curriculum, and these algorithms are awesome. I was wondering if there exists a version of the code without the solutions already written?

gulp jsdoc not working on windows.

Windows 7 - 64 bit
Results in cmder & msysgit

Message:
    Command failed: C:\Windows\system32\cmd.exe /s /c "./node_modules/.bin/jsdoc -c ./doc-config.json"
'.' is not recognized as an internal or external command,
operable program or batch file.

Details:
    killed: false
    code: 1
    signal: null
    cmd: C:\Windows\system32\cmd.exe /s /c "./node_modules/.bin/jsdoc -c ./doc-config.json"
    stdout:
    stderr: '.' is not recognized as an internal or external command,
operable program or batch file.

publish to npm?

Really great work here! Just wondering whether this is likely to be published at some point soon?

Module names for the documentation

I'd suggest module name corresponding to the directory name since most of the algorithms are well categorized. May be there's a place for improvement for some (like searching can have sub-categories - strings and lists and sorting may have strings sub-category).

BST - remove root node fix BUG

My fix for #46 explained here #47 was a 'woosh'. If root has children, with that change in place, they just become orphans and are lost in the void (rip).

if (node._parent !== null) {
  if (node._left) {
    this._replaceChild(node._parent, node, node._left);
  } else if (node._right) {
    this._replaceChild(node._parent, node, node._right);
  } else {
    this._replaceChild(node._parent, node, null);
  }
}else {
  this._root = null; // all children of that node lost in void
}

So I've removed the code from #47 and have a new fix that isn't stupid, tested, and works.

  exports.BinaryTree.prototype._replaceChild =
   function (parent, oldChild, newChild) {
    if (!parent) {
      this._root = newChild;
      if(this._root !== null){ //ADDED
        this._root._parent = null;
      } //ADDED
    } else {
      if (parent._left === oldChild) {
        parent._left = newChild;
      } else {
        parent._right = newChild;
      }
      if (newChild) {
        newChild._parent = parent;
      }
    }
  };

The new test - root removal with children:

  it('should remove root and replace with valid child (1)', function () {
    var bTree = new BinaryTree();
    bTree.insert(15);
    bTree.insert(30);
    bTree.insert(45);
    var node = bTree.find(15);
    bTree.remove(node);
    expect(bTree._root.value).toBe(30);
  });

will submit PR after #51 is handled (on same branch again 👯)

I'm going to be adding a bunch of tests to prevent these joys :godmode:

Copy/Paste in graphs

Looks like it is possible to make graphs module better according to DRY approach.

For example Edge class used in Prism algorithm and in Bellman–Ford algorithm and it was copy/pasted.

Maybe there is reason to make Graph data structure with edges and vertices inside data-structures folder and algorithms inside graphs folder will just process this graph?

Noting the source of the ported implementation

Some algorithms, obviously, are inspired by or ported from Robert Sedgewick Java implementations (e.g: unionfind, LLRBT).

Would it not be more appropriate to add a comment mentioning this.

Too deep namespace

Inside 'searching' folder we have subfolders with just one js file. Maybe better to make folder structure not so deep like this:

searching/
-- binarysearch.js
-- recursive-binarysearch.js
-- knuth-morris-pratt.js
-- longest-increasing-subsequence.js
-- quickselect.js
-- maximum-subarray-divide-and-conquer.js
-- maximum-subarray.js

?

Remove HashTable

I think it's better to remove the hash table from master until #66 is resolved.

Bug in interval-tree.js

Great library, but I think there's a bug in the IntervalTree implementation. Consider the following snippet:

it = new IntervalTree()
it.add([10383734, 10594186])
it.add([9495571, 9677853])

it.add([9303743, 9404967])
it.intersects([9303743, 9303744])

This should logically return true, right? The intersection test clearly overlaps the last added interval. Running this, however, yields false. If either of the first two add statements are removed, the intersection check at the end returns true as expected.

BST - remove root node error

The following test (that doesn't exist in codebase) fails with the current BST remove()

  it('should insert and remove single node properly', function () {
    var bTree = new BinaryTree();
    bTree.insert(15);
    var node = bTree.find(15);
    bTree.remove(node);
    expect(bTree._root).toBe(null);
  });

Here's the issue explained in comments

// this is called in .remove()
this._replaceChild(node._parent, node, null); // node._parent is null since the node is the root, node is valid
..
..
..
   function (parent, oldChild, newChild) {
    if (!parent) { //parent param is null, enter if
      this._root = newChild; //assigns null to root
      this._root._parent = null; // null._parent throws error
    } else {
      if (parent._left === oldChild) {
        parent._left = newChild;
      } else {
        parent._right = newChild;
      }

      if (newChild) {
        newChild._parent = parent;
      }
    }

I've got a fix in place, a bunch of tests for BST, and will submit a PR once #45 clears (changes are on my local master).

Browser compatibility

The library source code is now not fully compatible with browser Javascript.

For example, these files

  • src/sorting/bubblesort/bubblesort.js
  • src/searching/binarysearch/binarysearch.js
  • src/data-structures/heap.js
  • src/searching/longest-increasing-subsequence/longest-increasing-subsequence.js

use node.js exports keyword, so they can not be used in browser code without changing.

May be use some approach to allow node.js-browser compatibility? (one of the possible approaches offerd by caolan here.

BubbleSort ignoring the first element of the collection

First of all, thanks for creating the algorithms. Just tried the bubble sort.
Its working fine but one thing is that first element is ignored.
In line 18, changing
for (var j = i; j > 0; j -= 1)
to
for (var j = i; j >= 0; j -= 1);
fixed the issue.
Thanks once again.

Binary search test is wrong

This test from searching/ binarysearch.spec.js has two issues:

it('should find the eleent in position arr.length', function () {
    expect(binarySearch([1, 2, 3, 4, 6, 8], 1)).toBe(0);
 });
  • There can never be an element in position arr.length
  • The value found parameter should be 8 instead of 1, assuming its testing for the last element.

Enhancement suggestion

Hi all,

Although I know the XSLT (1.0) processing model isn't per se a "classic computer science" algorithm, I was just curious if there'd be any interest for this repo to include a simple, quite minimalistic (yet useful, I believe) JavaScript equivalent? FWIW, it also nicely happens to be purely functional (i.e., side effects-free) -- that is, modulo only a couple String and Array prototype augmentations (helpers) that could be easily moved out into their own scope / namespace, if so wished.

For some working examples, see this SO link:

http://stackoverflow.com/a/35663358/1409653

Let me know, &

Cheers,

Difficulty with running test

Hey,

can someone clarify this for me? I'm just trying to run the test file. I copied over the gulpfile.js into my project and did an npm install. I then did the gulp test and I'm getting the following result (which clearly didn't run the tests). Am I missing a step? Thanks in advance

➜ algorithms gulp test
[17:03:09] Using gulpfile ~/node_modules/algorithms/gulpfile.js
[17:03:09] Starting 'test'...
0 specs, 0 failures
Finished in 0 seconds
[17:03:09] Finished 'test' after 53 ms

Knuth-morris-pratt doesn't work

Knuth-morris-pratt algorithm freezes and doesn't return any result. My code:

var indexOf = require('../src/searching/knuth-morris-pratt').kmp;
console.log(indexOf('hello', 'll'));

Add `.jscsrc`

Add jscsrc and add it to gulp, in gulp build.

.jscrc should be copied from Yeoman's generator.

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.