Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 62 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
![npm](https://img.shields.io/npm/v/@sha1n/dagraph)

# DAGraph
A direct acyclic graph implementation
A directed acyclic graph (DAG) implementation in TypeScript.

- [DAGraph](#dagraph)
- [Features](#features)
- [Usage](#usage)
- [Basic](#basic)
- [Custom Objects](#custom-objects)
- [Install](#install)
- [Development](#development)

## Features
- Hosts your data in the graph structure
- Topological sort traversal generator
- Traverse root nodes generator
- Reverse graph API
- **Generic Graph Structure**: Store any identifiable data in the graph.
- **Topological Sort**: Iterate over nodes in topological order (dependencies first).
- **Cycle Detection**: Automatically detects and prevents cycles when adding edges.
- **Root Traversal**: Efficiently access all root nodes (nodes with no dependencies).
- **Graph Reversal**: Create a new graph with all edges reversed.
- **Depth-First Traversal**: Visit nodes with context, parent, depth, and index information.
- **TypeScript**: Written in TypeScript with full type definitions.

## Usage

Expand All @@ -31,33 +35,37 @@ import createDAG from '@sha1n/dagraph';
const dag = createDAG();

dag.addNode({id : 'a'});
dag.addEdge({id : 'b'}, {id : 'a'});
dag.addEdge({id : 'c'}, {id : 'a'});
dag.addEdge({id : 'd'}, {id : 'b'});
dag.addEdge({id : 'b'}, {id : 'a'}); // b -> a
dag.addEdge({id : 'c'}, {id : 'a'}); // c -> a
dag.addEdge({id : 'd'}, {id : 'b'}); // d -> b

for (const node of dap.topologicalSort()) {
...
// Topological sort
for (const node of dag.topologicalSort()) {
console.log(node.id);
}
```

### Custom Objects
Any object implementing the `Identifiable` interface (having an `id: string` property) can be stored.

```ts
import { createDAG, Identifiable } from '@sha1n/dagraph';

type MyThing = {
id: string; // <-- implicitly implements the 'Identifiable' interface
id: string;
name: string;
doSomething(): void;
};

const myThing = {
const myThing: MyThing = {
id: 'a',
name: 'my thing',
doSomething: () => {
console.log('A');
}
};

const myOtherThing = {
const myOtherThing: MyThing = {
id: 'b',
name: 'my other thing',
doSomething: () => {
Expand All @@ -67,14 +75,53 @@ const myOtherThing = {

const myDAG = createDAG<MyThing>();

myDAG.addEdge(myThing, myOtherThing);
// Add nodes explicitly or implicitly via addEdge
myDAG.addEdge(myThing, myOtherThing); // myThing -> myOtherThing
```

## Install

Using **pnpm** (recommended):
```bash
yarn install @sha1n/dagraph
pnpm add @sha1n/dagraph
```

Using npm:
```bash
npm i @sha1n/dagraph
```

Using yarn:
```bash
yarn add @sha1n/dagraph
```

## Development

This project uses [pnpm](https://pnpm.io/) for dependency management.

### Prerequisites
- Node.js (version specified in `.nvmrc` or `engines` in `package.json`)
- pnpm

### Commands

- **Install dependencies**:
```bash
pnpm install
```

- **Build**:
```bash
pnpm build
```

- **Run Tests**:
```bash
pnpm test
```

- **Lint**:
```bash
pnpm lint
```