SlideShare a Scribd company logo
Fun with Mazes
Hendrik Neumann
2
https://pragprog.com/titles/jbmaze/
Standing on the shoulders of giants (one in particular)
3
What is a Maze?
4
Some more..
5
grid
cells
walls
passages
Build mazes
6
Build mazes
7
For each cell in the grid,
randomly carve either north or
east.
• 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
8
„every cell can reach every
other cell by exactly one path”
logical and mathematical purity
perfect but yet flawed
Perfect maze
9
opposite of a perfect maze
„characterized by few (if any)
dead ends, and passages
forming loops”
multiple different paths
Braid maze
10
Easy by hand for a 4x4 maze ;-)
Computer should do the work:
Dijkstra
Solve a maze
11
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
12
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
13
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
14
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
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
• 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
16
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
17
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
18
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
19
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
20
Color a maze
21
 coloring emphazises the maze‘s texture
Color a maze
Binary Tree Sidewinder Recursive
Backtracker
22
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!
Example: 2x2 grid perfect maze
 four possibilities:
• Binary Tree: A, B  50%
• Sidewinder: A, B, C  75%
Bias
Random walk  for unbiased algorithm
23
Aldous-Broder Wilson‘s
Unbiased mazes
Random walk  for unbiased algorithm
24
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
25
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
26
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
27
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
28
Random Walks…
• ..can take a long time (Aldous-Broder)
• ..can need a lot of memory (Wilson‘s)
Bias not automatically a bad thing!
Let’s look a algorithms that add constraints to random walks:
• Hunt-and-Kill
• Recursive Backtracker
Adding Constraints to Random Walks
29
Starting at an arbitrary location,
perform a random walk,
avoiding previously visited cells.
When no more moves are
possible, backtrack to the most
recently visited cell and resume
the random walk from there.
The algorithm ends when it tries
to backtrack from the cell where
it started.
Recursive Backtrack
30
• 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
31
• 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
32
• 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
33
• 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
34
• Stack is empty. Algorithm is finished.
Recursive Backtracker
stack
deapth-first search
35
• 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
36
ABAP
• colored mazes in html view
• more 7.4/7.5 magic
Github
Javascript mazes… and UI
• D3.js
• openUI5
Future with Mazes
37
More is possible…
Thanks
Hendrik Neumann
h.neumann@reply.de

More Related Content

PDF
Fun with Mazes - sitNL 2015
PDF
Find the shortest route through a maze using linking and linked list.pdf
PPTX
Maze-Solving-Alhghgghgorithhggggggms.pptx
PPTX
Rat_in_a_maze((Dynamic programming-tabulation))
PPTX
Maze Problem Presentation
PPTX
Rat_in_a_maze((Dynamic programming-tabulation))
PPTX
Rat_in_Maze(Dynamic programming-tabulation)
Fun with Mazes - sitNL 2015
Find the shortest route through a maze using linking and linked list.pdf
Maze-Solving-Alhghgghgorithhggggggms.pptx
Rat_in_a_maze((Dynamic programming-tabulation))
Maze Problem Presentation
Rat_in_a_maze((Dynamic programming-tabulation))
Rat_in_Maze(Dynamic programming-tabulation)

Similar to Fun with Mazes (20)

PPTX
Cellular Automata for Pathfinding
PPTX
mini project_shortest path visualizer.pptx
PPTX
Maze-Solving Algorithmhgggggggs (1).pptx
PDF
Requirements Use JavaDo Not Use RecursionDo Not Use Array Lis.pdf
PPTX
Class Greedy Algorithms Lecture Slide.pptx
PDF
hello the code given is mostly complete but I need help with the Sol.pdf
PPT
Computer notes - Maze Generator
PDF
A Comprehensive and Comparative Study Of Maze-Solving Techniques by Implement...
PDF
E017142429
PPT
computer notes - Data Structures - 31
PPT
An elementary navigation simulated in Java
PDF
Topic Use stacks to solve Maze. DO NOT USE RECURSION. And do not us.pdf
PPTX
Lecture-11-CS345A-2023 of Design and Analysis
PDF
A* and Min-Max Searching Algorithms in AI , DSA.pdf
DOCX
1) IntroductionThis practical work consists of developing
DOCX
1) IntroductionThis practical work consists of developing
PPT
Greedy Algorithm
PPTX
AI UNIT 2 PPT AI UNIT 2 PPT AI UNIT 2 PPT.pptx
PPTX
Bidirectional graph search techniques for finding shortest path in image base...
Cellular Automata for Pathfinding
mini project_shortest path visualizer.pptx
Maze-Solving Algorithmhgggggggs (1).pptx
Requirements Use JavaDo Not Use RecursionDo Not Use Array Lis.pdf
Class Greedy Algorithms Lecture Slide.pptx
hello the code given is mostly complete but I need help with the Sol.pdf
Computer notes - Maze Generator
A Comprehensive and Comparative Study Of Maze-Solving Techniques by Implement...
E017142429
computer notes - Data Structures - 31
An elementary navigation simulated in Java
Topic Use stacks to solve Maze. DO NOT USE RECURSION. And do not us.pdf
Lecture-11-CS345A-2023 of Design and Analysis
A* and Min-Max Searching Algorithms in AI , DSA.pdf
1) IntroductionThis practical work consists of developing
1) IntroductionThis practical work consists of developing
Greedy Algorithm
AI UNIT 2 PPT AI UNIT 2 PPT AI UNIT 2 PPT.pptx
Bidirectional graph search techniques for finding shortest path in image base...
Ad

