Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

Similar documents
Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

Exam Data structures DIT960/DAT036

Exam Data structures DAT036/DAT037/DIT960

Exam Data structures DAT036/DAT037/DIT960

DIT960 Datastrukturer

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

DIT960 Datastrukturer

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Final Examination CSE 100 UCSD (Practice)

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

CS 315 Data Structures mid-term 2

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

Priority Queues Heaps Heapsort

CSE 2123: Collections: Priority Queues. Jeremy Morris

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Data structures. Priority queues, binary heaps. Dr. Alex Gerdes DIT961 - VT 2018

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

1. AVL Trees (10 Points)

Algorithms and Data Structures

Problem Pts Score Grader Problem Pts Score Grader

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

CISC 320 Midterm Exam

Priority Queues. 04/10/03 Lecture 22 1

University of Toronto Mississauga. Flip to the back cover and write down your name and student number.

CSE 373: Practice Final

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Mid-Term Exam. CSE2100 DS & Algorithms 1

Practice Midterm Exam Solutions

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

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

INFO1x05 Tutorial 09

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Priority Queues and Heaps

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Programming II (CS300)

Priority Queues Heaps Heapsort

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

COMP 250 Midterm #2 March 11 th 2013

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

Computer Science E-22 Practice Final Exam

CS171 Final Practice Exam

Recall: Properties of B-Trees

Binary search trees (chapters )

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Data Structures and Algorithms

Total Score /1 /20 /41 /15 /23 Grader

CS 261 Spring 2013 Midterm

Fall, 2015 Prof. Jungkeun Park

Closed Book Examination. One and a half hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Wednesday 20 th January 2010

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Priority Queue Sorting

Priority queues. Priority queues. Priority queue operations

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

Final Examination Term: 162 Year: 2016/2017 (v2)

Prelim 2 SOLUTION. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

Sorting and Searching

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Final Exam. EECS 2011 Prof. J. Elder - 1 -

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

Data Structures Brett Bernstein

Programming II (CS300)

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

Total Score /15 /20 /30 /10 /5 /20 Grader

Sorting and Searching

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

9. Heap : Priority Queue

CMSC 341 Lecture 14: Priority Queues, Heaps

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

Course Review for Finals. Cpt S 223 Fall 2008

CS 310: Priority Queues and Binary Heaps

MIDTERM Preparation. CSS 343: Data Structures Algorithms, and Discrete Math II

CS/ENGRD2110: Final Exam

CS 261 Data Structures. AVL Trees

Lecture 13: AVL Trees and Binary Heaps

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

Total Score /15 /20 /30 /10 /5 /20 Grader

CPSC 331 Term Test #2 March 26, 2007

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

CSE 214 Computer Science II Heaps and Priority Queues

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

Transcription:

Exam Datastrukturer DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE Day: 2017-05-31, Time: 8:30-12.30, Place: SB-MU Course responsible Alex Gerdes, tel. 031-772 6154. Will visit at around 9:30 and 11:00. Allowed aids One hand-written sheet of A4 paper. You may use both sides. You may also bring a dictionary. Grading The exam consists of six questions. For each question you can get a U, a G or a VG. To get a G on the exam, you need to answer at least three questions to G or VG standard. To get a VG on the exam, you need to answer at least five questions to VG standard. A fully correct answer for a question, including the parts labelled For a VG, will get a VG. A correct answer, without the For a VG parts, will get a G. An answer with minor mistakes might be accepted, but this is at the discretion of the marker. An answer with large mistakes will get a U. Inspection When the exams have been graded they are available for review in the student office on floor 4 in the EDIT building. If you want to discuss the grading, please contact the course responsible and book a meeting. In that case, you should leave the exam in the student office until after the meeting. Note Begin each question on a new page. Write your anonymous code (not your name) on every page. When a question asks for pseudocode, you don t have to write precise code, such as Java. But your answer should be well structured. Indenting and/or using brackets is a good idea. Apart from being readable your pseudocode should give enough detail that a competent programmer could easily implement the solution, and that it s possible to analyse the time complexity. Excessively complicated answers might be rejected. Write legibly! Solutions that are difficult to read are not evaluated! 1

