Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/ahmedgaafer/js-alogrithims
- Owner: ahmedgaafer
- License: mit
- Created: 2020-11-22T15:08:30.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-11-12T12:43:36.000Z (about 2 years ago)
- Last Synced: 2024-12-05T18:44:19.778Z (about 1 month ago)
- Language: JavaScript
- Size: 76.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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' ]
```