# UndirectedGraph | data-structure-typed
Documentation for data-structure-typed

# Class UndirectedGraph<V, E>

#### Type Parameters

- #### V extends UndirectedVertex

- #### E extends UndirectedEdge

#### Hierarchy

- AbstractGraph <V, E>
  - UndirectedGraph

- Defined in src/data-structures/graph/undirected-graph.ts:32

#####
Index

### Constructors

constructor

### Properties

_edges
_vertices

### Methods

addEdge
addVertex
bellmanFord
containsEdge
containsVertex
degreeOf
dijkstra
dijkstraWithoutHeap
edgeSet
edgesOf
floyd
getAllPathsBetween
getEdge
getEdgesOf
getEndsOfEdge
getMinCostBetween
getMinPathBetween
getNeighbors
getPathSumWeight
getVertex
getVertexId
removeAllVertices
removeEdge
removeEdgeBetween
removeVertex
setEdgeWeight
tarjan
vertexSet

## Constructors

### constructor

- new UndirectedGraph<V, E>(): UndirectedGraph <V, E>

- #### Type Parameters

  - #### V extends UndirectedVertex <V>

  - #### E extends UndirectedEdge <E>

  #### Returns UndirectedGraph <V, E>

  Overrides AbstractGraph.constructor

  - Defined in src/data-structures/graph/undirected-graph.ts:35

## Properties

### `Protected` _edges

_edges: Map<V, E[]> = ...

- Defined in src/data-structures/graph/undirected-graph.ts:33

### `Protected` _vertices

_vertices: Map<VertexId, V> = ...

Inherited from AbstractGraph._vertices

- Defined in src/data-structures/graph/abstract-graph.ts:59

## Methods

### addEdge

- addEdge(edge): boolean

- The function adds an edge to a graph by connecting two vertices.

  #### Parameters

  - ##### edge: E

    The `edge` parameter is an object of type `E`, which represents an edge in a graph.

  #### Returns boolean

  a boolean value.

  Overrides AbstractGraph.addEdge

  - Defined in src/data-structures/graph/undirected-graph.ts:67

### addVertex

- addVertex(newVertex): boolean

- The addVertex function adds a new vertex to a graph if it does not already exist.

  #### Parameters

  - ##### newVertex: V

    The parameter "newVertex" is of type V, which represents a vertex in a graph.

  #### Returns boolean

  The method is returning a boolean value. If the newVertex is already contained in the graph, it will return false. Otherwise, it will add the newVertex to the graph and return true.

  Inherited from AbstractGraph.addVertex

  - Defined in src/data-structures/graph/abstract-graph.ts:114

### bellmanFord

- bellmanFord(src, scanNegativeCycle?, getMin?, genPath?): {
  distMap: Map<V, number>;
  hasNegativeCycle: undefined | boolean;
  min: number;
  minPath: V[];
  paths: V[][];
  preMap: Map<V, V>;
  }

