Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typescript-graph

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typescript-graph

This library provides some basic graph data structures and algorithms.

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

typescript-graph

This library provides some basic graph data structures and algorithms.

Full docs are availible here.

It supports undirected graphs, directed graphs and directed acyclic graphs (including inforcing acyclicality). It does not support weighted graphs at this time.

The graphs created with this library can have nodes of any type, however at present it does not support attaching metadata to edges.

The most useful functionality is the ability to compute cyclicality and topological sort order on [[DirectedAcyclicGraph]]s. This can be used for determining things like task order or tracing dependencies.

Installing & Basic Usage

npm install 'typescript-graph'
import { Graph } from 'typescript-graph'

// Identify the node type to be used with the graph
type NodeType = { name: string, count: number, metadata: { [string: string]: string } }
// Define a custom identity function with which to identify nodes
const graph = new Graph<NodeType>((n: NodeType) => n.name)

// Insert nodes into the graph
const node1 = graph.insert({name: 'node1', count: 45, metadata: {color: 'green'}})
const node2 = graph.insert({name: 'node2', count: 5, metadata: {color: 'red', style: 'normal'}})
const node3 = graph.insert({name: 'node3', count: 15, metadata: {color: 'blue', size: 'large'}})

// Add edges between the nodes we created.
graph.addEdge(node1, node2)
graph.addEdge(node2, node3)

// Get a node
const node: NodeType = graph.getNode(node2);

Examples

Creating a directed graph and detecting cycles.

import { DirectedGraph, DirectedAcyclicGraph } from 'typescript-graph'

// Create the graph
type NodeType = { name: string, count: number }
const graph = new DirectedGraph<NodeType>((n: NodeType) => n.name)

// Insert nodes into the graph
const node1 = graph.insert({name: 'node1', count: 45})
const node2 = graph.insert({name: 'node2', count: 5})
const node3 = graph.insert({name: 'node3', count: 15})

// Check for cycles
console.log(graph.isAcyclic()) // true

// Add edges between the nodes we created.
graph.addEdge(node1, node2)
graph.addEdge(node2, node3)

// Check for cycles again
console.log(graph.isAcyclic()) // still true

// Converts the graph into one that enforces acyclicality
const dag = DirectedAcyclicGraph.fromDirectedGraph(graph)

// Try to add an edge that will cause an cycle
dag.addEdge(node3, node1) // throws an exception

// You can add the edge that would cause a cycle on the preview graph
graph.addEdge(node3, node1)

// Check for cycles again
console.log(graph.isAcyclic()) // now false

DirectedAcyclicGraph.fromDirectedGraph(graph) // now throws an exception because graph is not acyclic

Creating a directed acyclic graph and getting the nodes in topological order

import { DirectedAcyclicGraph } from 'typescript-graph'

// Create the graph
type NodeType = { name: string }
const graph = new DirectedAcyclicGraph<NodeType>((n: NodeType) => n.name)

// Insert nodes into the graph
const node1 = graph.insert({name: 'node1'})
const node2 = graph.insert({name: 'node2'})
const node3 = graph.insert({name: 'node3'})
const node4 = graph.insert({name: 'node4'})
const node5 = graph.insert({name: 'node5'})

// Add edges
graph.addEdge(node1, node2)
graph.addEdge(node2, node4)
graph.addEdge(node1, node3)
graph.addEdge(node3, node5)
graph.addEdge(node5, node4)

// Get the nodes in topologically sorted order
graph.topologicallySortedNodes() // returns roughly [{ name: 'node1' }, { name: 'node3' }, { name: 'node5' }, { name: 'node2' }, { name: 'node4' }]

License

MIT License

Author

Max Walker (max@maxwalker.me)

FAQs

Package last updated on 25 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc