0% found this document useful (0 votes)
93 views12 pages

1718 ST CS2040

Uploaded by

Qu Yuchen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
93 views12 pages

1718 ST CS2040

Uploaded by

Qu Yuchen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 12
National University of Singapore School of Computing CS2040 - Data Structures and Algorithms Final Assessment (Semester 4 AY2017/18) Time Allowed: 2 hours INSTRUCTIONS TO CANDIDATES: 1. Do NOT open this assessment paper until you are told to do so. 2, This assessment paper contains THREE (3) sections. It comprises TWELVE (12) printed pages, ineluding this page. 3. This is an Open Book Assessment. 4. Answer ALL questions within the boxed space in this booklet. Only if you need more space, then you can use the empty page 12 You can use either pen or pencil. Just make sure that you write legibly! 5. Important tips: Pace yourself! Do not spend too much time on one (hiard) question. Read all the questions first! Some questions might be easier than they appear. 6. You can use pseudo-code in your answer but beware of penalty marks for ambiguous answer. You can use standard, non-modified algorithm discussed in class by just mentioning its name. 7. Please write your Student Number below. Do NOT write your name, A/0/)1 This portion is for examiner's use only Section | Maximum Marks [Your Marks | Remarks A 20 B 30 c 50. Total 100 cs2040 A. Basics (20 marks) A.1_ Worst Case Time Complexity Analysis (10 marks) Write down the tightest! worst case time complexity of the various data structure operations or algorithms below. Each correct answer is worth 1 mark. The operations (algorithms) referred below are the unmodified version, as per discussion in class, eg, as currently explained in VisuAlgo or as currently implemented in Java API (version 8). Unless otherwise mentioned, there are currently n elements in the data structure. For graph- related operations, let n be the number of vertices and m the number of edges. You can assume that 10000 < n < 100000. AM/AL/DAG/SSSP are the abbreviations for Adjacency Matrix/ Adjacency List/Directed Acyclic Graph/Single-Source Shortest Paths, respectively. Unless specifically men- tioned, all graph-related operations are performed on simple graphs stored in an AL data structure, PQ is a Java PriorityQueue currently with n elements, HM is a Java HashMap currently with m elements, TH a 32-bit Integer. a Java TreeMap curr tly with n elements. v is No, Operations “Time Complexities 1 | while (a > 0) system.out.printin(PQ.pol1(); OO. 2 for (int i= 0; i 1) Characters of a String Z are pushed /enqueued one by one into a Stack $/Quene Q, respectively. After all n Characters are pushed/enqueued into $/Q, they are popped/dequeued from $/Q, respectively. Amazingly, the sequence of popped/dequeued Characters are identical. Thus Z isa String. pallest 3. The time complexity of partial Heap Sort algorithm if we are only interested to find k s Integers in a Binary Min Heap containing n Integers is O( ) 4, When the range of the Integer keys is small, e.g. (0..M-1], we can use Direct Addressing Table (DAT). A few examples of sinall range of Integer (or easily convertible into Integer) key: ], ete... Mention one “Singapore bus numbers [2..991] (at the moment)’, ASCII values [0..2% where DAT is applicable: more (Integer) ke 5, Java Hashtable always rehash its contents into a larger Hashtable with twice table size whenever 1n (number of keys) exceeds a: (load factor) x m (table size). Currently, a is set to default 0.75. If we know that we will not have more than 100000 keys to be inserted into our Hash Table, we shal] set the smallest initial m to be: — to avoid rehash. 6. Java TreeSet cannot have duplicates as it implements Interface Set that specifies this no- duplicate requirement. ‘Therefore, if need to allow duplicates in our application, we shall use — instead. 7. The best data structure to store a complete binary tree is 8, The best data structure to store a complete unweighted graph of V vertices 9, The maximum number of Connected Components (GCs) in a general graph G with V vertices and E edges (for any mmber of edge(s)) is 10. The shortest path from the source vertex s to another vertex 1 in a general weighted graph G is if vertex v is actually unreachable from s, ry Section A Marks = cs2040 B_ Intermediate (30 marks) B.1 Analysis (12 marks) Prove (show that the statement is correct) or disprove (give a counter example) the statements below. 1. We can prove that during AVL Tree insertion, if there is a need to do tree rotation, then rotating Just the lowest vertex with balance factor +2 (or -2) is sufficient. 2. To insert n (e.g, 10000 .. uu. 4 Section B.1 Marks C2040 B.2_ Implications (6 marks) Imagine that we have n (10000 < n < 100000) Strings of length not more than k = 20 characters. 15 that uses Separate Chaining as ASCII value of the first character of We want to insert these n Strings into a Hash Table of size m collision resolution technique. The hash function used is h(») the string. You can assume that the n Strings are collection of n city names in the world. In class, we learn that Separate Chaining usually uses auxiliary data structure: (Doubly) Linked List to store keys that collide into the same hash value. In this question, we replace Linked List with Balanced BST as the auxiliary data structure. What are the implications (which can be either negative, neutral, or even positive) of such modification? Bach correct answer is worth 1 mark, L 3 Section B.2 Marks cS2040 B.3_ Create Test Cases (12 marks) Create Test, Cases for each scenario below. Each valid test case worth 3 marks. 1. Suppose that we have an initially empty Hash Table with table size m = 5. The keys are Integer keys. The hash funetion is h(v) = v%m. If there is a collision, we use Quadratic Probing with probing sequence (h(w) + x k)%m as collision resolution technique for each probing step k. Insert 3, 4, or up to 5 Integers so that version of Quadratic Probing fails to find an empty slot albeit there is at least one empty cell 2, Insert V = 12 distinct integers one by one into an initially empty AVL Tree so that the resulting AVL Tree has height (number of edges from root to the deepest leaf) = 4. To simplify grading, the Integers must be positive and not more than 13. 3. Draw the highest possible rooted (root at the top and leaves at the bottom) undirected un- weighted Tree with V = 15 vertices consisting of 8 leaf vertices and 7 internal vertices. 4. Draw an undirected Graph with V = 9 vertices and E = 12 edges. Identify two vertices: Source vertex s and destination/sink vertex £ so that there are 16 distinct shortest paths from s to t. ‘Two paths are distinct if there is at least one edge difference between the two paths. Section B Marks = _ 82040 C Applications (50 marks) C.1_ SSSP in a Special "SLL” (20 marks) Disclaimer: This question is a modified version of a Kattis problem (CC-by-SA). You are given a special (Single/Singly) Linked List (SLL) with n (2 n-l (tail); yes it is a rather long SLL). Going through a normal ‘edge costs 1 unit. You will be given the value of n in the first line of input. Not just that, you are also given a special Integer S (2 < S < 105) as the second line of input. +t vertices i and j (i, > 0) in the linked list, there is also You are told that for every pair of disti 8 special edge/reference from vertex i —+ j if j > i, %8 = 0 and j%S = 0. Going through a special edge costs 2 unit, Of course, because of these special edges, the SLL now looks like @ graph. Your job is to find the shortest path from vertex 0 to vertex n-1 and output this shortest. path value (we do not need the actual shortest path). 2, shortest path value = 5 ( , $= 2, shortest path valu 03152545507 4 (only via 0 +126). Figure 1: Left picture: n = 6, 8 0-1-4344 5); Right picture: n solution: Design an efficient algorithm using the technique that we have learned in class (or beyond) to solve the problem above (remember that the last 8 marks are for time complexity analysis). A skeleton Java code has been provided for you: import java.io.#; class C1 { public static void main(Stringf] args) throws Exception { BufferedReador br = now BufferedReader (new InputStreanReader (System. in)); PrintWriter pw = new PrintWriter(Systen.out); // show how to read the inputs here (2 marks) cs2040 // show how to compute the answer here (14 marks) // you can use Java API (if necessary) to implement any ADT that you need J/ show how to print the output here (1 mark) pr.closeQ; > } // the tine complexity of ny solution above is 0¢ ) (3 marks) 8 Section C.1 Marks = cs2040 C.2 Post World Cup 2018 Football Matches (Easier, 20 marks) After FIFA World Cup 2018 ended recently, you are so into football. You now join a group of (virtual) friends (there are 1V players mumbered from 1 to N and 2 < N < 100000 - yes it is a large online gaming world) playing approximately M (1 v" that means player is 1 beats player v (the actual final score is irrelevant). If you are wondering why there is no draw, because the players turn on the penalty shootout option so that all football matches that end in draw will continue to penalty shootout that will uniquely determine the winner eventually. You want to test your hypothesis whether there is a transitivity relation between any three players 4a, b,¢, ie. if player « beats player b and player b beats player c, then player a should also beat player ¢ (if they happen to go head to head in at least one of the M matches). If this hypothesis remains true for all M matches, output “Correct hypothesis”. However if there is at least one case where this is not true, output “The ball is round” Your solution: Design an efficient algorithm using the technique that we have learned in class (or beyond) to solve the problem above (remember that the last 8 marks are for time complexity analysis) A skeleton Java code has been provided for you: import java.io.*; class C2 { // use this space for additional method(s) or global variable(s), if needed cs2040, Public static void main(String(] args) throws Exception { BufferedReader br = new BufferedReader (new InputStreanReader (Systen. in)); PrintWriter pw = new Printhriter(Systen. out); // show how to read the inputs here (6 marks) // show how to compute the answer here (10 marks) // you can use Java API (if necessary) to implement any ADT that you need // show how to print the output here (2 marks) pw.closeQ; + 3 // the time complexity of my solution above is 0¢ ) (3 marks) 10 ‘Section C.2 Marks cs2040 €.3. Post World Cup 2018 Football Matches (The Real One, 10 marks) Disclaimer: This question is a modified version of a Kattis problem (CC-by-SA). Please refer to the previous question for the full problem statement. For this question, the only change v", or “u < v" that means is this. The format of the M matches results is either: “u > v", “a player w beats/draws-with/loses-against player v. That's it, this time the matches can end in draws. Everything else is the same. So if player a and 6 draw, player 6 beats player ¢, player ¢ and d draw, then your hypothesis is broken if player a draws or beaten by player d (if they happen to go head to head in at least one of the M matches) Your solution: As your solution may likely use the solution that you have written in Section C.2, just mention in pseudocode on what are the changes that you will make to your code in Section C.2 above in order to make your solution work for this variant that can have draws (remember that the last 3 marks are for time complexity analysis) // the time complexity of my solution above is 0( ) @ marks) uw Section C Marks = _ cs2040 Extra blank papers ~ End of this Paper, All the Best 12

You might also like