|
| 1 | +// Graph visualization library for the problem-reductions paper |
| 2 | +#import "@preview/cetz:0.4.2": canvas, draw |
| 3 | + |
| 4 | +// ── Style defaults ───────────────────────────────────────────── |
| 5 | + |
| 6 | +// Color palette for k-coloring visualizations |
| 7 | +#let graph-colors = (rgb("#4e79a7"), rgb("#e15759"), rgb("#76b7b2")) |
| 8 | + |
| 9 | +// Weight-based fill colors for grid graph nodes |
| 10 | +#let weight-color(w) = if w == 1 { blue } else if w == 2 { red } else { green } |
| 11 | + |
| 12 | +// ── Primitives: g-node, g-edge ───────────────────────────────── |
| 13 | +// All graph drawing goes through these two functions. |
| 14 | +// They define the standard style; callers can override any parameter. |
| 15 | + |
| 16 | +// Draw a single graph node. |
| 17 | +// pos: (x, y) position |
| 18 | +// name: CetZ element name (for edge references) |
| 19 | +// label: none or content to place inside the node |
| 20 | +#let g-node( |
| 21 | + pos, |
| 22 | + name: none, |
| 23 | + radius: 0.2, |
| 24 | + fill: white, |
| 25 | + stroke: 0.5pt, |
| 26 | + label: none, |
| 27 | + label-size: 8pt, |
| 28 | +) = { |
| 29 | + draw.circle(pos, radius: radius, fill: fill, stroke: stroke, name: name) |
| 30 | + if label != none { |
| 31 | + draw.content(name, text(label-size, label)) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// Draw a single graph edge between two named nodes or positions. |
| 36 | +#let g-edge( |
| 37 | + from, |
| 38 | + to, |
| 39 | + stroke: 1pt + black, |
| 40 | +) = { |
| 41 | + draw.line(from, to, stroke: stroke) |
| 42 | +} |
| 43 | + |
| 44 | +// ── Pre-defined graph layouts ────────────────────────────────── |
| 45 | +// Each returns (vertices: [...], edges: [...]) |
| 46 | + |
| 47 | +// Petersen graph: outer pentagon (0-4) + inner star (5-9) |
| 48 | +#let petersen-graph() = { |
| 49 | + let r-outer = 1.2 |
| 50 | + let r-inner = 0.6 |
| 51 | + let vertices = () |
| 52 | + for i in range(5) { |
| 53 | + let angle = 90deg - i * 72deg |
| 54 | + vertices.push((calc.cos(angle) * r-outer, calc.sin(angle) * r-outer)) |
| 55 | + } |
| 56 | + for i in range(5) { |
| 57 | + let angle = 90deg - i * 72deg |
| 58 | + vertices.push((calc.cos(angle) * r-inner, calc.sin(angle) * r-inner)) |
| 59 | + } |
| 60 | + let edges = ( |
| 61 | + (0,1),(0,4),(0,5),(1,2),(1,6),(2,3),(2,7),(3,4),(3,8),(4,9), |
| 62 | + (5,7),(5,8),(6,8),(6,9),(7,9), |
| 63 | + ) |
| 64 | + (vertices: vertices, edges: edges) |
| 65 | +} |
| 66 | + |
| 67 | +// House graph: square base (0-1-3-2) + triangle roof (2-3-4) |
| 68 | +#let house-graph() = { |
| 69 | + let vertices = ((0, 0), (1, 0), (0, 1), (1, 1), (0.5, 1.7)) |
| 70 | + let edges = ((0,1),(0,2),(1,3),(2,3),(2,4),(3,4)) |
| 71 | + (vertices: vertices, edges: edges) |
| 72 | +} |
| 73 | + |
| 74 | +// Octahedral graph (K_{2,2,2}): 6 vertices, 12 edges |
| 75 | +// Layout: top/bottom poles with 4 equatorial vertices |
| 76 | +#let octahedral-graph() = { |
| 77 | + let vertices = ( |
| 78 | + (0, -1.2), // 0: bottom pole |
| 79 | + (-1.0, 0), // 1: left |
| 80 | + (0, 0.5), // 2: upper-center |
| 81 | + (0, -0.5), // 3: lower-center |
| 82 | + (1.0, 0), // 4: right |
| 83 | + (0, 1.2), // 5: top pole |
| 84 | + ) |
| 85 | + let edges = ( |
| 86 | + (0,1),(0,2),(0,3),(0,4),(1,2),(1,3),(1,5),(2,4),(2,5),(3,4),(3,5),(4,5), |
| 87 | + ) |
| 88 | + (vertices: vertices, edges: edges) |
| 89 | +} |
| 90 | + |
| 91 | +// ── Grid graph functions (JSON-driven) ───────────────────────── |
| 92 | +// Extract positions from JSON, draw with dense styling via g-node/g-edge. |
| 93 | + |
| 94 | +// King's subgraph from JSON with weight-based coloring |
| 95 | +#let draw-grid-graph(data, cell-size: 0.2) = canvas(length: 1cm, { |
| 96 | + let grid-data = data.grid_graph |
| 97 | + let positions = grid-data.nodes.map(n => (n.col * cell-size, -n.row * cell-size)) |
| 98 | + let fills = grid-data.nodes.map(n => weight-color(n.weight)) |
| 99 | + let edges = grid-data.edges.map(e => (e.at(0), e.at(1))) |
| 100 | + for (u, v) in edges { g-edge(positions.at(u), positions.at(v), stroke: 0.4pt + gray) } |
| 101 | + for (k, pos) in positions.enumerate() { |
| 102 | + g-node(pos, radius: 0.04, stroke: none, fill: fills.at(k)) |
| 103 | + } |
| 104 | +}) |
| 105 | + |
| 106 | +// Triangular lattice from JSON with weight-based coloring |
| 107 | +// Matches Rust GridGraph::physical_position_static for Triangular (offset_even_cols=true) |
| 108 | +#let draw-triangular-graph(data, cell-size: 0.2) = canvas(length: 1cm, { |
| 109 | + let grid-data = data.grid_graph |
| 110 | + let sqrt3_2 = calc.sqrt(3) / 2 |
| 111 | + let positions = grid-data.nodes.map(n => { |
| 112 | + let offset = if calc.rem(n.col, 2) == 0 { 0.5 } else { 0.0 } |
| 113 | + ((n.row + offset) * cell-size, -n.col * sqrt3_2 * cell-size) |
| 114 | + }) |
| 115 | + let fills = grid-data.nodes.map(n => weight-color(n.weight)) |
| 116 | + let edges = grid-data.edges.map(e => (e.at(0), e.at(1))) |
| 117 | + for (u, v) in edges { g-edge(positions.at(u), positions.at(v), stroke: 0.3pt + gray) } |
| 118 | + for (k, pos) in positions.enumerate() { |
| 119 | + g-node(pos, radius: 0.025, stroke: none, fill: fills.at(k)) |
| 120 | + } |
| 121 | +}) |
0 commit comments