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
49 changes: 47 additions & 2 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,59 @@ export class HashGraph {
return result;
}

kahnAlgorithm(origin: Hash, subgraph: ObjectSet<Hash>): Hash[] {
JanLewDev marked this conversation as resolved.
Show resolved Hide resolved
const result: Hash[] = [];
const inDegree = new Map<Hash, number>();
const queue: Hash[] = [];

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

for (const [_, children] of this.forwardEdges) {
for (const child of children) {
inDegree.set(child, (inDegree.get(child) || 0) + 1);
}
}
trungnotchung marked this conversation as resolved.
Show resolved Hide resolved

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 (!subgraph.has(child)) continue;
trungnotchung marked this conversation as resolved.
Show resolved Hide resolved
const inDegreeValue = inDegree.get(child);
if (inDegreeValue === undefined) {
log.error("::hashgraph::Kahn: Undefined in-degree value");
return [];
}
inDegree.set(child, inDegreeValue - 1);
if (inDegree.get(child) === 0) {
JanLewDev marked this conversation as resolved.
Show resolved Hide resolved
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.kahnAlgorithm(origin, subgraph);
JanLewDev marked this conversation as resolved.
Show resolved Hide resolved
if (!updateBitsets) return result;
this.reachablePredecessors.clear();
this.topoSortedIndex.clear();
Expand Down
18 changes: 9 additions & 9 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 Expand Up @@ -602,7 +602,7 @@ describe("Vertex timestamp tests", () => {
test("Test: Vertex's timestamp must not be less than any of its dependencies' timestamps", () => {
/*
__ V1:ADD(1) __
/ \
trungnotchung marked this conversation as resolved.
Show resolved Hide resolved
/ \
ROOT -- V2:ADD(2) ---- V4:ADD(4) (invalid)
\ /
-- V3:ADD(3) --
Expand Down
Loading