Class WeightedGraph<K>

A weighted graph data structure.

Type Parameters

  • K

    The type of the vertices in the graph.

Hierarchy

Constructors

Properties

map: Map<K, Edge<K>[]> = ...

Accessors

  • get size(): number
  • Returns the number of vertices in the graph.

    Returns number

    The number of vertices in the graph.

  • get vertices(): readonly K[]
  • Returns an array of vertices in the graph.

    Returns readonly K[]

    An array of vertices.

Methods

  • Adds an edge between two vertices with an optional weight.

    Parameters

    • v1: K

      The first vertex.

    • v2: K

      The second vertex.

    • weight: number = 0

      The weight of the edge (default is 0).

    Returns WeightedGraph<K>

    The updated weighted graph.

    Throws

    An error if the first vertex is not found or if the edge already exists.

  • Returns an array of vertices adjacent to the given vertex.

    Parameters

    • vertex: K

      The vertex to get the adjacent vertices for.

    Returns readonly K[]

    An array of adjacent vertices.

  • Returns the shortest path between two vertices in the graph.

    Parameters

    • v1: K

      The starting vertex.

    • v2: K

      The ending vertex.

    Returns readonly K[]

    An array of vertices representing the shortest path.

    Throws

    An error if the path could not be found.

  • Returns the weight of the edge between the first vertex and the second vertex, and optionally additional vertices if provided.

    Parameters

    • v1: K

      The first vertex.

    • v2: K

      The second vertex.

    • Rest ...vn: K[]

      Additional vertices (optional).

    Returns number

    The weight of the edge between the vertices.

    Throws

    Error if the first or second vertex is not found.

  • Returns true if the graph contains the given vertex, false otherwise.

    Parameters

    • vertex: K

      The vertex to check for.

    Returns boolean

    True if the graph contains the vertex, false otherwise.

  • Returns a boolean indicating if two vertices are adjacent in the graph.

    Parameters

    • v1: K

      The first vertex.

    • v2: K

      The second vertex.

    Returns boolean

    True if the vertices are adjacent, false otherwise.