Collection Kit

Graph

Non-Linear

A Graph is a collection of nodes (vertices) and edges that connect pairs of nodes, used to represent relationships between objects.

Import Statement
import { Graph } from "collection-kit";

Key Features

  • Flexible Structure: Can represent various types of relationships (e.g., social networks, transportation systems).
  • Directed/Undirected: Can have directed edges (one-way) or undirected edges (two-way).

Common Operations

AddVertex
Add a new vertex to the graph.
AddEdge
Connect two vertices with an edge.
RemoveVertex
Remove a vertex and its associated edges.
RemoveEdge
Remove an edge between two vertices.
GetNeighbors
Retrieve all adjacent vertices for a given vertex.

Example Code

import { Graph } from "collection-kit";

const graph = new Graph();
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addEdge("A", "B");
graph.addEdge("B", "C");

console.log("Neighbors of B:", graph.getNeighbors("B"));