{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load_ext watermark" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sebastian Raschka \n", "last updated: 2016-08-02 \n", "\n", "CPython 3.5.1\n", "IPython 5.0.0\n" ] } ], "source": [ "%watermark -a 'Sebastian Raschka' -u -d -v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Queues" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Queues are one of the basic, linear datastructures that have the characteristical 2 end points (here: a *front* and an *end*). Queues belong to the so-called **FIF** (*first in, first out*) data structures, which means that the first element that has been added is the the first to be removed (in contrast to the *last in, first out* LIFO principle that is implemented in stacks). So, if we `enqueue` an item to a queue, we add it to its end, and if we dequeue an item, we remove it from the front:\n", "\n", "![](images/queue-01.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Basically, you can picture a queue as a line of people waiting in front of the cahsier at the supermarket. A common application of queues is the PBS/TORQUE scheduler for managing jobs on a computing cluster." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Queue(object):\n", " \n", " def __init__(self):\n", " self.data = []\n", " \n", " def enqueue(self, item):\n", " self.data.insert(0, item)\n", " \n", " def dequeue(self):\n", " return self.data.pop()\n", " \n", " def show_front(self):\n", " return seld.data[-1]\n", "\n", " def show_end(self):\n", " return seld.data[0]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1]\n", "['a', 1]\n", "[3, 'a', 1]\n", "1\n", "a\n", "3\n", "[]\n" ] } ], "source": [ "q = Queue()\n", "q.enqueue(1)\n", "print(q.data)\n", "\n", "q.enqueue('a')\n", "print(q.data)\n", "\n", "q.enqueue(3)\n", "print(q.data)\n", "\n", "print(q.dequeue())\n", "print(q.dequeue())\n", "print(q.dequeue())\n", "print(q.data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we used a simple python `list` object to implement a primitive example of a queue. The disadvantage of this is that one of the two operations (either enqueue or dequeue) will have a time complexity of O(n), since we have to either delete from the beginning or insert at the beginning of the `list`. A better way to implement a queue would be to use a doubly linked list." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Deques" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Deques (not to be confused with the \"dequeue\" operation in queues) is a datastructure that is closely related to queues. Deque simply stands for \"double-ended queue,\" and as the name implies, it allows us to add and remove items at both sides of a queue." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/deque-01.png)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Deque(object):\n", " \n", " def __init__(self):\n", " self.data = []\n", " \n", " def add_front(self, item):\n", " self.data.insert(0, item)\n", " \n", " def remove_front(self):\n", " return self.data.pop(0)\n", " \n", " def add_end(self, item):\n", " self.data.append(item)\n", " \n", " def remove_end(self):\n", " return self.data.pop()\n", " \n", " def show_front(self):\n", " return seld.data[-1]\n", "\n", " def show_end(self):\n", " return seld.data[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, because of the nature of Python lists, the `remove_front` and `add_front` become O(n) operations." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }