-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
192 lines (174 loc) · 5.55 KB
/
Copy pathindex.ts
File metadata and controls
192 lines (174 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* Graph data structure implementation using an adjacency list.
* This implementation supports adding vertices and edges, retrieving all vertices,
* retrieving all edges, and getting the neighbors of a specific vertex.
*
* @class AdjacencyListGraph
* @description A class representing a graph using an adjacency list.
* @example
* ```typescript
* const graph = new AdjacencyListGraph()
* graph.addVertex('A')
* graph.addVertex('B')
* graph.addEdge('A', 'B')
* console.log(graph.getVertices()) // ['A', 'B']
* console.log(graph.getEdges()) // [['A', 'B']]
* ```
*/
export class AdjacencyListGraph {
private adjacencyList: Map<string, string[]>
constructor() {
this.adjacencyList = new Map()
}
/**
* Adds a vertex to the graph.
* If the vertex already exists, it will not be added again.
* @param {string} vertex The vertex to add to the graph.
*/
addVertex(vertex: string): void {
if (!this.adjacencyList.has(vertex)) {
this.adjacencyList.set(vertex, [])
}
}
/**
* Adds an edge between two vertices in the graph.
* If either vertex does not exist, an error will be thrown.
* If the edge already exists, it will not be added again.
* @param {string} vertex1 The first vertex of the edge.
* @param {string} vertex2 The second vertex of the edge.
* @throws Error if either vertex does not exist.
* @example
* ```typescript
* graph.addEdge('A', 'B')
* console.log(graph.getEdges()) // [['A', 'B']]
* ```
*/
addEdge(vertex1: string, vertex2: string): void {
if (!this.adjacencyList.has(vertex1) || !this.adjacencyList.has(vertex2)) {
throw new Error('One or both vertices do not exist')
}
this.adjacencyList.get(vertex1)!.push(vertex2)
this.adjacencyList.get(vertex2)!.push(vertex1) // Assuming an undirected graph
}
/**
* Retrieves all vertices in the graph.
* @returns An array of vertices in the graph.
* @example
* ```typescript
* console.log(graph.getVertices()) // ['A', 'B']
* ```
*/
getVertices(): string[] {
return Array.from(this.adjacencyList.keys())
}
/**
* Retrieves all edges in the graph.
* Each edge is represented as a tuple of two vertices.
* @returns {[string, string][]} An array of edges in the graph.
* @example
* ```typescript
* console.log(graph.getEdges()) // [['A', 'B']]
* ```
*/
getEdges(): [string, string][] {
const edges: [string, string][] = []
const visited = new Set<string>()
for (const [vertex, neighbors] of this.adjacencyList.entries()) {
for (const neighbor of neighbors) {
if (!visited.has(`${neighbor}-${vertex}`)) {
edges.push([vertex, neighbor])
}
}
visited.add(`${vertex}-${neighbors}`)
}
return edges
}
/**
* Retrieves the neighbors of a specific vertex.
* @param {string} vertex The vertex whose neighbors are to be retrieved.
* @returns {string[]} An array of neighbors of the specified vertex.
* @throws Error if the vertex does not exist.
* @example
* ```typescript
* console.log(graph.getNeighbors('A')) // ['B']
* ```
*/
getNeighbors(vertex: string): string[] {
if (!this.adjacencyList.has(vertex)) {
throw new Error('Vertex does not exist')
}
return this.adjacencyList.get(vertex)!
}
/**
* Performs a breadth-first search (BFS) starting from a given vertex.
* @param {string} startVertex The vertex to start the BFS from.
* @returns {string[]} An array of vertices visited in BFS order.
* @throws Error if the start vertex does not exist.
* @example
* ```typescript
* const graph = new AdjacencyListGraph()
* graph.addVertex('A')
* graph.addVertex('B')
* graph.addVertex('C')
* graph.addEdge('A', 'B')
* graph.addEdge('A', 'C')
* console.log(graph.breadthSearch('A')) // ['A', 'B', 'C']
* ```
*/
breadthSearch(startVertex: string): string[] {
if (!this.adjacencyList.has(startVertex)) {
throw new Error('Start vertex does not exist')
}
const visited = new Set<string>()
const queue: string[] = [startVertex]
const result: string[] = []
while (queue.length > 0) {
const currentVertex = queue.shift()!
if (!visited.has(currentVertex)) {
visited.add(currentVertex)
result.push(currentVertex)
const neighbors = this.adjacencyList.get(currentVertex)!
for (const neighbor of neighbors) {
if (!visited.has(neighbor)) {
queue.push(neighbor)
}
}
}
}
return result
}
/**
* Performs a depth-first search (DFS) starting from a given vertex.
* @param {string} startVertex The vertex to start the DFS from.
* @returns {string[]} An array of vertices visited in DFS order.
* @throws Error if the start vertex does not exist.
* @example
* ```typescript
* const graph = new AdjacencyListGraph()
* graph.addVertex('A')
* graph.addVertex('B')
* graph.addVertex('C')
* graph.addEdge('A', 'B')
* graph.addEdge('A', 'C')
* console.log(graph.depthSearch('A')) // ['A', 'B', 'C']
* ```
*/
depthSearch(startVertex: string): string[] {
if (!this.adjacencyList.has(startVertex)) {
throw new Error('Start vertex does not exist')
}
const visited = new Set<string>()
const result: string[] = []
const dfs = (vertex: string) => {
if (visited.has(vertex)) return
visited.add(vertex)
result.push(vertex)
const neighbors = this.adjacencyList.get(vertex)!
for (const neighbor of neighbors) {
dfs(neighbor)
}
}
dfs(startVertex)
return result
}
}