Skip to content

Latest commit

 

History

History
30 lines (25 loc) · 1.01 KB

03_graphs.md

File metadata and controls

30 lines (25 loc) · 1.01 KB

Using a graph

The default is to plan a path from one node of a grid to another but you can use any graph that has a weight (also known as cost) assigned to its edges.

This is based on the animation at Wikipedia about Dijkstra's algorithm

from pathfinding.core.diagonal_movement import DiagonalMovement
from pathfinding.core.graph import Graph
from pathfinding.core.node import Node
from pathfinding.finder.dijkstra import DijkstraFinder

# list values are: source node, target node, distance value
edges = [
    [1, 2, 7],
    [1, 3, 9],
    [1, 6, 14],
    [2, 3, 10],
    [2, 4, 15],
    [3, 4, 11],
    [3, 6, 2],
    [4, 5, 6],
    [6, 5, 9]
]

graph = Graph(edges=edges, bi_directional=True)
finder = DijkstraFinder()
path, runs = finder.find_path(graph.node(1), graph.node(5), graph)

the path holds the list of nodes to move to in order. Instead of numbers you can also use strings (e.g. waypoint or city names)