Skip to main content

Overview

This quickstart guide will walk you through using the most common data structures in UCX DSA. All examples use real code directly from the source implementation.
Make sure you have installed UCX DSA before running these examples.

Using a Stack

Stacks follow Last-In-First-Out (LIFO) ordering. The UCX DSA package provides both static and dynamic stack implementations.
1

Import and create a Stack

from dsa.stack import Stack

# Create a stack with default capacity of 10
stack = Stack()

# Or specify a custom capacity
stack = Stack(capacity=20)
2

Push elements onto the stack

stack.push(10)
stack.push(20)
stack.push(30)

print(stack)
# Output: [10, 20, 30] Top: 2 Capacity: 10
3

Access and remove elements

# View the top element without removing it
top_element = stack.peek()
print(top_element)  # Output: 30

# Pop the top element
popped = stack.pop()
print(popped)  # Output: 30
print(len(stack))  # Output: 2

# Check if stack is empty
print(stack.is_empty())  # Output: False
from dsa.stack import Stack

# Fixed capacity - will raise exception if exceeded
stack = Stack(capacity=5)
stack.push(1)
stack.push(2)
stack.push(3)

Using a Queue

Queues follow First-In-First-Out (FIFO) ordering. UCX DSA provides Queue, DynamicQueue, and CircularQueue implementations.
1

Import and create a Queue

from dsa.queue import Queue

# Create a queue with default capacity of 10
queue = Queue()

# Or create with initial contents
queue = Queue(contents=[1, 2, 3])
2

Enqueue elements

queue = Queue()
queue.enqueue("first")
queue.enqueue("second")
queue.enqueue("third")

print(queue)
# Output: [first, second, third] Count: 3 Capacity: 10
3

Dequeue and peek

# View front element without removing
front = queue.peek()
print(front)  # Output: first

# Remove and return front element
removed = queue.dequeue()
print(removed)  # Output: first
print(queue.to_ordered_list())  # Output: ['second', 'third']
from dsa.queue import Queue

queue = Queue(capacity=10)
queue.enqueue("A")
queue.enqueue("B")
queue.enqueue("C")
print(queue.dequeue())  # Output: A

Using an Array

Arrays provide indexed access to elements. UCX DSA offers Array, DynamicArray, and CircularArray classes.
1

Import and create an Array

from dsa.array import Array

# Create empty array with capacity 10
arr = Array()

# Create with initial contents
arr = Array(contents=[1, 2, 3, 4, 5])

# Create with specific capacity
arr = Array(capacity=20)
2

Add and access elements

arr = Array()
arr.append(10)
arr.append(20)
arr.append(30)

# Access by index
print(arr[0])  # Output: 10
print(arr[2])  # Output: 30

# Modify by index
arr[1] = 25
print(arr[1])  # Output: 25
3

Insert and delete

# Insert at specific index
arr.insert(1, 15)  # Insert 15 at index 1
print(arr.to_list())  # Output: [10, 15, 25, 30]

# Delete by index
arr.delete(2)  # Remove element at index 2
print(arr.to_list())  # Output: [10, 15, 30]

print(f"Count: {len(arr)}, Capacity: {arr.capacity()}")
from dsa.array import Array

# Fixed capacity array
arr = Array(capacity=5)
arr.append(1)
arr.append(2)
arr.insert(0, 0)  # [0, 1, 2]

Using a Graph

Graphs can be represented using adjacency matrices or adjacency lists. UCX DSA provides a factory pattern for creating graphs.
1

Create a graph using the factory

from dsa.graph import Graph

# Create an unweighted, undirected adjacency list graph
graph = Graph.create('adjacency_list', directed=False, weighted=False)

# Create a weighted, directed adjacency matrix graph
weighted_graph = Graph.create(
    'adjacency_matrix',
    directed=True,
    weighted=True,
    vertices=['A', 'B', 'C', 'D']
)
2

Add vertices and edges

# For unweighted graph
graph.add_vertex('A')
graph.add_vertex('B')
graph.add_vertex('C')
graph.add_edge('A', 'B')  # Undirected edge A-B
graph.add_edge('B', 'C')  # Undirected edge B-C

# For weighted graph
weighted_graph.add_edge('A', 'B', weight=5)
weighted_graph.add_edge('B', 'C', weight=3)
weighted_graph.add_edge('A', 'D', weight=7)
3

Query the graph

# Check if vertex exists
print(graph.has_vertex('A'))  # Output: True

# Check if edge exists
print(graph.has_edge('A', 'B'))  # Output: True

# Get adjacent vertices
print(graph.adjacents('B'))  # Output: ['A', 'C']

# Get all vertices and edges
print(graph.vertices())  # Output: ['A', 'B', 'C']
print(graph.edges())  # Output: [('A', 'B'), ('B', 'C'), ...]

# Get graph properties
print(graph.order())  # Number of vertices
print(graph.size())   # Number of edges
from dsa.graph import Graph

# Efficient for sparse graphs
graph = Graph.create_adjacency_list(
    directed=False,
    weighted=False
)
graph.add_edge('A', 'B')
graph.add_edge('B', 'C')
print(graph['B'])  # Adjacent to B

Next Steps

Now that you’ve seen the basics, explore more data structures and algorithms:

API Reference

Complete documentation for all classes and methods

GitHub Repository

View source code and revision history
All data structures support common Python operations like len(), equality testing with ==, and the in operator where applicable.

Build docs developers (and LLMs) love