GraphePython
GraphePython is a Python library for creating and analyzing graphs. It provides an implementation of Dijkstra's algorithm to find the shortest path in an undirected graph, along with tools for graph visualization and random graph generation.
Table of Contents
Features
- Create graphs with nodes and weighted edges
- Find the shortest path in an undirected graph
- Visualize graphs with NetworkX and Matplotlib
- Generate random connected graphs
Prerequisites
- Python 3.7 or higher
- You must have the following libraries installed :
matplotlib3.0 or highernetworkx2.0 or higher
Installation
You can install GraphePython via pip by running the following command :
pip install GraphePython
Tests
If you want 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
The basics
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)
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 :
Finding the shortest path
You can get the shortest path between two nodes by using the get_path function :
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']
Random graphs generation
You can also generate random graphs with GraphePython. Here's an example:
import GraphePython as gp
# Create a new graph instance
graph = gp.Graph()
# Generate a random graph with the following parameters:
# - 10 nodes
# - 15 edges
# - Edge weights ranging from 1 to 10
# - Node naming method: LETTERS (nodes will be named A, B, C, ...)
graph.generate_random_graph(
number_of_nodes=10,
number_of_edges=15,
weight_range=(1, 10),
node_naming_method="LETTERS"
)
# Visualize the generated graph
graph.draw_graph(path=[], path_text="Randomly Generated Graph")
# Save the generated graph to a file
graph.save_graph("random_graph.txt", "myGraphs/")
# Load the graph back from the file (to verify saving/loading works)
graph.load_graph("random_graph.txt", "myGraphs/")
graph.draw_graph(path=[], path_text="Loaded Random Graph")
This will generate a random graph, visualize it, save it to a file, and reload it for further use.
Saving and loading graphs
As mentioned above, 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.
Metadata
Release files for GraphePython 1.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| graphepython-1.4.1.tar.gz | 14.5 kB | Without provenance | Details for graphepython-1.4.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| graphepython-1.4.1-py3-none-any.whl | Python 3 | none | any | Details for graphepython-1.4.1-py3-none-any.whl |
Total release size: 27.0 kB
Release files / graphepython-1.4.1.tar.gz
| Download URL | graphepython-1.4.1.tar.gz |
|---|---|
| Size | 14.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
61d5571742373f464e70bd4b52c5b3b201f87d077720d9cc5fdb449ea8eec455
|
|
BLAKE2b-256 checksum How to use checksums |
90446c864bcd21affd46e7988e22a26b30eba5a0f06d09f64cca09674974a784
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Release files / graphepython-1.4.1-py3-none-any.whl
| Download URL | graphepython-1.4.1-py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
def583af271834c3d7dd4cdbb4909147ff69dca13b314892c7340353d34593e3
|
|
BLAKE2b-256 checksum How to use checksums |
2243eaceb37469cda4a1eed0eb3f51fa006a7aa537317190b44994ce85fca4c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Security for GraphePython 1.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.