






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Solutions to the final exam of CSE 373 course offered in Spring 2012. The exam covers topics such as AVL Trees, Maximum Spanning Trees, Graphs, Sorting, etc. solutions to problems related to these topics and explains the concepts in detail. It also includes code snippets and algorithms. The exam is useful for students who want to prepare for similar exams or gain a better understanding of the course material.
Typology: Exams
1 / 12
This page cannot be seen from the preview
Don't miss anything!
Given the following AVL Tree: (a) Draw the resulting BST after 5 is removed, but before any rebalancing takes place. Label each node in the resulting tree with its balance factor. Replace a node with both children using an appropriate value from the node's left child.
(b) Now rebalance the tree that results from (a). Draw a new tree for each rotation that occurs when rebalancing the AVL Tree (you only need to draw one tree that results from an RL or LR rotation). You do not need to label these trees with balance factors. We show the intermediate rotation from the RL rotation along with the final answer below:
Trace the sequence of operations below, using a disjoint set with union by size (without path compression). If there is a tie when performing a union, the tree with the smaller-valued root should be the root of the union. Assume there are initially 10 sets { 1 },{2},{ 3 },…,{ 10 }.
(a) Draw the final forest of up-trees that result from the operations above. (b) Draw the new forest of up-trees that results from doing a find( 9 ) with path compression on your forest of up- trees from (a).
For this problem, you will be finding the maximum spanning tree. You can still use Prim’s and Kruskal’s algorithms if you just switch “smallest” with “biggest” when examining edges. (a) Using Prim’s algorithm starting with vertex "A", list the vertices of the graph below in the order they are added to the maximum spanning tree. A, D, I, G, H, J, F, C, E, B (b) Using Kruskal’s algorithm, list the edges of the maximum spanning tree of the graph below in the order that they are added. (G, H) (C, F) (B, E) (H, J) (A, D) (F, H) (E, F) (A, I) (G, I)
(a) What is the purpose of AVL trees? AVL trees are binary search trees that are guaranteed to be balanced. They guarantee O(log n) operations on the tree. (b) What is the purpose of heaps? Heaps are how most priority queues are implemented. They allow you to quickly find the minimum value from the values stored in the heap without costly adds.
Assume a graph that represents all the web pages (vertices) and the hyperlinks (edges) between them on the Internet. Is the graph… (a) directed (b) cyclic (c) unweighted (d) What kinds of web pages have a high in-degree? Examples:
In the Arrays class, there is a static sort method: Arrays.sort(int[] a) The method sorts the specified array into ascending numerical order. The sorting algorithm is a tuned quicksort. Quicksort has a worst case running time of O(n^2 ) whereas mergesort has a worst case running time of O(n log n), but quicksort is generally chosen over mergesort. Why? Explain your answer. Quicksort generally runs in O(n log n) time. The constant factor is better than mergesort, because it does the sort with simple swaps, whereas mergesort has to create a new array to store the merged data.
Override the hashCode method of the following (poorly-designed) class. Your method should follow proper hashing principles and minimize collisions. You may assume that any non-primitive field in RentalCar has an equals methods and a good implementation of hashCode. public class RentalCar { private int year; private String color; private String model; private Person renter; // null if available ... public boolean equals(Object other) { if (!(other instanceof RentalCar)) { return false; } RentalCar o = (RentalCar)other; return (year / 10 == o.year / 10) // car model year in same decade && model.equals(o.model) && ((renter == null && o.renter == null) || (renter != null && renter.equals(o.renter))); } public int hashCode() { int result = 17; result = 37 * result + (year / 10); result = 37 * result + model.hashCode(); if (renter == null) { result = 37 * result + 1; } else { result = 37 * result + renter.hashCode(); } return result; } }
(a) Write a method that given an array of integers will print out a list of duplicates (separated by spaces). The contents of the array do not have to be preserved. The duplicates should appear exactly once, though they do not have to be in any particular order. It is OK to have an extra space at the end of the output. The method may use any auxiliary data structures. public static void printDuplicates(int[] array) { Arrays.sort(array); boolean foundDuplicate = false; int lastDuplicate = 0; for (int i = 1; i < array.length; i++) { if (array[i] != array[i-1]) { if (foundDuplicate) { System.out.print(lastDuplicate + " "); } foundDuplicate = false; } else { lastDuplicate = array[i]; foundDuplicate = true; } } if (foundDuplicate) { System.out.print(lastDuplicate); } System.out.println(); } public static void printDuplicates(int[] array) { Map<Integer, Integer> counts = new HashMap<Integer, Integer>(); for (int num : array) { if (counts.containsKey(num)) { counts.put(num, counts.get(num)+1); } else { counts.put(num, 1); } } for (int num : counts.keySet()) { if (counts.get(num) > 1) { System.out.print(num + " "); } } System.out.println(); }
{ 1, 2, 3, 4, 1, 2, 3 } 1 2 3 { 1, 2, 3, 4, 4, 3, 2, 1 } 2 4 3 1 { 1, 2, 3, 4 } { 1, 1, 1, 2, 2, 1, 1, 1 } 2 1
(b) What is the runtime of your implementation? Explain your answer. The runtime of the first method that uses sorting is dominated by the sort and takes O(n log n) time. Finding the duplicates from a sorted list takes linear time and is subsumed by the runtime of the sort. The runtime of the second is linear. It goes over the array and accesses the hash per element. Accessing the hash table takes constant time, so the total running time is linear. Printing the elements requires going over the hash values and only printing values that have a count greater than one. That also takes only linear time. O(n)
Given the following graph: For the following problems, assume alphabetical edge order. (a) Write (in order) the list of vertices that would be visited by running a breadth - first search (BFS) starting from G G D E H I J A B C F (b) Write (in order) the list of vertices that would be visited by running a depth - first search (DFS) starting from G. G D A B C E F H J I