|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/transform-python-maze-graph/ |
| Your Learning Plan →Your personalized Python learning plan | https://realpython.com/account/learning-plan/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest&_from=nav_library |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/search?kind=exercise&kind=quiz&_from=nav_library |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Mentor AI →NewYour personal Python mentor | https://realpython.com/mentor/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Live Courses →Live, instructor-led Python courses | https://realpython.com/live/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Unlock All Content and Become a Python Expert →Get unlimited access to all Real Python has to offer | https://realpython.com/account/join/?from=navbar_learn |
| Unlock All Content →Become a Python Expert | https://realpython.com/account/join/?from=navbar_learn |
|
More
| https://realpython.com/lessons/transform-python-maze-graph/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Contributor | https://realpython.com/jobs/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?intent=navbar&next=%2Flessons%2Ftransform-python-maze-graph%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-maze-solver |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-maze-solver |
|
| https://realpython.com/courses/python-maze-solver/#team |
| Mazes in Python: Build, Visualize, Store, and Solve | https://realpython.com/courses/python-maze-solver/ |
| Darren Jones | https://realpython.com/courses/python-maze-solver/#team |
| Recommended Tutorial | https://realpython.com/python-maze-solver/ |
| Course Slides (.pdf) | https://realpython.com/courses/python-maze-solver/downloads/mazes-in-python-slides/ |
| Sample Code (.zip) | https://realpython.com/courses/python-maze-solver/downloads/mazes-in-python-code/ |
| Ask a Question | https://realpython.com/lessons/transform-python-maze-graph/#discussion |
| https://realpython.com/feedback/survey/course/python-maze-solver/liked/?from=lesson-title |
| https://realpython.com/feedback/survey/course/python-maze-solver/disliked/?from=lesson-title |
| Contents | https://realpython.com/lessons/transform-python-maze-graph/#description |
| Transcript | https://realpython.com/lessons/transform-python-maze-graph/#transcript |
| Discussion | https://realpython.com/lessons/transform-python-maze-graph/#discussion |
| 00:01 | https://realpython.com/lessons/transform-python-maze-graph/#t=1.02 |
| Transform the Maze into a Graph. | https://realpython.com/lessons/transform-python-maze-graph/#t=1.02 |
| To create a graph, | https://realpython.com/lessons/transform-python-maze-graph/#t=3.438 |
| you’ll need to provide nodes and edges that connect them. | https://realpython.com/lessons/transform-python-maze-graph/#t=4.97 |
| You’ve already established that your maze consists of nodes that represent path | https://realpython.com/lessons/transform-python-maze-graph/#t=8.59 |
| intersections, dead ends, and special squares, such as enemies or the entrance. | https://realpython.com/lessons/transform-python-maze-graph/#t=12.41 |
| 00:17 | https://realpython.com/lessons/transform-python-maze-graph/#t=17.3 |
| However, if you connect them directly without taking the corners into account, | https://realpython.com/lessons/transform-python-maze-graph/#t=17.3 |
| then you won’t be able to follow the maze’s horizontal and vertical pathways. | https://realpython.com/lessons/transform-python-maze-graph/#t=21.9 |
| 00:26 | https://realpython.com/lessons/transform-python-maze-graph/#t=26.54 |
| The image on the left depicts six color-coded nodes, | https://realpython.com/lessons/transform-python-maze-graph/#t=26.54 |
| including two intersections in blue, two dead ends in red, | https://realpython.com/lessons/transform-python-maze-graph/#t=29.28 |
| as well as the entrance and the exit in green. | https://realpython.com/lessons/transform-python-maze-graph/#t=33.54 |
| 00:37 | https://realpython.com/lessons/transform-python-maze-graph/#t=37.34 |
| The image on the right is almost the same, but it adds four corners in yellow, | https://realpython.com/lessons/transform-python-maze-graph/#t=37.34 |
| which allow you to preserve the maze’s layout. | https://realpython.com/lessons/transform-python-maze-graph/#t=41.25 |
| Your Border model can already identify a .corner, an .intersection, | https://realpython.com/lessons/transform-python-maze-graph/#t=47.01 |
| and a .dead_end, | https://realpython.com/lessons/transform-python-maze-graph/#t=51.04 |
| while the Role model will tell you whether the associated square has some | https://realpython.com/lessons/transform-python-maze-graph/#t=52.31 |
| special function in the maze. | https://realpython.com/lessons/transform-python-maze-graph/#t=55.66 |
| 00:59 | https://realpython.com/lessons/transform-python-maze-graph/#t=59.66 |
| You can treat the square as a synonym for a graph node because NetworkX lets | https://realpython.com/lessons/transform-python-maze-graph/#t=59.66 |
| you use any object as a node, as long as it’s hashable. | https://realpython.com/lessons/transform-python-maze-graph/#t=64.08 |
| The Square class is hashable out of the box because it was implemented as an | https://realpython.com/lessons/transform-python-maze-graph/#t=67.99 |
| immutable, frozen data class. | https://realpython.com/lessons/transform-python-maze-graph/#t=71.93 |
| 01:14 | https://realpython.com/lessons/transform-python-maze-graph/#t=74.271 |
| To use consistent terminology, | https://realpython.com/lessons/transform-python-maze-graph/#t=74.271 |
| you’ll define a type alias for the Square class so that you can refer to it as a | https://realpython.com/lessons/transform-python-maze-graph/#t=76.87 |
| Node in your code. | https://realpython.com/lessons/transform-python-maze-graph/#t=81.29 |
| 01:23 | https://realpython.com/lessons/transform-python-maze-graph/#t=83.984 |
| Once you’ve done this, | https://realpython.com/lessons/transform-python-maze-graph/#t=83.984 |
| you’ll model the edge as a pair of two nodes, | https://realpython.com/lessons/transform-python-maze-graph/#t=86.06 |
| which you can implement as a named tuple. | https://realpython.com/lessons/transform-python-maze-graph/#t=88.2 |
| 01:48 | https://realpython.com/lessons/transform-python-maze-graph/#t=108.98 |
| It’s possible to represent edges of directed graphs with this class because | https://realpython.com/lessons/transform-python-maze-graph/#t=108.98 |
| it’s an ordered pair. | https://realpython.com/lessons/transform-python-maze-graph/#t=112.87 |
| But for now, | https://realpython.com/lessons/transform-python-maze-graph/#t=114.253 |
| you’ll only consider undirected graphs with two-way connections between their | https://realpython.com/lessons/transform-python-maze-graph/#t=115.07 |
| nodes. | https://realpython.com/lessons/transform-python-maze-graph/#t=119.27 |
| 02:03 | https://realpython.com/lessons/transform-python-maze-graph/#t=123.97 |
| Now that you have a representation for nodes and edges, | https://realpython.com/lessons/transform-python-maze-graph/#t=123.97 |
| you can start transforming your maze into a graph by first getting a collection | https://realpython.com/lessons/transform-python-maze-graph/#t=126.93 |
| of nodes from it. | https://realpython.com/lessons/transform-python-maze-graph/#t=130.67 |
| 02:11 | https://realpython.com/lessons/transform-python-maze-graph/#t=131.76 |
| The simple way would be to turn all squares into nodes. | https://realpython.com/lessons/transform-python-maze-graph/#t=131.76 |
| However, capturing only intersections, | https://realpython.com/lessons/transform-python-maze-graph/#t=135.58 |
| corners, and dead ends as nodes can lead to a more efficient and accurate | https://realpython.com/lessons/transform-python-maze-graph/#t=137.98 |
| solution for solving mazes. | https://realpython.com/lessons/transform-python-maze-graph/#t=142.19 |
| 02:24 | https://realpython.com/lessons/transform-python-maze-graph/#t=144.33 |
| You can identify these nodes by checking the role and border pattern of each | https://realpython.com/lessons/transform-python-maze-graph/#t=144.33 |
| square in the maze. | https://realpython.com/lessons/transform-python-maze-graph/#t=148.19 |
| If | https://realpython.com/lessons/transform-python-maze-graph/#t=149.616 |
| 02:59 | https://realpython.com/lessons/transform-python-maze-graph/#t=179.89 |
| a square is marked as either a wall or exterior, | https://realpython.com/lessons/transform-python-maze-graph/#t=179.89 |
| then it’s definitely not part of the graph, | https://realpython.com/lessons/transform-python-maze-graph/#t=182.54 |
| so you skip it with a continue statement. | https://realpython.com/lessons/transform-python-maze-graph/#t=184.85 |
| Otherwise, | https://realpython.com/lessons/transform-python-maze-graph/#t=188.15 |
| if the square has some special role other than None, | https://realpython.com/lessons/transform-python-maze-graph/#t=188.91 |
| then you record it as a graph node. | https://realpython.com/lessons/transform-python-maze-graph/#t=191.56 |
| 03:18 | https://realpython.com/lessons/transform-python-maze-graph/#t=198.53 |
| Finally, regardless of the square’s role, you take note of intersections, | https://realpython.com/lessons/transform-python-maze-graph/#t=198.53 |
| dead ends, and corners. | https://realpython.com/lessons/transform-python-maze-graph/#t=202.81 |
| 03:34 | https://realpython.com/lessons/transform-python-maze-graph/#t=214.36 |
| Thanks to using a Python set, | https://realpython.com/lessons/transform-python-maze-graph/#t=214.36 |
| you don’t have to worry about adding the same square more than once when, | https://realpython.com/lessons/transform-python-maze-graph/#t=216.16 |
| for example, a reward happens to be placed at an intersection. | https://realpython.com/lessons/transform-python-maze-graph/#t=219.48 |
| 03:43 | https://realpython.com/lessons/transform-python-maze-graph/#t=223.52 |
| That’s because sets filter out duplicates. | https://realpython.com/lessons/transform-python-maze-graph/#t=223.52 |
| Next, | https://realpython.com/lessons/transform-python-maze-graph/#t=227.855 |
| you’ll need to connect your nodes with edges. | https://realpython.com/lessons/transform-python-maze-graph/#t=228.48 |
| To do that, | https://realpython.com/lessons/transform-python-maze-graph/#t=231.256 |
| you’ll take advantage of the fact that the maze is a rectangular grid. | https://realpython.com/lessons/transform-python-maze-graph/#t=232.2 |
| 03:56 | https://realpython.com/lessons/transform-python-maze-graph/#t=236.65 |
| That means you can loop through the nodes you just identified, | https://realpython.com/lessons/transform-python-maze-graph/#t=236.65 |
| checking to the right and down for any adjacent nodes in the maze and stopping | https://realpython.com/lessons/transform-python-maze-graph/#t=240.14 |
| once you reach a wall. | https://realpython.com/lessons/transform-python-maze-graph/#t=243.89 |
| 04:07 | https://realpython.com/lessons/transform-python-maze-graph/#t=247.67 |
| You look for the immediate neighbors of each node supplied to the function. | https://realpython.com/lessons/transform-python-maze-graph/#t=247.67 |
| In this case, you will move east and south in the maze, | https://realpython.com/lessons/transform-python-maze-graph/#t=251.51 |
| but you could just as well follow the opposite directions, | https://realpython.com/lessons/transform-python-maze-graph/#t=254.83 |
| remembering to change the loop indexing and wall detection accordingly. | https://realpython.com/lessons/transform-python-maze-graph/#t=257.77 |
| 04:53 | https://realpython.com/lessons/transform-python-maze-graph/#t=293.83 |
| First, you check to the east. | https://realpython.com/lessons/transform-python-maze-graph/#t=293.83 |
| You use the bitwise AND operator (&) to check if a border around the current square | https://realpython.com/lessons/transform-python-maze-graph/#t=298.34 |
| has a right side, indicating a wall that terminates the search in that direction. | https://realpython.com/lessons/transform-python-maze-graph/#t=302.32 |
| 05:07 | https://realpython.com/lessons/transform-python-maze-graph/#t=307.58 |
| If it does, then you break out of the loop. | https://realpython.com/lessons/transform-python-maze-graph/#t=307.58 |
| On the other hand, | https://realpython.com/lessons/transform-python-maze-graph/#t=310.779 |
| if you find a square located in the same row as your source node is also present | https://realpython.com/lessons/transform-python-maze-graph/#t=312.46 |
| in the set of nodes, and there’s no wall between them, | https://realpython.com/lessons/transform-python-maze-graph/#t=316.82 |
| then you create an Edge instance. | https://realpython.com/lessons/transform-python-maze-graph/#t=319.39 |
| 05:21 | https://realpython.com/lessons/transform-python-maze-graph/#t=321.537 |
| You | https://realpython.com/lessons/transform-python-maze-graph/#t=321.537 |
| 05:29 | https://realpython.com/lessons/transform-python-maze-graph/#t=329.36 |
| then perform a similar check, but looking south, | https://realpython.com/lessons/transform-python-maze-graph/#t=329.36 |
| checking for bottom walls and checking for nodes in the same column, | https://realpython.com/lessons/transform-python-maze-graph/#t=332.48 |
| adding an edge as before. | https://realpython.com/lessons/transform-python-maze-graph/#t=335.86 |
| 06:08 | https://realpython.com/lessons/transform-python-maze-graph/#t=368.27 |
| Knowing the edges is sufficient to define a new NetworkX graph object. | https://realpython.com/lessons/transform-python-maze-graph/#t=368.27 |
| 06:31 | https://realpython.com/lessons/transform-python-maze-graph/#t=391.66 |
| The initializer method of nx.Graph can optionally take various graph data types, | https://realpython.com/lessons/transform-python-maze-graph/#t=391.66 |
| including a collection of edges. | https://realpython.com/lessons/transform-python-maze-graph/#t=396.59 |
| Normally, | https://realpython.com/lessons/transform-python-maze-graph/#t=399.092 |
| you should create an instance of nx.MultiGraph to account for parallel edges | https://realpython.com/lessons/transform-python-maze-graph/#t=399.91 |
| that might appear in one of your mazes. | https://realpython.com/lessons/transform-python-maze-graph/#t=404.01 |
| 06:46 | https://realpython.com/lessons/transform-python-maze-graph/#t=406.565 |
| However, | https://realpython.com/lessons/transform-python-maze-graph/#t=406.565 |
| the extra corners that you added before make each parallel connection unique, | https://realpython.com/lessons/transform-python-maze-graph/#t=407.44 |
| so there’s no need to use a multigraph in this case. | https://realpython.com/lessons/transform-python-maze-graph/#t=411.36 |
| In the next section, | https://realpython.com/lessons/transform-python-maze-graph/#t=418.65 |
| you’ll use NetworkX to find the shortest path from the entrance to the exit of | https://realpython.com/lessons/transform-python-maze-graph/#t=419.77 |
| the maze. | https://realpython.com/lessons/transform-python-maze-graph/#t=424.03 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/solve-python-maze-graph-based/ |
| Overview | https://realpython.com/courses/python-maze-solver/ |
| https://realpython.com/lessons/python-maze-shortest-path/ |
|
Mazes in Python: Build and Visualize 02:47
| https://realpython.com/videos/python-maze-solver-part-1-overview/ |
|
Check Out What You'll Make 02:39
| https://realpython.com/videos/python-maze-solver-project/ |
|
Lay the Groundwork 04:02
| https://realpython.com/videos/python-maze-solver-groundwork/ |
|
Scaffold the Project Structure 04:50
| https://realpython.com/lessons/python-maze-solver-structure/ |
|
Represent the Maze With Objects 06:37
| https://realpython.com/lessons/represent-maze-with-objects/ |
|
Create Border Patterns 06:32
| https://realpython.com/lessons/python-maze-border-patterns/ |
|
Model the Square 02:25
| https://realpython.com/lessons/model-python-maze-square/ |
|
Build the Maze 09:07
| https://realpython.com/lessons/build-python-maze/ |
|
Visualize the Maze With SVG 04:45
| https://realpython.com/lessons/python-maze-svg/ |
|
Implement Geometric Primitives 04:48
| https://realpython.com/lessons/python-maze-geometric-primitives/ |
|
Add More Primitives 03:47
| https://realpython.com/lessons/python-maze-more-primitives/ |
|
Decompose the Border Into Primitives 04:41
| https://realpython.com/lessons/decompose-python-maze-border/ |
|
Add More Border Patterns 03:58
| https://realpython.com/lessons/python-maze-more-border-patterns/ |
|
Build the SVG Renderer 03:49
| https://realpython.com/lessons/python-maze-svg-renderer/ |
|
Add Features to the Renderer 06:50
| https://realpython.com/lessons/python-maze-renderer-features/ |
|
Draw the Solution 03:13
| https://realpython.com/lessons/draw-python-maze-solution/ |
|
Preview the Rendered Maze and Its Solution 04:56
| https://realpython.com/lessons/preview-python-maze/ |
|
Mazes in Python: Build and Visualize (Recap) 01:14
| https://realpython.com/lessons/python-maze-solver-part-1-summary/ |
|
Mazes in Python: Store and Solve 01:48
| https://realpython.com/videos/python-maze-solver-part-2-overview/ |
|
Load the Maze From a Binary File 04:55
| https://realpython.com/videos/python-maze-binary-file/ |
|
Choose Your Data Alignment 05:09
| https://realpython.com/videos/python-maze-data-alignment/ |
|
Define the File Header and Body 08:12
| https://realpython.com/lessons/python-maze-file-header-and-body/ |
|
Serialize the Maze 05:22
| https://realpython.com/lessons/serialize-python-maze/ |
|
Deserialize the Maze 04:32
| https://realpython.com/lessons/deserialize-python-maze/ |
|
Make the Maze Self-Contained 04:30
| https://realpython.com/lessons/python-self-contained-maze/ |
|
Update the Maze Class 03:16
| https://realpython.com/lessons/python-maze-class/ |
|
Solve the Maze Using a Graph-Based Approach 03:01
| https://realpython.com/lessons/solve-python-maze-graph-based/ |
|
Transform the Maze Into a Graph 07:06
| https://realpython.com/lessons/transform-python-maze-graph/ |
|
Find the Shortest Path 05:23
| https://realpython.com/lessons/python-maze-shortest-path/ |
|
Add Weights to Graph Edges 02:36
| https://realpython.com/lessons/python-map-weights-graph-edges/ |
|
Introduce Enemies and Rewards 06:04
| https://realpython.com/lessons/python-maze-enemies-rewards/ |
|
Make a Runnable Script 03:59
| https://realpython.com/lessons/python-maze-runnable-script/ |
|
Mazes in Python: Store and Solve (Recap) 01:23
| https://realpython.com/lessons/python-maze-solver-part-2-summary/ |
| Privacy Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover