I have pieced together this topological sort in JavaScript, and have a graph based on this post:
const graph = {
edges: {
c: ['d', 'f'],
d: ['e'],
f: ['e'],
a: ['b', 'c'],
b: ['d', 'e'],
}
}
// Calcuate the incoming degree of each vertex
const vertices = Object.keys(graph.edges)
const inDegree = {}
for (const v of vertices) {
const neighbors = graph.edges[v]
neighbors?.forEach(neighbor => {
inDegree[neighbor] = inDegree[neighbor] + 1 || 1
})
}
const queue = vertices.filter((v) => !inDegree[v])
const list = []
while (queue.length) {
const v = queue.shift()
const neighbors = graph.edges[v]
list.push(v)
// adjust the incoming degree of its neighbors
neighbors?.forEach(neighbor => {
inDegree[neighbor]--
if (inDegree[neighbor] === 0) {
queue.push(neighbor)
}
})
}
console.log(list)
99% sure this is a correct implementation of topological sort in JS.
I am interested in doing hot-module reloading, and am interested in simulating updating the relevant nodes in the module graph. So say that d updated. Then we don't care about a, b, or c, they are fine, we only care about updating d and the future node then e, in the order [ d, e ]. We don't care about f because it's not inline with d.
How do I update this topsort function to take a key (the vertex/node), and from that point forward, include the elements, so I get [ d, e ] if I pass d?
Is it as simple as doing list.slice(list.indexOf('d')), or is there more trickiness to the generic/robust solution?
I don't think that is correct because if I did it for module b, we should only have to update [ b, d, e ], but my solution includes c, which is incorrect. Not sure how to go around that.
