Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ahmedgaafer/js-alogrithims

JavaScript algorithims package
https://github.com/ahmedgaafer/js-alogrithims

Last synced: 10 days ago
JSON representation

JavaScript algorithims package

Awesome Lists containing this project

README

        

# JavaScript Algorithims





Algorithms made by vanilla JS.

## Install

```bash
$ npm install js-alogrithims
```

## Algorithms :

- [X] BFS
- [X] DFS
- [X] Dijkstra
- [X] AStar

## Usage:

***Note***:

> this package is dependent on another package that I made that includes all of the data structures that I used. To avoid bugs use the pre installed dependency JS-Data-Structures package for all of the data structures you want.

> the graph is implemented with adjacency map -same as adjacency list but using maps instead of lists in the edges- "this affects the order of the graph algorithms."

### BFS:

```js
const { Graph } = require('@ahmeds.gaafer/js-data-structures');
const { graphAlgo } = require('js-alogrithims');
const isUniDirectional = true;
const isWeighted = false;

const g = new Graph(isUniDirectional, isWeighted);

g
.addVertex(1)
.addVertex(2)
.addVertex(3)
.addVertex(4)
.addVertex(5)
.addVertex(6);

g
.addEdge(1,4)
.addEdge(2,3)
.addEdge(3,4)
.addEdge(4,5)
.addEdge(5,6)
.addEdge(6,1);

g.view();

/*
Graph contains 6 vertcies.

1 => [4, 1] :
2 => [3, 1] :
3 => [4, 1] :
4 => [5, 1] :
5 => [6, 1] :
6 => [1, 1] :

*/

const ret = graphAlgo.BFS(g.graph, 1, 6);
let path = ret[0];
let visited = ret[1];

console.log(path); // logs => [ '1', '4', '5', '6' ]

```

### DFS:

```js
const { Graph } = require('@ahmeds.gaafer/js-data-structures');
const { graphAlgo } = require('js-alogrithims');
const isUniDirectional = true;
const isWeighted = false;

const g = new Graph(isUniDirectional, isWeighted);

g
.addVertex(1)
.addVertex(2)
.addVertex(3)
.addVertex(4)
.addVertex(5)
.addVertex(6);

g
.addEdge(1,4)
.addEdge(2,3)
.addEdge(3,4)
.addEdge(4,5)
.addEdge(5,6)
.addEdge(6,1);

g.view();

/*
Graph contains 6 vertcies.

1 => [4, 1] :
2 => [3, 1] :
3 => [4, 1] :
4 => [5, 1] :
5 => [6, 1] :
6 => [1, 1] :

*/

const ret = graphAlgo.DFS(g.graph, 1, 6);
let path = ret[0];
let visited = ret[1];

console.log(path); // logs => [ '1', '4', '5', '6' ]
```