- BellmanFord time:O(VE) space:O(V) one to rest pairs The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.

  #### Parameters

  - ##### src: VertexId | V

    The `src` parameter is the source vertex from which the Bellman-Ford algorithm will start calculating the shortest paths. It can be either a vertex object or a vertex ID.

  - ##### `Optional` scanNegativeCycle: boolean

    A boolean flag indicating whether to scan for negative cycles in the graph.

  - ##### `Optional` getMin: boolean

    The `getMin` parameter is a boolean flag that determines whether the algorithm should calculate the minimum distance from the source vertex to all other vertices in the graph. If `getMin` is set to `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum

  - ##### `Optional` genPath: boolean

    A boolean flag indicating whether to generate paths for all vertices from the source vertex.

  #### Returns {
  distMap: Map<V, number>;
  hasNegativeCycle: undefined | boolean;
  min: number;
  minPath: V[];
  paths: V[][];
  preMap: Map<V, V>;
  }

  The function `bellmanFord` returns an object with the following properties:

  - ##### distMap: Map<V, number>

  - ##### hasNegativeCycle: undefined | boolean

  - ##### min: number

  - ##### minPath: V[]

  - ##### paths: V[][]

  - ##### preMap: Map<V, V>

  Inherited from AbstractGraph.bellmanFord

  - Defined in src/data-structures/graph/abstract-graph.ts:636

### containsEdge

- containsEdge(v1, v2): boolean

- The function checks if there is an edge between two vertices in a graph.

  #### Parameters

  - ##### v1: VertexId | V

    The parameter v1 can be either a VertexId or a V. A VertexId represents the identifier of a vertex in a graph, while V represents the type of the vertex itself.

  - ##### v2: VertexId | V

    The parameter `v2` represents the second vertex in an edge. It can be either a `VertexId` or a `V` type.

  #### Returns boolean

  The function `containsEdge` returns a boolean value. It returns `true` if there is an edge between the vertices `v1` and `v2`, and `false` otherwise.

  Inherited from AbstractGraph.containsEdge

  - Defined in src/data-structures/graph/abstract-graph.ts:163

### containsVertex

- containsVertex(vertexOrId): boolean

- The function checks if a vertex exists in a graph.

  #### Parameters

  - ##### vertexOrId: VertexId | V

    The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID (`VertexId`).

  #### Returns boolean

  The method `containsVertex` returns a boolean value.

  Inherited from AbstractGraph.containsVertex

  - Defined in src/data-structures/graph/abstract-graph.ts:94

### degreeOf

- degreeOf(vertexOrId): number

- The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that vertex.

  #### Parameters

  - ##### vertexOrId: VertexId | V

    The parameter `vertexOrId` can be either a `VertexId` or a `V`.

  #### Returns number

  The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of edges that are incident to that vertex.

  Overrides AbstractGraph.degreeOf

  - Defined in src/data-structures/graph/undirected-graph.ts:127

### dijkstra

- dijkstra(src, dest?, getMinDist?, genPaths?): DijkstraResult<V>

- Dijkstra algorithm time: O(logVE) space: O(V + E) The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an optional destination vertex, and optionally returns the minimum distance, the paths, and other information.

  #### Parameters

  - ##### src: VertexId | V

    The `src` parameter represents the source vertex from which the Dijkstra algorithm will start. It can be either a vertex object or a vertex ID.

  - ##### `Optional` dest: null | VertexId | V

    The `dest` parameter is the destination vertex or vertex ID. It specifies the vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm will calculate the shortest paths to all other vertices from the source vertex.

  - ##### `Optional` getMinDist: boolean

    The `getMinDist` parameter is a boolean flag that determines whether the minimum distance from the source vertex to the destination vertex should be calculated and returned in the result. If `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance

  - ##### `Optional` genPaths: boolean

    The `genPaths` parameter is a boolean flag that determines whether or not to generate paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the shortest paths from the source vertex to all other vertices in the graph. If `genPaths @returns The function` dijkstra`returns an object of type`DijkstraResult<V>`.

  #### Returns DijkstraResult<V>

  Inherited from AbstractGraph.dijkstra

  - Defined in src/data-structures/graph/abstract-graph.ts:511

### dijkstraWithoutHeap

- dijkstraWithoutHeap(src, dest?, getMinDist?, genPaths?): DijkstraResult<V>

- Dijkstra algorithm time: O(VE) space: O(V + E) The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in a graph without using a heap data structure.

  #### Parameters

  - ##### src: VertexId | V

    The source vertex from which to start the Dijkstra's algorithm. It can be either a vertex object or a vertex ID.

  - ##### `Optional` dest: null | VertexId | V

    The `dest` parameter in the `dijkstraWithoutHeap` function is an optional parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its identifier. If no destination is provided, the value is set to `null`.

  - ##### `Optional` getMinDist: boolean

    The `getMinDist` parameter is a boolean flag that determines whether the minimum distance from the source vertex to the destination vertex should be calculated and returned in the result. If `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance

  - ##### `Optional` genPaths: boolean

    The `genPaths` parameter is a boolean flag that determines whether or not to generate paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the shortest paths from the source vertex to all other vertices in the graph. If `genPaths @returns The function` dijkstraWithoutHeap`returns an object of type`DijkstraResult<V>`.

  #### Returns DijkstraResult<V>

  Inherited from AbstractGraph.dijkstraWithoutHeap

  - Defined in src/data-structures/graph/abstract-graph.ts:385

### edgeSet

- edgeSet(): E[]

- The function "edgeSet" returns an array of unique edges from a set of edges.

  #### Returns E[]

  The method `edgeSet()` returns an array of type `E[]`.

  Overrides AbstractGraph.edgeSet

  - Defined in src/data-structures/graph/undirected-graph.ts:156

### edgesOf

- edgesOf(vertexOrId): E[]

- The function "edgesOf" returns an array of edges connected to a given vertex.

  #### Parameters

  - ##### vertexOrId: VertexId | V

    The parameter `vertexOrId` can be either a `VertexId` or a `V`.

  #### Returns E[]

  an array of edges connected to the specified vertex. If the vertex exists in the graph, the function returns the array of edges connected to that vertex. If the vertex does not exist in the graph, the function returns an empty array.

  Overrides AbstractGraph.edgesOf

  - Defined in src/data-structures/graph/undirected-graph.ts:143

### floyd

- floyd(): {
  costs: number[][];
  predecessor: (null | V)[][];
  }

- Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle all pairs The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a graph.

  #### Returns {
  costs: number[][];
  predecessor: (null | V)[][];
  }

  The function `floyd()` returns an object with two properties: `costs` and `predecessor`. The `costs` property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest path between vertices in the

  - ##### costs: number[][]

  - ##### predecessor: (null | V)[][]

  Inherited from AbstractGraph.floyd

  - Defined in src/data-structures/graph/abstract-graph.ts:734

### getAllPathsBetween

- getAllPathsBetween(v1, v2): V[][]

- The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.

  #### Parameters

  - ##### v1: VertexId | V

    The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`). It is the starting vertex for finding paths.

  - ##### v2: VertexId | V

    The parameter `v2` represents the destination vertex or its ID. It is the vertex that we want to find paths to from the starting vertex `v1`.

  #### Returns V[][]

  an array of arrays of vertices (V[][]). Each inner array represents a path between the given vertices (v1 and v2).

  Inherited from AbstractGraph.getAllPathsBetween

  - Defined in src/data-structures/graph/abstract-graph.ts:202

