:computer: Data Structures and Algorithms in Python

Overview

Algorithms in Python

Implementations of a few algorithms and datastructures for fun and profit!

Completed

  • Karatsuba Multiplication
  • Basic Sorting
  • Rabin-Miller primality test
  • Sieve of Eratosthenes for prime numbers
  • Binary Search
  • Counting Inversions in an array
  • Selecting ith order statistic in an array
  • Graph datastructure (directed & undirected)
  • Graph Algos
    • Topological Sorting
    • Shortest hops
    • DFS
    • BFS
    • Connected Components
    • Dijkstra's Shortest Path - O(mlogn)
    • Prim's Minimum Cost Spanning Tree - O(mlogn)
    • Kruskal's Minimum Spanning Tree - O(mlogn)
    • Max k Clustering
    • Bellman Ford
    • Floyd Warshall
    • Johnson's Algorithm
  • Heap datastructure
    • Max heaps
    • Min heaps (priority queue)
    • Heapsort
  • Job Scheduling
  • UnionFind Data Structure
  • Binary Search Tree
  • Kandane's Algorithm
  • Knapsack Problem (0/1 and unbounded)
  • Longest Increasing Subsequence
  • Longest Common Subsequence
  • Prefix Tries
  • Stack ADT (with example problems)
    • String Reverse
    • Parenthesis Matching
    • Infix to Postfix
  • Modular exponentiation
  • Modular multiplicative inverse

Tests

