Skip to main content

Implementation of Dijkstra's algorithm with graph visualization.

Project description

GraphePython

GraphePython is an implementation of Dijkstra's algorithm to find the shortest path in a non-oriented graph. This project allows you to easily create graphs and find the shortest path. It also supports a graphic visualisation.

Functionalities

  • Creation of graphs with nodes and weighted edges
  • Search of the shortest path in a non oriented graph
  • Support graph visualisation with networkx and matplotlib

Prerequisites

  • Python 3.7 or higher
  • You must have the following libraries installed :
    • matplotlib
    • networkx

Installation

You can install GraphePython via pip by running the following command :

pip install GraphePython

Tests

If you to run some tests to check if the module is working correctly, you can use the tests int the tests folder and run the following command :

python -m unittest discover tests

Example codes

To use GraphePython, you first have to import it and create a new graph :

import GraphePython as gp

graph = gp.Graph()

The next step is to add nodes to your graph. You can either add them one by one or from an array :

graph.add_node("A") # Adding a single node
graph.add_nodes_from_array(["B", "C", "D", "E", "F", "G", "H", "I", "J", "K"]) # Adding all the nodes from B to K at once

Now that you have multiple nodes you can connect them by adding edges. Unlike nodes, edges can only be added one at a time :

graph.add_edge("A", "B", 18) # Adds an edge from A to B (and from B to A) with a weight (or cost) of 18
graph.add_edge("A", "C", 22)
graph.add_edge("B", "C", 31)
graph.add_edge("C", "F", 17)
graph.add_edge("B", "E", 26)
graph.add_edge("B", "D", 12)
graph.add_edge("E", "F", 12)
graph.add_edge("D", "G", 24)
graph.add_edge("H", "G", 12)
graph.add_edge("H", "I", 7)
graph.add_edge("H", "K", 24)
graph.add_edge("K", "J", 18)
graph.add_edge("I", "J", 12)
graph.add_edge("F", "I", 13)
graph.add_edge("G", "E", 9)

Finally you can get the shortest path between two nodes :

graph.get_path("A", "K", draw=False) # Returns the shortest path between A and K in an array here : ['A', 'C', 'F', 'I', 'J', 'K']

You can also see your graph by setting the draw input to True or by calling the draw graph function :

graph.get_path("A", "K", draw=True) # Returns the shortest path and shows it in a Matplotlib window
graph.draw_graph(path = [], path_text = "Graph title") # This creates a new window. You can provide any path you want and it will be highlighted in red (e.g : ['A', 'B', 'E']) You can also provide a text that will be displayed above the graph in the window.

This should give you something like this :

Figure : graph visualization exemple using Matplotlib

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

GraphePython-1.1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

GraphePython-1.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file GraphePython-1.1.tar.gz.

File metadata

  • Download URL: GraphePython-1.1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.0

File hashes

Hashes for GraphePython-1.1.tar.gz
Algorithm Hash digest
SHA256 940c75ac0d395fcbfc89b98c58857fbd661a255062b637adcc50a5c80b7ff99e
MD5 fcf7263b68577da244d353456b60998f
BLAKE2b-256 84e16575c6160082732b3b7b5ef2b17135e18e7d0efe8fbc2235267b5a0f8b30

See more details on using hashes here.

File details

Details for the file GraphePython-1.1-py3-none-any.whl.

File metadata

  • Download URL: GraphePython-1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.0

File hashes

Hashes for GraphePython-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 defeec7dd42e5e7d22cfe4334f17206d059fb3309ae0cc49fcab69f1e25b146b
MD5 621d7e47f19bf24f9fa747552599cd2d
BLAKE2b-256 8d978f3cd2214030a10346786f40215fbc34e0ffba960bf2fef36b8cf9fdb2f3

See more details on using hashes here.

Supported by

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