{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "11aa5518", "metadata": {}, "outputs": [], "source": [ "import random\n", "\n", "def random_request():\n", " \"generates a single random request\"\n", " return sorted(random.sample(range(0,100), 2))\n", "\n", "def make_requests(n):\n", " \"generates n random requests\"\n", " # slower version\n", " #requests = []\n", " #for i in range(n):\n", " # requests.append(random_request())\n", " #return requests\n", " # faster version:\n", " return [random_request() for i in range(n)]\n", "\n", "def greedy_solution(requests):\n", " \"\"\"\n", " input is a set of requests, output is the\n", " greedy solution where best = earliest end\n", " time\n", " \"\"\"\n", " sorted_requests = sorted(requests, key=lambda x : x[1])\n", " \n", " solution = []\n", " solution.append(sorted_requests.pop(0))\n", " # when you call L.pop(i) for a list L, it removes\n", " # the element at index i and returns it for you\n", " \n", " # TODO: remove conflicts with this chosen meeting\n", " # and repeat until there are no sorted_requests\n", " while len(sorted_requests) > 0:\n", " # look at the first thing in sorted_requests\n", " # if it conflicts with a meeting we already accepted,\n", " # throw it away\n", " # otherwise, add it to our solution\n", " request = sorted_requests.pop(0)\n", " # is it good?\n", " if request[0] >= solution[-1][1]:\n", " # request[0] is the start of the request we're considering\n", " # solution[-1] is the last request we have in our solution\n", " # (solution[-1])[1] is its end time\n", " solution.append(request)\n", " \n", " return solution\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "274ea735", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[52, 82], [89, 95], [84, 87], [36, 73], [42, 57], [48, 59], [46, 63], [76, 94], [4, 53], [37, 41]]\n" ] } ], "source": [ "requests = make_requests(10)\n", "print(requests)" ] }, { "cell_type": "code", "execution_count": 4, "id": "712c9513", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[37, 41], [42, 57], [84, 87], [89, 95]]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "greedy_solution(requests)" ] }, { "cell_type": "code", "execution_count": 11, "id": "d1d0d130", "metadata": {}, "outputs": [], "source": [ "def plot_requests(requests):\n", " for r in sorted(requests, key=lambda x : x[1]):\n", " print(\" \"*(r[0]) + \"-\"*(r[1]-r[0]))" ] }, { "cell_type": "code", "execution_count": 13, "id": "6b43c92d", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " ---\n", " ---\n", " ---------\n" ] } ], "source": [ "plot_requests([[2,5],[4,7],[1,10]])" ] }, { "cell_type": "code", "execution_count": 26, "id": "dcfb1d3b", "metadata": {}, "outputs": [], "source": [ "R = make_requests(100_000)" ] }, { "cell_type": "code", "execution_count": 27, "id": "f517a23d", "metadata": {}, "outputs": [], "source": [ "#plot_requests(R)" ] }, { "cell_type": "code", "execution_count": 28, "id": "39cb764d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15], [15, 16], [16, 17], [17, 18], [18, 19], [19, 20], [20, 21], [21, 22], [22, 23], [23, 24], [24, 25], [25, 26], [26, 27], [27, 28], [28, 29], [29, 30], [30, 31], [31, 32], [32, 33], [33, 34], [34, 35], [35, 36], [36, 37], [37, 38], [38, 39], [39, 40], [40, 41], [41, 42], [42, 43], [43, 44], [44, 45], [45, 46], [46, 47], [47, 48], [48, 49], [49, 50], [50, 51], [51, 52], [52, 53], [53, 54], [54, 55], [55, 56], [56, 57], [57, 58], [58, 59], [59, 60], [60, 61], [61, 62], [62, 63], [63, 64], [64, 65], [65, 66], [66, 67], [67, 68], [68, 69], [69, 70], [70, 71], [71, 72], [72, 73], [73, 74], [74, 75], [75, 76], [76, 77], [77, 78], [78, 79], [79, 80], [80, 81], [81, 82], [82, 83], [83, 84], [84, 85], [85, 86], [86, 87], [87, 88], [88, 89], [89, 90], [90, 91], [91, 92], [92, 93], [93, 94], [94, 95], [95, 96], [96, 97], [97, 98], [98, 99]]\n", "99\n" ] } ], "source": [ "A = greedy_solution(R)\n", "print(A)\n", "print(len(A))" ] }, { "cell_type": "code", "execution_count": 29, "id": "d059f9aa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n", " -\n" ] } ], "source": [ "plot_requests(A)" ] }, { "cell_type": "code", "execution_count": null, "id": "d7aa5d57", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.1" } }, "nbformat": 4, "nbformat_minor": 5 }