.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/graph_theory/search/plot_01_astar_search.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_graph_theory_search_plot_01_astar_search.py: A* search on a grid with obstacles ======================================== Finds a shortest path across a grid with a wall. With the Manhattan distance as its heuristic, A* reaches the same distance as Dijkstra's algorithm while expanding far fewer vertices. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.graph_theory import astar_shortest_path, grid_graph .. GENERATED FROM PYTHON SOURCE LINES 17-19 Build the grid and search it twice ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-43 .. code-block:: Python rows = cols = 30 blocked = [(r, 15) for r in range(0, 24)] + [(8, c) for c in range(3, 15)] g, pos = grid_graph(rows, cols, blocked=blocked) source, target = 2 * cols + 2, (rows - 3) * cols + (cols - 3) def manhattan(v): return float(np.abs(pos[v] - pos[target]).sum()) guided = astar_shortest_path(g, source, target, manhattan) blind = astar_shortest_path(g, source, target) print(f"A* (Manhattan): distance {guided.distance:.0f}, expanded {guided.n_expanded} vertices") print(f"Dijkstra (h = 0): distance {blind.distance:.0f}, expanded {blind.n_expanded} vertices") fig, ax = plt.subplots(figsize=(6, 6)) wall = np.array([(c, r) for r, c in blocked]) ax.plot(*wall.T, "ks", ms=6) ax.plot(*pos[guided.path].T, "-", color="tab:red", lw=2.5, label="A* path") ax.plot(*pos[[source, target]].T, "o", color="tab:green", ms=10) ax.set_aspect("equal") ax.invert_yaxis() ax.legend() .. image-sg:: /api/gallery/graph_theory/search/images/sphx_glr_plot_01_astar_search_001.png :alt: plot 01 astar search :srcset: /api/gallery/graph_theory/search/images/sphx_glr_plot_01_astar_search_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none A* (Manhattan): distance 50, expanded 378 vertices Dijkstra (h = 0): distance 50, expanded 621 vertices .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.017 seconds) .. _sphx_glr_download_api_gallery_graph_theory_search_plot_01_astar_search.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_astar_search.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_astar_search.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_astar_search.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_