MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / JobGraph

Class JobGraph

graphs/topological-sort.js:43–67  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

41}
42
43class JobGraph {
44 constructor(jobs) {
45 this.nodes = [];
46 this.graph = {};
47 for (const job of jobs) {
48 this.addNode(job);
49 }
50 }
51
52 addPrereq(job, prereq) {
53 const jobNode = this.getNode(job);
54 const prereqNode = this.getNode(prereq);
55 jobNode.prereqs.push(prereqNode);
56 }
57
58 getNode(job) {
59 if (!this.graph.hasOwnProperty(job)) this.addNode(job);
60 return this.graph[job];
61 }
62
63 addNode(job) {
64 this.graph[job] = new JobNode(job);
65 this.nodes.push(this.graph[job]);
66 }
67}
68
69class JobNode {
70 constructor(job) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected