Graph

In mathematics, and more specifically in graph theory, a graph is a structure amounting to a set of objects in which some pairs of the objects are in some sense "related". The objects correspond to mathematical abstractions called vertices (also called nodes or points) and each of the related pairs of vertices is called an edge (also called link or line)

Read full: wiki/graphopen in new window

Graph types

Directed graph

A directed graph or digraph is a graph in which edges have orientations.

Weighted graph

A weighted graph or a network is a graph in which a number (the weight) is assigned to each edge. Such weights might represent for example costs, lengths or capacities, depending on the problem at hand.

Import

import {UndirectedGraph, DirectedGraph} from "@raikuxq/alg-ds/lib/exports/data-structures";

API reference

Graph API: /api/data-structures/graph

Example usage

import {UndirectedGraph} from "@raikuxq/alg-ds/lib/exports/data-structures";

const graph = new UndirectedGraph<string>();

graph
    .addVertex("Mike")
    .addVertex("Bob")
    .addVertex("Lisa")
    .addEdge("Mike", "Bob", 5)
    .addEdge("Bob", "Lisa", 10);

graph.hasVertex("Mike"); // true
graph.hasEdge("Mike", "Bob"); // true
graph.hasEdge("Mike", "Lisa"); // false
graph.getEdgeWeight("Mike", "Bob"); // 5

graph.removeVertex("Bob", "Lisa");
graph.hasEdge("Bob", "Lisa"); // false