Final Review Announcements

• PA4 is due tonight at 11pm • PA3 re-grade requests due by Monday • Study guide and pracce final posted on course website (website was upgraded to be mobile friendly). • Review session tomorrow from noon to 1pm • Secon A: If you have mulple finals (three or more) on Friday and need to take the exam on Wed, send me an email (with the necessary proof). • Secon B: If you have mulple finals (three or more) on Wed and need to take the exam on Friday, please respond to my Piazza poll Final Exam… • 4 Parts • Part 1: Basic knowledge of data structures and C++ – 20% to 30% of final score – Mulple choice • Part 2: Applicaon, Comparison and Implementaon of the data structures – 20% to 30% of final score – Short answers • Part 3: Simulang algorithms and run me analysis – 20% to 30% of final score – Short answers • Part 4: C++ and programming assignments

Final Exam Practice Questions CSE 100 (Fall 2014) CSE Department University of California, San Diego

Part A: The Basics

This section tests your basic knowledge of data structures via multiple choice questions. Sample questions include all the iclicker and reading quiz questions covered in class. Please make sure you review them.

Part B: Application, Comparison and Implementation

This section tests how well you understand what goes on under the hood of the data structures covered during the course, their strengths and weaknesses and their applications. The format is short answers and fill in the blanks.

1. B-trees.

(a) Construct a 2-3 by inserting the following keys in the order shown: 10, 15, 20, 25, 17, 30. You can check your answers and experiment with trees of your own design at the following web sites: https://www.cs.usfca.edu/Part 2: B-trees ˜galles/visualization/BTree.html http://ats.oka.nu/b-tree/b-tree.manual.html

(b) Which of the following are legal 2,3 trees (B tree of order 3)? For a tree that is not a valid 2,3 tree, state a reason why. 22 50 22 52

8 30 64 5 9 24 53 54

22 12 50 79

5 9 1 7 4 7 30 64 69 84 102 Insert the value 42 into the following BTree. 4. Consider the following of initially unrelated elements is S=0,1,2,3,4,5,6, 7, 8, 9, 10, 11, 12. Assume that the initial union-find is shown here, a forest of 12 singleton nodes with the header array initialized as shown.

0 1 2 3 4 5 6 7 8 9 10 11 12

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 4. Consider the following set of initially unrelated elements is S=0,1,2,3,4,5,6, 7, 8, 9, 10, 11, 12. Assume(a) Draw that the the final initial forest union-find of up-trees that data results structure from the is following shown here, sequence a forest of operations of 12 singleton using union- nodes with the headerby-size. array initialized Break ties by as keeping shown. the first argument as the root. Union(0,2), Union(3,4), Union(9,7), Union(9,3), Union (6,8), Union(6,0), Union(12,6), Union(1,11), Union(9,6)

0 1 2 3 4 5 6 7 8 9 10 11 12

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

(a) Draw the final forest of up-trees that results from the following sequence of operations using union- by-size. Break ties by keeping the first argument as the root. Union(0,2), Union(3,4), Union(9,7), Union(9,3), Union (6,8), Union(6,0), Union(12,6), Union(1,11), Union(9,6)

(b) Draw the new forest of up-trees that results from doing a Find(4) with path compression on your forest of up-trees from (a).

(b) Draw the new forest of up-trees that results from doing a Find(4) with path compression on your forest of up-trees from (a). 4. Consider the following set of initially unrelated elements is S=0,1,2,3,4,5,6, 7, 8, 9, 10, 11, 12. Assume that the initial union-find data structure is shown here, a forest of 12 singleton nodes with the header array initialized as shown.

0 1 2 3 4 5 6 7 8 9 10 11 12

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

(a) Draw the final forest of up-trees that results from the following sequence of operations using union- by-size. Break ties by keeping the first argument as the root. Union(0,2), Union(3,4), Union(9,7), Union(9,3), Union (6,8), Union(6,0), Union(12,6), Union(1,11), Union(9,6)

(b) Draw the new forest of up-trees that results from doing a Find(4) with path compression on your forest of up-trees from (a).

6

9 8 12 0

3 7 2

4 1

