Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use Kahn's algorithm for finding toposort #312

Merged
merged 10 commits into from
Jan 3, 2025
47 changes: 45 additions & 2 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,57 @@ export class HashGraph {
return result;
}

kahnsAlgorithm(origin: Hash, subgraph: ObjectSet<Hash>): Hash[] {
const result: Hash[] = [];
const inDegree = new Map<Hash, number>();
const queue: Hash[] = [];

for (const hash of subgraph.entries()) {
inDegree.set(hash, 0);
}

for (const [vertex, children] of this.forwardEdges) {
if (!inDegree.has(vertex)) continue;
for (const child of children) {
if (!inDegree.has(child)) continue;
inDegree.set(child, (inDegree.get(child) || 0) + 1);
}
}

let head = 0;
queue.push(origin);
while (queue.length > 0) {
const current = queue[head];
head++;
if (!current) continue;

result.push(current);

for (const child of this.forwardEdges.get(current) || []) {
if (!inDegree.has(child)) continue;
const inDegreeValue = inDegree.get(child) || 0;
inDegree.set(child, inDegreeValue - 1);
if (inDegreeValue - 1 === 0) {
queue.push(child);
}
}

if (head > queue.length / 2) {
queue.splice(0, head);
head = 0;
}
}

return result;
}

/* Topologically sort the vertices in the whole hashgraph or the past of a given vertex. */
topologicalSort(
updateBitsets = false,
origin: Hash = HashGraph.rootHash,
subgraph: ObjectSet<Hash> = new ObjectSet(this.vertices.keys()),
): Hash[] {
const result = this.depthFirstSearch(origin, subgraph);
result.reverse();
const result = this.kahnsAlgorithm(origin, subgraph);
d-roak marked this conversation as resolved.
Show resolved Hide resolved
if (!updateBitsets) return result;
this.reachablePredecessors.clear();
this.topoSortedIndex.clear();
Expand Down
16 changes: 8 additions & 8 deletions packages/object/tests/hashgraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ describe("HashGraph construction tests", () => {

const linearOps = obj2.hashGraph.linearizeOperations();
expect(linearOps).toEqual([
{ type: "add", value: 1 },
{ type: "add", value: 2 },
{ type: "add", value: 1 },
]);
});

Expand Down Expand Up @@ -170,8 +170,8 @@ describe("HashGraph for AddWinSet tests", () => {
const linearOps = obj1.hashGraph.linearizeOperations();
expect(linearOps).toEqual([
{ type: "add", value: 1 },
{ type: "add", value: 2 },
{ type: "remove", value: 1 },
{ type: "add", value: 2 },
]);
});

Expand Down Expand Up @@ -204,8 +204,8 @@ describe("HashGraph for AddWinSet tests", () => {
expect(linearOps).toEqual([
{ type: "add", value: 1 },
{ type: "add", value: 1 },
{ type: "remove", value: 5 },
{ type: "add", value: 10 },
{ type: "remove", value: 5 },
]);
});

Expand Down Expand Up @@ -235,9 +235,9 @@ describe("HashGraph for AddWinSet tests", () => {

const linearOps = obj1.hashGraph.linearizeOperations();
expect(linearOps).toEqual([
{ type: "add", value: 1 },
{ type: "add", value: 1 },
{ type: "add", value: 2 },
{ type: "add", value: 1 },
]);
});

Expand Down Expand Up @@ -288,12 +288,12 @@ describe("HashGraph for AddWinSet tests", () => {

const linearOps = obj1.hashGraph.linearizeOperations();
expect(linearOps).toEqual([
{ type: "add", value: 1 },
{ type: "add", value: 1 },
{ type: "remove", value: 2 },
{ type: "add", value: 2 },
{ type: "add", value: 1 },
{ type: "add", value: 3 },
{ type: "remove", value: 1 },
{ type: "add", value: 3 },
]);
});

Expand Down Expand Up @@ -345,11 +345,11 @@ describe("HashGraph for AddWinSet tests", () => {
const linearOps = obj1.hashGraph.linearizeOperations();
expect(linearOps).toEqual([
{ type: "add", value: 1 },
{ type: "remove", value: 2 },
{ type: "add", value: 1 },
{ type: "remove", value: 2 },
{ type: "add", value: 3 },
{ type: "remove", value: 2 },
{ type: "remove", value: 1 },
{ type: "add", value: 3 },
{ type: "add", value: 2 },
{ type: "remove", value: 1 },
]);
Expand Down
Loading