Spring 2018 Mentoring 13: April 25, New World Order. return a list of all the words that start with a given prefix.

Similar documents
(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Sorting Algorithms Spring 2019 Mentoring 10: 18 April, Asymptotics Potpourri

Programming II (CS300)

Introduction to Algorithms I

( ) n 3. n 2 ( ) D. Ο

( D. Θ n. ( ) f n ( ) D. Ο%

CS 512: Comments on Graph Search 16:198:512 Instructor: Wes Cowan

Final Exam Solutions

CS61B Fall 2015 Guerrilla Section 3 Worksheet. 8 November 2015

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Final Review Document Solution

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

Material covered. Areas/Topics covered. Logistics. What to focus on. Areas/Topics covered 5/14/2015. COS 226 Final Exam Review Spring 2015

n 2 ( ) ( ) + n is in Θ n logn

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Faster Sorting Methods

Topics for CSCI 151 Final Exam Wednesday, May 10

CS302 Data Structures using C++

Sorting algorithms Properties of sorting algorithm 1) Adaptive: speeds up to O(n) when data is nearly sorted 2) Stable: does not change the relative

L14 Quicksort and Performance Optimization

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Merge Sort Goodrich, Tamassia Merge Sort 1

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

( ) 1 B. 1. Suppose f x

) $ f ( n) " %( g( n)

Algorithm classification

Mergesort again. 1. Split the list into two equal parts

II (Sorting and) Order Statistics

CSE 373 Final Exam 3/14/06 Sample Solution

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

COS 226 Final Exam, Spring 2009

Programming II (CS300)

2. (a) Explain when the Quick sort is preferred to merge sort and vice-versa.

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

Sorting. Task Description. Selection Sort. Should we worry about speed?

Merge Sort

Algorithms and Applications

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

CS61BL. Lecture 5: Graphs Sorting

SORTING AND SELECTION

INDIAN STATISTICAL INSTITUTE

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

1. O(log n), because at worst you will only need to downheap the height of the heap.

O(n): printing a list of n items to the screen, looking at each item once.

Course Name: B.Tech. 3 th Sem. No of hours allotted to complete the syllabi: 44 Hours No of hours allotted per week: 3 Hours. Planned.

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

PROGRAMMING IN C++ (Regulation 2008) Answer ALL questions PART A (10 2 = 20 Marks) PART B (5 16 = 80 Marks) function? (8)

Question 7.11 Show how heapsort processes the input:

CS 171: Introduction to Computer Science II. Quicksort

Lecture 19 Sorting Goodrich, Tamassia

CS61B Spring 2016 Guerrilla Section 6 Worksheet

CSE 332 Winter 2015: Final Exam (closed book, closed notes, no calculators)

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

Sorting Goodrich, Tamassia Sorting 1

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Cornell University Computer Science 211 Second Preliminary Examination 18 April 2006

Second Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

CSE 332 Autumn 2016 Final Exam (closed book, closed notes, no calculators)

Sorting and Selection

CSC 273 Data Structures

Binary heaps (chapters ) Leftist heaps

a) For the binary tree shown below, what are the preorder, inorder and post order traversals.

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

AP Computer Science 4325

CERC 2018 Presentation of solutions. December 2, 2018

Lecture Notes for Advanced Algorithms

CS170 Discussion Section 4: 9/18

CS521 \ Notes for the Final Exam

QuickSort

Real-world sorting (not on exam)

Sorting Algorithms. + Analysis of the Sorting Algorithms

Exam 3 Practice Problems

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Sorting (Chapter 9) Alexandre David B2-206

CSE 373: Practice Final

Midterm 1 Solutions. (i) False. One possible counterexample is the following: n n 3

CSci 231 Final Review

looking ahead to see the optimum

Recursive Sorts. Recursive Sorts. Divide-and-Conquer. Divide-and-Conquer. Divide-and-conquer paradigm:

Lecture 23: Priority Queues, Part 2 10:00 AM, Mar 19, 2018

Quick-Sort. Quick-Sort 1

Outline. CS 561, Lecture 6. Priority Queues. Applications of Priority Queue. For NASA, space is still a high priority, Dan Quayle

Total Points: 60. Duration: 1hr

CS 561, Lecture 10. Jared Saia University of New Mexico

Advanced Database Systems

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

CS1 Lecture 30 Apr. 2, 2018

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

CS 171: Introduction to Computer Science II. Quicksort

ECE368 Exam 2 Spring 2016

