|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "%load_ext watermark" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 2, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [ |
| 17 | + { |
| 18 | + "name": "stdout", |
| 19 | + "output_type": "stream", |
| 20 | + "text": [ |
| 21 | + "Sebastian Raschka \n", |
| 22 | + "last updated: 2016-06-08 \n", |
| 23 | + "\n", |
| 24 | + "CPython 3.5.1\n", |
| 25 | + "IPython 4.2.0\n" |
| 26 | + ] |
| 27 | + } |
| 28 | + ], |
| 29 | + "source": [ |
| 30 | + "%watermark -a 'Sebastian Raschka' -u -d -v" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "# More Greedy Algorithm Examples" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "markdown", |
| 42 | + "metadata": {}, |
| 43 | + "source": [ |
| 44 | + "For an introduction to greedy algorithm, see the related notebook, [Introduction to Greedy Algorithms](greedy-algorithm-intro.ipynb)." |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "markdown", |
| 49 | + "metadata": {}, |
| 50 | + "source": [ |
| 51 | + "## Set Cover Problems" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "Set cover problems are problems where we want to find the minimum number of subsets such that their set union contains all items in a target set. This target set is typically called the \"universe.\" To borrow an example from the excellent [Wikipedia page](https://en.wikipedia.org/wiki/Set_cover_problem) on set cover problems, let's assume we have the universe \n", |
| 59 | + "\n", |
| 60 | + "- $U=\\{1, 2, 3, 4, 5\\}$\n", |
| 61 | + "\n", |
| 62 | + "and are given the collection of sets \n", |
| 63 | + "\n", |
| 64 | + "- $C=\\{\\{1, 2, 3\\}, \\{2, 4\\}, \\{3, 4\\}, \\{4, 5\\}\\}$\n", |
| 65 | + "\n", |
| 66 | + "The task is to find the minimum number of sets in $C$ so that their union equals $U$.\n", |
| 67 | + "\n", |
| 68 | + "Note that set cover problems are NP-complete, thus no computationally efficient solution exists. However, we can use greedy algorithms to approximate the solution; this solution may or may not be globally optimal.\n", |
| 69 | + "\n", |
| 70 | + "The greedy strategy we are going to employ is very simple and works as follows:\n", |
| 71 | + "\n", |
| 72 | + "- While not all elements in U are covered:\n", |
| 73 | + " - For all uncovered sets in C:\n", |
| 74 | + " - Pick the set that covers most of the elements in U" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": 3, |
| 80 | + "metadata": {}, |
| 81 | + "outputs": [ |
| 82 | + { |
| 83 | + "name": "stdout", |
| 84 | + "output_type": "stream", |
| 85 | + "text": [ |
| 86 | + "['set_1', 'set_2', 'set_4']\n" |
| 87 | + ] |
| 88 | + } |
| 89 | + ], |
| 90 | + "source": [ |
| 91 | + "collection = {'set_1': {1, 2, 3},\n", |
| 92 | + " 'set_2': {2, 4}, \n", |
| 93 | + " 'set_3': {3, 4}, \n", |
| 94 | + " 'set_4': {4, 5}}\n", |
| 95 | + "sets_used = []\n", |
| 96 | + "elements_not_covered = {1, 2, 3, 4, 5}\n", |
| 97 | + "\n", |
| 98 | + "\n", |
| 99 | + "while elements_not_covered:\n", |
| 100 | + " elements_covered = set()\n", |
| 101 | + " for set_ in collection.keys():\n", |
| 102 | + " \n", |
| 103 | + " if set_ in sets_used:\n", |
| 104 | + " continue\n", |
| 105 | + " \n", |
| 106 | + " current_set = collection[set_]\n", |
| 107 | + " would_cover = elements_covered.union(current_set)\n", |
| 108 | + " if len(would_cover) > len(elements_covered):\n", |
| 109 | + " elements_covered = would_cover\n", |
| 110 | + " sets_used.append(set_)\n", |
| 111 | + " elements_not_covered -= elements_covered\n", |
| 112 | + " \n", |
| 113 | + " if not elements_not_covered:\n", |
| 114 | + " break\n", |
| 115 | + " \n", |
| 116 | + "print(sets_used)" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "markdown", |
| 121 | + "metadata": {}, |
| 122 | + "source": [ |
| 123 | + "As a result, we can see that 3 sets are required to cover the universe U. In this case, the greedy algorithm has not found the globally optimal solution, which would be `'set_1'` and `'set_4'`. Note that this is just a trivial example, and greedy algorithms can be very useful approximators for solutions that are computationally infeasible.\n", |
| 124 | + "\n", |
| 125 | + "For instance, an exhaustive solution to this problem that would guaranteed to find the global optimum (remember that set cover problems are NP-complete) would involve iterating over a power set, which has $2^n$ elements, where $n$ is the number of sets in the collection. For example, a collection of 30 sets would already require comparing the solutions of $2^{30}=1,073,741,824$ million possible combinations!\n", |
| 126 | + "\n", |
| 127 | + "(Note that the greedy approach may have found the globally optimal solution in this simple example if it had iterated over the dictionary in a different order -- for example, if we had swapped the positions of {2, 4} and {4, 5})" |
| 128 | + ] |
| 129 | + } |
| 130 | + ], |
| 131 | + "metadata": { |
| 132 | + "kernelspec": { |
| 133 | + "display_name": "Python 3", |
| 134 | + "language": "python", |
| 135 | + "name": "python3" |
| 136 | + }, |
| 137 | + "language_info": { |
| 138 | + "codemirror_mode": { |
| 139 | + "name": "ipython", |
| 140 | + "version": 3 |
| 141 | + }, |
| 142 | + "file_extension": ".py", |
| 143 | + "mimetype": "text/x-python", |
| 144 | + "name": "python", |
| 145 | + "nbconvert_exporter": "python", |
| 146 | + "pygments_lexer": "ipython3", |
| 147 | + "version": "3.6.1" |
| 148 | + } |
| 149 | + }, |
| 150 | + "nbformat": 4, |
| 151 | + "nbformat_minor": 1 |
| 152 | +} |
0 commit comments