Giter Club home page Giter Club logo

algorithms's Introduction

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

algorithms's People

Contributors

antrianis avatar ayush1997 avatar jilljenn avatar manishrw avatar nitikataneja avatar prakhar1989 avatar sanketdg avatar the-shank avatar vedangmehta avatar viiicky avatar xuefeng-zhu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

algorithms's Issues

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'

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.

python Algorithms

Hi I want to contribute to this project by adding some python algorithme.This is my first contribution so let me know if I did something wrong!

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.

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

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.