Dijkstar


NameDijkstar JSON
Version 2.6.0 PyPI version JSON
download
home_pagehttps://github.com/wylee/Dijkstar
SummaryDijkstra/A*
upload_time2021-03-30 19:04:09
maintainer
docs_urlNone
authorWyatt Baldwin
requires_python
licenseMIT
keywords dijkstra a* algorithms
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Dijkstar
++++++++

Dijkstar is an implementation of Dijkstra's single-source shortest-paths
algorithm. If a destination node is given, the algorithm halts when that
node is reached; otherwise it continues until paths from the source node
to all other nodes are found.

Accepts an optional cost (or "weight") function that will be called on
every iteration.

Also accepts an optional heuristic function that is used to push the
algorithm toward a destination instead of fanning out in every
direction. Using such a heuristic function converts Dijkstra to A* (and
this is where the name "Dijkstar" comes from).

Example::

    >>> from dijkstar import Graph, find_path
    >>> graph = Graph()
    >>> graph.add_edge(1, 2, 110)
    >>> graph.add_edge(2, 3, 125)
    >>> graph.add_edge(3, 4, 108)
    >>> find_path(graph, 1, 4)
    PathInfo(
        nodes=[1, 2, 3, 4],
        edges=[110, 125, 108],
        costs=[110, 125, 108],
        total_cost=343)

In this example, the edges are just simple numeric values--110, 125,
108--that could represent lengths, such as the length of a street
segment between two intersections. ``find_path`` will use these values
directly as costs.

Example with cost function::

    >>> from dijkstar import Graph, find_path
    >>> graph = Graph()
    >>> graph.add_edge(1, 2, (110, 'Main Street'))
    >>> graph.add_edge(2, 3, (125, 'Main Street'))
    >>> graph.add_edge(3, 4, (108, '1st Street'))
    >>> def cost_func(u, v, edge, prev_edge):
    ...     length, name = edge
    ...     if prev_edge:
    ...         prev_name = prev_edge[1]
    ...     else:
    ...         prev_name = None
    ...     cost = length
    ...     if name != prev_name:
    ...         cost += 10
    ...     return cost
    ...
    >>> find_path(graph, 1, 4, cost_func=cost_func)
    PathInfo(
        nodes=[1, 2, 3, 4],
        edges=[(110, 'Main Street'), (125, 'Main Street'), (108, '1st Street')],
        costs=[120, 125, 118],
        total_cost=363)

The cost function is passed the current node (u), a neighbor (v) of the
current node, the edge that connects u to v, and the edge that was
traversed previously to get to the current node.

A cost function is most useful when computing costs dynamically. If
costs in your graph are fixed, a cost function will only add unnecessary
overhead. In the example above, a penalty is added when the street name
changes.

When using a cost function, one recommendation is to compute a base cost when
possible. For example, for a graph that represents a street network, the base
cost for each street segment (edge) could be the length of the segment
multiplied by the speed limit. There are two advantages to this: the size of
the graph will be smaller and the cost function will be doing less work, which
may improve performance.

Graph Export & Import
=====================

The graph can be saved to disk (pickled) like so::

    >>> graph.dump(path)