### getEdge

- getEdge(v1, v2): null | E

- The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.

  #### Parameters

  - ##### v1: null | VertexId | V

    The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`). It can also be `null`.

  - ##### v2: null | VertexId | V

    The parameter `v2` represents a vertex or vertex ID. It can be of type `V` (vertex object), `null`, or `VertexId` (vertex ID).

  #### Returns null | E

  an edge (E) or null.

  Overrides AbstractGraph.getEdge

  - Defined in src/data-structures/graph/undirected-graph.ts:47

### getEdgesOf

- getEdgesOf(vertexOrId): E[]

- The function "getEdgesOf" returns an array of edges connected to a given vertex or vertex ID.

  #### Parameters

  - ##### vertexOrId: VertexId | V

    The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID (`VertexId`).

  #### Returns E[]

  an array of edges (E[]) that are connected to the specified vertex or vertex ID.

  - Defined in src/data-structures/graph/undirected-graph.ts:172

### getEndsOfEdge

- getEndsOfEdge(edge): null | [V, V]

- The function "getEndsOfEdge" returns the vertices at the ends of a given edge, or null if the edge does not exist in the graph.

  #### Parameters

  - ##### edge: E

    The parameter "edge" is of type E, which represents an edge in a graph.

  #### Returns null | [V, V]

  The function `getEndsOfEdge` returns an array containing two vertices `[V, V]` if the edge exists in the graph and both vertices are found. If the edge does not exist or one or both vertices are not found, it returns `null`.

  Overrides AbstractGraph.getEndsOfEdge

  - Defined in src/data-structures/graph/undirected-graph.ts:209

### getMinCostBetween

- getMinCostBetween(v1, v2, isWeight?): null | number

- The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge weights or using a breadth-first search algorithm.

  #### Parameters

  - ##### v1: VertexId | V

    The parameter `v1` represents the starting vertex or vertex ID of the graph.

  - ##### v2: VertexId | V

    The parameter `v2` represents the second vertex in the graph. It can be either a vertex object or a vertex ID.

  - ##### `Optional` isWeight: boolean

    isWeight is an optional parameter that indicates whether the graph edges have weights. If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of the edges. If isWeight is set to false or not provided, the function will calculate the

  #### Returns null | number

  The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1` and `v2`) in a graph. If the `isWeight` parameter is `true`, it calculates the minimum weight between the vertices. If `isWeight` is `false` or not provided, it calculates the minimum number of edges between the vertices. If the vertices are not

  Inherited from AbstractGraph.getMinCostBetween

  - Defined in src/data-structures/graph/abstract-graph.ts:260

