graphs-for-js
Implementations of graph data structures for JavaScript and TypeScript
Features:
- Insert and remove nodes.
- Connect and disconnect nodes.
- Algorithms for graph data structures.
Table of contents generated with markdown-toc
Installation
$ npm install --save graphs-for-js
GraphBuilder Usage
Import the GraphBuilder
to build and initialize a graph.
The library supports 4 types of graphs:
- Weighted, Directed graphs
- Edges go in one-direction and can be assigned a value.
- Unweighted, Directed graphs
- Edges go in one-direction and cannot be assigned a value.
- Weighted, Undirected graphs
- Edges are bidirectional and can be assigned value.
- Unweighted, Undirected graphs
- Edges are bidirectional and cannot be assigned a value.
Import the GraphBuilder
const {GraphBuilder} = require('graph-lib')
import {GraphBuilder} from 'graph-lib'
Key Function
Because JavaScript does not directly hash/map objects and their contents to unique values, the GraphBuilder accepts a function for mapping a node value to a string key.
If a Key Function is not given, then the default behaviors are the following:
- Primitive Types
- This includes number, string, boolean, symbol, null, and undefined.
- All primitives are converted to their string equivalents.
- Caution: Floats are subject to float-precision issues.
- Objects
- A string representation of the object is used as the key, for which the string contains properties including keys and values.
- For example, the object
{a: 32, b: 23}
is mapped as '{a:32,b:23}'
- If an object value is circular, then it is mapped as the value of
toString()
, normally '[object Object]'
JavaScript initialization
const weightedGraph = GraphBuilder()
.withKeyFunction(i => `${i}`)
.directed.weighted()
const unweightedGraph = GraphBuilder()
.withoutKeyFunction()
.directed.unweighted()
TypeScript initialization
Use type parameters to specify the type of the nodes and of the edge values (if weighted).
const weightedGraph = GraphBuilder<string, number>()
.withoutKeyFunction()
.directed.weighted()
const unweightedGraph = GraphBuilder<string>()
.withoutKeyFunction()
.directed.unweighted()
Using the Graph
For the following examples, assume the nodes of graph
are numbers.
The examples show the differences among each type of graph when necessary.
Insert nodes
graph.insert(0)
graph.insert(1, 2, 3)
const result = graph.insert(...[4,5,6,7])
console.log(result)
Removing nodes
graph.remove(0)
graph.remove(1, 2, 3)
const result = graph.remove(...[4,5,6,7])
console.log(result)
Number of nodes
graph.count()
Forming edges
unweightedGraph.connect(1, 2)
weightedGraph.connect(1, 2, 0.5)
Removing edges
unweightedGraph.disconnect(1, 2)
weightedGraph.disconnect(1, 2)
weightedGraph.disconnect(1, 2, 0.5)
undirectedGraph.connect(1, 2)
undirectedGraph.disconnect(2, 1)
Get all of the nodes and edges in the graph
graph.nodes()
graph.edges()
Incoming and Outgoing edges
graph.outgoingEdgesOf(2)
graph.incomingEdgesOf(2)
Degree of Edge
graph.degreeOf(2)
graph.inDegreeOf(2)
graph.outDegreeOf(2)
Existence Methods
graph.contains(1)
graph.contains(1, 2, 3)
unweightedGraph.hasEdge(1, 2)
weightedGraph.hasEdge(1, 2)
weightedGraph.hasEdge(1, 2, 0.5)
undirectedGraph.hasEdge(x, y) === undirectedGraph.hasEdge(y, x)
GraphUtil Usage
Some helper functions are included in the GraphUtil
import.
Examples
const {GraphBuilder, GraphUtil} = require('graph-lib')
const graph = GraphBuilder().withoutKeyFunction().directed.unweighted()
GraphUtil.hasCycle(graph)
GraphUtil.findShortestPath(graph, 1, 2)
Contributing
Feel free to contribute by adding changes to the graph implementation or by writing implementations for graph algorithms in GraphUtil
! You can also add suggestions or issues here
If you'd like to make changes to the graph implementation or for GraphUtil
:
-
Fork this repository
-
Create a feature branch
-
Make changes 🛠
-
Make a PR to merge into this repo
License
ISC © Anthony Yang