Skip to main content

A comprehensive Python library for data structures and algorithms

Project description

Using the Python DSA Package

Installation

Install the package via pip:

pip install gsl_331

This package exposes a few classes to manage common data structures. The available classes are:

  • Queue
  • Stack
  • Graph
  • Node
  • LLNode
  • LinkedList
  • Priority_queue

Below are usage examples for each.

Queue

Use the Queue class to enqueue and dequeue items.

from gsl_331 import Queue

q = Queue()
q.enqueue("first")
q.enqueue("second")
print("Queue front:", q.top())  # prints "first"
print("Dequeued:", q.dequeue())
print("Queue is empty:", q.is_empty())

Stack

The Stack class provides LIFO operations.

from gsl_331 import Stack

s = Stack()
s.push("item1")
s.push("item2")
print("Stack top:", s.top())  # prints "item2"
print("Popped item:", s.pop())
print("Is stack empty:", s.is_empty())

Graph and Node

Create a graph, add vertices via random generation, and run BFS/DFS. Both the BFS and DFS operations return custom response objects. The BFS operation returns a BFS_response containing visited nodes, visit order, and parent relationships. Similarly, the DFS provides a DFS_response with entry and exit times, pending nodes, and a stack with the order of the processed nodes.

from gsl_331 import Graph, Node

# Create a directed graph with labels generated for n_node vertices.
g = Graph(n=5, directed=True)
g.generate_random_graph(n_node=5)

# Choose a starting node label (e.g., "a")
bfs_response = g.BFS("a")
print(bfs_response)  # prints visited nodes, order, and parent relationships given as a whole object of type bfs response

dfs_response = g.DFS("a")
print(dfs_response)  # prints visited nodes, entry/exit times, pending nodes, and processing stack given as a whole object of type bfs response

LLNode and LinkedList

Manage a linked list with LLNode and LinkedList classes.

from gsl_331 import LLNode, LinkedList

# Create the head LLNode and initialize the linked list.
head = LLNode(val=10, name="10")
ll = LinkedList(head)

# Insert values
ll.insertAtHead(5)
ll.insertAtTail(15)
print("Linked List:", ll)
print("Search for 15:", ll.search(15))
ll.delete(10)
print("After deleting 10:", ll)

Priority_queue

The Priority_queue supports heap operations with fast lookup by node name. Its responses include returning the top element and providing means to extract a specific node.

from gsl_331 import Priority_queue

# Initialize priority queue with a list of elements.
data = [4, 2, 6, 3]
pq = Priority_queue(data)

print("Priority Queue top:", pq.top())
pq.push(1, name="unique1")
print("Priority Queue after push:", pq)
extracted = pq.exctract_node("unique1")
print("Extracted node value:", extracted.val)

NOTE

that the priority queue contains a dictionary in pq.location_tracker that gives you the real time postions of objects from your pq and makes the extraction of any node O(logn) same as all other operations

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

gsl_331-1.0.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

gsl_331-1.0.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file gsl_331-1.0.0.tar.gz.

File metadata

  • Download URL: gsl_331-1.0.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for gsl_331-1.0.0.tar.gz
Algorithm Hash digest
SHA256 85c0c91d19f334a68de705997b28e27c2330f63b567b4114d022705acbc7a234
MD5 80c9f12cdc6295687e5100532eb86946
BLAKE2b-256 562f4149ea9bac21c9cc33ec756dd1653ec011a3f838a9c63b45e03225a34ae1

See more details on using hashes here.

File details

Details for the file gsl_331-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: gsl_331-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for gsl_331-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b1bbae756c402d55d5cd62fdffa8176c4af62bcab748edcbde9d93e7cd4bf21b
MD5 e294211552045919b6fec404fb31fb4d
BLAKE2b-256 669bf651c4eb342e964026ff63f6be60829e671be7d3ff9fc087cccca26305db

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