diff --git a/content_based_main.ipynb b/content_based_main.ipynb
new file mode 100644
index 0000000..5eacea5
--- /dev/null
+++ b/content_based_main.ipynb
@@ -0,0 +1,200 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "authorship_tag": "ABX9TyPMnz/K3NtBI7UR9n7BgQj6",
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "O_JKThHFZJOs"
+ },
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Here's an example project for a bachelor's student to implement a content-based recommendation system:\n",
+ "\n",
+ "Problem Statement:\n",
+ "Develop a content-based recommendation system for books that suggests books to users based on the books' descriptions, genres, and author information.\n",
+ "\n",
+ "Dataset:\n",
+ "Use the following sample book data for this project:\n",
+ "\n",
+ "Book\t Author\t Genre \tDescription\n",
+ "\n",
+ "B1,\tA1,\tG1,\tD1\n",
+ "\n",
+ "B2,\tA2,\tG2,\tD2\n",
+ "\n",
+ "B3,\tA1,\tG1,\tD3\n",
+ "\n",
+ "B4,\tA2,\tG2,\tD4\n",
+ "\n",
+ "B5,\tA3,\tG3,\tD5\n",
+ "\n",
+ "B6,\tA3,\tG3,\tD6\n",
+ "\n",
+ "Steps:\n",
+ "\n",
+ "Preprocess the book data and create a bag of words representation of the book descriptions.\n",
+ "\n",
+ "Compute the cosine similarity between the books based on the bag of words representation.\n",
+ "\n",
+ "Based on the cosine similarity scores, suggest top k most similar books to the user for a given book.\n",
+ "\n",
+ "Evaluation:\n",
+ "\n",
+ "To evaluate the performance of the recommendation system, you can use the following metrics:\n",
+ "\n",
+ "Precision: The number of recommended items that are relevant to the user divided by the number of total recommended items.\n",
+ "\n",
+ "Recall: The number of recommended items that are relevant to the user divided by the number of all relevant items.\n",
+ "\n",
+ "This project provides a good starting point for a bachelor's student to learn about content-based recommendation systems."
+ ],
+ "metadata": {
+ "id": "KiP00PAiZP12"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "book_dataset = [ {\"title\": \"The Great Gatsby\", \"genre\": \"Fiction\"}, {\"title\": \"To Kill a Mockingbird\", \"genre\": \"Fiction\"}, {\"title\": \"Pride and Prejudice\", \"genre\": \"Fiction\"}, {\"title\": \"The Hitchhiker's Guide to the Galaxy\", \"genre\": \"Science Fiction\"}, {\"title\": \"The Lord of the Rings\", \"genre\": \"Fantasy\"}, {\"title\": \"The Da Vinci Code\", \"genre\": \"Thriller\"}, {\"title\": \"The Catcher in the Rye\", \"genre\": \"Fiction\"}, {\"title\": \"The Hunger Games\", \"genre\": \"Science Fiction\"}, {\"title\": \"Harry Potter and the Philosopher's Stone\", \"genre\": \"Fantasy\"}, {\"title\": \"The Silence of the Lambs\", \"genre\": \"Thriller\"},]\n"
+ ],
+ "metadata": {
+ "id": "ajYkCFz1Znk4"
+ },
+ "execution_count": 1,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Next, we'll create a matrix of book titles and the genres they belong to using a one-hot encoding approach:"
+ ],
+ "metadata": {
+ "id": "lgsljeHHZw0A"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "import pandas as pd\n",
+ "from sklearn.preprocessing import MultiLabelBinarizer\n",
+ "\n",
+ "# Create a list of book titles\n",
+ "book_titles = [book['title'] for book in book_dataset]\n",
+ "\n",
+ "# Create a list of book genres\n",
+ "book_genres = [book['genre'] for book in book_dataset]\n",
+ "\n",
+ "# One-hot encode the genres\n",
+ "mlb = MultiLabelBinarizer()\n",
+ "book_genres_encoded = pd.DataFrame(mlb.fit_transform(book_genres), columns=mlb.classes_, index=book_titles)"
+ ],
+ "metadata": {
+ "id": "_kfgZU0NZxiA"
+ },
+ "execution_count": 2,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Next, we'll create a function to recommend books to a user based on the genres they prefer. We'll use cosine similarity to measure the similarity between the books:"
+ ],
+ "metadata": {
+ "id": "GH2gxTn6Z2eR"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from sklearn.metrics.pairwise import cosine_similarity\n",
+ "\n",
+ "def recommend_books(title, book_genres_encoded, cosine_sim=cosine_similarity(book_genres_encoded), n=5):\n",
+ " recommended_books = []\n",
+ " indices = pd.Series(book_genres_encoded.index)\n",
+ " idx = indices[indices == title].index[0]\n",
+ " score = list(enumerate(cosine_sim[idx]))\n",
+ " score = sorted(score, key=lambda x: x[1], reverse=True)\n",
+ " score = score[1:n+1]\n",
+ " book_indices = [i[0] for i in score]\n",
+ " for i in book_indices:\n",
+ " recommended_books.append(indices[i])\n",
+ " return recommended_books"
+ ],
+ "metadata": {
+ "id": "Gh8jrNYqZ31g"
+ },
+ "execution_count": 3,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Now, we can use the recommend_books function to recommend books to a user based on their preferred genres. For example, if a user likes The Great Gatsby, we can recommend 5 books with similar genres:\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "-t4IRD7sZ8CR"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "recommend_books(\"The Great Gatsby\", book_genres_encoded)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "UbGizsjLZ8rI",
+ "outputId": "8e9f2470-105d-46a9-f89e-9c17dda94033"
+ },
+ "execution_count": 4,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "['To Kill a Mockingbird',\n",
+ " 'Pride and Prejudice',\n",
+ " 'The Catcher in the Rye',\n",
+ " \"The Hitchhiker's Guide to the Galaxy\",\n",
+ " 'The Hunger Games']"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 4
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/data_structure.ipynb b/data_structure.ipynb
new file mode 100644
index 0000000..a5dce79
--- /dev/null
+++ b/data_structure.ipynb
@@ -0,0 +1,644 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The exam consists of 7 problems. Each of them has 10 points\n",
+ "**It is recommended to read the problems in order**, but it is not important\n",
+ "that you solve them in any specific order.\n",
+ "\n",
+ "\n",
+ "● Please read all instructions carefully. You may ask the instructor clarifying\n",
+ "questions during the exam.\n",
+ "\n",
+ "● This is a open book exam.\n",
+ "\n",
+ "● Please silence all cell phones and place them off the table.\n",
+ "\n",
+ "● There are 5 questions each with multiple parts. Partial solutions will be graded for partial credit.\n",
+ "\n",
+ "● Explain your answer in detail for each question.\n",
+ "\n",
+ "● You have 150 min to work on this exam."
+ ],
+ "metadata": {
+ "id": "YixcVplokall"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 1:**\n",
+ "\n",
+ "\n",
+ "a) In order of increasing asymptotic growth rate, list the following functions. Indicate whether two functions have the same asymptotic growth rate. Clearly explain your response.\n",
+ "\n",
+ "$lg n$, $1.1^n$, $n (lg n)$, $n(lg n)^2$, $3 (lg n)$, $2^ 5$, $n^{34}$\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "PRlripdJl3dg"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "1efgDXA1uPzX"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "State whether the following statements are true or false. explanation is needed. \n",
+ "\n",
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $(f(n))^2 = Θ((g(n))^2 )$ "
+ ],
+ "metadata": {
+ "id": "AS1zox4b9QED"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "ov4PvBC3uT58"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $f(n) = g(n)$"
+ ],
+ "metadata": {
+ "id": "kuqgRmoL9lut"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "d4aDJJQXuWMM"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^3 = O(3n^2 )$"
+ ],
+ "metadata": {
+ "id": "3yeHCcQT9uaf"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "4kNpRSquukxb"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^2 = O(2n )$"
+ ],
+ "metadata": {
+ "id": "qnpcj1bj912H"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "n4rE__8Dulxo"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 2:** \n",
+ "Consider **Bucket-sort, Insertion-Sort, Bubble-Sort**. For each algorithm, what will be the worst case asymptotic upper bound on the running time if you know additionally that (For each case and each sorting algorithm, state your answer and justify it in one sentence.)\n",
+ "* the input is already sorted?\n",
+ "\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "rW2PANQNpPzT"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "XWV3dhvssw_D"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* the input is reversely sorted?\n",
+ " \n"
+ ],
+ "metadata": {
+ "id": "AY_unIbNu2Uh"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "5T8AuMLCu4kZ"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* the input is a list containing n copies of the same number? (compare the space complexity also) "
+ ],
+ "metadata": {
+ "id": "EPTpzwdqu34k"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "Zx-nYZOIu92b"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 3:** \n",
+ "\n",
+ "Let's say you've added some elements to a hash table. When you examine it, you may notice that the outcome resembles the image below. You understand this is an issue since you are an excellent student.\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "* What is the problem here? \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "CXb8eWs9qaiM"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "RNzOSdaWvGj6"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give an example of a hash function that could give rise to this behavior. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "8jfGRQ13vHxg"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "zDqF_7-BvJtc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What would be a better hash function? \n"
+ ],
+ "metadata": {
+ "id": "AawD_B8PvLe_"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "7F9nCP56vNNU"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* It has been demonstrated that, in the typical scenario, deletion and search are both $O(1 + n/m)$ under the assumptions of uniform hashing, collision resolution via chaining, and constant time computable hash function. We've also demonstrated that, given one further supposition, this can be reduced to $O(1)$. In one sentence, describe this extra supposition."
+ ],
+ "metadata": {
+ "id": "gs3yD43mvOoS"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "VTbVd0pZvPd_"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 4**: \n",
+ "\n",
+ "Let's say we have a binary search tree with numbers between 1 and 100 and wish to look for the number 45. Which of the following sequences—possibly more than one—could include the nodes being examined? Explain in detail why the sequence can be or cannot be the search order.\n",
+ "\n",
+ "* 5, 2, 1, 10, 39, 34, 77, 63. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "TbTDNP1psS_5"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "J5MmKZBpvfas"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* 1, 2, 3, 4, 5, 6, 7, 8. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "CGxlGYVn-Ozq"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "ccwTZPO3vhJu"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 9, 8, 63, 0, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "WI_DMzQK-RBq"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "YUkRL2YbviSL"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 8, 7, 6, 5, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "XFO2SX3R-TNC"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "2dBYb-CHvkW8"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44, 42. \n"
+ ],
+ "metadata": {
+ "id": "Dt40Rch9-V9S"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "qJ4y3hdRvlXQ"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44. "
+ ],
+ "metadata": {
+ "id": "g8XDfOfL-Xtw"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "nAbdWGJzvm6n"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 5:** \n",
+ "Answer each of the following questions. Keep your answers brief. \n",
+ "\n",
+ "***Example)*** Give an example of a real-world situation where a stack works better as a model than a queue. Why is a stack preferable to a queue in this situation?\n",
+ "\n",
+ "***Answer:*** A stack is a better model for trying to match parenthesis in an expression than a queue. The parenthesis that needs to be matched is always kept at the top of the stack. However, the first parenthesis in a queue is the only one that is stored there, therefore it cannot be used for matching. \n",
+ "\n",
+ "* Describe a real-world situation where a queue works better as a model than a stack. Why is a queue superior to a stack in this situation?\n",
+ " "
+ ],
+ "metadata": {
+ "id": "W46brp6s6j09"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "0v3RVJQ4vrp0"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give an example of a real-world situation where a fixed-sized array would be a better model than a linked list. Describe why an array is preferable to a linked list in this situation.\n"
+ ],
+ "metadata": {
+ "id": "Jp67vpco-cuH"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "PoaUgpKhvtIr"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* Give an example of a real-world situation where a doubly linked list would be a better model than a binary search tree. Describe the advantages of a doubly linked list over a binary search tree in this situation."
+ ],
+ "metadata": {
+ "id": "VpD5wKzU-eo1"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "Ca-jMgZBvvB8"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 6:** \n",
+ "\n",
+ "Given the following directed graph answer the following questions:\n",
+ "\n",
+ ""
+ ],
+ "metadata": {
+ "id": "DR9DSPOY-gWN"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give one possible BFS traversal, beginning at node 0, printing the nodes both as they are found and when they are reached. The graph is directed, as shown by the edge from 8 to 4 but not from 4 to 8; this is only one example."
+ ],
+ "metadata": {
+ "id": "XDjKEfEb_u6-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "NKb4OucrvxmF"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Draw the Adjacency matrix and list. Comparet them and explain which of them is better to use."
+ ],
+ "metadata": {
+ "id": "KdViaTGhAAI4"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "uG8d5Xd5v0eP"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 7:**\n",
+ "\n",
+ "Consider a hashtable implementation using separate chaining of unsorted linked lists with N buckets and k items currently in the table. \n",
+ "\n",
+ "* What is the average number of items in a bucket?"
+ ],
+ "metadata": {
+ "id": "bPvaZp9vAbVF"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "pcMLW_Irv2gy"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* In the worst-case, how many items could be in a single bucket?"
+ ],
+ "metadata": {
+ "id": "EIMgzna_Al8t"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [],
+ "metadata": {
+ "id": "YSEDPL8cv5J3"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Given that k > N, which of the following are true?\n",
+ "\n",
+ "1. Some buckets may be empty\n",
+ "2. Some buckets may be full so that they cannot receive more items \n",
+ "3. Neither\t\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "2DY1zyjuAouX"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "ms6s45GIv6qt"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case running time in terms of k and N for finding the minimum value in the table ? \t"
+ ],
+ "metadata": {
+ "id": "hTluXLbyA22_"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "dyfR_sGqv8Pc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case run time for inserting a new item in the table ?"
+ ],
+ "metadata": {
+ "id": "atGbngRKA5q3"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "evNu0KvHv974"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If we resize the table to a table of size 2N, what is the worst case running time in terms of k and N to put all the items in the new table?"
+ ],
+ "metadata": {
+ "id": "H5MJNPl7A-64"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "K_kzyKk6wABO"
+ }
+ }
+ ]
+}
\ No newline at end of file
diff --git a/data_structure_exam.ipynb b/data_structure_exam.ipynb
new file mode 100644
index 0000000..f75bf98
--- /dev/null
+++ b/data_structure_exam.ipynb
@@ -0,0 +1,600 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The exam consists of 7 problems. Each of them has 10 points\n",
+ "**It is recommended to read the problems in order**, but it is not important\n",
+ "that you solve them in any specific order.\n",
+ "\n",
+ "\n",
+ "● Please read all instructions carefully. You may ask the instructor clarifying\n",
+ "questions during the exam.\n",
+ "\n",
+ "● This is a open book exam.\n",
+ "\n",
+ "● Please silence all cell phones and place them off the table.\n",
+ "\n",
+ "● There are 5 questions each with multiple parts. Partial solutions will be graded for partial credit.\n",
+ "\n",
+ "● Explain your answer in detail for each question.\n",
+ "\n",
+ "● You have 150 min to work on this exam.\n",
+ "\n",
+ "Use the following command to add image in your notebook:\n",
+ "\n",
+ "\n",
+ "from IPython import display\n",
+ "\n",
+ "display.Image(\"./image/image.png\")"
+ ],
+ "metadata": {
+ "id": "YixcVplokall"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 1:**\n",
+ "\n",
+ "\n",
+ "a) In order of increasing asymptotic growth rate, list the following functions. Indicate whether two functions have the same asymptotic growth rate. Clearly explain your response.\n",
+ "\n",
+ "$lg n$, $1.1^n$, $n (lg n)$, $n(lg n)^2$, $3 (lg n)$, $2^ 5$, $n^{34}$\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "PRlripdJl3dg"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "y7t8NKOHOnfs"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "State whether the following statements are true or false. explanation is needed. \n",
+ "\n",
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $(f(n))^2 = Θ((g(n))^2 )$ "
+ ],
+ "metadata": {
+ "id": "AS1zox4b9QED"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "vuIUFKcTssZu"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $f(n) = g(n)$"
+ ],
+ "metadata": {
+ "id": "kuqgRmoL9lut"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "jswf3D7QOtzu"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^3 = O(3n^2 )$"
+ ],
+ "metadata": {
+ "id": "3yeHCcQT9uaf"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "-lIb2kJWOtOf"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^2 = O(2n )$"
+ ],
+ "metadata": {
+ "id": "qnpcj1bj912H"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "fRjgbEqDOxO4"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 2:** \n",
+ "Consider **Bucket-sort, Insertion-Sort, Bubble-Sort**. For each algorithm, what will be the worst case asymptotic upper bound on the running time if you know additionally that (For each case and each sorting algorithm, state your answer and justify it in one sentence. )\n",
+ "* the input is already sorted?\n",
+ " \n",
+ "\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "rW2PANQNpPzT"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "XWV3dhvssw_D"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* the input is reversely sorted?\n",
+ " "
+ ],
+ "metadata": {
+ "id": "Ei_shE7uO_Cm"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "0RUZF_o9PAXo"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* the input is a list containing n copies of the same number? (compare the space complexity also) "
+ ],
+ "metadata": {
+ "id": "FUgYe5cePCP2"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "OldQ6edSPDFD"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 3:** \n",
+ "\n",
+ "Let's say you've added some elements to a hash table. When you examine it, you may notice that the outcome resembles the image below. You understand this is an issue since you are an excellent student.\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "* What is the problem here? \n"
+ ],
+ "metadata": {
+ "id": "CXb8eWs9qaiM"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "SNfvZQwSPNXG"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* Give an example of a hash function that could give rise to this behavior. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "BZxlzRenPOks"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "3Va-2GxtPQH2"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What would be a better hash function? \n"
+ ],
+ "metadata": {
+ "id": "siVBn0jqPRtH"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "tscKPSQsPTMF"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* It has been demonstrated that, in the typical scenario, deletion and search are both $O(1 + n/m)$ under the assumptions of uniform hashing, collision resolution via chaining, and constant time computable hash function. We've also demonstrated that, given one further supposition, this can be reduced to $O(1)$. In one sentence, describe this extra supposition."
+ ],
+ "metadata": {
+ "id": "5Oi_rLZ0PUa_"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "kElxIOGFPVO1"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 4**: \n",
+ "\n",
+ "Let's say we have a binary search tree with numbers between 1 and 100 and wish to look for the number 45. Which of the following sequences—possibly more than one—could include the nodes being examined?\n",
+ "\n",
+ "* 5, 2, 1, 10, 39, 34, 77, 63. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "TbTDNP1psS_5"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "hB6c7nb4PghL"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* 1, 2, 3, 4, 5, 6, 7, 8. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "CGxlGYVn-Ozq"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "xCHaMzyRPiLr"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 9, 8, 63, 0, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "WI_DMzQK-RBq"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "6VHRI7LJPjIi"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 8, 7, 6, 5, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "XFO2SX3R-TNC"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "w0tue1ANPj7z"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44, 42. \n"
+ ],
+ "metadata": {
+ "id": "Dt40Rch9-V9S"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "zWNVflXcPk_D"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44. "
+ ],
+ "metadata": {
+ "id": "g8XDfOfL-Xtw"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "pKIFPrRmPlyf"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 5:** \n",
+ "Answer each of the following questions. Keep your answers brief. \n",
+ "\n",
+ "***Example)*** Give an example of a real-world situation where a stack works better as a model than a queue. Why is a stack preferable to a queue in this situation?\n",
+ "\n",
+ "***Answer:*** A stack is a better model for trying to match parenthesis in an expression than a queue. The parenthesis that needs to be matched is always kept at the top of the stack. However, the first parenthesis in a queue is the only one that is stored there, therefore it cannot be used for matching. \n",
+ "\n",
+ "* Describe a real-world situation where a queue works better as a model than a stack. Why is a queue superior to a stack in this situation?\n",
+ " "
+ ],
+ "metadata": {
+ "id": "W46brp6s6j09"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "kcBK3bfQPrsr"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give an example of a real-world situation where a fixed-sized array would be a better model than a linked list. Describe why an array is preferable to a linked list in this situation.\n"
+ ],
+ "metadata": {
+ "id": "Jp67vpco-cuH"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "xGB9SLQcPtxz"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* Give an example of a real-world situation where a doubly linked list would be a better model than a binary search tree. Describe the advantages of a doubly linked list over a binary search tree in this situation."
+ ],
+ "metadata": {
+ "id": "VpD5wKzU-eo1"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "ctn13M6cPu5n"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 6:** \n",
+ "\n",
+ "Given the following directed graph answer the following questions:\n",
+ "\n",
+ ""
+ ],
+ "metadata": {
+ "id": "DR9DSPOY-gWN"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give one possible BFS traversal, beginning at node 0, printing the nodes both as they are found and when they are reached. The graph is directed, as shown by the edge from 8 to 4 but not from 4 to 8; this is only one example."
+ ],
+ "metadata": {
+ "id": "XDjKEfEb_u6-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "UZZFVMqaP0pF"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Draw the Adjacency matrix and list. Comparet them and explain which of them is better to use."
+ ],
+ "metadata": {
+ "id": "KdViaTGhAAI4"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "hxnjnQ5iP1oq"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 7:**\n",
+ "\n",
+ "Consider a hashtable implementation using separate chaining of unsorted linked lists with N buckets and k items currently in the table. \n",
+ "\n",
+ "* What is the average number of items in a bucket?"
+ ],
+ "metadata": {
+ "id": "bPvaZp9vAbVF"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* In the worst-case, how many items could be in a single bucket?"
+ ],
+ "metadata": {
+ "id": "EIMgzna_Al8t"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Given that k > N, which of the following are true?\n",
+ "\n",
+ "1. Some buckets may be empty\n",
+ "2. Some buckets may be full so that they cannot receive more items \n",
+ "3. Neither\t\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "2DY1zyjuAouX"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case running time in terms of k and N for finding the minimum value in the table ? \t"
+ ],
+ "metadata": {
+ "id": "hTluXLbyA22_"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case run time for inserting a new item in the table ?"
+ ],
+ "metadata": {
+ "id": "atGbngRKA5q3"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If we resize the table to a table of size 2N, what is the worst case running time in terms of k and N to put all the items in the new table?"
+ ],
+ "metadata": {
+ "id": "H5MJNPl7A-64"
+ }
+ }
+ ]
+}
\ No newline at end of file
diff --git a/db_exam.ipynb b/db_exam.ipynb
new file mode 100644
index 0000000..508086a
--- /dev/null
+++ b/db_exam.ipynb
@@ -0,0 +1,610 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "authorship_tag": "ABX9TyMpbrTYfNaF/c4c1nEfXQsv",
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The exam consists of 5 problems. \n",
+ "It is recommended to read the problems in order, but it is not important that you solve them in any specific order.\n",
+ "\n",
+ "● Please read all instructions carefully. You may ask the instructor clarifying questions during the exam.\n",
+ "\n",
+ "● This is a open book exam.\n",
+ "\n",
+ "● Please silence all cell phones and place them off the table.\n",
+ "\n",
+ "● There are 5 questions each with multiple parts. Partial solutions will be graded for partial credit.\n",
+ "\n",
+ "● Explain your answer in detail for each question.\n",
+ "\n",
+ "● You have 150 min to work on this exam.\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "2kYyR3ljPXPc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 1:10 points**\n",
+ "\n",
+ "\n",
+ "Create the following SQL database table. Create the table and columns for a half-credit. Set the nulls, constraints, and defaults correctly to receive full credit. \n",
+ "\n",
+ ""
+ ],
+ "metadata": {
+ "id": "XXGYFYiYPHLp"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "w-3mtW7AP1B9"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Insert two random instances from the the following instances: \n",
+ "\n",
+ ""
+ ],
+ "metadata": {
+ "id": "PaxGBrsYP3Zp"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "W2PxHEfPQa87"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 2: 10 points**\n",
+ "\n",
+ "Design an E/R diagram describing the following domain: \n",
+ "\n",
+ "• A Person has attributes pid (key) and name. \n",
+ "\n",
+ "• A Skier is a type of Person with attribute aptitude. \n",
+ "\n",
+ "• A Snowboarder is a type of Skier. \n",
+ "\n",
+ "• A PairOfSkis has attribute sid (key) and model. \n",
+ "\n",
+ "• A Snowboard has attribute sid (key) and model. \n",
+ "\n",
+ "• A Skier owns zero or more PairOfSkis. The ownership relation has a purchase price. A PairOfSkis is owned by at most one Skier. \n",
+ "\n",
+ "• A Snowboarder owns zero or more Snowboards. The ownership relation has a purchase price. A Snowboard is owned by at most one Snowboarder. \n",
+ "\n",
+ "• a Person can rent a PairOfSkis or a Snowboard. A person cannot rent more than one PairOfSkis or one Snowboard at the same time. A person cannot rent a PairOfSkis and a Snowboard at the same time either. A piece of equipment can be rented by at most one person at a time. The rental comes with a start date and an end date. "
+ ],
+ "metadata": {
+ "id": "0PLxewqrQbQq"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "LHKuJ038Q7m3"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Write the SQL CREATE TABLE statement for the owns relation between Skier and PairOfSkis. Make sure that your statement specifies the PRIMARY KEY and any FOREIGN KEYS. Additionally, we would like to enforce the constraint that purchase price be greater than zero"
+ ],
+ "metadata": {
+ "id": "2ueTy78RQ76-"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "-9ew5fr5RG8n"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 3: 20 points**\n",
+ "\n",
+ "This question uses relations from a database that a gym company uses to track membership and usage between multiple gyms in many different cities. The relations in this database are: \n",
+ "\n",
+ "Gym ( gid, name, city) \n",
+ "\n",
+ "Member ( mid , name , is_student, birthdate, city ) \n",
+ "\n",
+ "Visits ( timestamp, mid, gid ) \n",
+ "\n",
+ "The underlined attributes are primary keys for each relation. The attributes mid and gid in Visits reference the keys in Member and Gym. Some assumptions you can make about the table are: \n",
+ "\n",
+ "● Max length of Member.name and Member.city is 50 characters. \n",
+ "\n",
+ "● Member.is_student is a Boolean field (not an Int that can be 0 or 1). \n",
+ "\n",
+ "● Member.birthdate is a Date field. \n",
+ "\n",
+ "● Visit.timestamp is a Datetime field with the timestamp of the visit. "
+ ],
+ "metadata": {
+ "id": "FZ4qftpHRHPH"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Write the SQL Create statement for Member and Visits: "
+ ],
+ "metadata": {
+ "id": "1utWefycRR5f"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "kEgDoP1LRUkm"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Each item below describes a query to perform on the database using the schema from the previous page. Provide an SQL statement for each query. For reference we provide the SQLite date functions: \n",
+ "\n",
+ "date(timesamp/date) => “YYYY-MM-DD” \n",
+ "\n",
+ "date(‘now’) => “2017-07-21” (current date) \n",
+ "\n",
+ "year(timestamp/date) => “YYYY” \n",
+ "\n",
+ " \n",
+ "\n",
+ "The following SQL would find all gym visits that occurred in 2017 or on July 4, 2016: \n",
+ "\n",
+ "SELECT * from Visits where date(timestamp) = “2016-07-04” OR year(timestamp) = “2017”; \n",
+ "\n",
+ " \n",
+ "\n",
+ "* Find members who have attended two different gyms on the same day. Return only unique member ids in any order. "
+ ],
+ "metadata": {
+ "id": "jlE8Ya81RU8-"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "0zmw2yOoRfDX"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Find non-student members who have visited the gym less than 10 times (all gym visits). For each member return their name, number of times they visited a gym and first date they visited the gym (3 attributes). Include members who have never visit a gym. Order by decreasing visit count (high → low) and then by increasing name (A → Z). "
+ ],
+ "metadata": {
+ "id": "WN3Lg-J7Rfex"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "1OBPR61IRjbj"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Indicate what is the output of following queries:\n",
+ "\n",
+ "\n",
+ "\n",
+ "1. SELECT DISTINCT M.mid FROM Member M \n",
+ "\n",
+ "WHERE NOT EXISTS ( \n",
+ "\n",
+ "SELECT * FROM Visits V, Gym G \n",
+ "\n",
+ "WHERE V.mid = M.mid AND V.gid = G.gid \n",
+ "\n",
+ "AND G.city <> M.city \n",
+ "\n",
+ "); \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "lJlJeNkmRjw5"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "CHkTKTudSJo6"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "2. SELECT DISTINCT M.mid FROM Member M, Visits V \n",
+ "\n",
+ "WHERE M.mid = V.mid \n",
+ "\n",
+ "AND NOT EXISTS ( \n",
+ "\n",
+ "SELECT * from Gym G WHERE V.gid = G.gid \n",
+ "\n",
+ "AND G.city <> M.city \n",
+ "\n",
+ "); "
+ ],
+ "metadata": {
+ "id": "egSv0I0sSLlT"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "EKm4G7smSPxy"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "3. SELECT DISTINCT M.mid \n",
+ "\n",
+ "FROM Member M LEFT OUTER JOIN Visits V \n",
+ "\n",
+ "ON M.mid = V.mid LEFT OUTER JOIN Gym G ON V.gid = G.gid \n",
+ "\n",
+ "AND M.city <> G.city GROUP BY M.mid HAVING count(*) = 0;"
+ ],
+ "metadata": {
+ "id": "o-TsA7SESQpD"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "E90i8_TUSUVq"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "4. SELECT DISTINCT M.mid \n",
+ "\n",
+ "FROM Member M \n",
+ "\n",
+ "LEFT OUTER JOIN Visits V ON M.mid = V.mid \n",
+ "\n",
+ "LEFT OUTER JOIN Gym G ON V.gid = G.gid AND M.city <> G.city \n",
+ "\n",
+ "GROUP BY M.mid HAVING count(G.gid) = 0; "
+ ],
+ "metadata": {
+ "id": "OtkhKG5jSUnD"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "V-7q8Cm0SW4r"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 4: 10 points**\n",
+ "\n",
+ "A movie database: \n",
+ "\n",
+ " – MOVIE(id, title, year, type) \n",
+ "\n",
+ "– PERSON(id, name, sex) \n",
+ "\n",
+ "– GENRE(movie → MOVIE, genre) \n",
+ "\n",
+ "– PLAYS(movie → MOVIE, actor → PERSON, character) \n",
+ "\n",
+ " \n",
+ "\n",
+ "Find suitable relational algebra expressions! "
+ ],
+ "metadata": {
+ "id": "JCqUjqgSSbay"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* A list of all movies and their genres. "
+ ],
+ "metadata": {
+ "id": "y9DgNnmYSfkX"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "Z8JZK1HtSllc"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* A list of all action movies."
+ ],
+ "metadata": {
+ "id": "ISdRPQ24Snrk"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "35gReESCSold"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* A list of all movies having at least one of the the genres Action and Comedy. "
+ ],
+ "metadata": {
+ "id": "rnd2YaekSpJE"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "i2aS3SYwStPM"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* A list of all movies having both the genres action and comedy."
+ ],
+ "metadata": {
+ "id": "dLsiT5nXSt1s"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "rOG5xWLESw78"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 5: 20 points**\n",
+ "\n",
+ "For the follwoing tables wrtie the suitable and complete relational calculus:\n",
+ ""
+ ],
+ "metadata": {
+ "id": "ATvZ4iJZSydF"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* For each student, count the exams and compute average result. "
+ ],
+ "metadata": {
+ "id": "5DKufe93TRyH"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "Z_5p9BTTTTD3"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Write the relational calculs to select all female studens: "
+ ],
+ "metadata": {
+ "id": "dmspXT-QTTaQ"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "pG34kH21TV-v"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Select the names of all female student "
+ ],
+ "metadata": {
+ "id": "chj1ExhWTWQt"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "nQzScCeaTYEX"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* All male students with student number greater than 6000 "
+ ],
+ "metadata": {
+ "id": "UrjtJy8BTYno"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "D-z1Je7ETaYn"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Compute the union of all courses with id 100 and 102” (write both relational algebra and calculus)."
+ ],
+ "metadata": {
+ "id": "xzDZTxtqTa17"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "gfbajOYTTmGw"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Compute the cross product of students and exams” (write both relational algebra and calculus)."
+ ],
+ "metadata": {
+ "id": "SDzCaezRTmjn"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "gl9_S_MWTuQ6"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Compute a natural join of students and exams (write both relational algebra and calculus)."
+ ],
+ "metadata": {
+ "id": "FY6Zmjc8Tw9b"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "DEEz-p2qT9c5"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* List the names of all students that took some exam (write both relational algebra and calculus).\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "HOUla4bAT9Q_"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "FDsM6B8fUCJ5"
+ },
+ "execution_count": null,
+ "outputs": []
+ }
+ ]
+}
\ No newline at end of file
diff --git a/df_final_exam.ipynb b/df_final_exam.ipynb
new file mode 100644
index 0000000..5470697
--- /dev/null
+++ b/df_final_exam.ipynb
@@ -0,0 +1,606 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The exam consists of 7 problems. Each of them has 10 points\n",
+ "**It is recommended to read the problems in order**, but it is not important\n",
+ "that you solve them in any specific order.\n",
+ "\n",
+ "\n",
+ "● Please read all instructions carefully. You may ask the instructor clarifying\n",
+ "questions during the exam.\n",
+ "\n",
+ "● This is a open book exam.\n",
+ "\n",
+ "● Please silence all cell phones and place them off the table.\n",
+ "\n",
+ "● There are 5 questions each with multiple parts. Partial solutions will be graded for partial credit.\n",
+ "\n",
+ "● Explain your answer in detail for each question.\n",
+ "\n",
+ "● You have 150 min to work on this exam.\n",
+ "\n",
+ "Use the following command to add image in your notebook:\n",
+ "\n",
+ "\n",
+ "from IPython import display\n",
+ "\n",
+ "display.Image(\"./image/image.png\")"
+ ],
+ "metadata": {
+ "id": "YixcVplokall"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 1:**\n",
+ "\n",
+ "\n",
+ "a) In order of increasing asymptotic growth rate, list the following functions. Indicate whether two functions have the same asymptotic growth rate. Clearly explain your response.\n",
+ "\n",
+ "$lg n$, $1.1^n$, $n (lg n)$, $n(lg n)^2$, $3 (lg n)$, $2^ 5$, $n^{34}$\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "PRlripdJl3dg"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "Hgf_R-YSKbc3"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "State whether the following statements are true or false. explanation is needed. \n",
+ "\n",
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $(f(n))^2 = Θ((g(n))^2 )$ "
+ ],
+ "metadata": {
+ "id": "AS1zox4b9QED"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "Eisv_KkAOuwO"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $f(n) = g(n)$"
+ ],
+ "metadata": {
+ "id": "kuqgRmoL9lut"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "cd4HBW7wOvsO"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^3 = O(3n^2 )$"
+ ],
+ "metadata": {
+ "id": "3yeHCcQT9uaf"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "t22U7gHoOw7t"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^2 = O(2n )$"
+ ],
+ "metadata": {
+ "id": "qnpcj1bj912H"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "cjZewYjXOx45"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 2:** \n",
+ "Consider **Bucket-sort, Insertion-Sort, Bubble-Sort**. For each algorithm, what will be the worst case asymptotic upper bound on the running time if you know additionally that (For each case and each sorting algorithm, state your answer and justify it in one sentence.)\n",
+ "* the input is already sorted?\n",
+ "\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "rW2PANQNpPzT"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "TgBgleTPPKys"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* the input is reversely sorted?\n"
+ ],
+ "metadata": {
+ "id": "7hCNBdYAPOdt"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "EypmItDLPQem"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* the input is a list containing n copies of the same number? (compare the space complexity also) \n",
+ " "
+ ],
+ "metadata": {
+ "id": "-haoO2vSPSCc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "cN1IqakfPTLc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 3:** \n",
+ "\n",
+ "Let's say you've added some elements to a hash table. When you examine it, you may notice that the outcome resembles the image below. You understand this is an issue since you are an excellent student.\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "* What is the problem here? \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "CXb8eWs9qaiM"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "ij2waRqoP0W-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give an example of a hash function that could give rise to this behavior. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "7l5oiYttP3m7"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "CsSDF1P5P7Yr"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What would be a better hash function? \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "1XCeb53OP9X7"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "ZrVYXzaZP_HO"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* It has been demonstrated that, in the typical scenario, deletion and search are both $O(1 + n/m)$ under the assumptions of uniform hashing, collision resolution via chaining, and constant time computable hash function. We've also demonstrated that, given one further supposition, this can be reduced to $O(1)$. In one sentence, describe this extra supposition."
+ ],
+ "metadata": {
+ "id": "BtC0wGRKQAgk"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "I6A2tjadQBe-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 4**: \n",
+ "\n",
+ "Let's say we have a binary search tree with numbers between 1 and 100 and wish to look for the number 45. Which of the following sequences—possibly more than one—could include the nodes being examined?\n",
+ "\n",
+ "* 5, 2, 1, 10, 39, 34, 77, 63. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "TbTDNP1psS_5"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "dc3iZAsyQOxU"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* 1, 2, 3, 4, 5, 6, 7, 8. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "CGxlGYVn-Ozq"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "fleYv0VAQRx1"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [],
+ "metadata": {
+ "id": "2wrM4emcQWSz"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 9, 8, 63, 0, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "WI_DMzQK-RBq"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "QDjikcLNQU4v"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 8, 7, 6, 5, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "XFO2SX3R-TNC"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "HWbUwRy_QX_r"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44, 42. \n"
+ ],
+ "metadata": {
+ "id": "Dt40Rch9-V9S"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "nvL1D9BvQaRj"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44. "
+ ],
+ "metadata": {
+ "id": "g8XDfOfL-Xtw"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "92E6CD3cQdKs"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 5:** \n",
+ "Answer each of the following questions. Keep your answers brief. \n",
+ "\n",
+ "***Example)*** Give an example of a real-world situation where a stack works better as a model than a queue. Why is a stack preferable to a queue in this situation?\n",
+ "\n",
+ "***Answer:*** A stack is a better model for trying to match parenthesis in an expression than a queue. The parenthesis that needs to be matched is always kept at the top of the stack. However, the first parenthesis in a queue is the only one that is stored there, therefore it cannot be used for matching. \n",
+ "\n",
+ "* Describe a real-world situation where a queue works better as a model than a stack. Why is a queue superior to a stack in this situation?\n",
+ " "
+ ],
+ "metadata": {
+ "id": "W46brp6s6j09"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "y8lkvYxNQpS-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give an example of a real-world situation where a fixed-sized array would be a better model than a linked list. Describe why an array is preferable to a linked list in this situation.\n"
+ ],
+ "metadata": {
+ "id": "Jp67vpco-cuH"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "eK7DGJ5EQrxC"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* Give an example of a real-world situation where a doubly linked list would be a better model than a binary search tree. Describe the advantages of a doubly linked list over a binary search tree in this situation."
+ ],
+ "metadata": {
+ "id": "VpD5wKzU-eo1"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "IkM74JQsQvDc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 6:** \n",
+ "\n",
+ "Given the following directed graph answer the following questions:\n",
+ "\n",
+ ""
+ ],
+ "metadata": {
+ "id": "DR9DSPOY-gWN"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give one possible BFS traversal, beginning at node 0, printing the nodes both as they are found and when they are reached. The graph is directed, as shown by the edge from 8 to 4 but not from 4 to 8; this is only one example."
+ ],
+ "metadata": {
+ "id": "XDjKEfEb_u6-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "UCWYX7WzQ6wL"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Draw the Adjacency matrix and list. Comparet them and explain which of them is better to use."
+ ],
+ "metadata": {
+ "id": "KdViaTGhAAI4"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "xc62Cga_Q8Oz"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 7:**\n",
+ "\n",
+ "Consider a hashtable implementation using separate chaining of unsorted linked lists with N buckets and k items currently in the table. \n",
+ "\n",
+ "* What is the average number of items in a bucket?"
+ ],
+ "metadata": {
+ "id": "bPvaZp9vAbVF"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* In the worst-case, how many items could be in a single bucket?"
+ ],
+ "metadata": {
+ "id": "EIMgzna_Al8t"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Given that k > N, which of the following are true?\n",
+ "\n",
+ "1. Some buckets may be empty\n",
+ "2. Some buckets may be full so that they cannot receive more items \n",
+ "3. Neither\t\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "2DY1zyjuAouX"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case running time in terms of k and N for finding the minimum value in the table ? \t"
+ ],
+ "metadata": {
+ "id": "hTluXLbyA22_"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case run time for inserting a new item in the table ?"
+ ],
+ "metadata": {
+ "id": "atGbngRKA5q3"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If we resize the table to a table of size 2N, what is the worst case running time in terms of k and N to put all the items in the new table?"
+ ],
+ "metadata": {
+ "id": "H5MJNPl7A-64"
+ }
+ }
+ ]
+}
\ No newline at end of file
diff --git a/dm_contentbases_rec.ipynb b/dm_contentbases_rec.ipynb
new file mode 100644
index 0000000..ad1ddf7
--- /dev/null
+++ b/dm_contentbases_rec.ipynb
@@ -0,0 +1,293 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "authorship_tag": "ABX9TyPS33qGY4WUxhMgrCRAfECs",
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "id": "abPxsLMfQy0t"
+ },
+ "outputs": [],
+ "source": [
+ "import pandas as pd\n",
+ "import numpy as np\n",
+ "from sklearn.metrics.pairwise import cosine_similarity\n",
+ "from sklearn.model_selection import train_test_split"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "books = pd.DataFrame({'Book Title': ['To Kill a Mockingbird', 'Pride and Prejudice', 'The Great Gatsby', '1984', 'Wuthering Heights',\n",
+ " 'The Catcher in the Rye', 'The Adventures of Huckleberry Finn', 'The Grapes of Wrath', 'One Hundred Years of Solitude', 'Moby-Dick; or, The Whale',\n",
+ " 'The Odyssey', 'The Iliad', 'The Divine Comedy', 'Heart of Darkness', 'The Picture of Dorian Gray', 'A Tale of Two Cities', 'War and Peace', 'Madame Bovary',\n",
+ " 'Frankenstein; or, The Modern Prometheus'],\n",
+ " 'Author': ['Harper Lee', 'Jane Austen', 'F. Scott Fitzgerald', 'George Orwell', 'Emily Bronte',\n",
+ " 'J.D. Salinger', 'Mark Twain', 'John Steinbeck', 'Gabriel Garcia Marquez', 'Herman Melville',\n",
+ " 'Homer', 'Homer', 'Dante Alighieri', 'Joseph Conrad', 'Oscar Wilde', 'Charles Dickens', 'Leo Tolstoy', 'Gustave Flaubert',\n",
+ " 'Mary Shelley'],\n",
+ " 'Genre': ['Drama', 'Romance', 'Drama', 'Dystopian', 'Romance',\n",
+ " 'Drama', 'Adventure', 'Drama', 'Magical Realism', 'Adventure',\n",
+ " 'Epic Poetry', 'Epic Poetry', 'Epic Poetry', 'Drama', 'Drama', 'Drama', 'Historical Fiction', 'Drama',\n",
+ " 'Horror'],\n",
+ " 'User 1 Rating': [4.0, 3.5, 4.5, 5.0, 4.0, 4.5, 3.5, 4.0, 5.0, 4.0, 4.5, 5.0, 4.5, 4.0, 4.5, 5.0, 4.0, 4.5, 4.0],\n",
+ " 'User 2 Rating': [5.0, 4.0, 3.0, 5.0, 3.0, 5.0, 4.0, 4.5, 4.0, 3.5, 5.0, 4.0, 4.5, 4.0, 4.0, 4.0, 4.5, 4.0, 4.0],\n",
+ " 'User 3 Rating': [4.5, 4.5, 3.5, 4.0, 3.5, 4.0, 4.5, 3.0, 4.0, 3.0, 4.5, 4.5, 4.0, 3.5, 3.5, 4.0, 4.0, 3.5, 4.0]})\n"
+ ],
+ "metadata": {
+ "id": "_mENYhIkQ3ao"
+ },
+ "execution_count": 2,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "pivot_table = books.pivot_table(index='Book Title', values=['User 1 Rating', 'User 2 Rating', 'User 3 Rating'])\n"
+ ],
+ "metadata": {
+ "id": "W28ixJAOQ6JN"
+ },
+ "execution_count": 3,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "pivot_table = pivot_table.fillna(0)\n",
+ "matrix = pivot_table.to_numpy()\n"
+ ],
+ "metadata": {
+ "id": "7hytPQXsQ8tR"
+ },
+ "execution_count": 6,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "cosine_sim = cosine_similarity(matrix, matrix)\n",
+ "cosine_sim"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "51t4eg1HTTpl",
+ "outputId": "17fc38e8-94f1-4b54-b867-1b65eb0957c2"
+ },
+ "execution_count": 8,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "array([[1. , 0.99453584, 0.99493668, 0.99922929, 0.9986323 ,\n",
+ " 0.99832074, 0.99453584, 0.9809574 , 0.9809574 , 0.99873142,\n",
+ " 0.99892675, 0.99727571, 0.98403651, 0.99086739, 0.99620492,\n",
+ " 0.9986323 , 0.99086739, 0.99618673, 0.98823669],\n",
+ " [0.99453584, 1. , 0.99413485, 0.99557949, 0.99850675,\n",
+ " 0.99829125, 1. , 0.97948758, 0.97948758, 0.99006884,\n",
+ " 0.99560437, 0.98451715, 0.99719539, 0.99853097, 0.99014158,\n",
+ " 0.99850675, 0.98160671, 0.98949404, 0.99829125],\n",
+ " [0.99493668, 0.99413485, 1. , 0.99811498, 0.99483201,\n",
+ " 0.99326592, 0.99413485, 0.99483201, 0.99483201, 0.99591 ,\n",
+ " 0.99852398, 0.98702447, 0.98584357, 0.99591 , 0.99872693,\n",
+ " 0.99483201, 0.99591 , 0.99840383, 0.99326592],\n",
+ " [0.99922929, 0.99557949, 0.99811498, 1. , 0.99835324,\n",
+ " 0.99755132, 0.99557949, 0.98756023, 0.98756023, 0.99883479,\n",
+ " 0.999975 , 0.99447548, 0.98592988, 0.9940327 , 0.9983922 ,\n",
+ " 0.99835324, 0.9940327 , 0.9982549 , 0.99139359],\n",
+ " [0.9986323 , 0.99850675, 0.99483201, 0.99835324, 1. ,\n",
+ " 0.99989621, 0.99850675, 0.97938144, 0.97938144, 0.99535001,\n",
+ " 0.99813941, 0.99259662, 0.99189361, 0.99535001, 0.99356551,\n",
+ " 1. , 0.98617628, 0.99324409, 0.99401447],\n",
+ " [0.99832074, 0.99829125, 0.99326592, 0.99755132, 0.99989621,\n",
+ " 1. , 0.99829125, 0.97636924, 0.97636924, 0.99443734,\n",
+ " 0.99724929, 0.99255637, 0.99192177, 0.99443734, 0.99200141,\n",
+ " 0.99989621, 0.98396958, 0.9916805 , 0.99328859],\n",
+ " [0.99453584, 1. , 0.99413485, 0.99557949, 0.99850675,\n",
+ " 0.99829125, 1. , 0.97948758, 0.97948758, 0.99006884,\n",
+ " 0.99560437, 0.98451715, 0.99719539, 0.99853097, 0.99014158,\n",
+ " 0.99850675, 0.98160671, 0.98949404, 0.99829125],\n",
+ " [0.9809574 , 0.97948758, 0.99483201, 0.98756023, 0.97938144,\n",
+ " 0.97636924, 0.97948758, 1. , 1. , 0.98617628,\n",
+ " 0.98858783, 0.97125046, 0.96960387, 0.98617628, 0.99356551,\n",
+ " 0.97938144, 0.99535001, 0.99324409, 0.98225098],\n",
+ " [0.9809574 , 0.97948758, 0.99483201, 0.98756023, 0.97938144,\n",
+ " 0.97636924, 0.97948758, 1. , 1. , 0.98617628,\n",
+ " 0.98858783, 0.97125046, 0.96960387, 0.98617628, 0.99356551,\n",
+ " 0.97938144, 0.99535001, 0.99324409, 0.98225098],\n",
+ " [0.99873142, 0.99006884, 0.99591 , 0.99883479, 0.99535001,\n",
+ " 0.99443734, 0.99006884, 0.98617628, 0.98617628, 1. ,\n",
+ " 0.99868977, 0.99723374, 0.97685283, 0.9877551 , 0.99858913,\n",
+ " 0.99535001, 0.99591837, 0.99873956, 0.98396958],\n",
+ " [0.99892675, 0.99560437, 0.99852398, 0.999975 , 0.99813941,\n",
+ " 0.99724929, 0.99560437, 0.98858783, 0.98858783, 0.99868977,\n",
+ " 1. , 0.99380814, 0.98610941, 0.99444002, 0.99862264,\n",
+ " 0.99813941, 0.99444002, 0.99846391, 0.99179984],\n",
+ " [0.99727571, 0.98451715, 0.98702447, 0.99447548, 0.99259662,\n",
+ " 0.99255637, 0.98451715, 0.97125046, 0.97125046, 0.99723374,\n",
+ " 0.99380814, 1. , 0.96920568, 0.97823881, 0.9918907 ,\n",
+ " 0.99259662, 0.98773628, 0.99230431, 0.97428847],\n",
+ " [0.98403651, 0.99719539, 0.98584357, 0.98592988, 0.99189361,\n",
+ " 0.99192177, 0.99719539, 0.96960387, 0.96960387, 0.97685283,\n",
+ " 0.98610941, 0.96920568, 1. , 0.99668741, 0.97819509,\n",
+ " 0.99189361, 0.96693554, 0.97711167, 0.99828024],\n",
+ " [0.99086739, 0.99853097, 0.99591 , 0.9940327 , 0.99535001,\n",
+ " 0.99443734, 0.99853097, 0.98617628, 0.98617628, 0.9877551 ,\n",
+ " 0.99444002, 0.97823881, 0.99668741, 1. , 0.99069514,\n",
+ " 0.99535001, 0.98367347, 0.98990116, 0.99967122],\n",
+ " [0.99620492, 0.99014158, 0.99872693, 0.9983922 , 0.99356551,\n",
+ " 0.99200141, 0.99014158, 0.99356551, 0.99356551, 0.99858913,\n",
+ " 0.99862264, 0.9918907 , 0.97819509, 0.99069514, 1. ,\n",
+ " 0.99356551, 0.99858913, 0.99998174, 0.98694018],\n",
+ " [0.9986323 , 0.99850675, 0.99483201, 0.99835324, 1. ,\n",
+ " 0.99989621, 0.99850675, 0.97938144, 0.97938144, 0.99535001,\n",
+ " 0.99813941, 0.99259662, 0.99189361, 0.99535001, 0.99356551,\n",
+ " 1. , 0.98617628, 0.99324409, 0.99401447],\n",
+ " [0.99086739, 0.98160671, 0.99591 , 0.9940327 , 0.98617628,\n",
+ " 0.98396958, 0.98160671, 0.99535001, 0.99535001, 0.99591837,\n",
+ " 0.99444002, 0.98773628, 0.96693554, 0.98367347, 0.99858913,\n",
+ " 0.98617628, 1. , 0.99873956, 0.9787357 ],\n",
+ " [0.99618673, 0.98949404, 0.99840383, 0.9982549 , 0.99324409,\n",
+ " 0.9916805 , 0.98949404, 0.99324409, 0.99324409, 0.99873956,\n",
+ " 0.99846391, 0.99230431, 0.97711167, 0.98990116, 0.99998174,\n",
+ " 0.99324409, 0.99873956, 1. , 0.98601375],\n",
+ " [0.98823669, 0.99829125, 0.99326592, 0.99139359, 0.99401447,\n",
+ " 0.99328859, 0.99829125, 0.98225098, 0.98225098, 0.98396958,\n",
+ " 0.99179984, 0.97428847, 0.99828024, 0.99967122, 0.98694018,\n",
+ " 0.99401447, 0.9787357 , 0.98601375, 1. ]])"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 8
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "book_indices = pd.Series(books.index, index=books['Book Title'])\n"
+ ],
+ "metadata": {
+ "id": "etu99dR9TbwR"
+ },
+ "execution_count": 12,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "book_indices['Pride and Prejudice']"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "1V5NiUT5UG2B",
+ "outputId": "4a0b4398-bcc7-4ebf-c1b8-97bcbb7efa1a"
+ },
+ "execution_count": 18,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "1"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 18
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def get_recommendations(title, book_indices, cosine_sim=cosine_sim):\n",
+ " # get the index of the book\n",
+ " idx = book_indices[title]\n",
+ " # get the cosine similarity scores for the book\n",
+ " sim_scores = list(enumerate(cosine_sim[idx]))\n",
+ " # sort the books based on the cosine similarity scores\n",
+ " sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)\n",
+ " # get the top 10 most similar books\n",
+ " sim_scores = sim_scores[1:11]\n",
+ " # get the indices of the most similar books\n",
+ " book_indices = [i[0] for i in sim_scores]\n",
+ " # return the most similar books\n",
+ " return books['Book Title'].iloc[book_indices], sim_scores"
+ ],
+ "metadata": {
+ "id": "I3Ahi1LKTe2m"
+ },
+ "execution_count": 25,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "train, test = train_test_split(books, test_size=0.2)\n",
+ "\n",
+ "# use the training data to get recommendations for a book\n",
+ "recommendations, sim_scores = get_recommendations('Pride and Prejudice', book_indices, cosine_sim)\n",
+ "print(recommendations)\n",
+ "print(sim_scores)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "sO48zGQsTiTo",
+ "outputId": "80e2bbe0-61af-447d-9696-842dad2e4ed9"
+ },
+ "execution_count": 26,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "6 The Adventures of Huckleberry Finn\n",
+ "13 Heart of Darkness\n",
+ "4 Wuthering Heights\n",
+ "15 A Tale of Two Cities\n",
+ "5 The Catcher in the Rye\n",
+ "18 Frankenstein; or, The Modern Prometheus\n",
+ "12 The Divine Comedy\n",
+ "10 The Odyssey\n",
+ "3 1984\n",
+ "0 To Kill a Mockingbird\n",
+ "Name: Book Title, dtype: object\n",
+ "[(6, 1.0), (13, 0.998530965365327), (4, 0.9985067527134329), (15, 0.9985067527134329), (5, 0.9982912517345668), (18, 0.9982912517345668), (12, 0.9971953906935493), (10, 0.9956043743251828), (3, 0.9955794851491649), (0, 0.9945358423571875)]\n"
+ ]
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/ds_final.ipynb b/ds_final.ipynb
new file mode 100644
index 0000000..3b2e889
--- /dev/null
+++ b/ds_final.ipynb
@@ -0,0 +1,660 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The exam consists of 7 problems. Each of them has 10 points\n",
+ "**It is recommended to read the problems in order**, but it is not important\n",
+ "that you solve them in any specific order.\n",
+ "\n",
+ "\n",
+ "● Please read all instructions carefully. You may ask the instructor clarifying\n",
+ "questions during the exam.\n",
+ "\n",
+ "● This is a open book exam.\n",
+ "\n",
+ "● Please silence all cell phones and place them off the table.\n",
+ "\n",
+ "● There are 5 questions each with multiple parts. Partial solutions will be graded for partial credit.\n",
+ "\n",
+ "● Explain your answer in detail for each question.\n",
+ "\n",
+ "● You have 150 min to work on this exam.\n",
+ "\n",
+ "Use the following command to add image in your notebook:\n",
+ "\n",
+ "\n",
+ "from IPython import display\n",
+ "\n",
+ "display.Image(\"./image/image.png\")"
+ ],
+ "metadata": {
+ "id": "YixcVplokall"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 1:**\n",
+ "\n",
+ "\n",
+ "a) In order of increasing asymptotic growth rate, list the following functions. Indicate whether two functions have the same asymptotic growth rate. Clearly explain your response.\n",
+ "\n",
+ "$lg n$, $1.1^n$, $n (lg n)$, $n(lg n)^2$, $3 (lg n)$, $2^ 5$, $n^{34}$\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "PRlripdJl3dg"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "Hgf_R-YSKbc3"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "State whether the following statements are true or false. explanation is needed. \n",
+ "\n",
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $(f(n))^2 = Θ((g(n))^2 )$ "
+ ],
+ "metadata": {
+ "id": "AS1zox4b9QED"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "Eisv_KkAOuwO"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $f(n) = g(n)$"
+ ],
+ "metadata": {
+ "id": "kuqgRmoL9lut"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "cd4HBW7wOvsO"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^3 = O(3n^2 )$"
+ ],
+ "metadata": {
+ "id": "3yeHCcQT9uaf"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "t22U7gHoOw7t"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^2 = O(2n )$"
+ ],
+ "metadata": {
+ "id": "qnpcj1bj912H"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "cjZewYjXOx45"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 2:** \n",
+ "Consider **Bucket-sort, Insertion-Sort, Bubble-Sort**. For each algorithm, what will be the worst case asymptotic upper bound on the running time if you know additionally that (For each case and each sorting algorithm, state your answer and justify it in one sentence.)\n",
+ "* the input is already sorted?\n",
+ "\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "rW2PANQNpPzT"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "TgBgleTPPKys"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* the input is reversely sorted?\n"
+ ],
+ "metadata": {
+ "id": "7hCNBdYAPOdt"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "EypmItDLPQem"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* the input is a list containing n copies of the same number? (compare the space complexity also) \n",
+ " "
+ ],
+ "metadata": {
+ "id": "-haoO2vSPSCc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "cN1IqakfPTLc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 3:** \n",
+ "\n",
+ "Let's say you've added some elements to a hash table. When you examine it, you may notice that the outcome resembles the image below. You understand this is an issue since you are an excellent student.\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "* What is the problem here? \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "CXb8eWs9qaiM"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "ij2waRqoP0W-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give an example of a hash function that could give rise to this behavior. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "7l5oiYttP3m7"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "CsSDF1P5P7Yr"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What would be a better hash function? \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "1XCeb53OP9X7"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "ZrVYXzaZP_HO"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* It has been demonstrated that, in the typical scenario, deletion and search are both $O(1 + n/m)$ under the assumptions of uniform hashing, collision resolution via chaining, and constant time computable hash function. We've also demonstrated that, given one further supposition, this can be reduced to $O(1)$. In one sentence, describe this extra supposition."
+ ],
+ "metadata": {
+ "id": "BtC0wGRKQAgk"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "I6A2tjadQBe-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 4**: \n",
+ "\n",
+ "Let's say we have a binary search tree with numbers between 1 and 100 and wish to look for the number 45. Which of the following sequences—possibly more than one—could include the nodes being examined?\n",
+ "\n",
+ "* 5, 2, 1, 10, 39, 34, 77, 63. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "TbTDNP1psS_5"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "dc3iZAsyQOxU"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* 1, 2, 3, 4, 5, 6, 7, 8. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "CGxlGYVn-Ozq"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "fleYv0VAQRx1"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [],
+ "metadata": {
+ "id": "2wrM4emcQWSz"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 9, 8, 63, 0, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "WI_DMzQK-RBq"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "QDjikcLNQU4v"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 8, 7, 6, 5, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "XFO2SX3R-TNC"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "HWbUwRy_QX_r"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44, 42. \n"
+ ],
+ "metadata": {
+ "id": "Dt40Rch9-V9S"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "nvL1D9BvQaRj"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44. "
+ ],
+ "metadata": {
+ "id": "g8XDfOfL-Xtw"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "92E6CD3cQdKs"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 5:** \n",
+ "Answer each of the following questions. Keep your answers brief. \n",
+ "\n",
+ "***Example)*** Give an example of a real-world situation where a stack works better as a model than a queue. Why is a stack preferable to a queue in this situation?\n",
+ "\n",
+ "***Answer:*** A stack is a better model for trying to match parenthesis in an expression than a queue. The parenthesis that needs to be matched is always kept at the top of the stack. However, the first parenthesis in a queue is the only one that is stored there, therefore it cannot be used for matching. \n",
+ "\n",
+ "* Describe a real-world situation where a queue works better as a model than a stack. Why is a queue superior to a stack in this situation?\n",
+ " "
+ ],
+ "metadata": {
+ "id": "W46brp6s6j09"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "y8lkvYxNQpS-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give an example of a real-world situation where a fixed-sized array would be a better model than a linked list. Describe why an array is preferable to a linked list in this situation.\n"
+ ],
+ "metadata": {
+ "id": "Jp67vpco-cuH"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "eK7DGJ5EQrxC"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* Give an example of a real-world situation where a doubly linked list would be a better model than a binary search tree. Describe the advantages of a doubly linked list over a binary search tree in this situation."
+ ],
+ "metadata": {
+ "id": "VpD5wKzU-eo1"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "IkM74JQsQvDc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 6:** \n",
+ "\n",
+ "Given the following directed graph answer the following questions:\n",
+ "\n",
+ ""
+ ],
+ "metadata": {
+ "id": "DR9DSPOY-gWN"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give one possible BFS traversal, beginning at node 0, printing the nodes both as they are found and when they are reached. The graph is directed, as shown by the edge from 8 to 4 but not from 4 to 8; this is only one example."
+ ],
+ "metadata": {
+ "id": "XDjKEfEb_u6-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "UCWYX7WzQ6wL"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Draw the Adjacency matrix and list. Comparet them and explain which of them is better to use."
+ ],
+ "metadata": {
+ "id": "KdViaTGhAAI4"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "xc62Cga_Q8Oz"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 7:**\n",
+ "\n",
+ "Consider a hashtable implementation using separate chaining of unsorted linked lists with N buckets and k items currently in the table. \n",
+ "\n",
+ "* What is the average number of items in a bucket?"
+ ],
+ "metadata": {
+ "id": "bPvaZp9vAbVF"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "4cDs-ZGSAlkW"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* In the worst-case, how many items could be in a single bucket?"
+ ],
+ "metadata": {
+ "id": "EIMgzna_Al8t"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "lQREcPKkAoYg"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Given that k > N, which of the following are true?\n",
+ "\n",
+ "1. Some buckets may be empty\n",
+ "2. Some buckets may be full so that they cannot receive more items \n",
+ "3. Neither\t\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "2DY1zyjuAouX"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "kw4xoQOZA2dI"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case running time in terms of k and N for finding the minimum value in the table ? \t"
+ ],
+ "metadata": {
+ "id": "hTluXLbyA22_"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "E5cCB6KIA5R_"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case run time for inserting a new item in the table ?"
+ ],
+ "metadata": {
+ "id": "atGbngRKA5q3"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "Q41Bjl7eA-iR"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If we resize the table to a table of size 2N, what is the worst case running time in terms of k and N to put all the items in the new table?"
+ ],
+ "metadata": {
+ "id": "H5MJNPl7A-64"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "07zJ-rnFBA3y"
+ },
+ "execution_count": null,
+ "outputs": []
+ }
+ ]
+}
\ No newline at end of file
diff --git a/ds_final1.ipynb b/ds_final1.ipynb
new file mode 100644
index 0000000..438a201
--- /dev/null
+++ b/ds_final1.ipynb
@@ -0,0 +1,606 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The exam consists of 7 problems. Each of them has 10 points\n",
+ "**It is recommended to read the problems in order**, but it is not important\n",
+ "that you solve them in any specific order.\n",
+ "\n",
+ "\n",
+ "● Please read all instructions carefully. You may ask the instructor clarifying\n",
+ "questions during the exam.\n",
+ "\n",
+ "● This is a open book exam.\n",
+ "\n",
+ "● Please silence all cell phones and place them off the table.\n",
+ "\n",
+ "● There are 5 questions each with multiple parts. Partial solutions will be graded for partial credit.\n",
+ "\n",
+ "● Explain your answer in detail for each question.\n",
+ "\n",
+ "● You have 150 min to work on this exam.\n",
+ "\n",
+ "Use the following command to add image in your notebook:\n",
+ "\n",
+ "\n",
+ "from IPython import display\n",
+ "\n",
+ "display.Image(\"./image/image.png\")"
+ ],
+ "metadata": {
+ "id": "YixcVplokall"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 1:**\n",
+ "\n",
+ "\n",
+ "a) In order of increasing asymptotic growth rate, list the following functions. Indicate whether two functions have the same asymptotic growth rate. Clearly explain your response.\n",
+ "\n",
+ "$lg n$, $1.1^n$, $n (lg n)$, $n(lg n)^2$, $3 (lg n)$, $2^ 5$, $n^{34}$\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "PRlripdJl3dg"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "Hgf_R-YSKbc3"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "State whether the following statements are true or false. explanation is needed. \n",
+ "\n",
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $(f(n))^2 = Θ((g(n))^2 )$ "
+ ],
+ "metadata": {
+ "id": "AS1zox4b9QED"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "Eisv_KkAOuwO"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $f(n) = g(n)$"
+ ],
+ "metadata": {
+ "id": "kuqgRmoL9lut"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "cd4HBW7wOvsO"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^3 = O(3n^2 )$"
+ ],
+ "metadata": {
+ "id": "3yeHCcQT9uaf"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "t22U7gHoOw7t"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^2 = O(2n )$"
+ ],
+ "metadata": {
+ "id": "qnpcj1bj912H"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "cjZewYjXOx45"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 2:** \n",
+ "Consider **Bucket-sort, Insertion-Sort, Bubble-Sort**. For each algorithm, what will be the worst case asymptotic upper bound on the running time if you know additionally that (For each case and each sorting algorithm, state your answer and justify it in one sentence.)\n",
+ "* the input is already sorted?\n",
+ "\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "rW2PANQNpPzT"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "TgBgleTPPKys"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* the input is reversely sorted?\n"
+ ],
+ "metadata": {
+ "id": "7hCNBdYAPOdt"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "EypmItDLPQem"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* the input is a list containing n copies of the same number? (compare the space complexity also) \n",
+ " "
+ ],
+ "metadata": {
+ "id": "-haoO2vSPSCc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "cN1IqakfPTLc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 3:** \n",
+ "\n",
+ "Let's say you've added some elements to a hash table. When you examine it, you may notice that the outcome resembles the image below. You understand this is an issue since you are an excellent student.\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "* What is the problem here? \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "CXb8eWs9qaiM"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "ij2waRqoP0W-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give an example of a hash function that could give rise to this behavior. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "7l5oiYttP3m7"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "CsSDF1P5P7Yr"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What would be a better hash function? \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "1XCeb53OP9X7"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "ZrVYXzaZP_HO"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* It has been demonstrated that, in the typical scenario, deletion and search are both $O(1 + n/m)$ under the assumptions of uniform hashing, collision resolution via chaining, and constant time computable hash function. We've also demonstrated that, given one further supposition, this can be reduced to $O(1)$. In one sentence, describe this extra supposition."
+ ],
+ "metadata": {
+ "id": "BtC0wGRKQAgk"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "I6A2tjadQBe-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 4**: \n",
+ "\n",
+ "Let's say we have a binary search tree with numbers between 1 and 100 and wish to look for the number 45. Which of the following sequences—possibly more than one—could include the nodes being examined?\n",
+ "\n",
+ "* 5, 2, 1, 10, 39, 34, 77, 63. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "TbTDNP1psS_5"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "dc3iZAsyQOxU"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* 1, 2, 3, 4, 5, 6, 7, 8. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "CGxlGYVn-Ozq"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "fleYv0VAQRx1"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [],
+ "metadata": {
+ "id": "2wrM4emcQWSz"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 9, 8, 63, 0, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "WI_DMzQK-RBq"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "QDjikcLNQU4v"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 8, 7, 6, 5, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "XFO2SX3R-TNC"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "HWbUwRy_QX_r"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44, 42. \n"
+ ],
+ "metadata": {
+ "id": "Dt40Rch9-V9S"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "nvL1D9BvQaRj"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44. "
+ ],
+ "metadata": {
+ "id": "g8XDfOfL-Xtw"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "92E6CD3cQdKs"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 5:** \n",
+ "Answer each of the following questions. Keep your answers brief. \n",
+ "\n",
+ "***Example)*** Give an example of a real-world situation where a stack works better as a model than a queue. Why is a stack preferable to a queue in this situation?\n",
+ "\n",
+ "***Answer:*** A stack is a better model for trying to match parenthesis in an expression than a queue. The parenthesis that needs to be matched is always kept at the top of the stack. However, the first parenthesis in a queue is the only one that is stored there, therefore it cannot be used for matching. \n",
+ "\n",
+ "* Describe a real-world situation where a queue works better as a model than a stack. Why is a queue superior to a stack in this situation?\n",
+ " "
+ ],
+ "metadata": {
+ "id": "W46brp6s6j09"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "y8lkvYxNQpS-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give an example of a real-world situation where a fixed-sized array would be a better model than a linked list. Describe why an array is preferable to a linked list in this situation.\n"
+ ],
+ "metadata": {
+ "id": "Jp67vpco-cuH"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "eK7DGJ5EQrxC"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* Give an example of a real-world situation where a doubly linked list would be a better model than a binary search tree. Describe the advantages of a doubly linked list over a binary search tree in this situation."
+ ],
+ "metadata": {
+ "id": "VpD5wKzU-eo1"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "IkM74JQsQvDc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 6:** \n",
+ "\n",
+ "Given the following directed graph answer the following questions:\n",
+ "\n",
+ ""
+ ],
+ "metadata": {
+ "id": "DR9DSPOY-gWN"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give one possible BFS traversal, beginning at node 0, printing the nodes both as they are found and when they are reached. The graph is directed, as shown by the edge from 8 to 4 but not from 4 to 8; this is only one example."
+ ],
+ "metadata": {
+ "id": "XDjKEfEb_u6-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "UCWYX7WzQ6wL"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Draw the Adjacency matrix and list. Comparet them and explain which of them is better to use."
+ ],
+ "metadata": {
+ "id": "KdViaTGhAAI4"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "xc62Cga_Q8Oz"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 7:**\n",
+ "\n",
+ "Consider a hashtable implementation using separate chaining of unsorted linked lists with N buckets and k items currently in the table. \n",
+ "\n",
+ "* What is the average number of items in a bucket?"
+ ],
+ "metadata": {
+ "id": "bPvaZp9vAbVF"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* In the worst-case, how many items could be in a single bucket?"
+ ],
+ "metadata": {
+ "id": "EIMgzna_Al8t"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Given that k > N, which of the following are true?\n",
+ "\n",
+ "1. Some buckets may be empty\n",
+ "2. Some buckets may be full so that they cannot receive more items \n",
+ "3. Neither\t\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "2DY1zyjuAouX"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case running time in terms of k and N for finding the minimum value in the table ? \t"
+ ],
+ "metadata": {
+ "id": "hTluXLbyA22_"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case run time for inserting a new item in the table ?"
+ ],
+ "metadata": {
+ "id": "atGbngRKA5q3"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If we resize the table to a table of size 2N, what is the worst case running time in terms of k and N to put all the items in the new table?"
+ ],
+ "metadata": {
+ "id": "H5MJNPl7A-64"
+ }
+ }
+ ]
+}
\ No newline at end of file
diff --git a/ds_final_1.ipynb b/ds_final_1.ipynb
new file mode 100644
index 0000000..1be712a
--- /dev/null
+++ b/ds_final_1.ipynb
@@ -0,0 +1,606 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The exam consists of 7 problems. Each of them has 10 points\n",
+ "**It is recommended to read the problems in order**, but it is not important\n",
+ "that you solve them in any specific order.\n",
+ "\n",
+ "\n",
+ "● Please read all instructions carefully. You may ask the instructor clarifying\n",
+ "questions during the exam.\n",
+ "\n",
+ "● This is a open book exam.\n",
+ "\n",
+ "● Please silence all cell phones and place them off the table.\n",
+ "\n",
+ "● There are 5 questions each with multiple parts. Partial solutions will be graded for partial credit.\n",
+ "\n",
+ "● Explain your answer in detail for each question.\n",
+ "\n",
+ "● You have 150 min to work on this exam.\n",
+ "\n",
+ "Use the following command to add image in your notebook:\n",
+ "\n",
+ "\n",
+ "from IPython import display\n",
+ "\n",
+ "display.Image(\"./image/image.png\")"
+ ],
+ "metadata": {
+ "id": "YixcVplokall"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 1:**\n",
+ "\n",
+ "\n",
+ "a) In order of increasing asymptotic growth rate, list the following functions. Indicate whether two functions have the same asymptotic growth rate. Clearly explain your response.\n",
+ "\n",
+ "$lg n$, $1.1^n$, $n (lg n)$, $n(lg n)^2$, $3 (lg n)$, $2^ 5$, $n^{34}$\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "PRlripdJl3dg"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "Hgf_R-YSKbc3"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "State whether the following statements are true or false. explanation is needed. \n",
+ "\n",
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $(f(n))^2 = Θ((g(n))^2 )$ "
+ ],
+ "metadata": {
+ "id": "AS1zox4b9QED"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "Eisv_KkAOuwO"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $f(n) = g(n)$"
+ ],
+ "metadata": {
+ "id": "kuqgRmoL9lut"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "cd4HBW7wOvsO"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^3 = O(3n^2 )$"
+ ],
+ "metadata": {
+ "id": "3yeHCcQT9uaf"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "t22U7gHoOw7t"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^2 = O(2n )$"
+ ],
+ "metadata": {
+ "id": "qnpcj1bj912H"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "cjZewYjXOx45"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 2:** \n",
+ "Consider **Bucket-sort, Insertion-Sort, Bubble-Sort**. For each algorithm, what will be the worst case asymptotic upper bound on the running time if you know additionally that (For each case and each sorting algorithm, state your answer and justify it in one sentence.)\n",
+ "* the input is already sorted?\n",
+ "\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "rW2PANQNpPzT"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "TgBgleTPPKys"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* the input is reversely sorted?\n"
+ ],
+ "metadata": {
+ "id": "7hCNBdYAPOdt"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "EypmItDLPQem"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* the input is a list containing n copies of the same number? (compare the space complexity also) \n",
+ " "
+ ],
+ "metadata": {
+ "id": "-haoO2vSPSCc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "cN1IqakfPTLc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 3:** \n",
+ "\n",
+ "Let's say you've added some elements to a hash table. When you examine it, you may notice that the outcome resembles the image below. You understand this is an issue since you are an excellent student.\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "* What is the problem here? \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "CXb8eWs9qaiM"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "ij2waRqoP0W-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give an example of a hash function that could give rise to this behavior. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "7l5oiYttP3m7"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "CsSDF1P5P7Yr"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What would be a better hash function? \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "1XCeb53OP9X7"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "ZrVYXzaZP_HO"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* It has been demonstrated that, in the typical scenario, deletion and search are both $O(1 + n/m)$ under the assumptions of uniform hashing, collision resolution via chaining, and constant time computable hash function. We've also demonstrated that, given one further supposition, this can be reduced to $O(1)$. In one sentence, describe this extra supposition."
+ ],
+ "metadata": {
+ "id": "BtC0wGRKQAgk"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "I6A2tjadQBe-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 4**: \n",
+ "\n",
+ "Let's say we have a binary search tree with numbers between 1 and 100 and wish to look for the number 45. Which of the following sequences—possibly more than one—could include the nodes being examined?\n",
+ "\n",
+ "* 5, 2, 1, 10, 39, 34, 77, 63. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "TbTDNP1psS_5"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "dc3iZAsyQOxU"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* 1, 2, 3, 4, 5, 6, 7, 8. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "CGxlGYVn-Ozq"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "fleYv0VAQRx1"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [],
+ "metadata": {
+ "id": "2wrM4emcQWSz"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 9, 8, 63, 0, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "WI_DMzQK-RBq"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "QDjikcLNQU4v"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 8, 7, 6, 5, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "XFO2SX3R-TNC"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "HWbUwRy_QX_r"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44, 42. \n"
+ ],
+ "metadata": {
+ "id": "Dt40Rch9-V9S"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "nvL1D9BvQaRj"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44. "
+ ],
+ "metadata": {
+ "id": "g8XDfOfL-Xtw"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "92E6CD3cQdKs"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 5:** \n",
+ "Answer each of the following questions. Keep your answers brief. \n",
+ "\n",
+ "***Example)*** Give an example of a real-world situation where a stack works better as a model than a queue. Why is a stack preferable to a queue in this situation?\n",
+ "\n",
+ "***Answer:*** A stack is a better model for trying to match parenthesis in an expression than a queue. The parenthesis that needs to be matched is always kept at the top of the stack. However, the first parenthesis in a queue is the only one that is stored there, therefore it cannot be used for matching. \n",
+ "\n",
+ "* Describe a real-world situation where a queue works better as a model than a stack. Why is a queue superior to a stack in this situation?\n",
+ " "
+ ],
+ "metadata": {
+ "id": "W46brp6s6j09"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "y8lkvYxNQpS-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give an example of a real-world situation where a fixed-sized array would be a better model than a linked list. Describe why an array is preferable to a linked list in this situation.\n"
+ ],
+ "metadata": {
+ "id": "Jp67vpco-cuH"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "eK7DGJ5EQrxC"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* Give an example of a real-world situation where a doubly linked list would be a better model than a binary search tree. Describe the advantages of a doubly linked list over a binary search tree in this situation."
+ ],
+ "metadata": {
+ "id": "VpD5wKzU-eo1"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "IkM74JQsQvDc"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 6:** \n",
+ "\n",
+ "Given the following directed graph answer the following questions:\n",
+ "\n",
+ ""
+ ],
+ "metadata": {
+ "id": "DR9DSPOY-gWN"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give one possible BFS traversal, beginning at node 0, printing the nodes both as they are found and when they are reached. The graph is directed, as shown by the edge from 8 to 4 but not from 4 to 8; this is only one example."
+ ],
+ "metadata": {
+ "id": "XDjKEfEb_u6-"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "UCWYX7WzQ6wL"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Draw the Adjacency matrix and list. Comparet them and explain which of them is better to use."
+ ],
+ "metadata": {
+ "id": "KdViaTGhAAI4"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Answer:"
+ ],
+ "metadata": {
+ "id": "xc62Cga_Q8Oz"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 7:**\n",
+ "\n",
+ "Consider a hashtable implementation using separate chaining of unsorted linked lists with N buckets and k items currently in the table. \n",
+ "\n",
+ "* What is the average number of items in a bucket?"
+ ],
+ "metadata": {
+ "id": "bPvaZp9vAbVF"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* In the worst-case, how many items could be in a single bucket?"
+ ],
+ "metadata": {
+ "id": "EIMgzna_Al8t"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Given that k > N, which of the following are true?\n",
+ "\n",
+ "1. Some buckets may be empty\n",
+ "2. Some buckets may be full so that they cannot receive more items \n",
+ "3. Neither\t\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "2DY1zyjuAouX"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case running time in terms of k and N for finding the minimum value in the table ? \t"
+ ],
+ "metadata": {
+ "id": "hTluXLbyA22_"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case run time for inserting a new item in the table ?"
+ ],
+ "metadata": {
+ "id": "atGbngRKA5q3"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If we resize the table to a table of size 2N, what is the worst case running time in terms of k and N to put all the items in the new table?"
+ ],
+ "metadata": {
+ "id": "H5MJNPl7A-64"
+ }
+ }
+ ]
+}
\ No newline at end of file
diff --git a/test.ipynb b/test.ipynb
new file mode 100644
index 0000000..6db424a
--- /dev/null
+++ b/test.ipynb
@@ -0,0 +1,579 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "authorship_tag": "ABX9TyNFDtDSO5+nLMgC5qELmbAJ",
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The exam consists of 7 problems. Each of them has 10 points\n",
+ "**It is recommended to read the problems in order**, but it is not important\n",
+ "that you solve them in any specific order.\n",
+ "\n",
+ "\n",
+ "● Please read all instructions carefully. You may ask the instructor clarifying\n",
+ "questions during the exam.\n",
+ "\n",
+ "● This is a open book exam.\n",
+ "\n",
+ "● Please silence all cell phones and place them off the table.\n",
+ "\n",
+ "● There are 5 questions each with multiple parts. Partial solutions will be graded for partial credit.\n",
+ "\n",
+ "● Explain your answer in detail for each question.\n",
+ "\n",
+ "● You have 150 min to work on this exam."
+ ],
+ "metadata": {
+ "id": "YixcVplokall"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 1:**\n",
+ "\n",
+ "\n",
+ "a) In order of increasing asymptotic growth rate, list the following functions. Indicate whether two functions have the same asymptotic growth rate. Clearly explain your response.\n",
+ "\n",
+ "$lg n$, $1.1^n$, $n (lg n)$, $n(lg n)^2$, $3 (lg n)$, $2^ 5$, $n^{34}$\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "PRlripdJl3dg"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "JHWMyR-29jPu"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "State whether the following statements are true or false. explanation is needed. \n",
+ "\n",
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $(f(n))^2 = Θ((g(n))^2 )$ "
+ ],
+ "metadata": {
+ "id": "AS1zox4b9QED"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Answer:"
+ ],
+ "metadata": {
+ "id": "vuIUFKcTssZu"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "8aQGIChO8XQ-"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If $f(n) = O(g(n))$ and $f(n) = Ω(g(n))$, then we have $f(n) = g(n)$"
+ ],
+ "metadata": {
+ "id": "kuqgRmoL9lut"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "KIeT3cJu9tmN"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^3 = O(3n^2 )$"
+ ],
+ "metadata": {
+ "id": "3yeHCcQT9uaf"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "C4DakVpI9285"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* $2 n + n^2 = O(2n )$"
+ ],
+ "metadata": {
+ "id": "qnpcj1bj912H"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "1i0aUN7k-CX3"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 2:** \n",
+ "Consider **Bucket-sort, Insertion-Sort, Bubble-Sort**. For each algorithm, what will be the worst case asymptotic upper bound on the running time if you know additionally that \n",
+ "* the input is already sorted?\n",
+ "* the input is reversely sorted?\n",
+ "* the input is a list containing n copies of the same number? (compare the space complexity also) \n",
+ "For each case and each sorting algorithm, state your answer and justify it in one sentence. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "rW2PANQNpPzT"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Answer:"
+ ],
+ "metadata": {
+ "id": "XWV3dhvssw_D"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "k1rW2hAp-GzZ"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 3:** \n",
+ "\n",
+ "Let's say you've added some elements to a hash table. When you examine it, you may notice that the outcome resembles the image below. You understand this is an issue since you are an excellent student.\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "* What is the problem here? \n",
+ "\n",
+ "* Give an example of a hash function that could give rise to this behavior. \n",
+ "\n",
+ "* What would be a better hash function? \n",
+ "\n",
+ "* It has been demonstrated that, in the typical scenario, deletion and search are both $O(1 + n/m)$ under the assumptions of uniform hashing, collision resolution via chaining, and constant time computable hash function. We've also demonstrated that, given one further supposition, this can be reduced to $O(1)$. In one sentence, describe this extra supposition."
+ ],
+ "metadata": {
+ "id": "CXb8eWs9qaiM"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "ARC31n9Z-K7W"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 4**: \n",
+ "\n",
+ "Let's say we have a binary search tree with numbers between 1 and 100 and wish to look for the number 45. Which of the following sequences—possibly more than one—could include the nodes being examined?\n",
+ "\n",
+ "* 5, 2, 1, 10, 39, 34, 77, 63. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "TbTDNP1psS_5"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "E-lKPeLh-N2C"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* 1, 2, 3, 4, 5, 6, 7, 8. \n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "CGxlGYVn-Ozq"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "8mLVaD4R-QZk"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 9, 8, 63, 0, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "WI_DMzQK-RBq"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "TQnBifbn-Sw3"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 8, 7, 6, 5, 4, 3, 2, 1. \n"
+ ],
+ "metadata": {
+ "id": "XFO2SX3R-TNC"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "LHGjFOIs-Vl0"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44, 42. \n"
+ ],
+ "metadata": {
+ "id": "Dt40Rch9-V9S"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "4xLJmM8k-XWM"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* 50, 25, 26, 27, 40, 44. "
+ ],
+ "metadata": {
+ "id": "g8XDfOfL-Xtw"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "Rx9nuims-Zke"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 5:** \n",
+ "Answer each of the following questions. Keep your answers brief. \n",
+ "\n",
+ "***Example)*** Give an example of a real-world situation where a stack works better as a model than a queue. Why is a stack preferable to a queue in this situation?\n",
+ "\n",
+ "***Answer:*** A stack is a better model for trying to match parenthesis in an expression than a queue. The parenthesis that needs to be matched is always kept at the top of the stack. However, the first parenthesis in a queue is the only one that is stored there, therefore it cannot be used for matching. \n",
+ "\n",
+ "* Describe a real-world situation where a queue works better as a model than a stack. Why is a queue superior to a stack in this situation?\n",
+ " "
+ ],
+ "metadata": {
+ "id": "W46brp6s6j09"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "t0kkACij-cKb"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give an example of a real-world situation where a fixed-sized array would be a better model than a linked list. Describe why an array is preferable to a linked list in this situation.\n"
+ ],
+ "metadata": {
+ "id": "Jp67vpco-cuH"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "lfew6uCF-eOM"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "\n",
+ "* Give an example of a real-world situation where a doubly linked list would be a better model than a binary search tree. Describe the advantages of a doubly linked list over a binary search tree in this situation."
+ ],
+ "metadata": {
+ "id": "VpD5wKzU-eo1"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "8jg0Z__a-fSV"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 6:** \n",
+ "\n",
+ "Given the following directed graph answer the following questions:\n",
+ "\n",
+ ""
+ ],
+ "metadata": {
+ "id": "DR9DSPOY-gWN"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Give one possible BFS traversal, beginning at node 0, printing the nodes both as they are found and when they are reached. The graph is directed, as shown by the edge from 8 to 4 but not from 4 to 8; this is only one example."
+ ],
+ "metadata": {
+ "id": "XDjKEfEb_u6-"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "1T4PjD_p__pm"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Draw the Adjacency matrix and list. Comparet them and explain which of them is better to use."
+ ],
+ "metadata": {
+ "id": "KdViaTGhAAI4"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "8fzvFamtALP5"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Question 7:**\n",
+ "\n",
+ "Consider a hashtable implementation using separate chaining of unsorted linked lists with N buckets and k items currently in the table. \n",
+ "\n",
+ "* What is the average number of items in a bucket?"
+ ],
+ "metadata": {
+ "id": "bPvaZp9vAbVF"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "4cDs-ZGSAlkW"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* In the worst-case, how many items could be in a single bucket?"
+ ],
+ "metadata": {
+ "id": "EIMgzna_Al8t"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "lQREcPKkAoYg"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* Given that k > N, which of the following are true?\n",
+ "\n",
+ "1. Some buckets may be empty\n",
+ "2. Some buckets may be full so that they cannot receive more items \n",
+ "3. Neither\t\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "2DY1zyjuAouX"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "kw4xoQOZA2dI"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case running time in terms of k and N for finding the minimum value in the table ? \t"
+ ],
+ "metadata": {
+ "id": "hTluXLbyA22_"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "E5cCB6KIA5R_"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* What is the worst case run time for inserting a new item in the table ?"
+ ],
+ "metadata": {
+ "id": "atGbngRKA5q3"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "Q41Bjl7eA-iR"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "* If we resize the table to a table of size 2N, what is the worst case running time in terms of k and N to put all the items in the new table?"
+ ],
+ "metadata": {
+ "id": "H5MJNPl7A-64"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "07zJ-rnFBA3y"
+ },
+ "execution_count": null,
+ "outputs": []
+ }
+ ]
+}