Download algorithms Documentation

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
algorithms Documentation
Release 1.0.0
Nic Young
February 23, 2017
Contents
1
Usage
3
2
Features
5
3
Installation:
7
4
Tests:
9
5
Contributing:
11
6
Table of Contents:
6.1 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
13
Python Module Index
31
i
ii
algorithms Documentation, Release 1.0.0
Algorithms is a library of algorithms and data structures implemented in Python.
The main purpose of this library is to be an educational tool. You probably shouldn’t use these in production, instead,
opting for the optimized versions of these algorithms that can be found else where.
You should totally check out the docs for implementation details, complexities and further info.
Contents
1
algorithms Documentation, Release 1.0.0
2
Contents
CHAPTER 1
Usage
If you want to use the algorithms in your code it is as simple as:
from algorithms.sorting import bubble_sort
my_list = bubble_sort.sort(my_list)
3
algorithms Documentation, Release 1.0.0
4
Chapter 1. Usage
CHAPTER 2
Features
• Pseudo code, algorithm complexities and futher info with each algorithm.
• Test coverage for each algorithm and data structure.
• Super sweet documentation.
5
algorithms Documentation, Release 1.0.0
6
Chapter 2. Features
CHAPTER 3
Installation:
Installation is as easy as:
$ pip install algorithms
7
algorithms Documentation, Release 1.0.0
8
Chapter 3. Installation:
CHAPTER 4
Tests:
Pytest is used as the main test runner and all Unit Tests can be run with:
$ ./run_tests.py
9
algorithms Documentation, Release 1.0.0
10
Chapter 4. Tests:
CHAPTER 5
Contributing:
Contributions are always welcome. Check out the contributing guidelines to get started.
11
algorithms Documentation, Release 1.0.0
12
Chapter 5. Contributing:
CHAPTER 6
Table of Contents:
Algorithms
Data Structures
Binary Search Tree
The Binary Search Tree represents an ordered symbol table of generic key-value pairs. Keys must be comparable.
Does not permit duplicate keys. When assocating a value with a key already present in the BST, the previous value is
replaced by the new one. This implementation is for an unbalanced BST.
Pseudo Code: http://algs4.cs.princeton.edu/32bst
class algorithms.data_structures.binary_search_tree.BinarySearchTree
Bases: object
Implementation of a Binary Search Tree.
ceiling_key(key)
Returns the smallest key that is greater than or equal to the given value ‘key’
Worst Case Complexity: O(N)
Balanced Tree Complexity: O(lg N)
contains(key)
Returns True if the BST contains ‘key’, False otherwise
Worst Case Complexity: O(N)
Balanced Tree Complexity: O(lg N)
delete(key)
Remove the node with key equal to ‘key’
Worst Case Complexity: O(N)
Balanced Tree Complexity: O(lg N)
delete_max()
Remove the key-value pair with the largest key.
Worst Case Complexity: O(N)
Balanced Tree Complexity: O(lg N)
13
algorithms Documentation, Release 1.0.0
delete_min()
Remove the key-value pair with the smallest key.
Worst Case Complexity: O(N)
Balanced Tree Complexity: O(lg N)
floor_key(key)
Returns the biggest key that is less than or equal to the given value ‘key’
Worst Case Complexity: O(N)
Balanced Tree Complexity: O(lg N)
get(key)
Return the value paired with ‘key’
Worst Case Complexity: O(N)
Balanced Tree Complexity: O(lg N)
is_empty()
Returns True if the BST is empty, False otherwise
Worst Case Complexity: O(1)
Balanced Tree Complexity: O(1)
keys()
Return all of the keys in the BST in aschending order
Worst Case Complexity: O(N)
Balanced Tree Complexity: O(N)
max_key()
Return the maximum key in the BST
Worst Case Complexity: O(N)
Balanced Tree Complexity: O(lg N)
min_key()
Return the minimum key in the BST
Worst Case Complexity: O(N)
Balanced Tree Complexity: O(lg N)
put(key, val)
Add a new key-value pair.
Worst Case Complexity: O(N)
Balanced Tree Complexity: O(lg N)
rank(key)
Return the number of keys less than a given ‘key’.
Worst Case Complexity: O(N)
Balanced Tree Complexity: O(lg N)
select_key(rank)
Return the key with rank equal to ‘rank’
Worst Case Complexity: O(N)
14
Chapter 6. Table of Contents:
algorithms Documentation, Release 1.0.0
Balanced Tree Complexity: O(lg N)
size()
Return the number of nodes in the BST
Worst Case Complexity: O(1)
Balanced Tree Complexity: O(1)
class algorithms.data_structures.binary_search_tree.Node(key=None,
val=None,
size_of_subtree=1)
Bases: object
Implementation of a Node in a Binary Search Tree.
Directed Graph
The Digraph class represents a directed graph of vertices which can be any hashable value. Parallel edges and selfloops are permitted.
Pseudo Code: http://algs4.cs.princeton.edu/42directed/Digraph.java.html
class algorithms.data_structures.digraph.Digraph
add_edge(src, dest)
Adds an undirected edge ‘src’-‘dest’ to the graph.
Worst Case Complexity O(1)
adj(src)
Returns the vertices adjacent to vertex ‘src’.
Worst Case Complexity: O(1)
edge_count()
Returns the number of edges in the graph.
Worst Case Complexity: O(1)
outdegree(src)
Returns the degree of the vertex ‘src’
Worst Case Complexity: O(1)
reverse()
Returns the reverse of this digraph
Worst Case Complexity: O(V+E)
vertex_count()
Returns the number of vertices in the graph.
Worst Case Complexity: O(1)
vertices()
Returns an iterable of all the vertices in the graph.
Worst Case Complexity: O(V)
6.1. Algorithms
15
algorithms Documentation, Release 1.0.0
Queue
A Queue is a linear data structure, or more abstractly a sequential collection. The entities in the collection are kept in
order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position,
known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue
a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the
first one to be removed.
Pseudo Code: https://en.wikipedia.org/wiki/Queue_%28abstract_data_type%29
class algorithms.data_structures.queue.Queue
add(value)
Add element as the last item in the Queue.
Worst Case Complexity: O(1)
is_empty()
Returns a boolean indicating if the Queue is empty.
Worst Case Complexity: O(1)
remove()
Remove element from the front of the Queue and return it’s value.
Worst Case Complexity: O(1)
size()
Return size of the Queue.
Worst Case Complexity: O(1)
Singly Linked List
A linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest
form, each node is composed of data and a reference (in other words, a link) to the next node in the sequence; more
complex variants add additional links. This structure allows for efficient insertion or removal of elements from any
position in the sequence.
Pseudo Code: https://en.wikipedia.org/wiki/Linked_list
class algorithms.data_structures.singly_linked_list.Node(data=None, next=None)
get_data()
get_next()
set_data(data)
set_next(next)
class algorithms.data_structures.singly_linked_list.SinglyLinkedList
add(value)
Add element to list
Time Complexity: O(N)
16
Chapter 6. Table of Contents:
algorithms Documentation, Release 1.0.0
remove(value)
Remove element from list
Time Complexity: O(N)
search(value)
Search for value in list
Time Complexity: O(N)
size()
Return size of list
Stack
A stack or LIFO (last in, first out) is an abstract data type that serves as a collection of elements, with two principal
operations: push, which adds an element to the collection, and pop, which removes the last element that was added.
Pseudo Code: https://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29
class algorithms.data_structures.stack.Stack
add(value)
Add element at last
Time Complexity: O(1)
is_empty()
1 value returned on empty 0 value returned on not empty
Time Complexity: O(1)
remove()
Remove element from last return value
Time Complexity: O(1)
size()
Return size of stack
Time Complexity: O(1)
Undirected Graph
The Undirected_Graph class represents an undirected graph of vertices which can be any hashable value.
Pseudo Code: http://algs4.cs.princeton.edu/41undirected/Graph.java.html
class algorithms.data_structures.undirected_graph.Undirected_Graph
add_edge(src, dest)
Adds an undirected edge ‘src’-‘dest’ to the graph.
Time Complexity: O(1)
adj(src)
Returns the vertices adjacent to vertex ‘src’.
Time Complexity: O(1)
6.1. Algorithms
17
algorithms Documentation, Release 1.0.0
degree(src)
Returns the degree of the vertex ‘src’
Time Complexity: O(1)
edge_count()
Returns the number of edges in the graph.
Time Complexity: O(1)
vertex_count()
Returns the number of vertices in the graph.
Time Complexity: O(1)
vertices()
Returns an iterable of all the vertices in the graph.
Time Complexity: O(V)
Union Find:
A disjoint-set data structure, also called union-find data structure implements two functions:
union(A, B) - merge A’s set with B’s set
find(A) - finds what set A belongs to
Naive approach:
Find follows parent nodes until it reaches the root. Union combines two trees into one by attaching the root of one to
the root of the other
Time Complexity : O(N) (a highly unbalanced tree might be created, nothing better a linked-list)
Psuedo Code: http://en.wikipedia.org/wiki/Disjoint-set_data_structure
class algorithms.data_structures.union_find.UnionFind(N)
find(x)
is_connected(x, y)
make_set(x)
union(x, y)
Union Find by Rank
A disjoint-set data structure, also called union-find data structure implements two functions:
union(A, B) - merge A’s set with B’s set
find(A) - finds what set A belongs to
Union by rank approach: attach the smaller tree to the root of the larger tree
Time Complexity : O(logn)
Psuedo Code: http://en.wikipedia.org/wiki/Disjoint-set_data_structure
class algorithms.data_structures.union_find_by_rank.UnionFindByRank(N)
18
Chapter 6. Table of Contents:
algorithms Documentation, Release 1.0.0
find(x)
is_connected(x, y)
make_set(x)
union(x, y)
Union Find with path compression
A disjoint-set data structure, also called union-find data structure implements two functions:
union(A, B) - merge A’s set with B’s set
find(A) - finds what set A belongs to
Union with path compression approach:
Each node visited on the way to a root node may as well be attached directly to the root node. attach the smaller tree
to the root of the larger tree
Time Complexity : O(a(n)), where a(n) is the inverse of the function n=f(x)=A(x,x) and A is the extremely fast-growing
Ackermann function.
Psuedo Code: http://en.wikipedia.org/wiki/Disjoint-set_data_structure
class algorithms.data_structures.union_find_with_path_compression.UnionFindWithPathCompressio
find(x)
is_connected(x, y)
make_set(x)
parent(x)
union(x, y)
Dynamic Programming
Longest Common Subsequence
Implements the dynamic programming solution to the longest common subsequence algorithm.
Pseudo Code: http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
algorithms.dynamic_programming.lcs.build_lengths_matrix(str1, str2)
XXX: Needs documentation written.
algorithms.dynamic_programming.lcs.lcs(str1, str2)
XXX: Needs documentation written.
algorithms.dynamic_programming.lcs.read_from_matrix(matrix, str1, str2)
XXX: Needs documentation written.
6.1. Algorithms
19
algorithms Documentation, Release 1.0.0
Factorization
Fermat Factorization
Fermat’s factorization method is based on the representation of an odd integer as the difference of two squares:
N = a*a-b*b = (a-b)*(a+b)
algorithms.factorization.fermat.fermat(n)
Factorization of the integer n.
Parameters n – An integer to be factored.
Return type The factorization of n.
Pollard Rho Algorithm
Pollard’s rho algorithm is a special-purpose integer factorization algorithm. It was invented by John Pollard in 1975.
It is particularly effective for a composite number having a small prime factor.
algorithms.factorization.pollard_rho.f(x)
algorithms.factorization.pollard_rho.pollard_rho(x)
algorithms.factorization.pollard_rho.pollard_rho_rec(x, factors)
algorithms.factorization.pollard_rho.rho(n, x1=2, x2=2)
Trial Division
Trial division is the most laborious but easiest to understand of the integer factorization algorithms. Try to divide a
number n by all prime numbers < sqrt(n).
algorithms.factorization.trial_division.trial_division(n)
Uses trial division to find prime factors of n.
Parameters n – An integer to factor.
Return type The prime factors of n
Math
Approximate Cumulative Distribution Function
Calculates the cumulative distribution function (CDF) of the normal distribution based on an approximation by George
Marsaglia: Marsaglia, George (2004). “Evaluating the Normal Distribution”. Journal of Statistical Software 11 (4).
16 digit precision for 300 iterations when x = 10.
Equation:
f(x) = 1/2 + pdf(x) * (x + (x^3/3) + (x^5/3*5) + (x^7/3*7) + ...)
algorithms.math.approx_cdf.cdf(x, iterations=300)
Calculates the cumulative distribution function of the normal distribution. Uses a taylor exponent to calculate
this.
Parameters
• x – An integer that represents the taylor exponent.
20
Chapter 6. Table of Contents:
algorithms Documentation, Release 1.0.0
• iterations – An integer representing the number of iterations.
Return type The normal distribution
Extended Greatest Common Divisor
Implementation of the extended greatest common divisor algorithm.
Pseudo Code: http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
algorithms.math.extended_gcd.extended_gcd(p, q)
Find the greatest common divisor and returns them.
Parameters
• a – An integer.
• b – An integer.
Return type A tuple representing the greatest common divisor.
Lowest Common Multiple
Simple implementation of the Lowest Common Multiple Algorithm.
Pseudo Code: https://en.wikipedia.org/wiki/Least_common_multiple
algorithms.math.lcm.lcm(a, b)
Simple version of lcm, that does not have any dependencies.
Parameters
• a – Integer
• b – Integer
Return type The lowest common multiple of integers a and b
Primality Test
Implementation of a Primality Test that uses a cache to improve performance.
algorithms.math.primality_test.is_prime(number, cache=True)
Takes number and determines if it is prime.
Parameters
• number – The integer to be tested for primality.
• cache – A boolean to determine if a cache should be used to improve performance.
Return type A boolean that signifies if number is prime.
Sieve of Atkin
It is an optimized version of the ancient sieve of Eratosthenes which does some preliminary work and then marks off
multiples of the square of each prime, rather than multiples of the prime itself. It was created in 2004 by A. O. L.
Atkin and Daniel J. Bernstein.
Time Complexity: O(n/log log n)
6.1. Algorithms
21
algorithms Documentation, Release 1.0.0
Pseudocode: https://en.wikipedia.org/wiki/Sieve_of_Atkin
algorithms.math.sieve_atkin.atkin(limit)
Parameters limit – The upper limit in which to find all primes less than this value.
Sieve of Eratosthenes
Is a simple, ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as
composite (i.e. not prime) the multiples of each prime, starting with the multiples of 2.
The sieve of Eratosthenes is one of the most efficient ways to find all of the smaller primes (below 10 million or so).
Time Complexity: O(n log log n)
Pseudocode: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
algorithms.math.sieve_eratosthenes.eratosthenes(end, start=2, return_boolean=False)
Finds all primes < end.
Parameters
• end – An integer. The upper limit of the range to look for primes.
• start – An integer. The start of the range to look for primes.
• return_boolean – A boolean. Represents the type of return type.
Return type Depending on return_boolean either returns boolean and primes or just the primes.
Standard Normal Probability Density Function
Calculates the normal distribution’s probability density function (PDF). Calculates Standard normal pdf for mean=0,
std_dev=1.
Equation: f(x) = 1 / sqrt(2*pi) * e^(-(x-mean)^2/ 2*std_dev^2)
algorithms.math.std_normal_pdf.pdf(x, mean=0, std_dev=1)
Calculates the normal distribution’s probability density function.
Parameters
• x – An integer.
• mean – An integer.
• std_dev – An integer.
Return type The normal distribution
Random
Mersenne Twister
Generates high quality pseudo random integers with a long period. Used as the default random number generator for
several languages (including Python).
For a more technical overview, see the wikipedia entry.
Pseudocode: http://en.wikipedia.org/wiki/Mersenne_twister
22
Chapter 6. Table of Contents:
algorithms Documentation, Release 1.0.0
class algorithms.random.mersenne_twister.MersenneTwister
generate()
Generates 624 random numbers and stores in the state list.
randint()
Extracts a random number.
Return type A random integer
seed(seed)
Initialize generator.
Parameters seed – An integer value to seed the generator with
Searching
Binary Search
Recursively partitions the list until the key is found.
Time Complexity: O(lg n)
Psuedo Code: http://en.wikipedia.org/wiki/Binary_search
algorithms.searching.binary_search.search(seq, key)
Takes a list of integers and searches if the key is contained within the list.
Parameters
• seq – A list of integers
• key – The integer to be searched for
Return type The index of where the key is located in the list. If key is not found then False is
returned.
BMH Search
Search that attempts to find a substring in a string. Uses a bad-character shift of the rightmost character of the window
to compute shifts.
Time: Complexity: O(m + n), where m is the substring to be found.
Space: Complexity: O(m), where m is the substring to be found.
Psuedo Code: https://github.com/FooBarWidget/boyer-moore-horspool
algorithms.searching.bmh_search.search(text, pattern)
Takes a string and searches if the pattern is substring within text.
Parameters
• text – A string that will be searched.
• pattern – A string that will be searched as a substring within text.
Return type The indices of all occurences of where the substring pattern was found in text.
6.1. Algorithms
23
algorithms Documentation, Release 1.0.0
Depth First Search
Recursive implementation of the depth first search algorithm used to traverse trees or graphs. Starts at a selected node
(root) and explores the branch as far as possible before backtracking.
Time Complexity: O(E + V)
E = Number of edges
V = Number of vertices (nodes)
Pseudocode: https://en.wikipedia.org/wiki/Depth-first_search
algorithms.searching.depth_first_search.dfs(graph, start, path=[])
Depth first search that recursively searches the path. Backtracking occurs only when the last node in the path is
visited.
Parameters
• graph – A dictionary of nodes and edges.
• start – The node to start the recursive search with.
• path – A list of edges to search.
Return type A boolean indicating whether the node is included in the path.
KMP Search
Implementation of kmp search on string. Uses a prefix function to reduce the searching time.
Time Complexity: O(n + k), where k is the substring to be found
Psuedo Code: CLRS. Introduction to Algorithms. 3rd ed.
algorithms.searching.kmp_search.compute_prefix(word)
Returns the prefix of the word.
Parameters word – The sub string that the prefix will be computed for.
Return type Returns computed prefix of the word.
algorithms.searching.kmp_search.search(string, word)
Searches for occurrences of a “word” within a main “string” by employing the observation that when a mismatch
occurs, the word itself embodies sufficient information to determine where the next match could begin, thus
bypassing re-examination of previously matched characters.
Parameters
• string – The string to be searched.
• word – The sub string to be searched for.
Return type The indices of all occurences of where the substring is found in the string.
Implementation of Rabin-Karp search on a given string.
Rabin-Karp Search
Search for a substring in a given string, by comparing hash values of the strings.
Time Complexity: O(nm)
Psuedo Code: http://en.wikipedia.org/wiki/Rabin-Karp_algorithm
24
Chapter 6. Table of Contents:
algorithms Documentation, Release 1.0.0
algorithms.searching.rabinkarp_search.search(s, sub)
Uses hashing to find any one of a set of pattern strings in a text.
Parameters
• s – The string to be searched.
• sub – The substring to be searched for.
Return type The indices of all occurences of where the substring is found in the string.
Ternary search
Finds the maximum of unimodal function fn() within [left, right] To find the minimum, revert the if/else statement or
revert the comparison.
Time Complexity: O(log(n))
algorithms.searching.ternary_search.search(fn, left, right, precision)
Shuffling
Fisher-Yates/Knuth
Randomly picks integers to swap elements in an ubiased manner.
Time Complexity: O(n)
Space Complexity: O(n)n
Pseudocode: http://http://rosettacode.org/wiki/Knuth_shuffle
algorithms.shuffling.knuth.shuffle(seq)
Takes a list of integers and randomly swaps the elements in an unbiased manner.
Parameters seq – A list of integers
Return type A list of shuffled integers
Sorting
Bogo Sort
A naive sorting that picks two elements at random and swaps them.
Time Complexity: O(n * n!)
Space Complexity: O(1) Auxiliary
Stable: No
Psuedo code: None
WARNING: This algorithm may never sort the list correctly.
algorithms.sorting.bogo_sort.is_sorted(seq)
Takes a list of integers and checks if the list is in sorted order.
Parameters seq – A list of integers
Return type Boolean
6.1. Algorithms
25
algorithms Documentation, Release 1.0.0
algorithms.sorting.bogo_sort.sort(seq)
Takes a list of integers and sorts them in ascending order. This sorted list is then returned.
Parameters seq – A list of integers
Return type A list of sorted integers
Bubble Sort
A naive sorting that compares and swaps adjacent elements.
Time Complexity: O(n**2)
Space Complexity: O(1) Auxiliary
Stable: Yes
Psuedo code: http://en.wikipedia.org/wiki/Bubble_sort
algorithms.sorting.bubble_sort.sort(seq)
Takes a list of integers and sorts them in ascending order. This sorted list is then returned.
Parameters seq – A list of integers
Return type A list of sorted integers
Cocktail Sort
A bidirectional bubble sort. Walks the elements bidirectionally, swapping neighbors if one should come before/after
the other.
Time Complexity: O(n**2)
Space Complexity: O(1) Auxiliary
Stable: Yes
Psuedo Code: http://en.wikipedia.org/wiki/Cocktail_sort
algorithms.sorting.cocktail_sort.sort(seq)
Takes a list of integers and sorts them in ascending order. This sorted list is then returned.
Parameters seq – A list of integers
Return type A list of sorted integers
Comb Sort
Improves on bubble sort by using a gap sequence to remove turtles.
Time Complexity: O(n**2)
Space Complexity: O(1) Auxiliary
Stable: Yes
Psuedo code: http://en.wikipedia.org/wiki/Comb_sort
algorithms.sorting.comb_sort.sort(seq)
Takes a list of integers and sorts them in ascending order. This sorted list is then returned.
Parameters seq – A list of integers
26
Chapter 6. Table of Contents:
algorithms Documentation, Release 1.0.0
Return type A list of sorted integers
Gnome Sort
A sorting algorithm similar to insertion sort except that the element is moved to its proper place by a series of swaps.
Time Complexity: O(n**2)
Space Complexity: O(1) auxillary
Stable: No
Psuedo code: http://en.wikipedia.org/wiki/Gnome_sort
algorithms.sorting.gnome_sort.sort(seq)
Takes a list of integers and sorts them in ascending order. This sorted list is then returned.
Parameters seq – A list of integers
Return type A list of sorted integers
Heap Sort
Uses the max heap data structure implemented in a list.
Time Complexity: O(n log n)
Space Complexity: O(1) Auxiliary
Stable: Yes
Psuedo Code: CLRS. Introduction to Algorithms. 3rd ed.
algorithms.sorting.heap_sort.build_heap(seq)
Continously calls max_heapify on the list for each subtree.
Parameters seq – A list of integers
algorithms.sorting.heap_sort.max_heapify(seq, i, n)
The function of max_heapify is to let the value at seq[i] “float down” in the max-heap so that the subtree rooted
at index i becomes a max-heap.
Parameters
• seq – A list of integers
• i – An integer that is an index in to the list that represents the root of a subtree that max
heapify is called on.
• n – length of the list
algorithms.sorting.heap_sort.sort(seq)
Takes a list of integers and sorts them in ascending order. This sorted list is then returned.
Parameters seq – A list of integers
Return type A list of sorted integers
6.1. Algorithms
27
algorithms Documentation, Release 1.0.0
Insertion Sort
A sort that uses the insertion of elements in to the list to sort the list.
Time Complexity: O(n**2)
Space Complexity: O(n) total
Stable: Yes
Psuedo Code: CLRS. Introduction to Algorithms. 3rd ed.
algorithms.sorting.insertion_sort.sort(seq)
Takes a list of integers and sorts them in ascending order. This sorted list is then returned.
Parameters seq – A list of integers
Return type A list of integers
Merge Sort
Uses divide and conquer to recursively divide and sort the list
Time Complexity: O(n log n)
Space Complexity: O(n) Auxiliary
Stable: Yes
Psuedo Code: CLRS. Introduction to Algorithms. 3rd ed.
algorithms.sorting.merge_sort.merge(left, right)
Takes two sorted sub lists and merges them in to a single sorted sub list and returns it.
Parameters
• left – A list of sorted integers
• right – A list of sorted integers
Return type A list of sorted integers
algorithms.sorting.merge_sort.sort(seq)
Takes a list of integers and sorts them in ascending order. This sorted list is then returned.
Parameters seq – A list of integers
Return type A list of sorted integers
Quick Sort
Uses partitioning to recursively divide and sort the list
Time Complexity: O(n**2) worst case
Space Complexity: O(n**2) this version
Stable: No
Psuedo Code: CLRS. Introduction to Algorithms. 3rd ed.
algorithms.sorting.quick_sort.sort(seq)
Takes a list of integers and sorts them in ascending order. This sorted list is then returned.
Parameters seq – A list of integers
28
Chapter 6. Table of Contents:
algorithms Documentation, Release 1.0.0
Return type A list of sorted integers
Quick Sort in Place
Uses partitioning to recursively divide and sort the list
Time Complexity: O(n**2) worst case
Space Complexity: O(log n) this version
Stable: No
Psuedo Code: http://rosettacode.org/wiki/Quick_Sort
algorithms.sorting.quick_sort_in_place.partition(seq, left, right, pivot_index)
Reorders the slice with values lower than the pivot at the left side, and values bigger than it at the right side.
Also returns the store index.
Parameters
• seq – A list of integers
• left – An integer representing left index
• right – An integer representing left index
• pivot_index – An integer that we’re pivoting off
Return type An stored_index integer
algorithms.sorting.quick_sort_in_place.sort(seq, left, right)
Takes a list of integers and sorts them in ascending order. This sorted list is then returned.
Parameters
• seq – A list of integers
• left – An integer representing the beginning index
• right – An integer representing the end index
Return type A list of sorted integers
Selection Sort
A sorting that uses in-place comparison.
Time Complexity: O(n**2)
Space Complexity: O(1) Auxiliary
Stable: Yes
Psuedo Code: http://en.wikipedia.org/wiki/Selection_sort
algorithms.sorting.selection_sort.sort(seq)
Takes a list of integers and sorts them in ascending order. This sorted list is then returned.
Parameters seq – A list of integers
Return type A list of sorted integers
6.1. Algorithms
29
algorithms Documentation, Release 1.0.0
Shell Sort
Comparision sort that sorts far away elements first to sort the list
Time Complexity: O(n**2)
Space Complexity: O(1) Auxiliary
Stable: Yes
Psuedo Code: http://en.wikipedia.org/wiki/Shell_sort
algorithms.sorting.shell_sort.sort(seq)
Takes a list of integers and sorts them in ascending order. This sorted list is then returned.
Parameters seq – A list of integers
Return type A list of sorted integers
algorithms.data_structures.lcp_array.lcp_array(t_str, s_array, rank)
Parameters
• t_str – the string to calculate the lcp array for
• s_array – the suffix array of the string
• rank – the suffix array reversed
Returns the lcp array
algorithms.data_structures.lcp_array.suffix_array(t)
Suffix array of a string t :param t: the string to extract suffix array from :return (s_array, rank): return a tuple
that contain the suffix array and the rank array which is the reversed version of the suffix array
30
Chapter 6. Table of Contents:
Python Module Index
a
algorithms.shuffling.knuth, 25
algorithms.sorting.bogo_sort, 25
algorithms.data_structures.binary_search_tree,
algorithms.sorting.bubble_sort, 26
13
algorithms.sorting.cocktail_sort, 26
algorithms.data_structures.digraph, 15
algorithms.sorting.comb_sort, 26
algorithms.data_structures.lcp_array,
algorithms.sorting.gnome_sort, 27
30
algorithms.sorting.heap_sort, 27
algorithms.data_structures.queue, 15
algorithms.sorting.insertion_sort, 27
algorithms.data_structures.singly_linked_list,
algorithms.sorting.merge_sort, 28
16
algorithms.sorting.quick_sort, 28
algorithms.data_structures.stack, 17
algorithms.sorting.quick_sort_in_place,
algorithms.data_structures.undirected_graph,
29
17
algorithms.sorting.selection_sort, 29
algorithms.data_structures.union_find,
algorithms.sorting.shell_sort, 29
18
algorithms.data_structures.union_find_by_rank,
18
algorithms.data_structures.union_find_with_path_compression,
19
algorithms.dynamic_programming.lcs, 19
algorithms.factorization.fermat, 20
algorithms.factorization.pollard_rho,
20
algorithms.factorization.trial_division,
20
algorithms.math.approx_cdf, 20
algorithms.math.extended_gcd, 21
algorithms.math.lcm, 21
algorithms.math.primality_test, 21
algorithms.math.sieve_atkin, 21
algorithms.math.sieve_eratosthenes, 22
algorithms.math.std_normal_pdf, 22
algorithms.random.mersenne_twister, 22
algorithms.searching.binary_search, 23
algorithms.searching.bmh_search, 23
algorithms.searching.depth_first_search,
23
algorithms.searching.kmp_search, 24
algorithms.searching.rabinkarp_search,
24
algorithms.searching.ternary_search, 25
31
algorithms Documentation, Release 1.0.0
32
Python Module Index
Index
A
algorithms.random.mersenne_twister (module), 22
add() (algorithms.data_structures.queue.Queue method), algorithms.searching.binary_search (module), 23
algorithms.searching.bmh_search (module), 23
16
algorithms.searching.depth_first_search (module), 23
add() (algorithms.data_structures.singly_linked_list.SinglyLinkedList
algorithms.searching.kmp_search (module), 24
method), 16
add() (algorithms.data_structures.stack.Stack method), 17 algorithms.searching.rabinkarp_search (module), 24
add_edge() (algorithms.data_structures.digraph.Digraph algorithms.searching.ternary_search (module), 25
algorithms.shuffling.knuth (module), 25
method), 15
algorithms.sorting.bogo_sort (module), 25
add_edge() (algorithms.data_structures.undirected_graph.Undirected_Graph
algorithms.sorting.bubble_sort (module), 26
method), 17
adj()
(algorithms.data_structures.digraph.Digraph algorithms.sorting.cocktail_sort (module), 26
algorithms.sorting.comb_sort (module), 26
method), 15
algorithms.sorting.gnome_sort (module), 27
adj() (algorithms.data_structures.undirected_graph.Undirected_Graph
algorithms.sorting.heap_sort (module), 27
method), 17
algorithms.data_structures.binary_search_tree (module), algorithms.sorting.insertion_sort (module), 27
algorithms.sorting.merge_sort (module), 28
13
algorithms.sorting.quick_sort (module), 28
algorithms.data_structures.digraph (module), 15
algorithms.sorting.quick_sort_in_place (module), 29
algorithms.data_structures.lcp_array (module), 30
algorithms.sorting.selection_sort (module), 29
algorithms.data_structures.queue (module), 15
algorithms.data_structures.singly_linked_list (module), algorithms.sorting.shell_sort (module), 29
atkin() (in module algorithms.math.sieve_atkin), 22
16
algorithms.data_structures.stack (module), 17
algorithms.data_structures.undirected_graph (module), B
BinarySearchTree
(class
in
algo17
rithms.data_structures.binary_search_tree),
algorithms.data_structures.union_find (module), 18
13
algorithms.data_structures.union_find_by_rank
(modbuild_heap() (in module algorithms.sorting.heap_sort),
ule), 18
27
algorithms.data_structures.union_find_with_path_compression
build_lengths_matrix()
(in
module
algo(module), 19
rithms.dynamic_programming.lcs), 19
algorithms.dynamic_programming.lcs (module), 19
algorithms.factorization.fermat (module), 20
C
algorithms.factorization.pollard_rho (module), 20
algorithms.factorization.trial_division (module), 20
cdf() (in module algorithms.math.approx_cdf), 20
algorithms.math.approx_cdf (module), 20
ceiling_key() (algorithms.data_structures.binary_search_tree.BinarySearchT
algorithms.math.extended_gcd (module), 21
method), 13
algorithms.math.lcm (module), 21
compute_prefix()
(in
module
algoalgorithms.math.primality_test (module), 21
rithms.searching.kmp_search), 24
algorithms.math.sieve_atkin (module), 21
contains() (algorithms.data_structures.binary_search_tree.BinarySearchTree
algorithms.math.sieve_eratosthenes (module), 22
method), 13
algorithms.math.std_normal_pdf (module), 22
33
algorithms Documentation, Release 1.0.0
D
is_empty() (algorithms.data_structures.binary_search_tree.BinarySearchTre
method), 14
degree() (algorithms.data_structures.undirected_graph.Undirected_Graph
is_empty()
(algorithms.data_structures.queue.Queue
method), 17
method),
16
delete() (algorithms.data_structures.binary_search_tree.BinarySearchTree
is_empty()
(algorithms.data_structures.stack.Stack
method), 13
method), 17
delete_max() (algorithms.data_structures.binary_search_tree.BinarySearchTree
is_prime()
(in module algorithms.math.primality_test), 21
method), 13
is_sorted()
(in module algorithms.sorting.bogo_sort), 25
delete_min() (algorithms.data_structures.binary_search_tree.BinarySearchTree
method), 13
(in
module
algo- K
keys() (algorithms.data_structures.binary_search_tree.BinarySearchTree
rithms.searching.depth_first_search), 24
method), 14
Digraph (class in algorithms.data_structures.digraph), 15
dfs()
E
L
edge_count() (algorithms.data_structures.digraph.Digraph lcm() (in module algorithms.math.lcm), 21
lcp_array()
(in
module
algomethod), 15
rithms.data_structures.lcp_array),
30
edge_count() (algorithms.data_structures.undirected_graph.Undirected_Graph
lcs() (in module algorithms.dynamic_programming.lcs),
method), 18
19
eratosthenes()
(in
module
algorithms.math.sieve_eratosthenes), 22
M
extended_gcd()
(in
module
algomake_set() (algorithms.data_structures.union_find.UnionFind
rithms.math.extended_gcd), 21
method), 18
make_set()
(algorithms.data_structures.union_find_by_rank.UnionFindByR
F
method), 19
f() (in module algorithms.factorization.pollard_rho), 20
make_set() (algorithms.data_structures.union_find_with_path_compression.
fermat() (in module algorithms.factorization.fermat), 20
method), 19
find() (algorithms.data_structures.union_find.UnionFind
max_heapify() (in module algorithms.sorting.heap_sort),
method), 18
27
find() (algorithms.data_structures.union_find_by_rank.UnionFindByRank
max_key() (algorithms.data_structures.binary_search_tree.BinarySearchTre
method), 18
method), 14
find() (algorithms.data_structures.union_find_with_path_compression.UnionFindWithPathCompression
merge() (in module algorithms.sorting.merge_sort), 28
method), 19
MersenneTwister
(class
in
algofloor_key() (algorithms.data_structures.binary_search_tree.BinarySearchTree
rithms.random.mersenne_twister), 22
method), 14
min_key() (algorithms.data_structures.binary_search_tree.BinarySearchTree
method), 14
G
generate() (algorithms.random.mersenne_twister.MersenneTwister
N
method), 23
Node
(class
in
algoget() (algorithms.data_structures.binary_search_tree.BinarySearchTree
rithms.data_structures.binary_search_tree),
method), 14
15
get_data() (algorithms.data_structures.singly_linked_list.Node
Node
(class
in
algomethod), 16
rithms.data_structures.singly_linked_list),
get_next() (algorithms.data_structures.singly_linked_list.Node
16
method), 16
I
O
outdegree() (algorithms.data_structures.digraph.Digraph
is_connected() (algorithms.data_structures.union_find.UnionFind
method), 15
method), 18
is_connected() (algorithms.data_structures.union_find_by_rank.UnionFindByRank
P
method), 19
parent() (algorithms.data_structures.union_find_with_path_compression.Un
is_connected() (algorithms.data_structures.union_find_with_path_compression.UnionFindWithPathCompression
method), 19
method), 19
partition()
(in
module
algorithms.sorting.quick_sort_in_place), 29
34
Index
algorithms Documentation, Release 1.0.0
pdf() (in module algorithms.math.std_normal_pdf), 22
size() (algorithms.data_structures.binary_search_tree.BinarySearchTree
pollard_rho()
(in
module
algomethod), 15
rithms.factorization.pollard_rho), 20
size() (algorithms.data_structures.queue.Queue method),
pollard_rho_rec()
(in
module
algo16
rithms.factorization.pollard_rho), 20
size() (algorithms.data_structures.singly_linked_list.SinglyLinkedList
put() (algorithms.data_structures.binary_search_tree.BinarySearchTreemethod), 17
method), 14
size() (algorithms.data_structures.stack.Stack method),
17
Q
sort() (in module algorithms.sorting.bogo_sort), 25
sort() (in module algorithms.sorting.bubble_sort), 26
Queue (class in algorithms.data_structures.queue), 16
sort() (in module algorithms.sorting.cocktail_sort), 26
sort() (in module algorithms.sorting.comb_sort), 26
R
sort() (in module algorithms.sorting.gnome_sort), 27
randint() (algorithms.random.mersenne_twister.MersenneTwister
sort() (in module algorithms.sorting.heap_sort), 27
method), 23
sort() (in module algorithms.sorting.insertion_sort), 28
rank() (algorithms.data_structures.binary_search_tree.BinarySearchTree
sort() (in module algorithms.sorting.merge_sort), 28
method), 14
read_from_matrix()
(in
module
algo- sort() (in module algorithms.sorting.quick_sort), 28
sort()
(in
module
algorithms.dynamic_programming.lcs), 19
rithms.sorting.quick_sort_in_place),
29
remove()
(algorithms.data_structures.queue.Queue
sort() (in module algorithms.sorting.selection_sort), 29
method), 16
sort() (in module algorithms.sorting.shell_sort), 30
remove() (algorithms.data_structures.singly_linked_list.SinglyLinkedList
Stack (class in algorithms.data_structures.stack), 17
method), 16
(in
module
algoremove()
(algorithms.data_structures.stack.Stack suffix_array()
rithms.data_structures.lcp_array),
30
method), 17
reverse()
(algorithms.data_structures.digraph.Digraph
T
method), 15
(in
module
rho() (in module algorithms.factorization.pollard_rho), 20 trial_division()
rithms.factorization.trial_division), 20
S
algo-
U
search() (algorithms.data_structures.singly_linked_list.SinglyLinkedList
Undirected_Graph
(class
in
algomethod), 17
rithms.data_structures.undirected_graph),
search() (in module algorithms.searching.binary_search),
17
23
search() (in module algorithms.searching.bmh_search), union() (algorithms.data_structures.union_find.UnionFind
method), 18
23
search() (in module algorithms.searching.kmp_search), union() (algorithms.data_structures.union_find_by_rank.UnionFindByRank
method), 19
24
search()
(in
module
algo- union() (algorithms.data_structures.union_find_with_path_compression.Uni
method), 19
rithms.searching.rabinkarp_search), 24
(class
in
algosearch() (in module algorithms.searching.ternary_search), UnionFind
rithms.data_structures.union_find), 18
25
UnionFindByRank
(class
in
algoseed() (algorithms.random.mersenne_twister.MersenneTwister
rithms.data_structures.union_find_by_rank),
method), 23
18
select_key() (algorithms.data_structures.binary_search_tree.BinarySearchTree
UnionFindWithPathCompression
(class
in
algomethod), 14
rithms.data_structures.union_find_with_path_compression),
set_data() (algorithms.data_structures.singly_linked_list.Node
19
method), 16
set_next() (algorithms.data_structures.singly_linked_list.Node
V
method), 16
vertex_count() (algorithms.data_structures.digraph.Digraph
shuffle() (in module algorithms.shuffling.knuth), 25
method), 15
SinglyLinkedList
(class
in
algovertex_count() (algorithms.data_structures.undirected_graph.Undirected_Gr
rithms.data_structures.singly_linked_list),
method), 18
16
Index
35
algorithms Documentation, Release 1.0.0
vertices()
(algorithms.data_structures.digraph.Digraph
method), 15
vertices() (algorithms.data_structures.undirected_graph.Undirected_Graph
method), 18
36
Index