1.1.2 • Published 5 years ago
js-alogrithims v1.1.2
JavaScript Data Structures
Algorithms made by vanilla JS.
Install
$ npm install js-alogrithims
Algorithms :
- BFS
- DFS
- Dijkstra
- 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:
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:
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' ]