11 Ternary 5. Ternary search Trees.

(a) Consider the following ternary . Nodes with double circles have their end bits set to true. Circle all of the words from the list on the right that are in the tree and write in any words that are missing. At the end you should have a complete list of all words found in the tree, and only those words. a n

l d s air, all, a, and, as i l t r

(b) Does the height of this tree depend on the order in which the keys have been inserted? (c) Briefly explain why you would prefer to use a ternary search tree rather than a to implement getAllValidWords() in PA4. Part C: SimulatingPartPart C: C: Simulating Simulating Algorithms Algorithms Algorithms and Run and Time and Run AnalysisRun Time Time Analysis Analysis Part C: Simulating AlgorithmsPartPart C: C: Simulating Simulating and Run Algorithms Algorithms Time and Analysis and Run Run Time Time Analysis Analysis This sectionThisThis tests section howsection tests well tests how you how well understand well you understand you the understand algorithms the algorithms the during algorithms the during course. during the course. The the focus course. The is focus on The simulating is focus on simulating is on simulating This section tests how wellThis you section understand tests how the well algorithms you understand during the algorithms the course. during The the focus course. is The on focus simulating is on simulating well knownwell graph known algorithms graph algorithms and theThis application and section the tests application of how data well structures of you data understand structures to achieve the algorithms to achieve fast run during fast times. run the course. times.You will The You also focus will is also on simulating well knownwell known graph graph algorithms algorithmswell and known the graph and application the algorithms application of and data the structuresapplication of data structures of to data achieve structures to fast achieve to run achieve times. fast fast run You run times. times. will also You You will will also also be tested onbe testedanalyzing on analyzing the running thewell timerunning known of these time graph of algorithms. algorithms these algorithms. and The the application format The isformat of short data structures is answers short to answers achieve and fill fast and in run fillthe times. in the You will also be testedbe tested on analyzing on analyzing thebe running testedbe the tested running on time analyzingon analyzing of time these the the runningof algorithms. running these time algorithms. time of these The of these algorithms. format algorithms. The is format short The The format answers is format short is short is and short answers answers fill answers in and and the and fill fill in fill the in in the blanks. blanks. blanks.blanks. blanks.blanks. 1. Graphs.1. GivenGraphs. theGiven following the following graph, run graph, Dijkstra’s run Dijkstra’s algorithm algorithm on it, with on it, source with nodesourceA node. AlongsideA. Alongside the the 1. Graphs.1. Graphs.GivenGiven the following the1. Graphs. following1. Graphs. graph,GivenGiven rungraph, the Dijkstra’s the following run following Dijkstra’s graph, algorithm graph, run run algorithmDijkstra’s on Dijkstra’s it, withalgorithm on algorithm source it, on with it, on node with it, source with sourceA. source Alongside node node nodeA.. AlongsideA Alongside the. Alongside the the graph is thegraph data is structure the data structurefor each node for each that node you that will you modify, will modify,see below. see below. graphgraph is the data is the structure data structuregraph forgraph each is the for is node the data each data structure that node structure you for that will eachfor you each modify, node willnode that see thatmodify, you below. you will will modify, see modify, below. see below.see below. D A: dist =A: 0 dist prev= = 0 -1 prev=A: done= dist -1 = 0 f done= prev=adj: f ((B,5),(D,10),(E,6)) -1 adj: done= ((B,5),(D,10),(E,6)) f adj: ((B,5),(D,10),(E,6)) D D D D D A: dist =A: 0 dist prev= = 0 -1A: prev= dist done= = 0 -1 f prev= done= adj: -1 ((B,5),(D,10),(E,6)) done=f adj: f ((B,5),(D,10),(E,6)) adj: ((B,5),(D,10),(E,6)) 0 0 0 0 0 0 B: dist =B: distprev= = -1prev= done= -1 f done= adj: f ((D,3)) adj: ((D,3)) B: dist =B: distprev= = B: dist -1B:prev= dist = done= = prev= -1 fprev= done= -1 adj: -1 done= ((D,3)) done=f f adj: adj: f ((D,3)) adj: ((D,3)) ((D,3)) C C C C 3 3 C C 3 3 3 3 1 1 1 4 4 14 4 1 10 4 1 10 10 410 10C: distC: =C: dist dist =prev= = prev= -1prev=C: done= dist -1C: -1 dist = done= f done== prev= adj: fprev= f ((D,0)) -1 adj: adj: -1 done= ((D,0)) ((D,0)) done= f adj: f adj: ((D,0)) ((D,0)) 10 C: dist = prev= -1 done= f adj: ((D,0)) B E B B B E B E E 1 E 11 1 1 B E 1 D: dist = prev= -1 done= f adj: () 5 D: distD: =D: dist dist =prev= = prev= -1prev=D: done= dist-1 -1 = done= f done=prev= adj: f f () -1 adj: adj: done= () () f adj: () 5 5 5 5 D: dist = prev= -1 done= f adj: () 5 6 6 6 6 6 1 6 11 1 A 1 E: dist = prev= -1 done= f adj: ((C,3)) A A A A E: distE: =E: dist dist =prev= = prev= -1prev=1E: done= dist -1 -1 = done= f done=prev= adj: f f ((C,3)) -1 adj: adj: done= ((C,3)) ((C,3)) f adj: ((C,3)) A E: dist = prev= -1 done= f adj: ((C,3)) 1 (A,0) The boxes below represent1 the priority11 queue which1 is used in the algorithm. Each set of boxes represents The boxesTheThe below boxes boxes represent below below represent represent the priorityThe the the boxes priority queue priority below whichqueue represent queue which is which used the priority is in is used the used queue inalgorithm. in the which the algorithm. algorithm. is used Each in set Eachthe Each algorithm.of set boxes set of of boxes Eachrepresents boxes set represents represents of boxes represents (initial priority queue) the priority queue after the first pair has been1 dequeued and its associated node has been fully expanded thethe priorityThe priority boxes queue queue below after after representthe thethe first priority first pair thepair queue has priority has been after been thedequeued queue dequeued first pair which has and and been is its itsused dequeuedassociated associated in the and node algorithm. its node associated has has been been Each node fully fully has set expanded been of expanded boxes fully expanded represents the priority queue after the first pair(i.e. has its been neighbors, dequeued along with and their its associated distances, have node been has added been to the fully priority expanded queue as appropriate). You (i.e. its neighbors, along with their distances, have been added to the priority queue as appropriate). You (i.e. its neighbors,(i.e.(i.e. itsthe its neighbors, neighbors,priority along with queuealong along their with after with distances,will their the their fill first distances, these distances, have pair in. beenhas have have been added been been dequeued added to added the to priority to and the the priority its priority queue associated queue as queue appropriate). nodeas as appropriate). appropriate). has been You fully You You expanded will fill thesewillwill fill in.(i.e. fill these these its in.neighbors, in. alongwill fill with these their in. distances, have been added to the priority queue as appropriate). You (priority queue after exploringRun Dijkstra’s A) algorithm by (1) modifying the values in the dist, prev, done and done fields (above) as the will fill these in. Run Dijkstra’s algorithm by (1) modifying the values in the dist, prev, done and done fields (above) as the Run Dijkstra’sRunRun Dijkstra’s Dijkstra’salgorithm algorithm algorithm by (1) modifying by by (1)algorithm (1) modifying modifying the runs, values and the the (2) values in showing values the indist, inthe the the prev,valuesdist,dist, done stored prev, prev,and done in done thedoneand priorityandfieldsdonedone queuefields (above)fields after (above) each (above) as new the as node as the the is expanded. algorithmalgorithm runs, runs, and and (2) (2) showing showingalgorithm the runs,the values values and (2) stored storedshowing in in the the the values priority priority stored queue queue in the after priority after each each queue new new after node node each is new is expanded. expanded. node is expanded. algorithm runs,Run and Dijkstra’s (2) showing algorithm theFor values theby data (1) stored modifying structures in the at priority the the top, values you queue should in the after crossdist, each off prev, old new values done node asand you is expanded.done replacefields them. (above) For the priority as the For the data structures at the top, you should cross off old values as you replace them. For the priority For theFor dataFor the structuresalgorithm the data data structures structures at runs, the and top, at at the (2) you thequeue, top, showing top,should you you you should crossthe should values show off cross cross old the stored off valuescontents off old old in values as theof values the you priority whole as replace as you you queue queue replace replace them. in the after them. nextFor them. each setthe For Forof new priority boxesthe the node priority after priority is you expanded. have fully queue, you should show the contents of the whole queue in the next set of boxes after you have fully queue, youqueue,queue, shouldFor you youthe should show data should the structures show show contents the theexpanded/explored contents at contentsof the the top, ofwhole of the you the whole from queue should whole the queue nodein queuecross the that in next off youin the theold just set next next removedvalues of set boxes set of asfrom of boxes after you boxes the front replace afteryou after of have you the you them. queue have fully have in For fully the fully previousthe priority step. expanded/explored from the node that you just removed from the front of the queue in the previous step. expanded/exploredexpanded/exploredexpanded/exploredqueue, from you shouldthe from node from the show thattheThe node node youinitial the that justthatcontents priority you removed you just queue just of removed theremoved and from wholeinitial the from fromvalues front queue the the are of front alreadyfront thein the of queue of the filled next the queue in inqueue set thefor inyou.of previous in the boxes the You previous previous may after step. stop step.you the step. process have when fully The initial priority queue andThe initial initial priority values queue are andalready initial filled values in are for already you. filled You in may for you. stop You the may process stop the when process when The initialThe priorityexpanded/explored initial queue priority and queue initial from andeach values initial the node node are has values already been that marked are you filled already just as done. in removed filled for You you. inmay forfrom You not you. need maythe You all front stop of may the of the queues. the stop process queue the If you process when in do the need when previous more, just drawstep. each node has been markedeach as node done. has You been may marked not as need done. all You of may the not queues. need all If of you the queues. do need If you more, do need just more, draw just draw each nodeeach hasThe been node initial marked has been priority as marked done. queuethem Youas anddone. in may below initial You not the may need valuesfinal array, not all are need of to the thealready all side, queues. of or the filled on queues. theIf you in scratch for do If you. page. you need do You more, need may just more, stop draw just the draw process when them in below the final array,them to in the below side, the or final on array, the toscratch the side, page. or on the scratch page. them in belowthemeach the in below finalnode array, has the final been to the array, marked side, to orthe as on side,done. the or scratch You on the may page. scratch not need page. all of the queues. If you do need more, just draw them in below the final array, to the side, or on the scratch page. 3. Run Time Analysis

