Add Dijkstra's algorithm to Typescript (#291)

This commit is contained in:
Renan Duarte Leal
2024-12-07 10:34:16 -03:00
committed by GitHub
parent 1d5567fe9a
commit add60334fb
2 changed files with 85 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
export interface Graph<T> {
[key: string]: T;
}
export class GraphIterable<T> implements Iterable<string> {
private graph: Graph<T>;
constructor(graph: Graph<T>) {
this.graph = graph;
}
[Symbol.iterator](): Iterator<string> {
const keys = Object.keys(this.graph);
let index = 0;
return {
next: (): IteratorResult<string> => {
if (index < keys.length) {
return { value: keys[index++], done: false };
} else {
return { value: undefined, done: true };
}
},
};
}
}