pyalgs.basic package

Submodules

pyalgs.basic.bag module

This module implements a bag or multiset data structure.

A bag or multiset is a generalization of the set data structure which allows repeated or duplicate items to be stored. Items can only be added to the bag and may not be removed. When the items in the bag are iterated there is not restriction on the ordering of the items.

In this module, the implementation of bag is similar to a linked list based stack implementation. In the linked list based implementation, the bag object need to keep track of only the head node. Each node contains an item and a link to the next node.

Note

Python has a built-in class collections.Counter which is similar to a bag or multiset. instead of adding an item, 1 need to be added with the counter associated with that item and elements return all items (including duplicates) in the bag.

Complexity:
  • add – O(1)
class pyalgs.basic.bag.Bag[source]

Bases: object

An implementation of a bag or multiset with linked list.

add(item)[source]

Inserts an item to the bag.

isEmpty()[source]

Check if the bag is empty.

Returns:
True if the bag is empty. False otherwise.
size

The number of items in the bag.

pyalgs.basic.knuth_shuffle module

This module implements a shuffle method using Knuth’s algorithm.

pyalgs.basic.knuth_shuffle.shuffle(seq)[source]

Shuffle a list randomly using Knuth’s algorithm.

The method randomly shuffles a list by iterating over each position and exchanging the element with another random element. The original list is not maintained and will change.

Args:
seq: A list to shuffle.
Returns:
The original list with all elements shuffled.

pyalgs.basic.queue module

This module implements a linked list based queue data structure.

A queue is a data structure to hold a collection of items in order ad which supports operations such as the addition of an item (enqueue) and removal of an item (dequeue) can be performed. The items are always enqueued at the rear end of the queue and dequeued from the front end of the queue. As such, the queue can be also viewed as a First-In-First-Out(FIFO) data structure. In a FIFO data structure, the first item added to the structure is the first one to be removed. Apart from those two operations, peek operation can also be implemented, returning the value at the front end without removing it.

The particular implementation of queue in this module is based on linked list, as array based queue implementation is already supported in python’s list. In the linked list based implementation, the queue object need to keep track of the front and the rear nodes where each node contains an item and a link to the next node.

Note

For most practical purposes, the python’s implementation as dequeue suffices as a queue object. Use append method instead of enqueue and popleft method instead of dequeue for queue operations in a list.

Complexity:
  • push – O(1)
  • pop – O(1)
  • peek – O(1)
class pyalgs.basic.queue.Queue[source]

Bases: object

An implementation of a simple queue with linked list.

dequeue()[source]

Remove and return the first item from the queue.

Returns:
The first item from the queue.
Raises:
IndexError: If the queue is empty.
enqueue(item)[source]

Insert an item to the queue.

isEmpty()[source]

Check if the queue is empty.

Returns:
True if the queue is empty. False otherwise.
peek()[source]

Return the first item from the queue.

Returns:
The first item from the queue.
Raises:
IndexError: If the queue is empty.
size

The number of items in the queue.

pyalgs.basic.stack module

This module implements a linked list based stack data structure.

A stack is a data structure to hold a collection of items in which operations such as the addition of an item (push) and removal of an item (pop) can be performed. The items are always pushed or popped from the so called top of the data structure which is the last item added or first item to be removed. The stack can be also viewed as a Last-In-First-Out(LIFO) data structure. In a LIFO data structure, the last item added to the structure must be the first item one to be removed. Apart from those two operations, peek operation can also be implemented, returning the value of the top item without removing it.

The particular implementation of stack in this module is based on linked list, as array based stack implementation is already supported in python’s list. In the linked list based implementation, the stack object need to keep track of only the head node. Each node contains an item and a link to the next node.

Note

For most practical purposes, the python’s list suffices as a stack object. Use append method instead of push and pop method as it is for stack operations in a list.

Complexity:
  • push – O(1)
  • pop – O(1)
  • peek – O(1)
class pyalgs.basic.stack.Stack[source]

Bases: object

An implementation of a simple stack with linked list.

isEmpty()[source]

Check if the stack is empty.

Returns:
True if the stack is empty. False otherwise.
peek()[source]

Return the last added item from the stack.

Returns:
The last item added to the stack.
Raises:
IndexError: If the stack is empty.
pop()[source]

Remove and return the last added item from the stack.

Returns:
The last item added to the stack.
Raises:
IndexError: If the stack is empty.
push(item)[source]

Insert an item to the stack.

size

The number of items in the stack.

pyalgs.basic.union_find module

This module implements an union find or disjoint set data structure.

An union find data structure can keep track of a set of elements into a number of disjoint (nonoverlapping) subsets. That is why it is also known as the disjoint set data structure. Mainly two useful operations on such a data structure can be performed. A find operation determines which subset a particular element is in. This can be used for determining if two elements are in the same subset. An union Join two subsets into a single subset.

The complexity of these two operations depend on the particular implementation. It is possible to achieve constant time (O(1)) for any one of those operations while the operation is penalized. A balance between the complexities of these two operations is desirable and achievable following two enhancements:

  1. Using union by rank – always attach the smaller tree to the root of the larger tree.
  2. Using path compression – flattening the structure of the tree whenever find is used on it.
complexity:
class pyalgs.basic.union_find.UF(N)[source]

An implementation of union find data structure. It uses weighted quick union by rank with path compression.

connected(p, q)[source]

Check if the items p and q are on the same set or not.

count()[source]

Return the number of items.

find(p)[source]

Find the set identifier for the item p.

union(p, q)[source]

Combine sets containing p and q into a single set.

Module contents

This module contains few basic algorithms that do not fit in other packages. Trivial algorithms and data structures that are built into python are skipped.