Skip to main content

Ready to use data structures

Project description

Data Structure in Python

This repo contains python implementation of data structures. The following data structures are available:

  1. Tree
  2. Heap
  3. Disjoint sets
  4. Graph

Installation

To install from pypi

pip install py-ds

To install from source

git clone https://github.com/nitinkatyal1314/data-structures.git
python setup.py install

Examples

The examples are available in the examples directory and can be used as a playground or a starting point.

Tree creation and traversal (BFS)

from pyds.tree import TreeAPI, TreeWalkStrategy, TreeNodeKeys


name = TreeNodeKeys.NAME
data = TreeNodeKeys.DATA
children = TreeNodeKeys.CHILDREN

# tree as JSON with root node and 2 children (L1 and R1), and R2-1 is the child of R1
# if the tree is large, it is recommended to create smaller subtrees and use the add_child API
# refer examples/tree/add_subtree.py for the API
tree_data = {
    name: "R_",
    data: {},
    children: [
        {
            name: "L1",
            children: [],
            data: {}
        },
        {
            name: "R1",
            children: [
                {
                    name: "R2-1",
                    data: {},
                    children: []
                },
            ],
            data: {}
        }
    ]
}

# callback methof to print the node
def print_nodename(node):
    print(node.name)

# the main API
def main():
    api = TreeAPI()
    root = api.parse(tree_data)
    print("Walking - Breadth First")
    api.walk(root, strategy=TreeWalkStrategy.BREADTH_FIRST, callback=print_nodename)


# run the main method
main()

Heap (Insertion and Deletion)

from pyds.heap import HeapType, Heap

# max heap
heap = Heap(heap_type=HeapType.MAX)

heap.add_node(1)
heap.add_node(2)
heap.add_node(3)
heap.add_node(4)
heap.add_node(5)

# heap print node callback
def print_node(el):
  print("Node : ", el)


# walk the heap
heap.walk(print_node)

# delete the node from the heap
heap.delete_node()

Disjoint set (check items are disjoint)

from pyds.disjointsets import DisjointSetAPI

items = ["A", "B", "C", "D"]
api = DisjointSetAPI()

item1 = "A"
item2 = "B"

# creates 4 sets each with single item
api.create(items)

is_disjoint = api.is_disjoint(item1, item2)

print("Checking if %s and %s are disjoint sets" % (item1, item2))
print(is_disjoint)

print("==========================")
print("Taking a union of sets now")
api.union(item1, item2)

print("==========================")
print("Checking if post union %s and %s are disjoint sets" % (item1, item2))
is_disjoint = api.is_disjoint(item1, item2)
print(is_disjoint)

Graph (Creation and DFS Walk)

from pyds.graph import GraphAPI, GraphTypes

# callback method to print the node during traversal
def print_node(node):
    """
    Callback function to print the node.

    :param node: node of the graph
    :type node: str
    :return:
    :rtype:
    """
    print("Node : ", node)

# create graph, add nodes
api = GraphAPI()

# change this to Directed for directed graphs
graph = api.init_graph(graph_type=GraphTypes.UNDIRECTED)

api.add_node(graph, "A")
api.add_node(graph, "B")
api.add_node(graph, "C")
api.add_node(graph, "D")
api.add_node(graph, "E")
api.add_node(graph, "F")

# connect nodes in graph
api.add_edge(graph, "A", "B")
api.add_edge(graph, "A", "C")
api.add_edge(graph, "C", "B")
api.add_edge(graph, "B", "D")
api.add_edge(graph, "D", "F")
api.add_edge(graph, "C", "E")
api.add_edge(graph, "E", "F")

print("DFS Start ")
api.walk_dfs(graph, "A", print_node)

Graph (Dijikstra - Single source shortest path)

from pyds.graph import GraphAPI, GraphTypes

# create graph (DIRECTED), add nodes
# create graph, add nodes
api = GraphAPI()
graph = api.init_graph(graph_type=GraphTypes.DIRECTED)

api.add_node(graph, "A")
api.add_node(graph, "B")
api.add_node(graph, "C")
api.add_node(graph, "D")
api.add_node(graph, "E")
api.add_node(graph, "F")

# connect nodes in graph
api.add_edge(graph, "A", "B", 5)
api.add_edge(graph, "A", "C", 2)
api.add_edge(graph, "B", "D", 3)
api.add_edge(graph, "B", "C", 9)
api.add_edge(graph, "C", "E", 5)
api.add_edge(graph, "E", "D", 4)
api.add_edge(graph, "E", "F", 6)
api.add_edge(graph, "D", "F", 1)

source_node = "A"

print("Running Dijikstra algorithm starting from node %s. " % source_node)
shortest_distance_data = api.find_shortest_path_using_dijikstra(graph, start_node=source_node)
print("Shortest distance to all nodes from source node [%s] is: " % source_node)
print(shortest_distance_data)

Minimum cost spanning Tree (Prim's and Kruskal)

from pyds.graph import GraphAPI, GraphTypes

# create graph, add nodes
api = GraphAPI()

graph_obj = api.init_graph(graph_type=GraphTypes.UNDIRECTED)

api.add_node(graph_obj, "A")
api.add_node(graph_obj, "B")
api.add_node(graph_obj, "C")
api.add_node(graph_obj, "D")
api.add_node(graph_obj, "E")
api.add_node(graph_obj, "F")
api.add_node(graph_obj, "G")

# connect nodes in graph
api.add_edge(graph_obj, "A", "B", 10)
api.add_edge(graph_obj, "A", "C", 28)
api.add_edge(graph_obj, "B", "E", 25)
api.add_edge(graph_obj, "E", "D", 24)
api.add_edge(graph_obj, "E", "F", 22)
api.add_edge(graph_obj, "C", "D", 14)
api.add_edge(graph_obj, "C", "G", 16)
api.add_edge(graph_obj, "F", "D", 18)
api.add_edge(graph_obj, "F", "G", 12)

spanning_tree = api.min_cost_spanning_tree_using_prims(graph_obj)
print(spanning_tree)

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

py-ds-0.2.1.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

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

py_ds-0.2.1-py3-none-any.whl (28.2 kB view details)

Uploaded Python 3

File details

Details for the file py-ds-0.2.1.tar.gz.

File metadata

  • Download URL: py-ds-0.2.1.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for py-ds-0.2.1.tar.gz
Algorithm Hash digest
SHA256 f7c7a8c771d1298520b6d5033e1ef2e4d03fd6476f590f279694aaee8d06cb3d
MD5 18c15a3677994bd05f52f717a76aa909
BLAKE2b-256 d60b74c2f1ab7479fc3ab8f028f0a95d84206c20d61c1c9de0bb5035022f79ce

See more details on using hashes here.

File details

Details for the file py_ds-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: py_ds-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 28.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for py_ds-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a8274752f7391051db8b84b0252cbb5e1cacfb630e18db758f34a9bcc60dca24
MD5 e42830345293a6240d84c81295512499
BLAKE2b-256 80c884e3b4bd277e9eb3016f3bc91283cac3f39f4b97f0db62484c41c1cfc0f6

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