Algorithms Documentation Release 1.0.0
Total Page:16
File Type:pdf, Size:1020Kb
algorithms Documentation Release 1.0.0 Nic Young April 14, 2018 Contents 1 Usage 3 2 Features 5 3 Installation: 7 4 Tests: 9 5 Contributing: 11 6 Table of Contents: 13 6.1 Algorithms................................................ 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 self- loops 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.