|
| 1 | +# Minimum Spanning Tree |
| 2 | + |
| 3 | +A minimum spanning tree (MST) is an example of a visualization of [Prim's algorithm](https://en.wikipedia.org/wiki/Prim%27s_algorithm) for finding the shortest lengths connecting all randomly placed dots. All of the points are [connected](https://visualgo.net/en/mst) in a tree path with the shortest total edge lengths, with only one path (no 'cycle'). |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## How Prim's Algorithm Works |
| 8 | + |
| 9 | +1. Start with any vertex (point) in the graph |
| 10 | +2. Find the shortest edge connecting a visited vertex to an unvisited vertex |
| 11 | +3. Add that edge to the tree and mark the new vertex as visited |
| 12 | +4. Repeat steps 2-3 until all vertices are visited |
| 13 | + |
| 14 | +This algorithm takes a *greedy* approach, always choosing the shortest available edge, which guarantees finding the minimum spanning tree. |
| 15 | + |
| 16 | +## Code |
| 17 | + |
| 18 | +```lua |
| 19 | +require("L5") |
| 20 | +-- Minimum Spanning Tree using Prim's algorithm |
| 21 | +points = {} |
| 22 | +numPoints = 30 -- how many points to draw |
| 23 | +tree = {} -- Edges in the MST |
| 24 | +visited = {} |
| 25 | + |
| 26 | +function setup() |
| 27 | + size(800, 600) |
| 28 | + windowTitle("Minimum Spanning Tree") |
| 29 | + background(255) |
| 30 | + |
| 31 | + -- Generate random points |
| 32 | + for i = 1, numPoints do |
| 33 | + points[i] = { |
| 34 | + x = random(0, width), |
| 35 | + y = random(0, height) |
| 36 | + } |
| 37 | + end |
| 38 | + |
| 39 | + -- Build MST using Prim's algorithm |
| 40 | + buildMST() |
| 41 | + |
| 42 | + describe("30 points connected with the shortest total length of lines, forming a tree structure.") |
| 43 | +end |
| 44 | + |
| 45 | +function buildMST() |
| 46 | + -- Start with first point |
| 47 | + visited[1] = true |
| 48 | + local numVisited = 1 |
| 49 | + |
| 50 | + -- Build tree by adding edges one at a time |
| 51 | + while numVisited < numPoints do |
| 52 | + local minDist = math.huge --equivalent to infinity in Lua. alternatively, use a large number like 999999 |
| 53 | + local minEdge = nil |
| 54 | + |
| 55 | + -- Find shortest edge connecting visited to unvisited |
| 56 | + for i = 1, numPoints do |
| 57 | + if visited[i] then |
| 58 | + for j = 1, numPoints do |
| 59 | + if not visited[j] then |
| 60 | + local d = dist(points[i].x, points[i].y, points[j].x, points[j].y) |
| 61 | + if d < minDist then |
| 62 | + minDist = d |
| 63 | + minEdge = {from = i, to = j, dist = d} |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + -- Add edge to tree |
| 71 | + if minEdge then |
| 72 | + table.insert(tree, minEdge) |
| 73 | + visited[minEdge.to] = true |
| 74 | + numVisited = numVisited + 1 |
| 75 | + else |
| 76 | + break -- No more edges found |
| 77 | + end |
| 78 | + end |
| 79 | +end |
| 80 | + |
| 81 | +function draw() |
| 82 | + background(255) |
| 83 | + |
| 84 | + -- Draw all edges in the MST |
| 85 | + stroke(100, 100, 255) |
| 86 | + strokeWeight(2) |
| 87 | + for _, edge in ipairs(tree) do |
| 88 | + local p1 = points[edge.from] |
| 89 | + local p2 = points[edge.to] |
| 90 | + line(p1.x, p1.y, p2.x, p2.y) |
| 91 | + end |
| 92 | + |
| 93 | + -- Draw all points |
| 94 | + noStroke() |
| 95 | + fill(50) |
| 96 | + for _, point in ipairs(points) do |
| 97 | + circle(point.x, point.y, 8) |
| 98 | + end |
| 99 | +end |
| 100 | +``` |
| 101 | + |
| 102 | +## More things to try |
| 103 | + |
| 104 | +* You can change `numPoints` to see different densities |
| 105 | +* Modify the point generation to use a grid pattern instead of random |
| 106 | +* Color edges based on their length |
| 107 | +* Animate the MST construction by adding edges one at a time |
| 108 | + |
| 109 | +## Related References |
| 110 | + |
| 111 | +* [if](/reference/if) |
| 112 | +* [line()](/reference/line) |
| 113 | +* [dist()](/reference/dist) |
| 114 | +* [draw()](/reference/draw) |
| 115 | +* [random()](/reference/random) |
| 116 | +* [sqrt()](/reference/sqrt) |
| 117 | +* [table](/reference/table) |
| 118 | + |
| 119 | +## Related Examples |
| 120 | + |
| 121 | +* [10print](10print.md) - An implementaton of the famous 1980s one-liner maze-drawing program |
| 122 | +* [Animation with Events](animation-and-variables-animation-with-events.md) - Pause and resume animation. |
| 123 | +* [Basic Pong](basic-pong.md) - A simple program demonstrating a basic implementation of pong with enemy AI player |
| 124 | +* [Conway's Game of Life](conways-life.md) - An implementation of the zero-player game and simulation formulated by mathematician John Conway |
| 125 | +* [Walking lines](walking-lines.md) - Visualizes randomly drawn lines bouncing around a box with their intersecting points highlighted |
| 126 | + |
0 commit comments