Skip to main content

path finding algorithms in python

Project description

PathFind

Implementation of path finding algorithms including:

  • Depth-First Search (DFS)
  • Breadth-First Search (BFS)
  • Dijkstra Search
  • Greedy Best-First Search
  • A*
  • D*-Lite

Install

pip install pathfind

Basic Usage

Define a graph to transform graph from a matrix, then find a path from start point to end point. The value in m indicates a cost at that node. Note that the -1 in m represents the cost in that node is infinity, which means this node is not connected to others.

import pathfind

m = [
    [1, 1, 1, 1, 1],
    [1, 2, -1, 1, 1],
    [1, 1, 1, 1, 1],
    [8, 3, 1, 1, 1],
    [1, 1, 1, 1, 1],
]
graph = pathfind.transform.matrix2graph(m)
path = pathfind.find(graph, start="4,0", end="0,0")
# ['4,0', '4,1', '3,1', '2,1', '2,0', '1,0', '0,0']

graph.plot(trace=path)
drawing

Finder can be changed by passing a string method ("a*", "bfs", "greedy", "dijkstra", "dfs").

path = pathfind.find(graph, start="4,0", end="0,0", method="bfs")
# ['2,2', '2,1', '1,1', '0,1', '0,2']

graph.plot(trace=path)
drawing

Graph setup

Another way to define a graph is to config the edge by give [node1's name, node2's name, cost] pairs.

conf = [
    # [node1's name, node2's name, weight, *back_weight]
    ["n1", "n2", 0.1],
    ["n1", "n3", 0.2],
    ["n2", "n3", 0.3]
]
graph = pathfind.Graph(conf)
graph.plot()
drawing

Or you can set edge's and node's details by following way:

my_n0 = pathfind.Node(name="my_n0")  # node name set to "my_n0"
auto_name = pathfind.Node()  # node name automatically set to "n0"
n2 = "n2"  # pass a string to represent node name
e0 = pathfind.Edge(node1=my_n0, node2=auto_name, weight=0.2)
e1 = pathfind.Edge(node1=my_n0, node2=n2, weight=0.1)
e2 = pathfind.Edge(auto_name, n2, weight=0)

g = pathfind.Graph()
g.add_edges([e0, e1, e2])
g.edges
"""
{'my_n0:n0': my_n0:n0, 'my_n0:n2': my_n0:n2, 'n0:n2': n0:n2}
"""

More examples

More examples and usages can be found in test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pathfind-0.0.4.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

pathfind-0.0.4-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file pathfind-0.0.4.tar.gz.

File metadata

  • Download URL: pathfind-0.0.4.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.9

File hashes

Hashes for pathfind-0.0.4.tar.gz
Algorithm Hash digest
SHA256 61e060bb973d6f9a338e018602b27ef122b42a19fe3124b92b2f9be117fd2415
MD5 e6d2e447471d651bfa4e764796b2afd7
BLAKE2b-256 9809c47647c40324d3ef47a43571ebfb1a6719f0a6f923980fdb935f93557e04

See more details on using hashes here.

File details

Details for the file pathfind-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: pathfind-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.9

File hashes

Hashes for pathfind-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 abcf41a239fd0398d6cc49ea2f37672c77c37f0cbfe4f64a39723c1a6f0d80a1
MD5 70942ce377a035ccd357e1dc05091f61
BLAKE2b-256 15a67be63fe4a6ce8b167debe12109795f5d5cf311d471b0d509cb96ad3aef0a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page