@utilityjs/graph
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

Graph

An implementation of Graph data structure.

license npm latest package npm downloads types

npm i @utilityjs/graph | yarn add @utilityjs/graph

Graph(isDirected?)

interface SearchCallbacks<T> {
  onEnter: (previous: Vertex<T> | null, current: Vertex<T>) => void;
  onLeave: (previous: Vertex<T> | null, current: Vertex<T>) => void;
  shouldTraverse: (
    previous: Vertex<T> | null,
    current: Vertex<T>,
    next: Vertex<T>
  ) => boolean;
}

export declare class Vertex<T> {
  constructor(value: T, key?: string | null);
  getValue(): T;
  setValue(value: T): void;
  addEdge(edge: Edge<T>): void;
  deleteEdge(edge: Edge<T>): void;
  getKey(): string;
  getEdges(): Edge<T>[];
  getDegree(): number;
  getNeighborEdge(vertex: Vertex<T>): Edge<T> | null;
  hasEdge(edge: Edge<T>): boolean;
  getSelfLoop(): Edge<T> | null;
  hasSelfLoop(): boolean;
  hasNeighbor(vertex: Vertex<T>): boolean;
  getNeighbors(): Vertex<T>[];
  clearEdges(): void;
}

export declare class Edge<T> {
  constructor(
    vA: Vertex<T>,
    vB: Vertex<T>,
    weight?: number,
    key?: string | null
  );
  setVA(vA: Vertex<T>): void;
  setVB(vB: Vertex<T>): void;
  getVA(): Vertex<T>;
  getVB(): Vertex<T>;
  isSelfLoop(): boolean;
  setWeight(weight: number): void;
  getWeight(): number;
  getKey(): string;
  reverse(): void;
}

export default class Graph<T> {
  constructor(isDirected?: boolean);
  isDirected(): boolean;
  getVertex(key: string): Vertex<T> | null;
  addVertex(vertex: Vertex<T>): void;
  getVertices(): Vertex<T>[];
  getEdges(): Edge<T>[];
  getWeight(): number;
  getVerticesIndexMap(): Record<string, number>;
  reverse(): void;
  addEdge(edge: Edge<T>): void;
  findEdge(edge: Edge<T>): Edge<T> | null;
  findEdge(vA: Vertex<T>, vB: Vertex<T>): Edge<T> | null;
  deleteEdge(edge: Edge<T>): void;
  getAdjacencyMatrix(unweighted?: boolean): number[][];
  breadthFirstSearch(
    startVertex: Vertex<T>,
    callbacks?: SearchCallbacks<T>
  ): void;
  depthFirstSearch(
    startVertex: Vertex<T>,
    callbacks?: SearchCallbacks<T>
  ): void;
}

Package Sidebar

Install

npm i @utilityjs/graph

Weekly Downloads

1

Version

1.2.1

License

MIT

Unpacked Size

31.5 kB

Total Files

8

Last publish

Collaborators

  • mimshins