More from Hendrik Neumann (6)

PDF
SAP Stammtisch Frankfurt PLUS 11.10.2017 - introduction
PDF
SAP Stammtisch Frankfurt 2017-10-11 - abapGit introduction
PDF
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
PDF
What the git? - SAP Inside Track Munich 2016
PDF
TDD in the ABAP world - sitNL 2013 edition
PDF
ABAP Unit and TDD
SAP Stammtisch Frankfurt PLUS 11.10.2017 - introduction
SAP Stammtisch Frankfurt 2017-10-11 - abapGit introduction
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
What the git? - SAP Inside Track Munich 2016
TDD in the ABAP world - sitNL 2013 edition
ABAP Unit and TDD
Ad

Recently uploaded (20)

PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
WOOl fibre morphology and structure.pdf for textiles
PPTX
Chapter 5: Probability Theory and Statistics
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
Tartificialntelligence_presentation.pptx
PDF
Getting started with AI Agents and Multi-Agent Systems
PPT
What is a Computer? Input Devices /output devices
PPTX
TLE Review Electricity (Electricity).pptx
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PPTX
The various Industrial Revolutions .pptx
PPT
Module 1.ppt Iot fundamentals and Architecture
PPTX
Modernising the Digital Integration Hub
Group 1 Presentation -Planning and Decision Making .pptx
A comparative study of natural language inference in Swahili using monolingua...
Hindi spoken digit analysis for native and non-native speakers
gpt5_lecture_notes_comprehensive_20250812015547.pdf
WOOl fibre morphology and structure.pdf for textiles
Chapter 5: Probability Theory and Statistics
NewMind AI Weekly Chronicles – August ’25 Week III
Zenith AI: Advanced Artificial Intelligence
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
1 - Historical Antecedents, Social Consideration.pdf
Tartificialntelligence_presentation.pptx
Getting started with AI Agents and Multi-Agent Systems
What is a Computer? Input Devices /output devices
TLE Review Electricity (Electricity).pptx
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
O2C Customer Invoices to Receipt V15A.pptx
A contest of sentiment analysis: k-nearest neighbor versus neural network
The various Industrial Revolutions .pptx
Module 1.ppt Iot fundamentals and Architecture
Modernising the Digital Integration Hub

Fun with Mazes

  • 3. 3 What is a Maze?
  • 7. 7 For each cell in the grid, randomly carve either north or east. • 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
  • 8. 8 „every cell can reach every other cell by exactly one path” logical and mathematical purity perfect but yet flawed Perfect maze
  • 9. 9 opposite of a perfect maze „characterized by few (if any) dead ends, and passages forming loops” multiple different paths Braid maze
  • 10. 10 Easy by hand for a 4x4 maze ;-) Computer should do the work: Dijkstra Solve a maze
  • 11. 11 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
  • 12. 12 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
  • 13. 13 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
  • 14. 14 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
  • 15. 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 • 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
  • 16. 16 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
  • 17. 17 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
  • 18. 18 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
  • 19. 19 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. 21  coloring emphazises the maze‘s texture Color a maze Binary Tree Sidewinder Recursive Backtracker
  • 22. 22 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! Example: 2x2 grid perfect maze  four possibilities: • Binary Tree: A, B  50% • Sidewinder: A, B, C  75% Bias Random walk  for unbiased algorithm
  • 23. 23 Aldous-Broder Wilson‘s Unbiased mazes Random walk  for unbiased algorithm
  • 24. 24 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
  • 25. 25 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
  • 26. 26 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
  • 27. 27 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
  • 28. 28 Random Walks… • ..can take a long time (Aldous-Broder) • ..can need a lot of memory (Wilson‘s) Bias not automatically a bad thing! Let’s look a algorithms that add constraints to random walks: • Hunt-and-Kill • Recursive Backtracker Adding Constraints to Random Walks
  • 29. 29 Starting at an arbitrary location, perform a random walk, avoiding previously visited cells. When no more moves are possible, backtrack to the most recently visited cell and resume the random walk from there. The algorithm ends when it tries to backtrack from the cell where it started. Recursive Backtrack
  • 30. 30 • 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
  • 31. 31 • 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
  • 32. 32 • 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
  • 33. 33 • 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
  • 34. 34 • Stack is empty. Algorithm is finished. Recursive Backtracker stack deapth-first search
  • 35. 35 • 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
  • 36. 36 ABAP • colored mazes in html view • more 7.4/7.5 magic Github Javascript mazes… and UI • D3.js • openUI5 Future with Mazes