Skip to main content

Graph algorithms package: Dijkstra’s shortest path and BFS shortest path

Project description

graphs-sayuj0

A Python library that implements classic graph algorithms.
This package was built for a software packaging assignment and includes:

  • Dijkstra’s shortest path algorithm (for weighted graphs)
  • BFS shortest path algorithm (for unweighted graphs)

Dijkstra's Shortest Path Algorithm

Dijkstra’s Shortest Path Algorithm is a classic greedy algorithm used to find the shortest path from a single source node to all other nodes in a weighted graph with non-negative edge weights. It works by maintaining a set of nodes whose shortest distance from the source is already known, then repeatedly selecting the node with the smallest tentative distance, updating the distances of its neighbors, and marking it as processed. This process continues until all reachable nodes have their shortest distances finalized. Essentially, it builds the shortest path tree step by step, ensuring that once a node’s shortest distance is determined, it is never updated again

Breadth-First Search Algorithm

BFS is a fundamental graph traversal algorithm that explores a graph level by level, starting from a source node. It uses a queue to visit all immediate neighbors of the source before moving on to their neighbors, ensuring the closest nodes are always processed first. In unweighted graphs, BFS naturally finds the shortest path from the source to all other reachable nodes because it visits nodes in order of increasing distance. Unlike Dijkstra’s (which handles weighted graphs), BFS is simpler and more efficient when edge weights are all equal (or absent).

Installation

Install the latest package:

pip install graphs-sayuj0

Usage

Prepare a text file describing your graph:

  • The first line is a header (ignored).
  • Each following line contains: source destination weight

Example (graph.txt):

# s d w
0 1 4
0 2 1
2 1 2
2 3 5
1 3 1

Run the program:

python test.py graph.txt

Output:

Shortest distances from 0:
{0: 0, 1: 3, 2: 1, 3: 4}
spf to 0: [0]
spf to 1: [0, 2, 1]
spf to 2: [0, 2]
spf to 3: [0, 2, 1, 3]

Development

  • main branch is protected
  • dev branch is used for active development

Links

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

graphs_sayuj0-0.0.2.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

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

graphs_sayuj0-0.0.2-py3-none-any.whl (2.0 kB view details)

Uploaded Python 3

File details

Details for the file graphs_sayuj0-0.0.2.tar.gz.

File metadata

  • Download URL: graphs_sayuj0-0.0.2.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for graphs_sayuj0-0.0.2.tar.gz
Algorithm Hash digest
SHA256 a57e2bcbde2f01fd279c20342e1352e7557cc5b893f8bd795764871fe6772075
MD5 d38c3e0d43f94d3e4abc59a9ce505d5f
BLAKE2b-256 eb6d48cd2f498690461e0d92bb347bec990d3b4cf123a6d626dc2d404dbb8c0b

See more details on using hashes here.

File details

Details for the file graphs_sayuj0-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: graphs_sayuj0-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 2.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for graphs_sayuj0-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c0ddd9ac6ad9b7770ec0d7378936e62eee2e408dd275d90e075f3bf6e1dfd55b
MD5 fc3a84bbe88803176bfea0757186c0de
BLAKE2b-256 2719e4fe82a723afd0adf1e167a06d66c01731feed6d04394026f3726401e7fe

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