# @fmtk/graph

> Library for graph structures

Latest version **0.2.1** (published 2019-10-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install @fmtk/graph
pnpm add @fmtk/graph
yarn add @fmtk/graph
bun add @fmtk/graph
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2019-10-01 |
| First published | 2019-10-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 38.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Gordon Leigh |
| Maintainers | gordonmleigh, stewartml |

## Links

- npm: https://www.npmjs.com/package/@fmtk/graph
- Repository: https://github.com/futurematik/graph
- Homepage: https://github.com/futurematik/graph#readme
- Issues: https://github.com/futurematik/graph/issues
- npm.io page: https://npm.io/package/@fmtk/graph

## Recent versions

- 0.2.1 (latest) — 2019-10-01
- 0.2.0 — 2019-10-01
- 0.1.0 — 2019-10-01
- 0.0.2 — 2019-10-01
- 0.0.1 — 2019-10-01

## README

# @fmtk/graph

This library includes some functions for working with graph structures.

## Basics

A graph is represented by a `Map<T, T[]>` – i.e. a map that has a key for each vertex, with the value being an array of children. Note that leaf nodes will be present with an empty array of children.

A graph can also be represented by a list of edges (`[T, T][]`). Each element in the array is a 2-element array with the source as the first element and the target as the second.

## Functions

### fromEdges

Build a `Map<T, T[]>` from a list of edges (`[T, T][]`).

```typescript
function fromEdges<T>(edges: [T, T][]): Map<T, T[]>;
```

### fromNodes

Build a `Map<T, T[]>` from an existing arbitrary graph structure.

```typescript
function fromNodes<T>(root: T, getChildren: (node: T) => T[]): Map<T, T[]>;
```

### getLeaves

Get an array of leaf nodes (nodes with no children) from the given graph.

```typescript
function getLeaves<T>(graph: Map<T, T[]>): T[];
```

### hasCycles

Returns true if the graph contains a cycle reachable from the given node.

```typescript
function hasCycles<T>(graph: Map<T, T[]>, from: T): boolean;
```

### toEdges

Build a list of edges (`[T, T][]`) from a graph map (`Map<T, T[]>`).

```typescript
function toEdges<T>(graph: Map<T, T[]>): [T, T][];
```

### topologicalSort

Build a list of all nodes, sorted in an order such that for any given node, all nodes which point to that node come first in the list.

```typescript
function topologicalSort<T>(graph: Map<T, T[]>): T[];
```

---
_Source: https://npm.io/package/@fmtk/graph · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
