I made ER diagrams with the fewest line crossings, and proved it
I was modelling the ER diagram for a new project, and it got messy quickly once I had to share it with other people. Lines crossed everywhere, and nobody could follow which table pointed where. Auto-layout tools place the boxes sensibly, but the ones I have used don't count how many relationship lines cross, so nothing stops the mess. So I built erlayout to do that one thing: lay out an ER…
The author created an ER diagram layout tool called erlayout that minimizes line crossings and can prove the result is optimal. The tool takes SQL DDL, DBML or JSON input and determines how to arrange the tables in columns and rows to minimize crossings. It then demonstrates that no other layout within its model could have fewer crossings.
The Sakila sample database, with 15 tables and 22 foreign keys, goes from 8 crossings in the standard layered layout to 2 crossings in the erlayout optimized layout, which is proven to be optimal. The tool works by modeling tables in vertical columns, with foreign keys crossing gaps between columns, and counting the number of inverted pairs of edges spanning each gap to calculate crossings.
An optimization pass further shortens the lines after the initial layout is found. For larger schemas, erlayout breaks the schema into groups of tables that reference each other often and solves each group separately, arranging the diagrams in a grid. The resulting drawing can be exported to draw.io, Excalidraw or Mermaid diagrams.
The author invites contributions to improve the tool, such as drawing self-referential foreign keys as loops, adding adapters for Laravel migrations, and improving lower bounds for dense schemas.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.