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 an undirected 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 an undirected 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 in the tests folder and run the following command :

python -m unittest discover tests

Exemple 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

Finally, you can save your graphs into text files :

graph.save_graph("graph.txt", "myGraphs/") # This will save the current graph into the graph.txt file in the myGraphs folder
graph.load_graph("graph.txt", "myGraphs/") # This loads the previously saved graph

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.2.tar.gz (7.5 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.2-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for GraphePython-1.2.tar.gz
Algorithm Hash digest
SHA256 bd54d12c48d5a35c49cb585895806969ad38f4e7c022f265cb40e6a5aff8354b
MD5 4a5cfd3d9cc45f5874943c80c60e8bef
BLAKE2b-256 79b0bb26e13ab7935bf7d002e92d6db61a9a95d8e826c062deae5fd5e80e57dc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: GraphePython-1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d7b78f61c08d8060713de2b0641b9065976fdeaf8f2e03e44712580f435648dd
MD5 f954d38ccc85a114c14d850d17e68f83
BLAKE2b-256 aaa8b55d22f619c7cfc8ce4b83acb7cb90532decae6fc9c0aa83bdbba19fa7fe

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