Exercise 1 Consider the following algorithm that sorts a list of n elements by using a priority queue: public static void sort(list<integer> xs) { PriorityQueue<Integer> pq = new PriorityQueue<>(); } for (Integer x : xs) pq.insert(x); while (!pq.empty()) { Integer x = pq.deletemin(); System.out.println(x); } What is the worst-case time complexity of this algorithm, if the priority queue is implemented using: a) an ordinary binary search tree? b) an AVL tree? c) an unsorted linked list? d) a 2-3 tree? e) a sorted array? You may assume that printing out a value takes constant time. Recall that you can find the smallest value in a binary search tree by starting at the root and following the left child until you find a node without a left child this is the node with the smallest value. The complexity should be expressed in terms of n, the size of the input list. You should express the complexity in the simplest form possible. Apart from the final result you should also describe how you reached it, that is, show your complexity analysis. 2

Exercise 2 Your task is to implement a map from keys to values in Haskell using a binary search tree. Your solution should define a type Map k v that represents a map from keys k to values v, together with two functions: data Map k v =... lookup :: Ord k => k -> Map k v -> Maybe v update :: Ord k => k -> v -> Map k v -> Map k v The lookup function looks up a key in the map, while update adds a key/value pair to the map, or updates the value if the key already exists in the map. You may like to take inspiration from the following Haskell code which implements a set using a binary search tree. data BST a = Empty Node (BST a) a (BST a) member :: Ord a => a -> BST a -> Bool member x Empty = False member x (Node l y r) x < y = member x l x > y = member x r otherwise = True insert :: Ord a => a -> BST a -> BST a insert x Empty = Node Empty x Empty insert x t@(node l y r) x < y = Node (insert x l) y r x > y = Node l y (insert x r) otherwise = t For a VG only: Write a Haskell function to delete an element. It should take two parameters, which are the element to delete and the map, and have the following type: delete :: Ord k => k -> Map k v -> Map k v The complexity of your function should be O(height of tree), i.e., O(log n) for balanced trees, O(n) for unbalanced trees. Hint: it will help to define and use a function that finds/deletes the smallest (alternatively the greatest) element when implementing delete. 3

Exercise 3 Which array out of A, B and C represents a binary heap? Only one answer is right. A 3 12 23 10 15 35 45 15 18 20 21 0 1 2 3 4 5 6 7 8 9 10 11 B 2 13 20 21 65 54 67 41 30 83 52 0 1 2 3 4 5 6 7 8 9 10 11 C 3 8 22 11 43 79 87 32 13 50 49 0 1 2 3 4 5 6 7 8 9 10 11 a) Write the heap out as a binary tree. b) Add 19 to the heap, making sure to restore the heap invariant. How does the array look now? c) For a VG only: Describe the procedure how we can build a heap from an arbitrary array. Answer either in pseudocode or English (or Swedish). 4

Exercise 4 You are given the following undirected weighted graph: 4 4 A B C 2 6 3 D E 5 1 7 8 2 3 F G H I 9 8 a) Compute a minimal spanning tree for the following graph by manually performing Prim s algorithm using A as starting node. Your answer should be the set of edges which are members of the spanning tree you have computed. The edges should be listed in the order they are added as Prim s algorithm is executed. Refer to each edge by the labels of the two nodes that it connects, e.g. DF for the edge between nodes D and F. b) For a VG only: Suppose we perform Dijkstra s algorithm starting from node F. In which order does the algorithm visit the nodes, and what is the computed distance to each of them? 5

Exercise 5 Consider the following hash table implemented using linear probing, where the hash function is the identity, h(x) = x mod 10, where mod is the modulo operator that calculates the remainder of a division. 10 XX 2 33 5 16 19 0 1 2 3 4 5 6 7 8 9 a) The value that was previously at index 1 has been deleted, which is represented by the XX in the hash table. Which value might have been stored there, before it was deleted? There may be several correct answers, and you should write down all of them. A) 26 B) 9 C) 20 D) 38 E) 41 F) 13 b) For a VG only: Hash tables typically have better performance than balanced binary search trees. Even so, both are widely used in practice. One reason is that a hash table does not support all the operations that a BST does. Give an example of an operation which can be efficiently implemented for a binary search tree but not for a hash table. 6

Exercise 6 Design a data structure for storing a set of strings. It should support the following operations: new: create a new, empty set insert: add a string to the set member: test if a given string is in the set delete: delete a string from the set deletelarge: delete all strings with more than 42 characters You may use existing standard data structures as part of your solution you don t have to start from scratch. Write down the data structure or design you have chosen, plus pseudocode showing how the operations would be implemented. The operations must have the following time complexities: For a G: O(1) for new, O(log n) for insert/member/delete, O(n log n) for deletelarge (where n is the number of elements in the set) For a VG: as for G but deletelarge must take O(1) time 7