|
| 1 | +# CLRS Patterns |
| 2 | + |
| 3 | +Use these patterns as first defaults for standard data structures before inventing new selectors. |
| 4 | + |
| 5 | +## Linked structures (stack/queue/list) |
| 6 | + |
| 7 | +Recommended defaults: |
| 8 | + |
| 9 | +- Linear flow: `orientation(selector="next", directions=["directlyRight"])` |
| 10 | +- Show payload: `attribute(field="data")` |
| 11 | +- Hide sentinels/primitive noise: `hideAtom(selector="NoneType + int + str")` (adjust per model) |
| 12 | + |
| 13 | +```python |
| 14 | +import spytial |
| 15 | + |
| 16 | +@spytial.orientation(selector="next", directions=["directlyRight"]) |
| 17 | +@spytial.attribute(field="data") |
| 18 | +class Node: |
| 19 | + def __init__(self, data, nxt=None): |
| 20 | + self.data = data |
| 21 | + self.next = nxt |
| 22 | +``` |
| 23 | + |
| 24 | +## Trees (BST/RB/heap-like) |
| 25 | + |
| 26 | +Recommended defaults: |
| 27 | + |
| 28 | +- Left branch: `orientation(..., ["below", "left"])` |
| 29 | +- Right branch: `orientation(..., ["below", "right"])` |
| 30 | +- Optional sibling alignment: `align(..., direction="horizontal")` |
| 31 | +- Surface key metadata with `attribute(field="key")` |
| 32 | + |
| 33 | +```python |
| 34 | +import spytial |
| 35 | + |
| 36 | +@spytial.orientation(selector="left & (TreeNode->TreeNode)", directions=["below", "left"]) |
| 37 | +@spytial.orientation(selector="right & (TreeNode->TreeNode)", directions=["below", "right"]) |
| 38 | +@spytial.attribute(field="key") |
| 39 | +class TreeNode: |
| 40 | + def __init__(self, key, left=None, right=None): |
| 41 | + self.key = key |
| 42 | + self.left = left |
| 43 | + self.right = right |
| 44 | +``` |
| 45 | + |
| 46 | +## Graphs from adjacency structures |
| 47 | + |
| 48 | +Recommended defaults: |
| 49 | + |
| 50 | +- Derive explicit edges with `inferredEdge` |
| 51 | +- Hide raw container atoms (`list`, tuples, helper wrappers) |
| 52 | +- Keep node labels through `attribute(field="key")` or domain field names |
| 53 | + |
| 54 | +```python |
| 55 | +import spytial |
| 56 | + |
| 57 | +graph = spytial.inferredEdge( |
| 58 | + selector="{a, b : Node | b in a.neighbors}", |
| 59 | + name="edge", |
| 60 | +)(graph_obj) |
| 61 | +graph = spytial.hideAtom(selector="list + tuple")(graph) |
| 62 | +spytial.diagram(graph) |
| 63 | +``` |
| 64 | + |
| 65 | +## Hash-table and bucketed layouts |
| 66 | + |
| 67 | +Recommended defaults: |
| 68 | + |
| 69 | +- Group buckets with selector-based `group(...)` |
| 70 | +- Orient chain relations directly left/right |
| 71 | +- Hide housekeeping fields such as `prev` where needed |
| 72 | + |
| 73 | +Selectors from CLRS-style examples often look like: |
| 74 | + |
| 75 | +- `group(selector="(NoneType.~key) - ((iden & next).Node)", name="T")` |
| 76 | +- `orientation(selector="next & (Node->Node)", directions=["directlyRight"])` |
| 77 | + |
| 78 | +## Matrix / DP table layouts |
| 79 | + |
| 80 | +Recommended defaults: |
| 81 | + |
| 82 | +- Build row and column selectors |
| 83 | +- `align` rows and columns |
| 84 | +- Add directional orientation across row/column deltas |
| 85 | + |
| 86 | +Pattern from memoization examples: |
| 87 | + |
| 88 | +- `align(selector=SAME_ROW, direction="horizontal")` |
| 89 | +- `align(selector=SAME_COL, direction="vertical")` |
| 90 | +- `orientation(selector=DIFF_ROWS, directions=["below"])` |
| 91 | +- `orientation(selector=DIFF_COLS, directions=["right"])` |
| 92 | + |
| 93 | +## Disjoint sets / grouped regions |
| 94 | + |
| 95 | +Recommended defaults: |
| 96 | + |
| 97 | +- Group by selector to expose set membership |
| 98 | +- Hide technical atoms (`int`, helper lists) after structure checks |
| 99 | +- Keep representative fields visible with `attribute(...)` |
| 100 | + |
| 101 | +## Picking the closest notebook |
| 102 | + |
| 103 | +Map use case to source notebook: |
| 104 | + |
| 105 | +- Stacks/queues: `spytial-clrs/src/stacksqueues.ipynb` |
| 106 | +- Linked lists: `spytial-clrs/src/linked-lists.ipynb` |
| 107 | +- Heaps: `spytial-clrs/src/heaps.ipynb` |
| 108 | +- Trees: `spytial-clrs/src/trees.ipynb` |
| 109 | +- Hash tables: `spytial-clrs/src/hash-tables.ipynb` |
| 110 | +- Graphs: `spytial-clrs/src/graphs.ipynb` |
| 111 | +- Disjoint sets: `spytial-clrs/src/disjoint-sets.ipynb` |
| 112 | +- Memoization tables: `spytial-clrs/src/memoization.ipynb` |
0 commit comments