python -m tests.graph_test
python -m tests.digraph_test
python -m tests.graph_algorithms_test
python -m tests.heap_test
python -m tests.unionfind_test
python -m tests.singly_linked_list_test
python -m tests.modular_exponentiation_test
python -m tests.modular_multiplicative_inverse_test
Comments
  • Queue implementation currently uses a list under the hood. It should be modified to use a deque to make it efficient.

    Queue implementation currently uses a list under the hood. It should be modified to use a deque to make it efficient.

    The Queue implementation, as it currently stands uses a list and then does a self.items.insert(0, item) to insert an item in the Queue. This is inefficient. A better approach is to modify Queue to use a deque under the hood, instead of a list for increased efficiency. Please let me know if you agree with the above approach so that I can send a pull request with the appropriate changes.

    opened by the-shank 4
  • Created simple setup.py so I could use union_find package

    Created simple setup.py so I could use union_find package

    I wrote a simple setup.py program based on this quickstart guide, which required that I add a README.txt file with the same content as your README.md (I don't believe that the markdown files can be read by the method called), and that I tag the current build as 'v1.0'. I successfully installed the union_find package and was able to use it in my work :+1: :D Hopefully, this fork will give an basis for how to use setup.py, and will allow others to include more of the project's packages if desired. Thank you for this excellent project!

    opened by wbkboyer 4
  • Added Hierholzer's algorithm to find Eulerian Tour in a graph.

    Added Hierholzer's algorithm to find Eulerian Tour in a graph.

    This program finds an Eulerian Tour in the given graph. The program is based on Hierholzer's algorithm. You can read about this algorithm at https://en.wikipedia.org/wiki/Eulerian_path

    opened by viiicky 4
  • Binary search

    Binary search

    A simple iterative binary search algorithm.

    Please review. I know I didn't update my fork but can you please merge this by resolving the merge conflicts?

    Thank you.

    opened by SanketDG 4
  • fix initial value

    fix initial value

    The initial value will be wrongly added twice when is bigger than the second value:

        max_till_here[-1] + n
    

    This test case will fail:

    assert find_max_subarray2(([1,-1]) == 1 // it will return 2
    
    opened by antrianis 3
  • AttributeError in trees/binarysearchtree.py

    AttributeError in trees/binarysearchtree.py

    a = BinarySearchTree()
    a.insert(12)
    a.insert(1)
    a.insert(10)
    

    raises error

    Traceback (most recent call last):
      File "binarysearchtree.py", line 147, in <module>
        a.insert(1)
      File "binarysearchtree.py", line 109, in insert
        if node.value == value:
    AttributeError: 'NoneType' object has no attribute 'value'
    
    opened by ayush1997 2
  • Bubble sort in sorting.py

    Bubble sort in sorting.py

    @prakhar1989 Is bubblesort in sorting.py actually bubble sort? The code gives correct end result but according to standard bubblesort, for each iteration the largest element is moved to n-1th index

    for i in range(len(a)):
            for j in range(i, len(a)):
                if a[i] > a[j]:
                    a[i], a[j] = a[j], a[i]
            print a  # print array after each iteration
    

    The result of this for a=[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

    [1, 10, 9, 8, 6, 4, 2]  # element 10 is not moved to last index
    [1, 2, 10, 9, 8, 6, 4]
    [1, 2, 4, 10, 9, 8, 6]
    [1, 2, 4, 6, 10, 9, 8]
    [1, 2, 4, 6, 8, 10, 9]
    [1, 2, 4, 6, 8, 9, 10]
    [1, 2, 4, 6, 8, 9, 10]
    
    # gives the desired result
    for i in range(len(a)-1):
            for j in range(0, len(a)-1-i):
                        if a[j+1] < a[j]:
                                a[j+1], a[j] = a[j], a[j+1]
            print a
    
    opened by munendrasn 2
  • Create heapsort.py

    Create heapsort.py

    Figured I'd add to your list of algorithms. This is a max heap sorting algorithm. This algorithm can change the initial order of the items in the list.

    opened by AdamBurstynski 2
  • Error

    Error

    I'm getting this error:

      File "test.py", line 42
        print dijkstra (graph, 1)
                     ^
    SyntaxError: invalid syntax
    

    when I use this code

    from heapq import heappush, heappop
    graph = {
        's' : {'t':6, 'y':7},
        't' : {'x':5, 'z':4, 'y':8 },
        'y' : {'z':9, 'x':3},
        'z' : {'x':7, 's': 2},
        'x' : {'t':2}
    }
    
    def read_graph(file):
        graph = dict()
        with open(file) as f:
            for l in f:
                (u, v, w) = l.split()
                if int(u) not in graph:
                    graph[int(u)] = dict()
                graph[int(u)][int(v)] = int(w)
        return graph
    
    inf = float('inf')
    def dijkstra(graph, s):
        n = len(graph.keys())
        dist = dict()
        Q = list()
    
        for v in graph:
            dist[v] = inf
        dist[s] = 0
    
        heappush(Q, (dist[s], s))
    
        while Q:
            d, u = heappop(Q)
            if d < dist[u]:
                dist[u] = d
            for v in graph[u]:
                if dist[v] > dist[u] + graph[u][v]:
                    dist[v] = dist[u] + graph[u][v]
                    heappush(Q, (dist[v], v))
        return dist
    
    print dijkstra(graph, 1)
    

    All I did was omit the read file line and uncomment the graph.

    opened by zwhitchcox 2
  • fixed bug in mergesort function of sorting.py

    fixed bug in mergesort function of sorting.py

    Found and fixed small bug in mergesort function of sorting.py. Changed standard division to floor division to fix.

    Results of running sorting.py BEFORE change:

    Trying:
        bubblesort([6, 4, 8, 2, 1, 9, 10])
    Expecting:
        [1, 2, 4, 6, 8, 9, 10]
    ok
    Trying:
        insertionsort([6, 4, 8, 2, 1, 9, 10])
    Expecting:
        [1, 2, 4, 6, 8, 9, 10]
    ok
    Trying:
        mergesort([5, 4, 1, 6, 2, 3, 9, 7])
    Expecting:
        [1, 2, 3, 4, 5, 6, 7, 9]
    **********************************************************************
    File "c:/Users/parker/Documents/GitHub/Algorithms/sorting and basics/sorting.py", line 4, in __main__.mergesort
    Failed example:
        mergesort([5, 4, 1, 6, 2, 3, 9, 7])
    Exception raised:
        Traceback (most recent call last):
          File "C:\Users\parker\Anaconda3\lib\doctest.py", line 
    1329, in __run
            compileflags, 1), test.globs)
          File "<doctest __main__.mergesort[0]>", line 1, in <module>
            mergesort([5, 4, 1, 6, 2, 3, 9, 7])
          File "c:/Users/parker/Documents/GitHub/Algorithms/sorting and basics/sorting.py", line 12, in mergesort
            a1 = mergesort(arr[:n/2])
        TypeError: slice indices must be integers or None or have an __index__ method
    Trying:
        mergesort([3, 2, 4, 2, 1])
    Expecting:
        [1, 2, 2, 3, 4]
    **********************************************************************
    File "c:/Users/parker/Documents/GitHub/Algorithms/sorting and basics/sorting.py", line 7, in __main__.mergesort
    Failed example:
        mergesort([3, 2, 4, 2, 1])
    Exception raised:
        Traceback (most recent call last):
          File "C:\Users\parker\Anaconda3\lib\doctest.py", line 
    1329, in __run
            compileflags, 1), test.globs)
          File "<doctest __main__.mergesort[1]>", line 1, in <module>
            mergesort([3, 2, 4, 2, 1])
          File "c:/Users/parker/Documents/GitHub/Algorithms/sorting and basics/sorting.py", line 12, in mergesort
            a1 = mergesort(arr[:n/2])
        TypeError: slice indices must be integers or None or have an __index__ method
    Trying:
        quicksort([6, 4, 8, 2, 1, 9, 10])
    Expecting:
        [1, 2, 4, 6, 8, 9, 10]
    ok
    Trying:
        selectionsort([6, 4, 8, 2, 1, 9, 10])
    Expecting:
        [1, 2, 4, 6, 8, 9, 10]
    ok
    2 items had no tests:
        __main__
        __main__.merge
    4 items passed all tests:
       1 tests in __main__.bubblesort
       1 tests in __main__.insertionsort
       1 tests in __main__.quicksort
       1 tests in __main__.selectionsort
    **********************************************************************
    1 items had failures:
       2 of   2 in __main__.mergesort
    6 tests in 7 items.
    4 passed and 2 failed.
    ***Test Failed*** 2 failures.
    

    Results of running sorting.py AFTER change:

    Trying:
    bubblesort([6, 4, 8, 2, 1, 9, 10])
    Expecting:
    [1, 2, 4, 6, 8, 9, 10]
    ok
    Trying:
    insertionsort([6, 4, 8, 2, 1, 9, 10])
    Expecting:
    [1, 2, 4, 6, 8, 9, 10]
    ok
    Trying:
    mergesort([5, 4, 1, 6, 2, 3, 9, 7])
    Expecting:
    [1, 2, 3, 4, 5, 6, 7, 9]
    ok
    Trying:
    mergesort([3, 2, 4, 2, 1])
    Expecting:
    [1, 2, 2, 3, 4]
    ok
    Trying:
    quicksort([6, 4, 8, 2, 1, 9, 10])
    Expecting:
    [1, 2, 4, 6, 8, 9, 10]
    ok
    Trying:
    selectionsort([6, 4, 8, 2, 1, 9, 10])
    Expecting:
    [1, 2, 4, 6, 8, 9, 10]
    ok
    2 items had no tests:
    main
    main.merge
    5 items passed all tests:
    1 tests in main.bubblesort
    1 tests in main.insertionsort
    2 tests in main.mergesort
    1 tests in main.quicksort
    1 tests in main.selectionsort
    6 tests in 7 items.
    6 passed and 0 failed.
    Test passed.
    
    opened by parkershamblin 1
  • Bug in algorithm

    Bug in algorithm

    In the longest sequence code, the algorithms fails for this test

    seq = [5,0,1,2,3,4,5,6,7,8,9,10,11,12, 2, 8, 10, 3, 6, 9, 7]

    The output is (13, [0, 1, 2, 3, 4, 5, 6, 7]), when there is clearly a longer sequence.

    Just to let you know!

    opened by zwhitchcox 1
  • Update quicksort.py

    Update quicksort.py

    This python program can generate 10 random numbers and sort them accordingly using the Quick Sort algorithm also implement code for display execution time.

    opened by akshaypatil314 0
  • Adding two algorithms

    Adding two algorithms

    Adding two algorithms to the repository.

    1. fractional knapsack which is a problem of dynamic programming and add to fix an issue #66 in the demand of fractional knapsack
    2. Longest palindrome substring is a problem of dynamic programming and add in the repository by seeing the demand of the people to find the solution of the longest palindrome substring in linear time complexity. I use manacher algorithm to find the solution in the least time complexity.
    opened by anubhav9199 0
