Skip to content

Commit dffc2a6

Browse files
committed
add minimum spanning trees example, and links from other pages
1 parent cf0bd63 commit dffc2a6

6 files changed

Lines changed: 154 additions & 1 deletion

File tree

9.95 KB
Loading

docs/examples/10print.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,6 @@ end
7070
* [Conditions](animation-and-variables-conditions.md) - Use if and else statements to make decisions while your sketch runs.
7171
* [Copy Image Data](imported-media-copy-image-data.md) - Paint from an image file onto the canvas.
7272
* [Conway's Game Of Life](conways-life.md) - An implementation of the zero-player game and simulation formulated by mathematician John Conway
73+
* [Minimum Spanning Tree](min-span-tree.md) - An example of implementing Prim's algorithm for finding the shortest lengths to connect all randomly placed dots
74+
* [Walking lines](walking-lines.md) - Visualizes randomly drawn lines bouncing around a box with their intersecting points highlighted
75+

docs/examples/conways-life.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,12 @@ end
202202
* [function](/reference/function)
203203
* [local](/reference/local)
204204
* [mouseDragged()](/reference/mouseDragged)
205+
206+
## Related Examples
207+
208+
* [10print](10print.md) - An implementaton of the famous 1980s one-liner maze-drawing program
209+
* [Animation with Events](animation-and-variables-animation-with-events.md) - Pause and resume animation.
210+
* [Basic Pong](basic-pong.md) - A simple program demonstrating a basic implementation of pong with enemy AI player
211+
* [Minimum Spanning Tree](min-span-tree.md) - An example of implmenting Prim's algorithm for finding the shortest lengths to connect all randomly placed dots
212+
* [Walking lines](walking-lines.md) - Visualizes randomly drawn lines bouncing around a box with their intersecting points highlighted
213+

docs/examples/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* [Walking Lines](walking-lines.md) - Animated intersecting lines
1616
* [10Print](10print.md) - An implementation of the classic maze-drawing algorithm
1717
* [Conway's Game Of Life](conways-life.md) - An implementation of the zero-player game and simulation formulated by mathematician John Conway
18+
* [Minimum Spanning Tree](min-span-tree.md) - An example of implementing Prim's algorithm for finding the shortest lengths to connect all randomly placed dots
1819

1920
## Imported Media
2021

docs/examples/min-span-tree.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
![30 points connected with the shortest total length of lines, forming a tree structure](/assets/examples/min-span-tree.webp "30 points connected with the shortest total length of lines, forming a tree structure")
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+

docs/examples/walking-lines.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,21 @@ function intersectPoint(aX1, aY1, aX2, aY2, bX1, bY1, bX2, bY2)
123123
end
124124
```
125125

126+
## Related References
127+
128+
* [if](/reference/if)
129+
* [line()](/reference/line)
130+
* [draw()](/reference/draw)
131+
* [frameRate()](/reference/frameRate)
132+
* [random()](/reference/random)
133+
126134
## Related Examples
127135

128136
* [10print](10print.md) - An implementation of the classic maze-drawing algorithm
129-
* [Conway's Game of Life](conways-life.md) - An implementation of the zero-player game and simulation formulated by mathematician John Conway
137+
* [Animation with Events](animation-and-variables-animation-with-events.md) - Pause and resume animation.
138+
* [Basic Pong](basic-pong.md) - A simple program demonstrating a basic implementation of pong with enemy AI player
139+
* [Conditions](animation-and-variables-conditions.md) - Use if and else statements to make decisions while your sketch runs.
140+
* [Copy Image Data](imported-media-copy-image-data.md) - Paint from an image file onto the canvas.
141+
* [Conway's Game Of Life](conways-life.md) - An implementation of the zero-player game and simulation formulated by mathematician John Conway
142+
* [Minimum Spanning Tree](min-span-tree.md) - An example of implementing Prim's algorithm for finding the shortest lengths to connect all randomly placed dots
143+

0 commit comments

Comments
 (0)