-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdi-graph.ds.ts
More file actions
26 lines (20 loc) · 594 Bytes
/
Copy pathdi-graph.ds.ts
File metadata and controls
26 lines (20 loc) · 594 Bytes
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
import { IGraph } from './graph.ds.interface';
export class DiGraph implements IGraph<string> {
private nodes = new Map<string, string[]>();
addNode(nodeVal: string) {
this.nodes.set(nodeVal, []);
}
addEdge(source: string, destination: string) {
const sourceEdges = this.nodes.get(source);
if (!sourceEdges) {
throw new Error('Source node not found!');
}
if (!this.nodes.has(destination)) {
throw new Error('Destination node not found!');
}
sourceEdges.push(destination);
}
getGraph() {
return Object.fromEntries(this.nodes);
}
}