Owner
Prakhar Srivastav
Prakhar Srivastav
Algorithms and data structures for educational, demonstrational and experimental purposes.

Algorithms and Data Structures (ands) Introduction This project was created for personal use mostly while studying for an exam (starting in the month

null 50 Dec 6, 2022
Cormen-Lib - An academic tool for data structures and algorithms courses

The Cormen-lib module is an insular data structures and algorithms library based on the Thomas H. Cormen's Introduction to Algorithms Third Edition. This library was made specifically for administering and grading assignments related to data structure and algorithms in computer science.

Cormen Lib 12 Aug 18, 2022
zoofs is a Python library for performing feature selection using an variety of nature inspired wrapper algorithms. The algorithms range from swarm-intelligence to physics based to Evolutionary. It's easy to use ,flexible and powerful tool to reduce your feature size.

zoofs is a Python library for performing feature selection using a variety of nature-inspired wrapper algorithms. The algorithms range from swarm-intelligence to physics-based to Evolutionary. It's easy to use , flexible and powerful tool to reduce your feature size.

Jaswinder Singh 168 Dec 30, 2022
Genetic algorithms are heuristic search algorithms inspired by the process that supports the evolution of life.

Genetic algorithms are heuristic search algorithms inspired by the process that supports the evolution of life. The algorithm is designed to replicate the natural selection process to carry generation, i.e. survival of the fittest of beings.

Mahdi Hassanzadeh 4 Dec 24, 2022
Code for generating alloy / disordered structures through the special quasirandom structure (SQS) algorithm

Code for generating alloy / disordered structures through the special quasirandom structure (SQS) algorithm

Bruno Focassio 1 Nov 10, 2021
Repository for data structure and algorithms in Python for coding interviews

Python Data Structures and Algorithms This repository contains questions requiring implementation of data structures and algorithms concepts. It is us

Prabhu Pant 1.9k Jan 1, 2023
Python Client for Algorithmia Algorithms and Data API

Algorithmia Common Library (python) Python client library for accessing the Algorithmia API For API documentation, see the PythonDocs Algorithm Develo

Algorithmia 138 Oct 26, 2022
Planning Algorithms in AI and Robotics. MSc course at Skoltech Data Science program

Planning Algorithms in AI and Robotics course T2 2021-22 The Planning Algorithms in AI and Robotics course at Skoltech, MS in Data Science, during T2,

Mobile Robotics Lab. at Skoltech 6 Sep 21, 2022
Algorithms-in-Python - Programs related to DSA in Python for placement practice

Algorithms-in-Python Programs related to DSA in Python for placement practice CO

MAINAK CHAUDHURI 2 Feb 2, 2022
All Algorithms implemented in Python

The Algorithms - Python All algorithms implemented in Python (for education) These implementations are for learning purposes only. Therefore they may

The Algorithms 150.6k Jan 3, 2023
Algorithms implemented in Python

Python Algorithms Library Laurent Luce Description The purpose of this library is to help you with common algorithms like: A* path finding. String Mat

Laurent Luce 264 Dec 6, 2022
A command line tool for memorizing algorithms in Python by typing them.

Algo Drills A command line tool for memorizing algorithms in Python by typing them. In alpha and things will change. How it works Type out an algorith

Travis Jungroth 43 Dec 2, 2022
Python sample codes for robotics algorithms.

PythonRobotics Python codes for robotics algorithm. Table of Contents What is this? Requirements Documentation How to use Localization Extended Kalman

Atsushi Sakai 17.2k Jan 1, 2023
🧬 Performant Evolutionary Algorithms For Python with Ray support

?? Performant Evolutionary Algorithms For Python with Ray support

Nathan 49 Oct 20, 2022
All algorithms implemented in Python for education

The Algorithms - Python All algorithms implemented in Python - for education Implementations are for learning purposes only. As they may be less effic

null 1 Oct 20, 2021
Implementation of Apriori algorithms via Python

Installing run bellow command for installing all packages pip install -r requirements.txt Data Put csv data under this directory "infrastructure/data

Mahdi Rezaei 0 Jul 25, 2022
A simple python application to visualize sorting algorithms.

Visualize sorting algorithms A simple python application to visualize sorting algorithms. Sort Algorithms Name Function Name O( ) Bubble Sort bubble_s

Duc Tran 3 Apr 1, 2022
Programming Foundations Algorithms With Python

Programming-Foundations-Algorithms Algorithms purpose to solve a specific proplem with a sequential sets of steps for instance : if you need to add di

omar nafea 1 Nov 1, 2021
Repository for Comparison based sorting algorithms in python

Repository for Comparison based sorting algorithms in python. This was implemented for project one submission for ITCS 6114 Data Structures and Algorithms under the guidance of Dr. Dewan at the University of North Carolina at Charlotte, Fall 2021.

Devashri Khagesh Gadgil 1 Dec 20, 2021