Upload
hendrik-neumann
View
223
Download
2
Embed Size (px)
Citation preview
2
„Mazes for Programmers“
by Jamis Buck
Published 2015
by The Pragmatic Programmers
Website:
https://pragprog.com/titles/jbmaze/
Standing on the shoulders of giants (one in particular)
11
Algorithm
For each cell in the grid,
randomly carve either north or
east.
Characteristics
• Strong diagonal texture
tending toward the north-
east corner of the grid.
• Corridors run the length of
the northern row and the
eastern column.
Binary Tree
Live Demo. Whiteboard style.
12
„every cell can reach every
other cell by exactly one path”
logical and mathematical purity
perfect but yet flawed
Perfect Maze
13
opposite of a perfect maze
„characterized by few (if any)
dead ends, and passages
forming loops”
multiple different paths
Braid Maze
We only look at perfect mazes.
15
Measures the shortest distance
between some starting point
(which we specify), and every
other cell in the maze.
• shortest path from a chosen
endpoint to our starting point
Dijstra‘s Algorithm
0
16
Measures the shortest distance
between some starting point
(which we specify), and every
other cell in the maze.
• shortest path from a chosen
endpoint to our starting point
Dijstra‘s Algorithm
0 1
17
Measures the shortest distance
between some starting point
(which we specify), and every
other cell in the maze.
• shortest path from a chosen
endpoint to our starting point
Dijstra‘s Algorithm
0 1 2
2
18
Measures the shortest distance
between some starting point
(which we specify), and every
other cell in the maze.
• shortest path from a chosen
endpoint to our starting point
Dijstra‘s Algorithm
0 1 2 3 4
3 2 3 8 5
4 3 6 7 6
5 4 5 6 7
6 7 8 7 8
19
Measures the shortest distance
between some starting point
(which we specify), and every
other cell in the maze.
• shortest path from a chosen
endpoint to our starting point
• walk backward from end to
start.
Dijstra‘s Algorithm
0 1 2 3 4
3 2 3 8 5
4 3 6 7 6
5 4 5 6 7
6 7 8 7 8
20
Finding the longest path in a
maze
• find the longest path
between two cells
• might not be the longest
path of the maze! – but just
part of it
• run Dijstra again in the other
direction
Dijstra‘s Algorithm
21
Finding the longest path in a
maze
• find the longest path
between two cells
• might not be the longest
path of the maze! – but just
part of it
• run Dijstra again in the other
direction
Dijstra‘s Algorithm
22
Finding the longest path in a
maze
• find the longest path
between two cells
• might not be the longest
path of the maze! – but just
part of it
• run Dijstra again in the other
direction
Dijstra‘s Algorithm
23
Finding the longest path in a
maze
• find the longest path
between two cells
• might not be the longest
path of the maze! – but just
part of it
• run Dijstra again in the other
direction
Dijstra‘s Algorithm
26
Binary Tree
Recursive Backtracker
Sidewinder
Texture in a maze is a result of
the bias of the algorithm.
Other signs might be long
passages or the amount of
dead ends.
Not automatically a bad thing!
Bias
27
Example: 2x2 grid perfect maze four possibilities:
Bias
Use Binary Tree to build these mazes – which of the four are possible?
Answer: A, B 50%
29
Algorithm
Starting at an arbitrary location
in the grid, move randomly from
cell to cell.
If moving to a previously
unvisited cell, carve a passage
to it.
End when all cells have been
visited.
Aldous-Broder
30
1. Pick a cell at random
2. Pick a random neighbour: east – not visited yet, link cells together
3. Pick next random neighbour..
Aldous-Broder
Disclaimer: Presentation
was 1st held at SAP Inside
Track Munich in October –
hence the beerglas ;-)
31
Aldous-Broder
4. Pick a random neighbour: west – not visited yet: link cells together
5. Pick a random neighbour: north – already visited: don‘t link cells
6. Finish the maze by randomly visiting neighbours until all are visited
32
Charecteristics
Starts quickly but can take a
very long time to finish.
Mazes are guaranteed
perfectly random.
Unbiased – no preference to
any particular texture or feature.
Aldous-Broder
33
Random Walks unbiased mazes but..
• ..can take a long time (Aldous-Broder)
• ..can need a lot of memory (Wilson‘s)
Let’s look a algorithms that add constraints to random walks:
• Hunt-and-Kill
• Recursive Backtracker
Adding Constraints to Random Walks
Bias not automatically a bad thing!
34
Algorithm
1. Starting at an arbitrary
location, perform a random
walk, avoiding previously
visited cells.
2. When no more moves are
possible, backtrack to the
most recently visited cell
and resume the random
walk from there.
3. The algorithm ends when it
tries to backtrack from the
cell where it started.
Recursive Backtracker
35
• Start at A4. Put current cell on the stack
• Choose an unvisited neighbor randomly - Carve a path and push it
on the stack. A3 = current cell
Recursive Backtracker
stack
A4
stack
A3
A4
stack
36
• Continue with the process.. Put the cells on the stack
• We‘re stuck - B4 has no unvisited neighbors
Recursive Backtracker
stack
C4
C3
…
A3
A4
stack
B4
C4
C3
…
A3
A4
37
• Pop the dead end from the stack C4 is again our current cell
• C4 has an unvisited neighbor D4
Recursive Backtracker
stack
B4
C4
C3
…
A3
A4
stack
D4
C4
C3
…
A3
A4
38
• Continue until every cell has been visited
• Final cell is always a dead end backtrack to the beginning.
Recursive Backtracker
stack
A2
A1
B1
…
A3
A4
stack
A2
A1
B1
…
A3
A4
40
Characteristics
• long, twisty passages with
relatively few dead ends
• fast - as it visits every cell
only twice
• needs considerably memory
to keep track of visited cells
Recursive Backtrack
45
ABAP coding
• Grid & textual output
• colored mazes in html view
• useage of 7.4/7.5 magic
Watch SCN for blog and
Github for source
Javascript mazes… and UI
my Future with Mazes