Enhancing the Efficiency of Radix Sort by Using Clustering Mechanism

Transcription:

CSM 61B Final Review Spring 2018 Mentoring 13: April 25, 2017 1 New World Order 1.1 Draw the trie that results from inserting "hi", "hello", and "hey". h e i l y l o 1.2 Given a list of words (possibly repeated), devise a strategy to efficiently return a list of all the words that start with a given prefix. Put all the names into a trie, lookup the prefix in the trie, and iterate across all the children rooted at that node. 1.3 Given a dictionary of words, describe a procedure for checking if a new word can be created out of the concatenation of two words in the dictionary. For example, if our dictionary contains the words, "news", "paper", "new", and "ape", we should be able to discover the new word, "newspaper". We can put all of the words in the dictionary into a trie. Then we can check a prefix of the word and see if the remainder of the word is in the trie as well.

2 Final Review 2 Out of Sorts 2.1 Each column below gives the contents of a list at some step during sorting. Match each column with its corresponding algorithm. Merge sort Quicksort Heap sort LSD radix sort MSD radix sort For quicksort, choose the topmost element as the pivot. Use the recursive (top-down) implementation of merge sort. Start A B C D E Sorted 1 4873 1876 1874 1626 9573 2212 1626 2 1874 1874 1626 1874 7121 8917 1874 3 8917 2212 1876 1876 9132 7121 1876 4 1626 1626 1897 4873 6973 1626 1897 5 4982 3492 2212 4982 4982 9132 2212 6 9132 1897 3492 8917 8917 6152 3492 7 9573 4873 4873 9132 6152 4873 4873 8 1876 9573 4982 9573 1876 9573 4982 9 6973 6973 6973 1897 1626 6973 6152 10 1897 9132 6152 3492 1897 1874 6973 11 9587 9587 7121 6973 1874 1876 7121 12 3492 4982 8917 9587 3492 9877 8917 13 9877 9877 9132 2212 4873 4982 9132 14 2212 8917 9573 6152 2212 9587 9573 15 6152 6152 9587 7121 9587 3492 9587 16 7121 7121 9877 9877 9877 1897 9877 From left to right: unsorted list, quicksort, MSD radix sort, merge sort, heap sort, LSD radix sort, completely sorted. MSD Look at the left-most digits. They should be sorted. Mark this immediately as MSD. LSD One of the digits should be sorted. Start by looking at the right most digit of the remaining sorts. Then check the second from right digit of the remaining sorts and so on. As soon as you find one in which at

Final Review 3 least something is sorted, mark that as LSD. Heap Max-oriented heap so check that the bottom is in sorted order and that the top element is the next max element. Merge Realize that the first pass of merge sort fixes items in groups of 2. Identify the passes and look for sorted runs. Quick Run quicksort using the pivot strategy outlined above. Look for partitions and check that 4873 is in its correct final position. 2.2 Web developers use many different sorts for the different types of lists that they might want to sort. For each of these, provide the best sorting algorithm amongst the following: Mergesort, Quicksort (with Hoare Partitioning), Insertion Sort, LSD Sort. Also, state the worst-case runtime. (a) A list of N packets received by a server over time. Each packet has the timestamp at which the sender sent it. Sort this list by that timestamp (sent time). Since we expect the list to be largely sorted by time already, with a few packets out of place, we should use insertion sort. Worst case runtime is O(N 2 ). (b) A list of N websites. Each website has the number of total visitors. Sort this list by visitor count. Quicksort, since it s generally pretty fast for sorting what is effectively a random list of numbers. Worst case runtime is O(N 2 ). Could also argue for LSD sort since there might be some limit k on the total number of visitors, but less preferable. (c) After sorting by visitor count, we now want to sort by webpage file size. If websites have the same file size, they should be ordered by visitor count. Mergesort, since we want to sort stably. O(N log N). Worst case runtime is (d) A list of 20 names. Sort in alphabetical order. Insertion sort, since it has the least overhead and is fastest for small lists. Worst case runtime is O(1), since 20 is a constant, and we assume that all names are shorter than some fixed constant as well. (e) A list of N user ages, where user age is at most 120. LSD Sort, since we have a fixed alphabet and radix. Worst case runtime is O(N), since the max width is just 3.