### getMinPathBetween

- getMinPathBetween(v1, v2, isWeight?): null | V[]

- The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or using a breadth-first search algorithm.

  #### Parameters

  - ##### v1: VertexId | V

    The parameter `v1` represents the starting vertex or its ID.

  - ##### v2: VertexId | V

    The parameter `v2` represents the destination vertex or its ID. It is the vertex that we want to find the minimum path to from the source vertex `v1`.

  - ##### `Optional` isWeight: boolean

    A boolean flag indicating whether to consider the weight of edges in finding the minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set to false, the function will use breadth-first search (BFS) to find the minimum path. If

  #### Returns null | V[]

  The function `getMinPathBetween` returns an array of vertices (`V[]`) representing the minimum path between two vertices (`v1` and `v2`). If no path is found, it returns `null`.

  Inherited from AbstractGraph.getMinPathBetween

  - Defined in src/data-structures/graph/abstract-graph.ts:317

### getNeighbors

- getNeighbors(vertexOrId): V[]

- The function "getNeighbors" returns an array of neighboring vertices of a given vertex.

  #### Parameters

  - ##### vertexOrId: VertexId | V

    The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID (`VertexId`).

  #### Returns V[]

  an array of vertices (V[]).

  Overrides AbstractGraph.getNeighbors

  - Defined in src/data-structures/graph/undirected-graph.ts:186

### getPathSumWeight

- getPathSumWeight(path): number

- The function calculates the sum of weights along a given path.

  #### Parameters

  - ##### path: V[]

    An array of vertices (V) representing a path in a graph.

  #### Returns number

  The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.

  Inherited from AbstractGraph.getPathSumWeight

  - Defined in src/data-structures/graph/abstract-graph.ts:238

### getVertex

- getVertex(vertexOrId): null | V

- The function `getVertex` returns the vertex object associated with a given vertex ID or vertex object, or null if it does not exist.

  #### Parameters

  - ##### vertexOrId: VertexId | V

    The parameter `vertexOrId` can be either a `VertexId` or a `V`.

  #### Returns null | V

  The function `getVertex` returns the vertex object (`V`) corresponding to the given `vertexOrId` parameter. If the vertex is found in the `_vertices` map, it is returned. Otherwise, `null` is returned.

  Inherited from AbstractGraph.getVertex

  - Defined in src/data-structures/graph/abstract-graph.ts:72

### getVertexId

- getVertexId(vertexOrId): VertexId

- The function `getVertexId` returns the id of a vertex, whether it is passed as an instance of `AbstractVertex` or as a `VertexId`.

  #### Parameters

  - ##### vertexOrId: VertexId | V

    The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID (`VertexId`).

  #### Returns VertexId

  the id of the vertex.

  Inherited from AbstractGraph.getVertexId

  - Defined in src/data-structures/graph/abstract-graph.ts:84

### removeAllVertices

- removeAllVertices(vertices): boolean

- The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.

  #### Parameters

  - ##### vertices: VertexId[] | V[]

    The `vertices` parameter can be either an array of vertices (`V[]`) or an array of vertex IDs (`VertexId[]`).

  #### Returns boolean

  a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices were removed.

  Inherited from AbstractGraph.removeAllVertices

  - Defined in src/data-structures/graph/abstract-graph.ts:140

### removeEdge

- removeEdge(edge): null | E

- The removeEdge function removes an edge between two vertices in a graph.

  #### Parameters

  - ##### edge: E

    The parameter "edge" is of type E, which represents an edge in a graph.

  #### Returns null | E

  The method is returning either the removed edge (of type E) or null if the edge was not found.

  Overrides AbstractGraph.removeEdge

  - Defined in src/data-structures/graph/undirected-graph.ts:116

### removeEdgeBetween

