Skip to main content

A small example package

Project description

Pathfinding

This package provides a pure Python implementation for Dijkstra and A-Star as well as a faster Cython implementation.

Using the pure Python Djikstra

from pypathfinder.Dijkstra import Node, bestpath
def example_nodesystem():
    # https://de.wikipedia.org/wiki/Dijkstra-Algorithmus#Beispiel_mit_bekanntem_Zielknoten
    frankfurt = Node("Frankfurt")
    mannheim = Node("Mannheim")
    kassel = Node("Kassel")
    wuerzburg = Node("Würzburg")
    frankfurt.connect({mannheim: 85, wuerzburg: 217, kassel: 173}, True)
    # True value results in connecting all the given Nodes to frankfurt and back with the same cost
    karlsruhe = Node("Karlsruhe")
    mannheim.connect({karlsruhe:80}, True)

    erfurt = Node("Erfurt")
    nuernberg = Node("Nürnberg")
    wuerzburg.connect({erfurt: 186, nuernberg: 103}, True)

    stuttgart = Node("Stuttgart")
    nuernberg.connect({stuttgart: 183}, True)

    augsburg = Node("Augsburg")
    karlsruhe.connect({augsburg: 250}, True)

    muenchen = Node("München")
    muenchen.connect({augsburg: 84, nuernberg: 167, kassel: 502}, True)
    return frankfurt, muenchen

frankfurt, muenchen = example_nodesystem()
path = bestpath(frankfurt, muenchen)
# path includes start and endpoint
# all Nodes have a cost value now

Using the Cython Djikstra

from pypathfinder.fast import CNode, djikstra_bestpath
def example_nodesystem():
    # https://de.wikipedia.org/wiki/Dijkstra-Algorithmus#Beispiel_mit_bekanntem_Zielknoten
    frankfurt = CNode("Frankfurt")
    mannheim = CNode("Mannheim")
    kassel = CNode("Kassel")
    wuerzburg = CNode("Würzburg")
    frankfurt.connect({mannheim: 85, wuerzburg: 217, kassel: 173}, True)

    karlsruhe = CNode("Karlsruhe")
    mannheim.connect({karlsruhe:80}, True)

    erfurt = CNode("Erfurt")
    nuernberg = CNode("Nürnberg")
    wuerzburg.connect({erfurt: 186, nuernberg: 103}, True)

    stuttgart = CNode("Stuttgart")
    nuernberg.connect({stuttgart: 183}, True)

    augsburg = CNode("Augsburg")
    karlsruhe.connect({augsburg: 250}, True)

    muenchen = CNode("München")
    muenchen.connect({augsburg: 84, nuernberg: 167, kassel: 502}, True)
    return frankfurt, muenchen

frankfurt, muenchen = example_nodesystem()
path = bestpath(frankfurt, muenchen)
# path includes start and endpoint
# all Nodes have a cost value now

Using INode (example for the pure Python version)

...

from pypathfinder.Dijkstra import INode, ibestpath, copy_graph
from pypathfinder.utils import PathError

point1 = INode(1)
point2 = INode(2, lambda x: x % 2 == 1)
point3 = INode(3, lambda x: x % 2 == 0)
point4 = INode(4)

point1.connect({point2: 15, point3: 5}, True)
point4.connect({point2: 15, point3: 10}, True)
try:
    path = bestpath(point1, point4) # time 0 at startingposition
except PathError:
    print("Error, no path")
else:
    print(path)

point1, point4, graph = copy_graph(point1, point4)
graph.get(point2).connect({graph.get(point3): 2}, True)
try:
    path = bestpath(point1, point4)
except PathError:
    print("Error, no path")
else:
    print(path)

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

pypathfinder-1.2.tar.gz (196.5 kB view details)

Uploaded Source

Built Distribution

pypathfinder-1.2-cp37-cp37m-win_amd64.whl (318.4 kB view details)

Uploaded CPython 3.7m Windows x86-64

File details

Details for the file pypathfinder-1.2.tar.gz.

File metadata

  • Download URL: pypathfinder-1.2.tar.gz
  • Upload date:
  • Size: 196.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for pypathfinder-1.2.tar.gz
Algorithm Hash digest
SHA256 0c00b9879585cc22422a201209618711c296d88aea3d88c4d13ee1632b5962aa
MD5 c854f1baf9b219d76a92da13d4f8a4eb
BLAKE2b-256 30d63dae6d0bc50a9128349182105556d7bf3db5544ecfe3b5e58f23771efb9b

See more details on using hashes here.

File details

Details for the file pypathfinder-1.2-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: pypathfinder-1.2-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 318.4 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for pypathfinder-1.2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 b45f8175870d97595b75069e2f92c9ca401940130b34e278c8244ca5dadf22fc
MD5 2a53a3f716260db524dc2a7e25689f4e
BLAKE2b-256 b905156bf73a7f30514d35afea2f6bf24579c289c3104dd1e78e25c4a6b9357b

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