4 Final Review 3 T,F,G,V,E 3.1 State if the following statements are True or False, and justify. For all graphs, assume that edge weights are positive and distinct, unless otherwise stated. (a) Adding some positive constant k to every edge weight does not change the shortest path tree from vertex S. False. (b) Doubling every edge weight does not change the shortest path tree. True. (c) Adding some positive constant k to every edge weight does not change the minimum spanning tree. True. (d) Doubling every edge weight does not change the minimum spanning tree. True. For the four parts above, we can consider when graph transformations affect the two algorithms: MST algorithms depend on the relative order of edge weights. Hence, adding a constant, or doubling the edge weights does not alter the MST. (More broadly, any monotonically increasing function can be applied, such as squaring the edge weights, assuming they are all positive.) Shortest path algorithms depend on the relative order of sums of edge weights. More specifically, we are concerned about sums of edge weights that represent paths to vertices in the graphs. We can see then that adding a constant k to all edge weights does alter the relative order of these sums. In fact, as k increases, the algorithm becomes more biased towards paths that are shorter in hop-length, i.e. number of vertices in the path. One intuitive way to think about this would be to make k a very large number, tending towards infinity. Then all edge weights are approximately the same length, and shortest path algorithms will find the shortest path by hop-length, just like BFS. On the other hand, if we double every edge weight, the relative order of sums does not change. 2w 1 +2w 2 +2w 3 = 2(w 1 +w 2 +w 3 ). We see that we can factorize out the multiplier, and the ordering is still dependent on the original sums of edge weights. More broadly, we can consider any positive multiplication

Final Review 5 of edge weights to not affect shortest path trees. (e) Let (S, V S) be a specific cut of the graph. If an edge e is not the lightest edge across this cut, it cannot be a part of any MST. False. Consider the graph {(A, B, 1), (B, C, 2)}. Even though edge (B, C) is not the lightest edge across the cut {A}, {B, C}, it is necessarily still a part of all MSTs (since this graph is a tree). (f) If an edge e is the lightest edge connected to vertex S, it must be a part of the shortest path tree from vertex S. True. If e connects S to T, then that must be the shortest path from S to T. Assume there is some shorter path to T from S. Then it must exit S via edge e which has strictly larger weight than e, creating a contradiction.

6 Final Review 4 Roleplaying Game 4.1 You are the king of a large kingdom! In order to manage your kingdom, you have appointed lords to rule towns within your kingdom. Every lord can govern over his town and any town that he is connected to by road. Your job as king is to figure out the optimal way to allocate lords and build roads. Formally, consider a graph G with vertices V and edges E. Each vertex v represents a town. It has an associated cost c, the cost of installing a lord in the town. Each edge e represents a potential road. It has an edge weight w, the cost of building that road. Devise an algorithm that can efficiently compute which towns to install lords in and which roads to build, such that every town in the kingdom is governed (either has a lord in it or is connected by some number of roads to a town with a lord in it). We can formulate this problem as a Minimum Spanning Tree problem. We create a dummy node S. We connect S to every vertex with edge weight c, the cost of that vertex. We then find the MST of this modified graph, and that is the solution. Why does this method work? We know that the MST must include S, by definition of spanning tree. Every edge in te MST that is outgoing from S represents a selected town. In the MST, every vertex is either directly connected to S, i.e. a town with a lord, or connected to S by a series of selected edges, i.e. connected to some town with a lord via some roads. Since the MST finds the minimum cost solution, this is our desired arrangement of lords and roads 4.2 You are a software engineer for a newspaper company! Your users are complaining about how slowly your website loads. After performing some performance profiling, you realize that the database queries are slowing the system down. To fix the issue, you decide to implement a cache that contains the most recently accessed articles. The cache is only fast if it s small so you can only store a maximum of N articles. You want to keep only the N most recent articles that people have read. If a new, unique article is accessed, then the oldest article should be replaced. Describe how you would implement this cache. What combinations of data structures would you use to build this efficiently? Use a HashMap with references to nodes in a doubly linked list. The HashMap will map the name of the article to a tuple containing the article as well as a reference to a node in a doubly linked list. If the article we are trying to access is in the HashMap, we deliver the article, and then retrieve its respective node in the linked list and move it to the front. If the article is not in the

Final Review 7 HashMap, and the size of the HashMap is still less than N, we can fetch from the main server and create a new node to append to the front of the linked list. If the HashMap is at capacity, we look at the tail of the linked list and delete that article from both the linked list as well as the HashMap. We then add the new article into the HashMap and to the front of the linked list. In Java there s actually a data structure for this: java.util.linkedhashmap!