- removeEdgeBetween(v1, v2): null | E

- The function removes an edge between two vertices in a graph and returns the removed edge, or null if either of the vertices does not exist.

  #### Parameters

  - ##### v1: VertexId | V

    The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).

  - ##### v2: VertexId | V

    V | VertexId: The second vertex or vertex ID of the edge to be removed.

  #### Returns null | E

  the removed edge (E) if it exists, or null if either of the vertices (v1 or v2) does not exist.

  Overrides AbstractGraph.removeEdgeBetween

  - Defined in src/data-structures/graph/undirected-graph.ts:90

### removeVertex

- removeVertex(vertexOrId): boolean

- The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.

  #### Parameters

  - ##### vertexOrId: VertexId | V

    The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID (`VertexId`).

  #### Returns boolean

  The method `removeVertex` returns a boolean value.

  Inherited from AbstractGraph.removeVertex

  - Defined in src/data-structures/graph/abstract-graph.ts:128

### setEdgeWeight

- setEdgeWeight(srcOrId, destOrId, weight): boolean

- The function sets the weight of an edge between two vertices in a graph.

  #### Parameters

  - ##### srcOrId: VertexId | V

    The `srcOrId` parameter can be either a `VertexId` or a `V` object. It represents the source vertex of the edge.

  - ##### destOrId: VertexId | V

    The `destOrId` parameter represents the destination vertex of the edge. It can be either a `VertexId` or a vertex object `V`.

  - ##### weight: number

    The weight parameter represents the weight of the edge between the source vertex (srcOrId) and the destination vertex (destOrId).

  #### Returns boolean

  a boolean value. If the edge exists between the source and destination vertices, the function will update the weight of the edge and return true. If the edge does not exist, the function will return false.

  Inherited from AbstractGraph.setEdgeWeight

  - Defined in src/data-structures/graph/abstract-graph.ts:181

### tarjan

- tarjan(needArticulationPoints?, needBridges?, needSCCs?, needCycles?): {
  SCCs: Map<number, V[]>;
  articulationPoints: V[];
  bridges: E[];
  cycles: Map<number, V[]>;
  dfnMap: Map<V, number>;
  lowMap: Map<V, number>;
  }

- Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs. Tarjan can find cycles in directed or undirected graph Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time, Tarjan solve the bi-connected components of undirected graphs; Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs. The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges, strongly connected components (SCCs), and cycles in a graph.

  #### Parameters

  - ##### `Optional` needArticulationPoints: boolean

    A boolean value indicating whether or not to calculate and return the articulation points in the graph. Articulation points are the vertices in a graph whose removal would increase the number of connected components in the graph.

  - ##### `Optional` needBridges: boolean

    A boolean flag indicating whether the algorithm should find and return the bridges (edges whose removal would increase the number of connected components in the graph).

  - ##### `Optional` needSCCs: boolean

    A boolean value indicating whether the Strongly Connected Components (SCCs) of the graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the SCCs will not be calculated or returned.

  - ##### `Optional` needCycles: boolean

    A boolean flag indicating whether the algorithm should find cycles in the graph. If set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values are arrays of vertices that form cycles within the SCCs.

  #### Returns {
  SCCs: Map<number, V[]>;
  articulationPoints: V[];
  bridges: E[];
  cycles: Map<number, V[]>;
  dfnMap: Map<V, number>;
  lowMap: Map<V, number>;
  }

  The function `tarjan` returns an object with the following properties:

  - ##### SCCs: Map<number, V[]>

  - ##### articulationPoints: V[]

  - ##### bridges: E[]

  - ##### cycles: Map<number, V[]>

  - ##### dfnMap: Map<V, number>

  - ##### lowMap: Map<V, number>

  Inherited from AbstractGraph.tarjan

  - Defined in src/data-structures/graph/abstract-graph.ts:794

### vertexSet

- vertexSet(): Map<VertexId, V>

- The function `vertexSet()` returns a map of vertices.

  #### Returns Map<VertexId, V>

  The method `vertexSet()` returns a map of vertex IDs to vertex objects.

  Inherited from AbstractGraph.vertexSet

  - Defined in src/data-structures/graph/abstract-graph.ts:102

Generated using TypeDoc