(a) Consider the following algorithm operating of a graph G(V, E) and a source vertex s i. Initially, set the visited field of all vertices in V as FALSE ii. Start at source vertex, s. Set the visited field of s as TRUE, set counter to 1 iii. Enqueue s into a queue iv. While the queue is not empty or counter less than or equal to V | | A. Dequeue the vertex v from the head of the queue B. For each of v0s neighbor vertex, w (with visited FALSE): • Mark the visited field of w as TRUE • Increment counter by 1 • Enqueue w into the queue i. Identify the above graph algorithm.

ii. Assume the graph is implemented as an adjacency list. Write the Big O running time of each step of the algorithm (against each line of above) and derive the tightest overall Big O bound. Data structure Comparison

Is operaon K faster in data structure X or data structure Y? Data structure Comparison Of the following pairs of data structures, which of the pair is the beer choice for... (circle 1 in each set)

• Inserng a list of sorted elements: Randomized search tree / Binary search tree / They are about equal • Ease of implementaon (assume it’s not built in): Skip list / Red-Black tree (RBT) / about equal • In-order traversal of elements: Hashtable / Binary search tree / They are about equal • Smallest average-case (Big-O) me to find an element: Hashtable / AVL tree / They are about equal • Fastest actual me to find an element from secondary storage (NOT big-O): RBT / AVL tree / B-trees/They are all about equal • Requires less space: Mul-way / Ternary tree / They are about equal • Congratulaons on pulling through – you are a survivor ! • Hope you go on to do wonderful things with your knowledge of advanced data structures.

Good luck with PA4, the final and life in general J