And read back like this (load is a classmethod that returns a
populated Graph instance)::

    >>> Graph.load(path)



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/wylee/Dijkstar",
    "name": "Dijkstar",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "Dijkstra A* algorithms",
    "author": "Wyatt Baldwin",
    "author_email": "self@wyattbaldwin.com",
    "download_url": "https://files.pythonhosted.org/packages/59/91/339563337ba35701743a69467c8a95678860935bd99745b5c4fe56028956/Dijkstar-2.6.0.tar.gz",
    "platform": "",
    "description": "Dijkstar\n++++++++\n\nDijkstar is an implementation of Dijkstra's single-source shortest-paths\nalgorithm. If a destination node is given, the algorithm halts when that\nnode is reached; otherwise it continues until paths from the source node\nto all other nodes are found.\n\nAccepts an optional cost (or \"weight\") function that will be called on\nevery iteration.\n\nAlso accepts an optional heuristic function that is used to push the\nalgorithm toward a destination instead of fanning out in every\ndirection. Using such a heuristic function converts Dijkstra to A* (and\nthis is where the name \"Dijkstar\" comes from).\n\nExample::\n\n    >>> from dijkstar import Graph, find_path\n    >>> graph = Graph()\n    >>> graph.add_edge(1, 2, 110)\n    >>> graph.add_edge(2, 3, 125)\n    >>> graph.add_edge(3, 4, 108)\n    >>> find_path(graph, 1, 4)\n    PathInfo(\n        nodes=[1, 2, 3, 4],\n        edges=[110, 125, 108],\n        costs=[110, 125, 108],\n        total_cost=343)\n\nIn this example, the edges are just simple numeric values--110, 125,\n108--that could represent lengths, such as the length of a street\nsegment between two intersections. ``find_path`` will use these values\ndirectly as costs.\n\nExample with cost function::\n\n    >>> from dijkstar import Graph, find_path\n    >>> graph = Graph()\n    >>> graph.add_edge(1, 2, (110, 'Main Street'))\n    >>> graph.add_edge(2, 3, (125, 'Main Street'))\n    >>> graph.add_edge(3, 4, (108, '1st Street'))\n    >>> def cost_func(u, v, edge, prev_edge):\n    ...     length, name = edge\n    ...     if prev_edge:\n    ...         prev_name = prev_edge[1]\n    ...     else:\n    ...         prev_name = None\n    ...     cost = length\n    ...     if name != prev_name:\n    ...         cost += 10\n    ...     return cost\n    ...\n    >>> find_path(graph, 1, 4, cost_func=cost_func)\n    PathInfo(\n        nodes=[1, 2, 3, 4],\n        edges=[(110, 'Main Street'), (125, 'Main Street'), (108, '1st Street')],\n        costs=[120, 125, 118],\n        total_cost=363)\n\nThe cost function is passed the current node (u), a neighbor (v) of the\ncurrent node, the edge that connects u to v, and the edge that was\ntraversed previously to get to the current node.\n\nA cost function is most useful when computing costs dynamically. If\ncosts in your graph are fixed, a cost function will only add unnecessary\noverhead. In the example above, a penalty is added when the street name\nchanges.\n\nWhen using a cost function, one recommendation is to compute a base cost when\npossible. For example, for a graph that represents a street network, the base\ncost for each street segment (edge) could be the length of the segment\nmultiplied by the speed limit. There are two advantages to this: the size of\nthe graph will be smaller and the cost function will be doing less work, which\nmay improve performance.\n\nGraph Export & Import\n=====================\n\nThe graph can be saved to disk (pickled) like so::\n\n    >>> graph.dump(path)\n\nAnd read back like this (load is a classmethod that returns a\npopulated Graph instance)::\n\n    >>> Graph.load(path)\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Dijkstra/A*",
    "version": "2.6.0",
    "project_urls": {
        "Homepage": "https://github.com/wylee/Dijkstar"
    },
    "split_keywords": [
        "dijkstra",
        "a*",
        "algorithms"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5389620e594fe1a58517d224a9a9cd05d56583e2c12a5a520cb3a67851601570",
                "md5": "ddeb52a6c3feb7ae2a7fc88c6c26e024",
                "sha256": "49c7faad4273bc88609f688d5c00b80761d06793cb3285268cb385510c6a6e76"
            },
            "downloads": -1,
            "filename": "Dijkstar-2.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ddeb52a6c3feb7ae2a7fc88c6c26e024",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 12142,
            "upload_time": "2021-03-30T19:04:08",
            "upload_time_iso_8601": "2021-03-30T19:04:08.244708Z",
            "url": "https://files.pythonhosted.org/packages/53/89/620e594fe1a58517d224a9a9cd05d56583e2c12a5a520cb3a67851601570/Dijkstar-2.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5991339563337ba35701743a69467c8a95678860935bd99745b5c4fe56028956",
                "md5": "d80c0065634db95f7cc2d60ca1ff70b1",
                "sha256": "ba9fa254c2ab9667a89dbaa9e322e59fc72f20fc6730ea6fe7be1f85e15a5bb7"
            },
            "downloads": -1,
            "filename": "Dijkstar-2.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d80c0065634db95f7cc2d60ca1ff70b1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 14645,
            "upload_time": "2021-03-30T19:04:09",
            "upload_time_iso_8601": "2021-03-30T19:04:09.691686Z",
            "url": "https://files.pythonhosted.org/packages/59/91/339563337ba35701743a69467c8a95678860935bd99745b5c4fe56028956/Dijkstar-2.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-03-30 19:04:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "wylee",
    "github_project": "Dijkstar",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "dijkstar"
}
        
Elapsed time: 0.18129s