|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/python-map-weights-graph-edges/ |
| 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/python-map-weights-graph-edges/ |
| 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%2Fpython-map-weights-graph-edges%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/python-map-weights-graph-edges/#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/python-map-weights-graph-edges/#description |
| Transcript | https://realpython.com/lessons/python-map-weights-graph-edges/#transcript |
| Discussion | https://realpython.com/lessons/python-map-weights-graph-edges/#discussion |
| 00:00 | https://realpython.com/lessons/python-map-weights-graph-edges/#t=0.74 |
| Add Weights to Graph Edges. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=0.74 |
| Go back to the converter module and update the Edge class by adding a property | https://realpython.com/lessons/python-map-weights-graph-edges/#t=3.74 |
| that will calculate the actual distance between its two nodes. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=7.55 |
| 00:13 | https://realpython.com/lessons/python-map-weights-graph-edges/#t=13.14 |
| Here, | https://realpython.com/lessons/python-map-weights-graph-edges/#t=13.14 |
| you want to calculate the Euclidean distance between the nodes using Python’s | https://realpython.com/lessons/python-map-weights-graph-edges/#t=13.43 |
| math module, so it’s imported first. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=17.39 |
| 00:31 | https://realpython.com/lessons/python-map-weights-graph-edges/#t=31.89 |
| The math.dist() function takes two points specified as sequences of | https://realpython.com/lessons/python-map-weights-graph-edges/#t=31.89 |
| coordinates. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=36.07 |
| In this case, | https://realpython.com/lessons/python-map-weights-graph-edges/#t=37.071 |
| you provide tuples of the row and column indices of squares corresponding to | https://realpython.com/lessons/python-map-weights-graph-edges/#t=38.33 |
| the nodes. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=42.39 |
| 00:43 | https://realpython.com/lessons/python-map-weights-graph-edges/#t=43.202 |
| Note that you could define distance differently— | https://realpython.com/lessons/python-map-weights-graph-edges/#t=43.202 |
| for example, | https://realpython.com/lessons/python-map-weights-graph-edges/#t=47.08 |
| as the sum of absolute values of differences in the horizontal and vertical | https://realpython.com/lessons/python-map-weights-graph-edges/#t=47.64 |
| directions. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=51.58 |
| You can update the function | https://realpython.com/lessons/python-map-weights-graph-edges/#t=52.531 |
| make_graph() to use the new property. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=54.94 |
| 01:06 | https://realpython.com/lessons/python-map-weights-graph-edges/#t=66.38 |
| Notice how you change the graph data passed to nx.Graph, | https://realpython.com/lessons/python-map-weights-graph-edges/#t=66.38 |
| which is now a sequence of tuples consisting of the two nodes and a | https://realpython.com/lessons/python-map-weights-graph-edges/#t=69.65 |
| dictionary with extra attributes. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=73.4 |
| 01:16 | https://realpython.com/lessons/python-map-weights-graph-edges/#t=76.04 |
| You specify only one attribute named weight, | https://realpython.com/lessons/python-map-weights-graph-edges/#t=76.04 |
| whose value is equal to the edge’s distance. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=78.63 |
| Next, update the functions in the solver module to tell NetworkX | https://realpython.com/lessons/python-map-weights-graph-edges/#t=85.0 |
| which attribute to use as the weight. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=88.77 |
| 01:38 | https://realpython.com/lessons/python-map-weights-graph-edges/#t=98.2 |
| The weight parameter lets you specify the name of an attribute in a dictionary | https://realpython.com/lessons/python-map-weights-graph-edges/#t=98.2 |
| associated with each graph edge. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=101.9 |
| 01:54 | https://realpython.com/lessons/python-map-weights-graph-edges/#t=114.05 |
| Now, when you call solve_all() on the Pac-Man maze, | https://realpython.com/lessons/python-map-weights-graph-edges/#t=114.05 |
| you’ll get two equivalent solutions, which lead you to the exit on the left. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=116.89 |
| Clearly, both paths have the same length, | https://realpython.com/lessons/python-map-weights-graph-edges/#t=121.55 |
| so it shouldn’t matter which one you choose. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=124.1 |
| 02:06 | https://realpython.com/lessons/python-map-weights-graph-edges/#t=126.817 |
| However, | https://realpython.com/lessons/python-map-weights-graph-edges/#t=126.817 |
| notice that the upper path contains three extra nodes representing the | https://realpython.com/lessons/python-map-weights-graph-edges/#t=127.66 |
| intersections, | https://realpython.com/lessons/python-map-weights-graph-edges/#t=131.28 |
| including one with a chamber opening in the middle of the maze, while the lower | https://realpython.com/lessons/python-map-weights-graph-edges/#t=131.92 |
| path doesn’t. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=135.72 |
| 02:19 | https://realpython.com/lessons/python-map-weights-graph-edges/#t=139.48 |
| Now you have weights in your graph, | https://realpython.com/lessons/python-map-weights-graph-edges/#t=139.48 |
| you can use other factors besides the distance to influence the algorithm’s | https://realpython.com/lessons/python-map-weights-graph-edges/#t=141.2 |
| decision. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=145.26 |
| For example, | https://realpython.com/lessons/python-map-weights-graph-edges/#t=145.921 |
| you may want to favor paths with fewer enemies or more rewards, | https://realpython.com/lessons/python-map-weights-graph-edges/#t=147.04 |
| and you’ll find out how to do that in the next section of the course. | https://realpython.com/lessons/python-map-weights-graph-edges/#t=151.08 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/python-maze-shortest-path/ |
| Overview | https://realpython.com/courses/python-maze-solver/ |
| https://realpython.com/lessons/python-maze-enemies-rewards/ |
|
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