An interactive Graph Editor & Tree DP Visualizer for competitive programming — build trees visually, write DP formulas in a mini DSL, run them, and see results on every node.
🔗 Live: https://angelator312.github.io/tree-dp-and-graph-editor/
- Visual Graph Editor — Add, connect, edit, drag nodes on a canvas
- Tree DP Engine — Write formulas, run with Ctrl+Enter, see results instantly
- 30+ DSL Functions — sum, prod, min, max, abs, gcd, lcm, filter, map, prefix, suffix, and more
- Pre-built Examples — 9 classic tree DP problems ready to load (Subtree Size, Max Independent Set, Min Vertex Cover, Tree Diameter, Sum of Distances, Tree Matching, Tree Coloring, Longest Path)
- Custom Formulas — Save your own formulas to the dropdown for quick access
- All Tree DP Types — Subtree (bottom-up), Rerooting (top-down via
par()), multi-state, edge-weighted - CP Format Export — Copy tree data in competitive programming format
- Undo/Redo, Import/Export, localStorage persistence
- Example Files — All example formulas available in the
examples/folder
- Open the live site
- Click Add (A) mode and click on the canvas to add nodes
- Switch to Connect (C) mode and click two nodes to create an edge
- Pick an example from the Examples dropdown, or write your own formula
- Click Run (Ctrl+Enter) to compute DP values
- Check the Data tab for a table of all node values
| Variable | Description |
|---|---|
val |
Node weight |
children |
Array of child IDs |
edgeWeight |
Edge weight to this node |
id |
Node ID |
childCount |
Number of children |
isLeaf |
1 if leaf, 0 otherwise |
depth |
Depth from root (root = 0) |
subtreeSize |
Subtree size including self |
n |
Total number of nodes |
Aggregation: sum, prod, max, min, count, avg
Math: abs, floor, ceil, sqrt, log, log2, pow, mod, gcd, lcm, sign, clamp
Access: par(var), len(arr), allNodes(), findNodes(condition)
Arrays: range, map, filter, sort (supports "asc"/"desc" as last arg), reverse, concat, unique, flatten, prefix, suffix, slice, indexOf
+ - * / % ^ == != < > <= >= && || ! ? :
# Subtree Size
sz = sum(children, sz) + 1
# Max Independent Set
dp0 = sum(children, max({dp0, dp1}))
dp1 = sum(children, dp0) + val
# Sum of Distances (Rerooting)
sz = sum(children, sz) + 1
down = sum(children, down + sz)
ans = par(ans) - sz + (n - sz) + down
# Count nodes matching condition
highWeightCount = len(findNodes(val > 5))
leafCount = len(findNodes(isLeaf == 1))
# get root weight using findNodes and map
rootWeight = map(findNodes(isRoot == 1), val)[0]
# Sort children by DP value descending
topChild = sort(children, dp, "desc")[0]
- Fork or clone this repo
- Go to Settings → Pages → Source: main branch, / (root)
- Your site is live!