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

[DO NOT MERGE] try Kahn's toposort #2833

Closed
wants to merge 2 commits into from
Closed

Conversation

dmaskasky
Copy link
Collaborator

@dmaskasky dmaskasky commented Nov 22, 2024

FOR DEMONSTRATION PURPOSES ONLY

Related: #2821 (comment)

Summary

Kahn's Topological Sort uses an iterative BFS approach to sort nodes in a DAG. It uses in-degrees a count of the number of remaining unsorted dependencies a node as. As nodes are sorted, in-degrees is decremented. When in-degrees reaches zero for a node, that node is appended to the sorted set.

The difficulty with using Kahn's in our application is that we first need to do a bunch of expensive admin work.

  • construct a map of all dependent nodes to their dependents. Implemented here is an iterative BFS solution, but a DFS approach has the same complexity
  • create a map of every atom to their in-degree
  • iterate through all atoms and enqueu where in-degree === 0

The result averages (100x) show that Kahns runs ~25% slower than iterative DFS.

--------------------------------------------------------------------------------
LINEAR
kahnsToposort: 25.4166
getSortedDependents: 20.7627
kahn times are 22.4148 percent slower than iterative DFS
--------------------------------------------------------------------------------
STAR
kahnsToposort: 22.9688
getSortedDependents: 19.8854
kahn times are 15.5060 percent slower than iterative DFS
--------------------------------------------------------------------------------
K-ARY
kahnsToposort: 26.1628
getSortedDependents: 21.1882
kahn times are 23.4780 percent slower than iterative DFS

Copy link

vercel bot commented Nov 22, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jotai ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 22, 2024 5:07am

Copy link

codesandbox-ci bot commented Nov 22, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link

github-actions bot commented Nov 22, 2024

LiveCodes Preview in LiveCodes

Latest commit: b7bc3f1
Last updated: Nov 22, 2024 5:07am (UTC)

Playground Link
React demo https://livecodes.io?x=id/7UMXHYEPQ

See documentations for usage instructions.

@dmaskasky
Copy link
Collaborator Author

Update: if I use indexes instead of map, its a little faster but more ugly.

LINEAR --------------------------------------------------------------------------------
kahnsToposort: 18.4981
getSortedDependents: 18.0733
kahn times are 2.3504 percent slower than toposort
STAR --------------------------------------------------------------------------------
kahnsToposort: 19.8348
getSortedDependents: 20.3942
kahn times are 2.7431 percent faster than toposort
K-ARY --------------------------------------------------------------------------------
kahnsToposort: 20.2995
getSortedDependents: 20.3445
kahn times are 0.2209 percent faster than toposort

@dmaskasky
Copy link
Collaborator Author

Update: using arrays seems to help

LINEAR --------------------------------------------------------------------------------
kahnsToposort: 20.1562
getSortedDependents: 20.0564
kahn times are 0.4973 percent slower than toposort
STAR --------------------------------------------------------------------------------
kahnsToposort: 17.1281
getSortedDependents: 20.2366
kahn times are 15.3605 percent faster than toposort
K-ARY --------------------------------------------------------------------------------
kahnsToposort: 19.4536
getSortedDependents: 19.6978
kahn times are 1.2398 percent faster than toposort

@dmaskasky
Copy link
Collaborator Author

Closing this for now. While Kahn's algorithm is slightly faster for extremely large K-Ary and Star graphs, it comes with higher memory overhead and performs worse on smaller atom graphs. Given that the average case involves smaller atom graphs, I don't believe the added complexity of Kahn's algorithm is justified.

@dmaskasky dmaskasky closed this Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant