private adjacencyList: Map<string, string[]>;
this.adjacencyList = new Map();
addVertex(vertex: string): void {
if (!this.adjacencyList.has(vertex)) {
this.adjacencyList.set(vertex, []);
// Add an undirected edge
addEdge(vertex1: string, vertex2: string): void {
// Add edges (undirected)
this.adjacencyList.get(vertex1)!.push(vertex2);
this.adjacencyList.get(vertex2)!.push(vertex1);
// Check if an edge exists
hasEdge(vertex1: string, vertex2: string): boolean {
const neighbors = this.adjacencyList.get(vertex1);
return neighbors ? neighbors.includes(vertex2) : false;
const graph = new Graph();
console.log(graph.hasEdge("A", "B")); // Output: true
console.log(graph.hasEdge("A", "C")); // Output: false