diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..3f621ab --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: python +python: + - "2.7" + - "3.6" +install: + - pip install . + - pip install -r requirements.txt +script: py.test \ No newline at end of file diff --git a/README.md b/README.md index 87c500a..1136f12 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,273 @@ -# data-structures -Data-Structures +# Data-Structures + +[![Build Status](https://travis-ci.org/markreynoso/data-structures.svg?branch=bst_traversals)](https://travis-ci.org/markreynoso/data-structures) + +Where a variety of data-structures found in python are being explored, such as: +* A simple class LinkedList for building linked lists as well as a subclass Stack to implement a stack. + +## Authors + +ChaiChaitanya Narukulla and Mark Reynoso created this code base as a project for Python 401 at Code Fellows Seattle. + +### Requirements for testing and development + +In order to create a development and testing environment, you will need the following: + +``` +python2, python3, ipython, pytest, pytest-watch, pytest-cov, tox +``` + +``` +To install environment: + +pip install -e .[testing][development] +``` + +### Linkded List Class Usage + +``` +To create an instance of a the LinkedList() class contained in package, from python: + +new = LinkedList() *you may choose and optional parameter of a single value or an iterable of one of the following: a tuple, a list, or a string.* + +LinkedList() contains the following methods: +* _push(val) (O1)_ - will insert the value ‘val’ at the head of the list. +* _pop() (O1)_ - will pop the first value off the head of the list and return it. Raises an exception with an appropriate message if there are no values to return. +* _size() (01)_ - will return the length of the list. +* _search(val) (On)_ - will return the node containing ‘val’ in the list, if present, else None. +* _remove(node) (On)_ - will remove the given node from the list, wherever it might be (node must be an item in the list). If the node is not in the list, it will raise an exception with an appropriate message. +* _display() (O1)_ - will return a unicode string representing the list as if it were a Python tuple literal: “(12, ‘sam’, 37, ‘tango’)” + +To access any contained methods: +new.push(val) +new.pop() +new.size() +new.search(val) +new.remove(node) +new.display() + +Additionally, LinkedList() will interact with these built-in Python functions: + +* _len(new)_ - returns the size of the list. +* _print(new)_ - returns what the display() method returns. + +``` + +### Stack Class Usage + +``` +To create an instance of a Stack() class contained in package, from python: + +new = Stack() *you may choose and optional parameter of a single value or an iterable of one of the following: a tuple, a list, or a string.* + +Stack() contains the following methods: +* _push(val) (O1)_ - will insert the value ‘val’ at the head of the stack. +* _pop() (O1)_ - will pop the first value off the head of the stack and return it. Raises an exception with an appropriate message if there are no values to return. + +To access any contained methods: +new.push(val) +new.pop() + +Additionally, Stack() will interact with these built-in Python functions: + +* _len(new)_ - returns the size of the list. + +``` + +### Doubly Linked List Class Usage + +``` +To create an instance of a Dll() class contained in package, from python: + +new = Dll() *you may not call Dll() with any parameters.* + +Dll() contains the following methods: +* _push(val) (O1)_ - will insert the value ‘val’ at the head of the list. +* _append(val) (O1)_ - will insert the value ‘val’ at the tail of the list. +* _pop() (O1)_ - will pop the first value off the head of the stack and return it. Raises an exception with an appropriate message if there are no values to return. +* _shift() (O1)_ - will remove the last value off the tail of the list and return it. Raises an exception with an appropriate message if there are no values to return. +* _remove(node) (On)_ - will remove the given node from the list, wherever it might be (node must be an item in the list). If the node is not in the list, it will raise an exception with an appropriate message. + +To access any contained methods: +new.push(val) +new.append(val) +new.pop() +new.shift() +new.remove() + +Additionally, Dll() will interact with these built-in Python functions: + +* _len(new)_ - returns the size of the list. + +``` + +### Queue Class Usage + +``` +To create an instance of a Queue() class contained in package, from python: + +new = Queue() *you may not call Queue() with any parameters.* + +Queue() contains the following methods: +* _enqueue(val) (O1)_ - will insert the value ‘val’ at the end of the queue. +* _dequeue() (O1)_ - will remove the last value off the front of the queue and return it. Raises an exception with an appropriate message if there are no values to return. +* _peek() (O1)_ - shows the value of the node at the end of the queue. +* _size() (O1)_ - displays the number of node in the queue. + +To access any contained methods: +new.enqueue(val) +new.dequeue() +new.peek() +new.size() + +Additionally, Queue will interact with these built-in Python functions: + +* _len(new)_ - returns the size of the list. + +``` + +### Deque Class Usage + +``` +To create an instance of a Deque() class contained in package, from python: + +new = Deque() *you may not call Deque() with any parameters.* + +Deque() contains the following methods: +* _append(val) (O1)_ - will insert the value ‘val’ at the end of the deque. +* _appendleft() (O1)_ - will insert a value to the front of the deque. +*_pop() (01)_ - removes the first val from the end of the deque. +*_popleft() (01)_ - removes the first val from the front of the deque. +* _peek() (O1)_ - shows the value of the item at the end of the deque. +* _peekleft() (O1)_ - shows the value of the item at the font of the deque. +* _size() (O1)_ - displays the number items in the deque. + +To access any contained methods: +new.append(val) +new.appendleft(val) +new.pop() +new.popleft() +new.peek() +new.peekleft() +new.size() + +``` + +### Binaary Heap (Binheap) Class Usage + +``` +To create an instance of a Binheap() class contained in package, from python: + +new = Binheap() *you may call Binheap() with any uniqe number or iterable of unique numbers.* + +Binheap() contains the following methods: +* _push(val) (Olog(n))_ - will insert the value ‘val’ at the end of the heap. +*_pop() (0log(n))_ - removes the first val from the end of the heap. + +To access any contained methods: +new.push(val) +new.pop() + +``` + +### Priorityq (Priorityq) Class Usage + +``` +To create an instance of a Priorityq() class contained in package, from python: + +new = Priorityq() *you may call Priorityq() with any uniqe number or iterable of unique numbers.* + +Priorityq() contains the following methods: +*_insert(val,priority) (O1)_ - will insert the value ‘val’ at the end of the heap. +*_pop() (01)_ - removes the higest priority and returns the higest priority. +*_peek() (01)_ - returns the higest priority. + +To access any contained methods: +new.insert(val) +new.pop() +new.peek() + +``` + +### Graph Class (weighted) Usage + +``` +To create an instance of a Graph() class contained in package, from python: + +new = Graph() *you may not call Graph() with any parameters.* + +Graph() contains the following methods: +* _nodes() (O1)_ - will return a list of all nodes in the graph. +* _edges() (On2)_ - will return a dictionary of edges in graph with weights. +*_add_node(val) (01)_ - adds a new node to the graph. +*_add_edge(val1, val2, weight) (0n)_ - add a new edge to nodes in the graph. +* _del_node(val) (On2)_ - delete node from graph. +* _del_edge(val1, val2) (On)_ - delete edge between two nodes. +* _has_node(val) (O1)_ - returns true if node exists and false if not. +* _neighbors(val) (On2)_ - return a list of nodes with edges to input node. +* _adjacent(val1, val2) (On)_ - return true if edge exists between two inputs, + otherwise false. +* _depth_first_traversal(start_val) (Ologn)_ - will return full visited path of traversal completed. +* _breadth_first_traversal(start_val)(Ologn)_ - will return full visited path of traversal completed. + +To access any contained methods: +new.nodes() +new.edges() +new.add_node(val) +new.add_edge(val1, val2, weight) +new.del_node(val) +new.del_edge(val1, val2) +new.has_node(val) +new.neighbors(val) +new.adjacent(val1, val2) +new.depth_first_traversal(start_val) +new.breadth_first_traversal(start_val) + +``` + +### Shortest Distance Problem + +``` +The problem of the traveling salesperson finding the shortest distance between points can be solved in a variety of ways. We have used Dijkstra's Algorith as our first method to a solution. + +_Dijkstra's Algorithm_ (On) - finds the shortest path exploring all edges of a graph from it's starting point. By comparing the distance or weight of the edges from the start to each child as it traverses the graph it is able to calculate the shortest route by saving the parent path which will have the lowest cost from start to finish. + +To use this function: +dijkstra(graph, start, end) + +``` + +### Binary Search Tree + +``` +To create an instance if the binary search tree, Bst(), from python: + +new = Bst() *you may not call Bst() no parameters or with an iterable item containing unique integers.* + +Bst() contains the following methods: +* _insert(val) (Olog(n))_ - will insert the value ‘val’ in the appropriate position in the tree. +* _search(val) (0log(n))_ - will find a given value in the tree, if not in tree will return none. +* _size() (O(1))_ - will return the size of the tree. +* _depth(root) (0(n))_ - returns the depth of the tree. +* _contains(val) (Olog(n))_ - will return true or false if value is in tree. +*_ balance() (0(n))_ - returns the depth of the left minus the right side of the tree as an integer. +* _in_order() (O(n))_ - returns a generator the entire tree in order of lowest to highest value. +* _pre_order() (0(n)(log(n)))_ - returns a generator of parent followed by children from left side to right side. +* _post_order() (O(n)log(n)))_ - returns a generator of children followed by parent from left side to right side. +*_ breadth_first() (0(n))_ - returns generator of tree ordered from root one level at a time. +*_ delete(val) (0log(n))_ - delete a node with given val. + + +To access any contained methods: +new.insert(val) +new.search(val) +new.size() +new.depth(root) +new.contains(val) +new.balance() +new.in_order() +new.pre_order() +new.post_order() +new.breadth_first() +new.delete(val) +``` \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4f44f7d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,29 @@ +appnope==0.1.0 +argh==0.26.2 +colorama==0.3.9 +coverage==4.4.2 +-e git+https://github.com/markreynoso/data-structures.git@094d8f6f7f5c7e9af06a5e4442a872acbe11c94f#egg=data_structures +decorator==4.1.2 +docopt==0.6.2 +ipython-genutils==0.2.0 +jedi==0.11.0 +parso==0.1.0 +pathtools==0.1.2 +pexpect==4.3.0 +pickleshare==0.7.4 +pluggy==0.5.2 +prompt-toolkit==1.0.15 +ptyprocess==0.5.2 +py==1.5.2 +Pygments==2.2.0 +pytest==3.2.5 +pytest-cov==2.5.1 +pytest-watch==4.1.0 +PyYAML==3.12 +simplegeneric==0.8.1 +six==1.11.0 +tox==2.9.1 +traitlets==4.3.2 +virtualenv==15.1.0 +watchdog==0.8.3 +wcwidth==0.1.7 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..8011f7b --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +from setuptools import setup + +setup( + name='data-structures', + description=('A package for building and ' + 'running the data-structures module'), + package_dir={'': 'src'}, + author='Mark and chai', + author_email='chaitanyanarukulla@gmail.com mreynoso@spu.edu', + py_modules=['binheap'], + install_requires=[], + extras_require={ + 'testing': ['pytest', 'pytest-cov', 'pytest-watch', 'tox'], + 'development': ['ipython'] + }, +) diff --git a/src/binheap.py b/src/binheap.py new file mode 100644 index 0000000..2bc2ae6 --- /dev/null +++ b/src/binheap.py @@ -0,0 +1,80 @@ +"""Binary heap class.""" + + +class Binheap(object): + """Iniitialize the class node.""" + + def __init__(self, iterable=None): + """Create a new min heap.""" + self.heaplist = [] + self._size = 0 + if isinstance(iterable, (list, tuple)): + for item in iterable: + self.push(item) + + def push(self, val): + """Push a value to the end of heap and sort up.""" + if type(val) == int or type(val) == float: + self.heaplist.append(val) + self._size += 1 + sort_up = True + idx = self._size - 1 + while sort_up: + if idx > 0: + if idx % 2 == 0: + parent = (idx - 2) // 2 + else: + parent = (idx - 1) // 2 + if val < self.heaplist[parent]: + self.heaplist[idx] = self.heaplist[parent] + self.heaplist[parent] = val + idx = parent + else: + sort_up = False + else: + sort_up = False + else: + raise ValueError('You must input numbers only.') + + def pop(self): + """Remove first value in heap and sort down.""" + if self._size == 0: + raise IndexError('There are no values to pop.') + elif self._size == 1: + self._size -= 1 + return self.heaplist.pop() + elif self._size > 1: + pop_val = self.heaplist[0] + self.heaplist[0] = self.heaplist.pop() + self._size -= 1 + idx = 0 + sort_down = True + while sort_down: + l_child = idx * 2 + 1 + r_child = idx * 2 + 2 + if r_child <= self._size - 1 and l_child <= self._size - 1: + if self.heaplist[l_child] > self.heaplist[r_child]: + if self.heaplist[r_child] < self.heaplist[idx]: + self.heaplist[r_child], self.heaplist[idx] =\ + self.heaplist[idx], self.heaplist[r_child] + idx = r_child + else: + sort_down = False + else: + if self.heaplist[l_child] < self.heaplist[r_child]: + if self.heaplist[l_child] < self.heaplist[idx]: + self.heaplist[idx], self.heaplist[l_child] =\ + self.heaplist[l_child], self.heaplist[idx] + idx = l_child + else: + sort_down = False + elif l_child == self._size - 1: + if self.heaplist[l_child] < self.heaplist[idx]: + self.heaplist[idx], self.heaplist[l_child] =\ + self.heaplist[l_child], self.heaplist[idx] + sort_down = False + else: + sort_down = False + else: + sort_down = False + return pop_val diff --git a/src/bst.py b/src/bst.py new file mode 100644 index 0000000..377efd3 --- /dev/null +++ b/src/bst.py @@ -0,0 +1,328 @@ +"""Binary Search Tree.""" + + +class Node(object): + """Node object for bst.""" + + def __init__(self, data, lc=None, rc=None, mom=None): + """Initialize a new node.""" + self.data = data + self.left = lc + self.right = rc + self.parent = mom + + +class Bst(object): + """Binary Search Tree class.""" + + def __init__(self, iterable=None): + """Initiate a new instance of bst.""" + self.root = None + self._size = 0 + if isinstance(iterable, (list, tuple)): + for item in iterable: + self.insert(item) + + def insert(self, val): + """Insert a value into bst.""" + if not isinstance(val, (int, float)): + raise ValueError('Sorry, I only take numbers right now.') + if self.root is None: + self.root = Node(val) + self._size += 1 + return + elif val == self.root.data: + raise ValueError('This node already exists.') + current = self.root + while current: + if val == current.data: + raise ValueError('This node already exists.') + elif val < current.data: + if current.left: + current = current.left + else: + current.left = Node(val) + current.left.parent = current + self._size += 1 + break + elif val > current.data: + if current.right: + current = current.right + else: + current.right = Node(val) + current.right.parent = current + self._size += 1 + break + + def search(self, val): + """Search bst for val and return node of this val, else none.""" + if self.root is None or not isinstance(val, (int, float)): + return + current = self.root + while current: + if val == current.data: + return current + elif val < current.data: + current = current.left + elif val > current.data: + current = current.right + + def size(self): + """Return size of bst.""" + return self._size + + def depth(self, root=None): + """Return total number of levels in bst.""" + if root is None: + if self.root is None: + return 0 + else: + root = self.root + if not root.left and not root.right: + return 0 + elif root.left and not root.right: + return self.depth(root.left) + 1 + elif root.right and not root.left: + return self.depth(root.right) + 1 + else: + return max(self.depth(root.left), self.depth(root.right)) + 1 + + def contains(self, val): + """Search for val in tree and return boolean.""" + if self.search(val) is not None: + return True + else: + return False + + def balance(self): + """Return integer indicating balance of tree.""" + if self.root is None: + return 'There are no nodes in this tree.' + elif not self.root.left and not self.root.right: + return 0 + elif self.root.left and not self.root.right: + return self.depth(self.root.left) + elif self.root.right and not self.root.left: + return self.depth(self.root.right) + else: + return self.depth(self.root.right) - self.depth(self.root.left) + + def breadth_first(self): + """Return generator of breadth first search.""" + if self.root is None: + raise ValueError('There are no nodes in this tree.') + bfs = [self.root] + while bfs: + current = bfs.pop(0) + if current.left: + bfs.append(current.left) + if current.right: + bfs.append(current.right) + yield current.data + + def in_order(self): + """Return generator of in order search.""" + if self.root is None: + raise ValueError('There are no nodes in this tree.') + current = self.root + ios = [] + while current or ios: + if current: + ios.append(current) + current = current.left + else: + current = ios.pop() + yield current.data + current = current.right + + def pre_order(self): + """Return generator of pre order search.""" + if self.root is None: + raise ValueError('There are no nodes in this tree.') + current = self.root + ios = [] + while current or ios: + if current: + yield current.data + if current.right: + ios.append(current.right) + current = current.left + else: + current = ios.pop() + + def post_order(self): + """Return generator of post order wearch.""" + if self.root is None: + raise ValueError('There are no nodes in this tree.') + current = self.root + child = None + ios = [] + while current or ios: + if current: + ios.append(current) + current = current.left + else: + if ios[-1].right and ios[-1].right is not child: + current = ios[-1].right + else: + child = ios.pop() + yield child.data + + def delete(self, val): + """Delete node with given val.""" + if self.root is None or not isinstance(val, (int, float)): + return + on_deck = self.search(val) + if not on_deck.left and not on_deck.right: + self._delete_no_children(on_deck) + elif on_deck.left and not on_deck.right: + self._delete_one_child(on_deck) + elif on_deck.right and not on_deck.left: + self._delete_one_child(on_deck) + elif on_deck.left and on_deck.right: + self._delete_two_children(on_deck) + + def _delete_no_children(self, on_deck): + """Delete node with no children.""" + self._size -= 1 + parent = on_deck.parent + if not on_deck.parent: + self.root = None + elif on_deck.parent.data < on_deck.data: + on_deck.parent.right = None + elif on_deck.parent.data > on_deck.data: + on_deck.parent.left = None + + def _delete_one_child(self, on_deck): + """Delete node with only one child.""" + self._size -= 1 + if on_deck == self.root: + if self.root.right: + self.root = self.root.right + self.root.right = on_deck.right + self.root.left = on_deck.left + else: + self.root = self.root.left + self.root.right = on_deck.right + self.root.left = on_deck.left + self.root.parent is None + elif on_deck.parent.data < on_deck.data: + if on_deck.left: + on_deck.parent.right = on_deck.left + on_deck.left.parent = on_deck.parent + elif on_deck.right: + on_deck.parent.right = on_deck.right + on_deck.right.parent = on_deck.parent + else: + if on_deck.left: + on_deck.parent.left = on_deck.left + on_deck.left.parent = on_deck.parent + elif on_deck.right: + on_deck.parent.left = on_deck.right + on_deck.right.parent = on_deck.parent + + def _delete_two_children(self, on_deck): + """Delete node with two children.""" + self._size -= 1 + current = on_deck.right + while current: + if current.left: + current = current.left + else: + break + if current.parent == on_deck: + current.parent = on_deck.parent + current.left = on_deck.left + if current.left: + current.left.parent = current + if current.parent: + if current.parent.data < current.data: + current.parent.right = current + else: + current.parent.left = current + else: + self.root = current + elif current.right: + current.right.parent = current.parent + current.parent.left = current.right + current.parent = on_deck.parent + current.right = on_deck.right + current.left = on_deck.left + current.left.parent = current + current.right.parent = current + if current.parent: + if current.parent.data < current.data: + current.parent.right = current + else: + current.parent.left = current + else: + self.root = current + else: + current.parent.left = None + current.parent = on_deck.parent + current.right = on_deck.right + current.left = on_deck.left + current.left.parent = current + current.right.parent = current + if current.parent: + if current.parent.data < current.data: + current.parent.right = current + else: + current.parent.left = current + else: + self.root = current + + +if __name__ == '__main__': # pragma: no cover + import timeit + + insert_time_ub = Bst() + num = (x for x in range(1000)) + a = timeit.timeit('insert_time_ub.insert(next(num))', + setup='from __main__ import insert_time_ub, num', + number=1000) + search_time_ub = Bst() + for i in range(100): + search_time_ub.insert(i) + b = timeit.timeit('search_time_ub.search(99)', + setup='from __main__ import search_time_ub', + number=1000) + c = timeit.timeit('search_time_ub.search(0)', + setup='from __main__ import search_time_ub', + number=1000) + insert_time_b = Bst() + + def insert_time(val): + """.""" + if (500 + val) % 2 == 0: + insert_time_b.insert(500 + val) + else: + insert_time_b.insert(500 - val) + + num_b = (x for x in range(1000)) + d = timeit.timeit('insert_time(next(num_b))', + setup='from __main__ import insert_time, num_b', + number=1000) + + search_time_b = Bst() + for i in range(1000): + if (500 + i) % 2 == 0: + search_time_b.insert(500 + i) + else: + search_time_b.insert(500 - i) + e = timeit.timeit('search_time_b.search(999)', + setup='from __main__ import search_time_b', + number=1000) + f = timeit.timeit('search_time_b.search(500)', + setup='from __main__ import search_time_b', + number=1000) + + print('The following time relates to worst case insert.') + print('Insert unbalanced: {}'.format(a)) + print('Insert balanced: {}'.format(d)) + print('\nThe following time relates to worst case search.') + print('Search unbalanced leaf: {}'.format(b)) + print('Search balanced leaf: {}'.format(e)) + print('\nThe following time relates to best base search.') + print('Search unbalanced head: {}'.format(c)) + print('Search balanced head: {}'.format(f)) diff --git a/src/conftest.py b/src/conftest.py new file mode 100644 index 0000000..07f0426 --- /dev/null +++ b/src/conftest.py @@ -0,0 +1,59 @@ +"""Fixutres for test_binheap.py.""" +import pytest + + +@pytest.fixture +def ebh(): + """Initialize an empty binary heap.""" + from binheap import Binheap + return Binheap() + + +@pytest.fixture +def pq(): + """Initialize a empty pq.""" + from priorityq import Priorityq + return Priorityq() + + +@pytest.fixture +def g(): + """Initialize a empty pq.""" + from graph import Graph + return Graph() + + +@pytest.fixture +def bst(): + """Initialize empty bst for tests.""" + from bst import Bst + return Bst() + + +@pytest.fixture +def bst_full(): + """Create bst will values for tests.""" + from bst import Bst + new = Bst() + new.insert(3254) + new.insert(908543) + new.insert(58490543) + return new + + +@pytest.fixture +def bst_big(): + """Create bst will values for tests.""" + from bst import Bst + new = Bst() + new.insert(50) + new.insert(40) + new.insert(68) + new.insert(10) + new.insert(110) + new.insert(1) + new.insert(500) + new.insert(18) + new.insert(80) + new.insert(5000) + return new diff --git a/src/doubly_linked_list.py b/src/doubly_linked_list.py new file mode 100644 index 0000000..48b03dd --- /dev/null +++ b/src/doubly_linked_list.py @@ -0,0 +1,121 @@ +"""Create a new instance of a doubly linked list.""" + + +class Node(object): + """Create new instance of Node class.""" + + def __init__(self, data=None, prve=None, next=None, tail=None): + """Initiate new Node with no defined values.""" + self.data = data + self.next = next + self.prve = prve + + def get_prve(self): + """Get previous node.""" + return self.prve + + def get_data(self): + """Get data of current node.""" + return self.data + + def get_next(self): + """Get next node.""" + return self.next + + def set_next(self, new_next): + """Set next node.""" + self.next = new_next + + def set_prve(self, new_prve): + """Set previous node.""" + self.prve = new_prve + + +class Dll(object): + """Create instance of doubly linked link class.""" + + def __init__(self): + """Initialize doubly linked list with no default values.""" + self.head = None + self.tail = None + self._size = 0 + + def push(self, val): + """Push value to top of list.""" + new_node = Node(val) + if self._size < 1: + self.head = new_node + self.tail = new_node + else: + new_node.set_next(self.head) + self.head.set_prve(new_node) + self.head = new_node + self._size += 1 + + def append(self, val): + """Append value to tail of list.""" + new_node = Node(val) + if self._size < 1: + self.head = new_node + self.tail = new_node + else: + new_node.set_prve(self.tail) + self.tail.set_next(new_node) + self.tail = new_node + self._size += 1 + + def pop(self): + """Remove current head of list.""" + current = self.head + if self.head is None: + raise IndexError('This is an empty list. No values to pop.') + elif self._size == 1: + self.tail = None + self.head = None + self._size -= 1 + elif self._size > 1: + self.head = current.get_next() + self.head.set_prve(None) + self._size -= 1 + return current.data + + def shift(self): + """Remove current tail of list.""" + current = self.tail + if self.tail is None: + raise IndexError('This is an empty list. No values to shift.') + elif self._size == 1: + self.tail = None + self.head = None + self._size -= 1 + elif self._size > 1: + self.tail = current.get_prve() + self.tail.set_next(None) + self._size -= 1 + return current.data + + def remove(self, val): + """Remove inputted value.""" + current = self.head + while current: + if current.get_data() == val: + if current == self.head: + self.pop() + return None + if current == self.tail: + self.shift() + return None + else: + npn = current.get_prve() + nnn = current.get_next() + npn.set_next(nnn) + nnn.set_prve(npn) + self._size -= 1 + return None + current = current.get_next() + else: + raise ValueError('Your node does not exist in this linked list.') + + def __len__(self): + """Display size of list.""" + return self._size diff --git a/src/graph.py b/src/graph.py new file mode 100644 index 0000000..6215399 --- /dev/null +++ b/src/graph.py @@ -0,0 +1,156 @@ +"""Implement a class for a graph data structure.""" + + +class Graph(object): + """Graph class.""" + + def __init__(self): + """Initialize a graph.""" + self._graph = {} + + def nodes(self): + """Return a list of all nodes in graph.""" + return list(self._graph.keys()) + + def edges(self): + """Return a list of edges in graph.""" + edges = {} + for key in self._graph: + for i in self._graph[key]: + edges[(key, i)] = self._graph[key][i] + return edges + + def add_node(self, val): + """Add a node with value of val to graph.""" + self._graph[val] = {} + + def add_edge(self, val1, val2, weight=0): + """Add a new edge to graph between val1 & val2 as well as add vals.""" + if val1 in self._graph and val2 in self._graph: + if val2 not in self._graph[val1]: + self._graph[val1][val2] = weight + elif val1 in self._graph and val2 not in self._graph: + self._graph[val2] = {} + self._graph[val1][val2] = weight + elif val2 in self._graph and val1 not in self._graph: + self._graph[val1] = {} + self._graph[val1][val2] = weight + else: + self._graph[val1] = {val2: weight} + self._graph[val2] = {} + + def del_node(self, val): + """Delete node w/val from graph, raises exception if not exist.""" + if val in self._graph: + del self._graph[val] + for key in self._graph: + for i in self._graph[key]: + if i == val: + del self._graph[key] + else: + raise ValueError('There is no node of that value in the graph.') + + def del_edge(self, val1, val2): + """Delete edge between val1 & val2 from graph.""" + try: + if val2 in self._graph[val1]: + del self._graph[val1] + else: + raise ValueError('These edges do not exist.') + except KeyError: + raise ValueError('These edges do not exist.') + + def has_node(self, val): + """Return true or false if node has value.""" + if val in self._graph: + return True + else: + return False + + def neighbors(self, val): + """Return list of nodes connected node(val).""" + neighbors = [] + try: + for key in self._graph[val]: + neighbors.append(key) + return neighbors + except KeyError: + raise ValueError('This node dosent exit') + + def adjacent(self, val1, val2): + """Return true if edge between vals, else false, & error if no val.""" + if val1 in self._graph or val2 in self._graph: + if val1 in self._graph[val2]: + return True + if val2 in self._graph[val1]: + return True + return False + else: + raise ValueError('These edges do not exist.') + + def depth_first_traversal(self, start_val): + """Traverse the graph from first edge of each node until ultimate.""" + if start_val in self._graph: + depth_traversal = [] + path = [start_val] + while path: + val = path.pop() + if val not in depth_traversal: + depth_traversal.append(val) + path = path + list(self._graph[val].keys()) + return depth_traversal + else: + raise ValueError('Value is not in graph.') + + def breadth_first_traversal(self, start_val): + """Traverse the graph by node's edges before moving to next node.""" + if start_val in self._graph: + depth_traversal = [] + path = [start_val] + while path: + val = path.pop(0) + if val not in depth_traversal: + depth_traversal.append(val) + path = path + list(self._graph[val].keys()) + return depth_traversal + else: + raise ValueError('Value is not in graph.') + + +if __name__ == '__main__': # pragma: no cover + graph_data = { + 'A': {'B': 0, 'D': 0}, + 'B': {'A': 0, 'E': 0, 'D': 0}, + 'C': {'A': 0, 'B': 0}, + 'D': {}, + 'E': {'D': 0, 'C': 0, 'B': 0, 'A': 0} + } + g = Graph() + g._graph = graph_data + print('Using the following graph:\n\n{}\n\nthe lists below show a ' + 'depth_first_traversal followed by a ' + 'breadth_first_traversal:'.format(graph_data)) + print('\nDepth First Traversal:') + print(g.depth_first_traversal('A')) + print('\nBreadth First Traversal:') + print(g.breadth_first_traversal('A')) + + graph_data = { + 'George': {'Steve': 0, 'Jane': 0, 'Phil': 0}, + 'Anne': {'Abe': 0, 'Uma': 0, 'George': 0}, + 'George': {'Abe': 0, 'Steve': 0}, + 'Steve': {'Anne': 0}, + 'Abe': {'George': 0, 'Uma': 0, 'Steve': 0, 'Phil': 0}, + 'Uma': {'Steve': 0}, + 'Phil': {'Uma': 0, 'George': 0, 'Phil': 0}, + 'Jane': {'Anne': 0}, + } + g2 = Graph() + g2._graph = graph_data + print('\n\nHere is another example on the following graph:\n\n{}\n\nthe ' + 'lists below, again, show a depth_first_traversal followed by a ' + 'breadth_first_traversal:'.format(graph_data)) + print('\nDepth First Traversal:') + print(g2.depth_first_traversal('Abe')) + print('\nBreadth First Traversal:') + print(g2.breadth_first_traversal('Abe')) diff --git a/src/priorityq.py b/src/priorityq.py index 04fa8a3..e9912c4 100644 --- a/src/priorityq.py +++ b/src/priorityq.py @@ -12,6 +12,9 @@ def __init__(self): def insert(self, val, priority=0): """Insert a new value into the queue.""" + if len(self._que) == 0: + self._highest = priority + self._lowest = priority if priority <= self._highest: self._highest = priority if priority >= self._lowest: @@ -39,4 +42,7 @@ def pop(self): def peek(self): """View the hightest priority item.""" - return self._que[self._highest][0] + if len(self._que) == 0: + raise IndexError('There are no items to view.') + else: + return self._que[self._highest][0] diff --git a/src/que_.py b/src/que_.py new file mode 100644 index 0000000..9e0510c --- /dev/null +++ b/src/que_.py @@ -0,0 +1,36 @@ +"""Create queue class composed from instance of Dll().""" +from doubly_linked_list import Dll + + +class Queue(object): + """Simple class from Dll() instance.""" + + def __init__(self): + """Initiate an empty stack.""" + self.func = Dll() + + def enqueue(self, val): + """Push an item into stack.""" + return self.func.push(val) + + def dequeue(self): + """Remove first item pushed into stack.""" + try: + return self.func.shift().data + except IndexError: + raise IndexError('There are no nodes to dequeue.') + + def peek(self): + """Return the value of the next node to be removed.""" + try: + return self.func.tail.data + except AttributeError: + return None + + def size(self): + """Return the size.""" + return self.func._size + + def __len__(self): + """Print length of stack.""" + return self.func._size diff --git a/src/shortest_dis.py b/src/shortest_dis.py new file mode 100644 index 0000000..0c40a95 --- /dev/null +++ b/src/shortest_dis.py @@ -0,0 +1,41 @@ +"""Using Dijkstra's algorithm to solve the shortest path.""" + + +def dijkstra(graph, start, end): + """Dijkysta algorithm to calculate the shortest path.""" + distance = {start: 0} + parents = {} + + not_visited = list(graph._graph.keys()) + + while not_visited: + min_node = None + for val in not_visited: + if val in distance: + if min_node is None: + min_node = val + elif distance[val] < distance[min_node]: + min_node = val + not_visited.remove(min_node) + if graph._graph[min_node] == {}: + return parents + else: + for edge in graph._graph[min_node]: + length = distance[min_node] + graph._graph[min_node][edge] + if edge not in distance or length < distance[edge]: + distance[edge] = length + parents[edge] = min_node + + return parents + + +def shortest_distance(graph, start, end): + """Utilize dijkstra to find the shortest path.""" + d = dijkstra(graph, start, end) + if d == {}: + raise ValueError('This start node has no edges.') + path = [end] + while end != start: + path.insert(0, d[end]) + end = d[end] + return path diff --git a/src/test_binheap.py b/src/test_binheap.py new file mode 100644 index 0000000..7b4ac0f --- /dev/null +++ b/src/test_binheap.py @@ -0,0 +1,78 @@ +"""Test binheap.py.""" +import pytest + + +def test_initialize_empty_heap(ebh): + """Test if init binary is empty.""" + assert ebh + + +def test_push_one_value(ebh): + """Test size after push one value into heap.""" + ebh.push(9) + assert ebh._size == 1 + + +def test_push_mulitp_value(ebh): + """Test size after push multiple values into heap.""" + for i in range(20): + ebh.push(i) + assert ebh._size == 20 + + +def test_push_one_non_num(ebh): + """Test for erron if pushing non numerical value into heap.""" + with pytest.raises(ValueError): + ebh.push('NOOOOOOOO!!!!!!!!!') + + +def test_push_iterable(): + """Test if push successfully takes iterable.""" + from binheap import Binheap + heap = Binheap([1, 2, 3, 4, 5, 6, 7, 8]) + assert heap._size == 8 + + +def test_pop_on_empty_heap_raises_indexerror(ebh): + """Test for index error trying to pop from empty heap.""" + with pytest.raises(IndexError): + ebh.pop() + + +def test_pop_on_heap_with_one_item(ebh): + """Test pop returns item when only one item in heap.""" + ebh.push(1) + assert ebh.pop() == 1 + + +def test_pop_twice_on_heap_with_one_item(ebh): + """Test pop on fully popped heap.""" + ebh.push(1) + ebh.pop() + with pytest.raises(IndexError): + ebh.pop() + + +def test_pop_returns_sorted_values(): + """Test pop entire bin returns sorted values.""" + from binheap import Binheap + import random + rand_nums = list(set([random.randint(0, 1000) + for i in range(20)])) + heap = Binheap(rand_nums) + # import pdb; pdb.set_trace() + all_popped = [heap.pop() for i in range(heap._size)] + assert all_popped == sorted(rand_nums) + + +def test_pop_returns_sorted_values_limited(ebh): + """Test pop in controlled environment.""" + ebh.push(23), + ebh.push(2), + ebh.push(30), + ebh.push(50), + ebh.push(6), + ebh.push(17), + ebh.push(29) + all_popped = [ebh.pop() for i in range(7)] + assert all_popped == [2, 6, 17, 23, 29, 30, 50] diff --git a/src/test_bst.py b/src/test_bst.py new file mode 100644 index 0000000..b20de31 --- /dev/null +++ b/src/test_bst.py @@ -0,0 +1,433 @@ +"""Test binary search tree.""" +import pytest + + +def test_initialize_bst_returns_empty_bst(bst): + """Test initialize empty bst returns empty bst.""" + assert bst.root is None + + +def test_initialize_bst_iteratble_returns_size_n(): + """Test initialize with iterable returns proper size tree.""" + from bst import Bst + tree = Bst([2, 6, 3, 7, 8, 1]) + assert tree._size == 6 + + +def test_initialize_bst_iteratble_root_is_first_item(): + """Test initialize with iterable root is first item in.""" + from bst import Bst + tree = Bst([2, 6, 3, 7, 8, 1]) + assert tree.root.data == 2 + + +def test_size_returns_0_if_bst_is_empty(bst): + """Test size method returns 0 on empty tree.""" + assert bst.size() == 0 + + +def test_size_returns_appropriate_size_after_iterable(): + """Test size returns proper size when initiated by iterable.""" + from bst import Bst + tree = Bst([2, 6, 3, 7, 8, 1]) + assert tree.size() == 6 + + +def test_size_returns_10_if_insert_manually_10_items(bst_big): + """Test if 10 items inserted that size is 10.""" + assert bst_big.size() == 10 + + +def test_insert_10_items_returns_first_in_as_root(bst_big): + """Test that root is first in if 10 items inserted.""" + assert bst_big.root.data == 50 + + +def test_insert_identicle_values_raise_valueerror(bst): + """Test insert same values raises value error.""" + bst.insert(1) + with pytest.raises(ValueError): + bst.insert(1) + + +def test_insert_non_int_raises_valueerror(bst): + """Test insert non number raises value error.""" + with pytest.raises(ValueError): + bst.insert('howdy') + + +def test_insert_iterable_raises_valueerror(bst): + """Test insert iterable raises appropriate error.""" + with pytest.raises(ValueError): + bst.insert([1, 3, 5, 9]) + + +def test_search_node_on_empty_tree_returns_none(bst): + """Test search on empty tree returns none.""" + assert bst.search(10) is None + + +def test_search_node_not_in_tree_returns_none(bst_full): + """Test search for node not in tree returns none.""" + assert bst_full.search(1) is None + + +def test_search_for_non_int_returns_non(bst_full): + """Test search for non number returns none.""" + assert bst_full.search('cake') is None + + +def test_search_val_in_tree_returns_node(bst_full): + """Test search for val in tree returns node.""" + bst_full.insert(5) + assert isinstance(bst_full.search(5), object) + + +def test_search_val_in_tree_retruns_node_with_data(bst_full): + """Test search for val in tree returns node with data of val.""" + bst_full.insert(5) + assert bst_full.search(5).data == 5 + + +def test_depth_returns_int_of_total_depth_of_tree(bst_full): + """Test depth returns integer of depth of tree.""" + assert bst_full.depth(bst_full.root) == 2 + + +def test_depth_empty_tree_returns_none(bst): + """Test depth on epmty tree returns None.""" + assert bst.depth(bst.root) == 0 + + +def test_depth_on_large_tree_returns_full_size(bst_big): + """Test depth on large tree returns actual size.""" + assert bst_big.depth(bst_big.root) == 4 + + +def test_depth_of_tree_with_only_root_is_0(bst): + """Test if only root in tree that depth is 0.""" + bst.insert(1) + assert bst.depth(bst.root) == 0 + + +def test_contains_returns_false_if_val_not_in_tree(bst_big): + """Test contains returns false if val not in tree.""" + assert bst_big.contains(102948686) is False + + +def test_contains_returns_false_if_non_int_entered(bst_big): + """"Test contains returns false if non int entered.""" + assert bst_big.contains('pie') is False + + +def test_contains_returns_true_if_val_in_tree(bst_big): + """Test contains returns true if val in tree.""" + assert bst_big.contains(40) is True + assert bst_big.contains(50) is True + assert bst_big.contains(68) is True + + +def test_balance_returns_string_if_bst_is_empty(bst): + """Test balance returns none if bst is empty.""" + assert bst.balance() == 'There are no nodes in this tree.' + + +def test_balance_returns_int(bst_big): + """Test balancde returns int..""" + assert isinstance(bst_big.balance(), int) + + +def test_balance_returns_int_of_r_minus_l_of_tree(bst_big): + """Test balance returns int of left minus right sides of tree.""" + assert bst_big.balance() == 1 + + +def test_balance_returns_int_of_r_minus_l_of_tree_two(bst_full, bst): + """Test balance returns int of left minus right sides of tree.""" + bst.insert(2) + bst.insert(1) + bst.insert(3) + assert bst.balance() == 0 + assert bst_full.balance() == 1 + + +def test_balance_returns_int_of_r_minus_l_of_tree_three(bst): + """Test balance returns int of left minus right sides of tree.""" + bst.insert(1) + bst.insert(2) + bst.insert(3) + bst.insert(4) + assert bst.balance() == 2 + + +def test_breadth_first_returns_object(bst_big): + """Test breadth first returns generator object.""" + b = bst_big.breadth_first() + assert isinstance(b, object) + + +def test_breadth_first_is_valid_generator(bst_big): + """Test breadth first returns valid generator.""" + g = bst_big.breadth_first() + assert next(g) == 50 + + +def test_breadth_first_return_children_l_to_r(bst_big): + """Test breadth first returns all children l to r.""" + g = bst_big.breadth_first() + output = [] + for i in range(10): + output.append(next(g)) + assert output == [50, 40, 68, 10, 110, 1, 18, 80, 500, 5000] + + +def test_breadth_first_on_empty_bst_raises_value_error(bst): + """Test breadth first search raises value error if bst empty.""" + g = bst.breadth_first() + with pytest.raises(ValueError): + next(g) + + +def test_in_order_returns_object(bst): + """Test in order returns object.""" + g = bst.in_order() + assert isinstance(g, object) + + +def test_in_order_is_valid_generator(bst_big): + """Test in order returns valid generator.""" + g = bst_big.in_order() + assert next(g) == 1 + + +def test_in_order_returns_tree_in_ascending_order(bst_big): + """Test in order returns ordered vals.""" + g = bst_big.in_order() + output = [] + for i in range(10): + output.append(next(g)) + assert output == [1, 10, 18, 40, 50, 68, 80, 110, 500, 5000] + + +def test_in_order_on_empty_bst_raises_value_error(bst): + """Test in order search raises value error if bst empty.""" + g = bst.in_order() + with pytest.raises(ValueError): + next(g) + + +def test_pre_order_retuns_object(bst): + """Test pre order returns object.""" + g = bst.pre_order() + assert isinstance(g, object) + + +def test_pre_order_returns_valid_generator(bst_big): + """Test pre order returns valid generator object.""" + g = bst_big.pre_order() + assert next(g) == 50 + + +def test_pre_order_returns_left_side_of_all_nodes_first(bst_big): + """Test pre order returns left side of each node first.""" + g = bst_big.pre_order() + output = [] + for i in range(10): + output.append(next(g)) + assert output == [50, 40, 10, 1, 18, 68, 110, 80, 500, 5000] + + +def test_pre_order_returns_proper_order_unabalances(bst_full): + """Test pre order returns proper order from unbalanced tree.""" + g = bst_full.pre_order() + output = [] + for i in range(3): + output.append(next(g)) + assert output == [3254, 908543, 58490543] + + +def test_pre_order_on_empty_bst_raises_value_error(bst): + """Test pre order search raises value error if bst empty.""" + g = bst.pre_order() + with pytest.raises(ValueError): + next(g) + + +def test_post_order_returns_object(bst): + """Test post order returns object.""" + g = bst.post_order() + assert isinstance(g, object) + + +def test_post_order_returns_valid_generator(bst_big): + """Test post order returns valid generator object.""" + g = bst_big.post_order() + assert next(g) == 1 + + +def test_post_order_returns_root_last(bst_big): + """Test post order returns left side then right with root in middle.""" + g = bst_big.post_order() + output = [] + for i in range(10): + output.append(next(g)) + assert output[-1] == 50 + + +def test_post_order_returns_proper_order_unbalanced(bst_full): + """Test post order returns proper order on unbalanced tree.""" + g = bst_full.post_order() + output = [] + for i in range(3): + output.append(next(g)) + assert output == [58490543, 908543, 3254] + + +def test_post_order_returns_proper_order_on_balanced(bst_big): + """Test post order returns proper order on balanced tree.""" + g = bst_big.post_order() + output = [] + for i in range(10): + output.append(next(g)) + assert output == [1, 18, 10, 40, 80, 5000, 500, 110, 68, 50] + + +def test_post_order_on_empty_bst_raises_value_error(bst): + """Test post order search raises value error if bst empty.""" + g = bst.post_order() + with pytest.raises(ValueError): + next(g) + + +def test_delete_empty_tree_returns_none(bst): + """Test delete on empty tree returns none.""" + assert bst.delete(5) is None + + +def test_delete_on_tree_with_only_root(bst): + """Test delete on tree with only root node.""" + bst.insert(5) + bst.delete(5) + assert bst.size() == 0 + assert bst.search(5) is None + + +def test_delete_on_root_in_large_bst(bst_big): + """Test delete on root node in large bst.""" + bst_big.delete(50) + output = [] + b_output = [] + o = bst_big.in_order() + b = bst_big.breadth_first() + for i in range(9): + output.append(next(o)) + b_output.append(next(b)) + assert bst_big.search(50) is None + assert 50 not in output + assert b_output == [68, 40, 110, 10, 80, 500, 1, 18, 5000] + + +def test_delete_on_node_with_no_chirdren(bst_big): + """Test delete on node with no children.""" + bst_big.delete(5000) + output = [] + b_output = [] + o = bst_big.in_order() + b = bst_big.breadth_first() + for i in range(9): + output.append(next(o)) + b_output.append(next(b)) + assert bst_big.search(5000) is None + assert 5000 not in output + assert b_output == [50, 40, 68, 10, 110, 1, 18, 80, 500] + + +def test_delete_on_node_with_one_child_right(bst_big): + """Test delete on node with only one right child.""" + bst_big.delete(500) + output = [] + b_output = [] + o = bst_big.in_order() + b = bst_big.breadth_first() + for i in range(9): + output.append(next(o)) + b_output.append(next(b)) + assert bst_big.search(500) is None + assert 500 not in output + assert b_output == [50, 40, 68, 10, 110, 1, 18, 80, 5000] + + +def test_delete_on_node_with_two_children(bst_big): + """Test delete on node with one left child.""" + bst_big.delete(10) + output = [] + b_output = [] + o = bst_big.in_order() + b = bst_big.breadth_first() + for i in range(9): + output.append(next(o)) + b_output.append(next(b)) + assert bst_big.search(10) is None + assert 10 not in output + assert b_output == [50, 40, 68, 18, 110, 1, 80, 500, 5000] + + +def test_delete_on_root_unbalanced_left(bst): + """Test delete when only left nodes.""" + bst.insert(100) + bst.insert(80) + bst.insert(60) + bst.insert(40) + bst.delete(100) + assert bst.search(100) is None + assert bst.size() == 3 + + +def test_delete_on_root_on_unbalanced_right(bst): + """Test unbalanced when only right nodes.""" + bst.insert(40) + bst.insert(60) + bst.insert(80) + bst.insert(100) + bst.delete(40) + assert bst.search(40) is None + assert bst.size() == 3 + + +def test_delete_back_to_back(bst_big): + """Test delete multiple times.""" + bst_big.delete(110) + bst_big.delete(18) + bst_big.delete(50) + output = [] + o = bst_big.in_order() + for i in range(7): + output.append(next(o)) + assert 110 not in output + assert 18 not in output + assert 50 not in output + assert output == [1, 10, 40, 68, 80, 500, 5000] + assert bst_big.search(110) is None + assert bst_big.search(18) is None + assert bst_big.search(50) is None + + +def test_delete_one_child_parent_greater(bst_big): + """Test delete node with one child when parent is greater.""" + bst_big.delete(40) + assert bst_big.search(40) is None + + +def test_delete_two_children_if_next_has_one_child(bst_big): + """Test delete node with one child when parent is greater.""" + bst_big.insert(85) + bst_big.insert(55) + bst_big.delete(68) + assert bst_big.search(68) is None + + +def test_delete_two_children_if_next_no_child(bst_big): + """Test delete node with one child when parent is greater.""" + bst_big.insert(55) + bst_big.delete(68) + assert bst_big.search(68) is None diff --git a/src/test_graph.py b/src/test_graph.py new file mode 100644 index 0000000..ea9aae0 --- /dev/null +++ b/src/test_graph.py @@ -0,0 +1,425 @@ +"""Testing graph.py.""" +import pytest + + +def test_graph_iinitialize_empty_dict(g): + """Test initialize an empty pq.""" + assert g._graph == {} + + +def test_add_node(g): + """Test it adds note.""" + g.add_node(5) + assert g._graph[5] == {} + + +def test_nodes_returns_list_of_all_nodes(g): + """Test nodes return list of all nodes.""" + g.add_node(5) + g.add_node(7) + g.add_node(8) + assert 5 in g.nodes() + assert 7 in g.nodes() + assert 8 in g.nodes() + assert type(g.nodes()) == list + + +def test_edges_return_dict_of_tuples_of_all_edge(g): + """Test if edges returns dict of tuples as edge and weight as value.""" + g.add_node(5) + g.add_node(7) + g.edges() + assert g.edges() == {} + + +def test_add_edges_returns_dict_of_edges_as_tuple_keys(g): + """Test if edges are added if 2 val are given with default weight.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + assert g.edges() == {(5, 7): 0} + + +def test_add_two_edges(g): + """Test if edges are added if 2 val are given.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_edge(5, 7) + g.add_edge(7, 9) + assert g.edges() == {(5, 7): 0, (7, 9): 0} + + +def test_add_edges_will_add_mutipule_edges_to_a_node(g): + """Test if edges are added if 2 val are given.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_node(10) + g.add_edge(5, 7) + g.add_edge(5, 9) + g.add_edge(5, 10) + assert g.edges() == {(5, 7): 0, (5, 9): 0, (5, 10): 0} + + +def test_add_edges_with_one_val_in_graph_add_second_val(g): + """Test if edges are added if 2 val are given.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_node(10) + g.add_edge(5, 7) + g.add_edge(5, 9) + g.add_edge(5, 15) + assert g.edges() == {(5, 7): 0, (5, 9): 0, (5, 15): 0} + + +def test_add_edges_with_first_val_new_and_second_val_in_graph(g): + """Test if edges are added if 2 val are given.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_node(10) + g.add_edge(5, 7) + g.add_edge(5, 9) + g.add_edge(15, 5) + assert (15, 5) in g.edges() + assert (5, 7) in g.edges() + assert (5, 9) in g.edges() + + +def test_add_edges_with_two_new_nodes(g): + """Test add edges when both nodes are new to graph.""" + g.add_edge(19, 100) + assert g.edges() == {(19, 100): 0} + + +def test_del_node_deletes_the_node_given(g): + """Test del node delets the node.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.del_node(7) + assert 7 not in g.nodes() + + +def test_del_node_raises_value_error(g): + """Test del node raises value error if node has been deleted.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.del_node(5) + g.del_node(7) + g.del_node(9) + with pytest.raises(ValueError): + g.del_node(5) + + +def test_del_edge_deletes_all_edges(g): + """Test if del_edges deletes all edges.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + g.del_edge(5, 7) + assert g.edges() == {} + + +def test_del_edge_raises_value_error_if_too_many_edges_deleted(g): + """Test if del_edges raises value error.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + g.del_edge(5, 7) + with pytest.raises(ValueError): + g.del_edge(5, 7) + + +def test_del_edge_raises_value_error_if_values_not_in_dict(g): + """Test if del_edges raises value error for values not in dict.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + g.del_edge(5, 7) + with pytest.raises(ValueError): + g.del_edge(9, 0) + + +def test_has_node_is_false_on_empty_graph(g): + """Test false if there are no nodes in graph.""" + assert g.has_node(2) is False + + +def test_has_node_if_value_in_graph(g): + """Test false if node does not exist.""" + g.add_node(5) + assert g.has_node(5) is True + + +def test_neighbors_return_list_of_nodes_connected_to_input(g): + """Test neighbor retutns the list of nodes connected input value.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 9) + g.add_edge(5, 8) + assert sorted(g.neighbors(5)) == [7, 8, 9] + + +def test_neighbors_raises_valueerror_if_node_not_exist(g): + """Test neighbor raises value error if input not a node.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 9) + with pytest.raises(ValueError): + g.neighbors(20) + + +def test_adjacent_if_two_nodes_have_edge_is_true(g): + """Test adjacent has edges returns true when edge exists.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7) + assert g.adjacent(5, 7) is True + + +def test_adjacent_when_edge_when_no_edges(g): + """Test adjacent has no edges returns false.""" + g.add_node(5) + g.add_node(7) + assert g.adjacent(5, 7) is False + + +def test_adjacent_raises_valueerror_if_input_not_node(g): + """Test adjacent raises value error if node does not exist.""" + g.add_node(5) + g.add_node(7) + with pytest.raises(ValueError): + g.adjacent(9, 0) + + +def test_depth_first_one_node_many_edges(g): + """Test depth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_node(7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 6) + g.add_edge(5, 7) + g.add_edge(5, 8) + g.add_edge(5, 9) + assert 5 in g.depth_first_traversal(5) + assert 9 in g.depth_first_traversal(5) + assert 6 in g.depth_first_traversal(5) + assert 7 in g.depth_first_traversal(5) + assert 8 in g.depth_first_traversal(5) + assert type(g.depth_first_traversal(5)) == list + + +def test_depth_first_node_no_edge_return_only_self(g): + """Test depth first traversal returns start val if no edges.""" + g.add_node(8) + g.add_node(9) + g.add_node(10) + g.add_edge(10, 8) + assert g.depth_first_traversal(8) == [8] + + +def test_depth_first_one_edge_per_node(g): + """Test depth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_node(7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 6) + g.add_edge(6, 7) + g.add_edge(7, 8) + g.add_edge(8, 9) + assert g.depth_first_traversal(5) == [5, 6, 7, 8, 9] + + +def test_df_raises_value_error_node_not_exist(g): + """Test df traversal raises value error if val not in graph.""" + with pytest.raises(ValueError): + g.depth_first_traversal('blerg') + + +def test_depth_first_many_node_many_edges(g): + """Test depth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_node(7) + g.add_node(8) + g.add_node(9) + g.add_edge(8, 9) + g.add_edge(8, 5) + g.add_edge(5, 9) + g.add_edge(6, 9) + g.add_edge(7, 8) + g.add_edge(9, 5) + g.add_edge(9, 6) + g.add_edge(9, 8) + g.add_edge(6, 5) + g.add_edge(7, 5) + g.add_edge(6, 8) + dt = g.depth_first_traversal(8) + assert dt == [8, 5, 9, 6] or dt == [8, 9, 6 , 5] or dt == [8, 9, 5, 6] + + +def test_breadth_first_one_node_many_edges(g): + """Test breadth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_node(7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 6) + g.add_edge(5, 7) + g.add_edge(5, 8) + g.add_edge(5, 9) + assert 5 in g.breadth_first_traversal(5) + assert 9 in g.breadth_first_traversal(5) + assert 6 in g.breadth_first_traversal(5) + assert 7 in g.breadth_first_traversal(5) + assert 8 in g.breadth_first_traversal(5) + assert type(g.breadth_first_traversal(5)) == list + + +def test_breadth_first_node_no_edge_return_only_self(g): + """Test breadth first traversal returns start val if no edges.""" + g.add_node(8) + g.add_node(9) + g.add_node(10) + g.add_edge(10, 8) + assert g.depth_first_traversal(8) == [8] + + +def test_depth_first_one_edge_per_node(g): + """Test depth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_node(7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 6) + g.add_edge(6, 7) + g.add_edge(7, 8) + g.add_edge(8, 9) + assert g.depth_first_traversal(5) == [5, 6, 7, 8, 9] + + +def test_bf_raises_value_error_node_not_exist(g): + """Test bf traversal raises value error if val not in graph.""" + with pytest.raises(ValueError): + g.depth_first_traversal('blerg') + + +def test_bf_first_many_node_many_edges(g): + """Test bf first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_node(7) + g.add_node(8) + g.add_node(9) + g.add_edge(5, 9) + g.add_edge(6, 9) + g.add_edge(7, 8) + g.add_edge(8, 5) + g.add_edge(9, 5) + g.add_edge(9, 6) + g.add_edge(9, 8) + g.add_edge(6, 5) + g.add_edge(7, 5) + g.add_edge(8, 9) + g.add_edge(6, 8) + assert 5 in g.depth_first_traversal(8) + assert 9 in g.depth_first_traversal(8) + assert 6 in g.depth_first_traversal(8) + assert 5 in g.depth_first_traversal(8) + # assert g.depth_first_traversal(8) == [8, 9, 6, 5] + + +def test_breadth_first_works_on_cyclic_graph(g): + """Test breadth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_edge(5, 6) + g.add_edge(6, 5) + assert g.breadth_first_traversal(5) == [5, 6] + + +def test_depth_first_works_on_cyclic_graph(g): + """Test breadth first traversal returns all paths from one node.""" + g.add_node(5) + g.add_node(6) + g.add_edge(5, 6) + g.add_edge(6, 5) + assert g.breadth_first_traversal(5) == [5, 6] + + +def test_add_edges_w_weights_returns_dict_of_edges_as_tuple_keys(g): + """Test if edges are added if 2 val are given with default weight.""" + g.add_node(5) + g.add_node(7) + g.add_edge(5, 7, 6) + assert g.edges() == {(5, 7): 6} + + +def test_add_two_edges_w_weights_(g): + """Test if edges are added if 2 val are given with weights returns dict.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_edge(5, 7, 100) + g.add_edge(7, 9, 1) + assert g.edges() == {(5, 7): 100, (7, 9): 1} + + +def test_add_edges_w_weights_will_add_mutipule_edges_to_a_node(g): + """Test if edges are added if 2 val are given with weights.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_node(10) + g.add_edge(5, 7, 4) + g.add_edge(5, 9) + g.add_edge(5, 10, 12039) + assert g.edges() == {(5, 7): 4, (5, 9): 0, (5, 10): 12039} + + +def test_add_edges_with_weights_with_one_val_in_graph_add_second_val(g): + """Test if edges are added if 2 val are given.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_node(10) + g.add_edge(5, 7, 0) + g.add_edge(5, 9, 3) + g.add_edge(5, 15, 6) + assert g.edges() == {(5, 7): 0, (5, 9): 3, (5, 15): 6} + + +def test_add_edges_w_weights_with_first_val_new_and_second_val_in_graph(g): + """Test if edges are added if 2 val are given.""" + g.add_node(5) + g.add_node(7) + g.add_node(9) + g.add_node(10) + g.add_edge(5, 7, 4) + g.add_edge(5, 9, 7) + g.add_edge(15, 5, 9) + assert (15, 5) in g.edges() + assert (5, 7) in g.edges() + assert (5, 9) in g.edges() + + +def test_add_edges_with_weights_with_two_new_nodes(g): + """Test add edges when both nodes are new to graph.""" + g.add_edge(19, 100, 1000) + assert g.edges() == {(19, 100): 1000} diff --git a/src/test_shortest_dis.py b/src/test_shortest_dis.py new file mode 100644 index 0000000..5498915 --- /dev/null +++ b/src/test_shortest_dis.py @@ -0,0 +1,111 @@ +"""Test for Shorest distance graph.""" +from graph import Graph + +import pytest + +from shortest_dis import dijkstra, shortest_distance + + +graph = { + 'A': {'C': 4, + 'B': 2, + 'D': 5 + }, + 'C': {'G': 20, + 'D': 8 + }, + 'B': {'E': 8 + }, + 'D': {'C': 4, + 'B': 3, + 'F': 10, + 'G': 15 + }, + 'E': {'F': 8, + 'D': 8 + }, + 'F': {'G': 5 + }, + 'G': {} +} + +test_graph = { + 'A': {'B': 5, + 'C': 6 + }, + 'B': {'D': 2 + }, + 'C': {'E': 8, + 'F': 4 + }, + 'D': {'E': 2, + 'G': 10 + }, + 'E': {'G': 10, + 'F': 4 + }, +} + + +def test_dijkstra_returns_best_parents(): + """Test dijkstra returns each nodes best parents.""" + g = Graph() + g._graph = test_graph + path = dijkstra(g, 'A', 'E') + assert path == {'B': 'A', 'C': 'A', 'D': 'B', 'E': 'D', 'F': 'C', 'G': 'D'} + + +def test_dijkstra_returns_best_parents_on_diffrent_path(): + """Test dijkstra returns each nodes best parents.""" + g = Graph() + g._graph = test_graph + path = dijkstra(g, 'A', 'F') + assert path == {'B': 'A', 'C': 'A', 'D': 'B', 'E': 'D', 'F': 'C', 'G': 'D'} + + +def test_dijkstra_returns_best_parents_of_the_node(): + """Test dijkstra returns each nodes best parents.""" + g = Graph() + g._graph = graph + path = dijkstra(g, 'A', 'E') + assert path == {'B': 'A', 'C': 'A', 'D': 'A', 'E': 'B', 'F': 'D', 'G': 'D'} + + +def test_shortest_distance(): + """Test for Shortest path from start node to end node.""" + g = Graph() + g._graph = graph + path = shortest_distance(g, 'A', 'F') + assert path == ['A', 'D', 'F'] + + +def test_shortest_distance_returns_list(): + """Test for Shortest path from start node to end node.""" + g = Graph() + g._graph = test_graph + path = shortest_distance(g, 'A', 'E') + assert path == ['A', 'B', 'D', 'E'] + + +def test_shortest_distance_returns_list_from_path_a_g(): + """Test for Shortest path from start node to end node.""" + g = Graph() + g._graph = test_graph + path = shortest_distance(g, 'A', 'G') + assert path == ['A', 'B', 'D', 'G'] + + +def test_dijkstra_that_start_no_children_return_emty_dict(): + """Test dijkstra returns each nodes best parents.""" + g = Graph() + g._graph = graph + path = dijkstra(g, 'G', 'E') + assert path == {} + + +def test_shortest_distance_raises_valueerror_if_start_has_no_edges(): + """Test shortest distance raises ValueError if start has no edges.""" + g = Graph() + g._graph = test_graph + with pytest.raises(ValueError): + shortest_distance(g, 'G', 'A') diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..06c5283 --- /dev/null +++ b/tox.ini @@ -0,0 +1,9 @@ +[tox] + +envlist = py27, py36 + +[testenv] +commands = py.test src --cov=src --cov-report term-missing +deps = + pytest + pytest-cov \ No newline at end of file