fl15-mid2-sol.pdf - CS 374 Fall 2015 Midterm 2 Solutions 1...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
“CS 374” Fall 2015 — Midterm 2 Solutions 1 Short Questions (15 pts) (a) (5 pts) Give an asymptotically tight solution to the following recurrence. T ( n ) = T ( 7 n / 8 ) + T ( n / 8 ) + n for n > 8 and T ( n ) = 1 for 1 n 8. Solution: ( n log n ) . Ñ Rubric: 5 points for a correct answer. (b) (5 pts) Let G = ( V , E ) be an undirected graph with non-negative edge weights given by w ( e ) , e 2 E . Consider the following generalization of the MST problem. The input consists of G , a node s 2 V and an integer k . The goal is to find a minimum weight tree that contains s and exactly k nodes (including s ). Prove via a counter example that Prim’s algorithm may fail in general for values of k other than 1 or n . You should specify a small graph with edge weights, s and k and show the optimum solution and the output of running Prim’s algorithm when stopped with k nodes. Solution: Let k = 3. Consider the following graph: s w v u 1 2 0 Prim’s algorithm will take { s , w } and then { s , v } with weight 3, whereas the optimal solution is { s , v } and { v , u } with weight 2. Ñ Rubric: Out of 5 points, we allocate: +1 point for showing a graph with s marked and k specified. +2 points for describing the optimum solution. +2 points for describing the output for modified Prim’s algorithm. (c) (5 pts) Suppose you are given an array A of n numbers and are told that it has very few distinct numbers, say k . Describe a O ( n log k ) algorithm that given A and k decides whether A has at most k distinct numbers or more than k . Solution: A self-balancing binary search tree (such as Red-Black or AVL) is a data structure where finding whether a number exists and inserting a number can both be done in O ( log | T | ) time, where | T | is the number of elements in the tree. We must make sure that the tree size is O ( k ) , so that each operation can be performed in O ( log k ) time. Algorithm: (1) T a self-balancing BST.
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
“CS 374” Midterm 2 Solutions Fall 2015 (2) count 0. (3) For i 1 to n : (a) Find A [ i ] in T . (b) If A [ i ] is not found, insert A [ i ] in T , and increment count . (c) If count > k , return “more than k ”. (4) Return “at most k ”. The algorithm maintains a self-balancing BST that contains at most k + 1 elements, so finding and inserting takes O ( log k ) time. So the overall running time is O ( n log k ) . Ñ Rubric: Out of 5 points, your score will be: 5 points max for an O ( n log k ) algorithm. 3 points max for an O ( nk ) algorithm. 1 points max for an O ( n log n ) algorithm. Your score may go down for several reasons, including but not limited to: using inappropriate data structures or claiming incorrect running time for those data structures allowing the data structure to become large, so that the running time per operation is O ( log n ) or O ( n ) rather than O ( log k ) or O ( k ) . (This is the only exception for having a running time slower than what it could have been.) not determining the final answer “more than k ” versus “at most k 2
Image of page 2
“CS 374” Midterm 2 Solutions Fall 2015 2 Incomparable Pairs (15 pts) Let G = ( V , E ) be a directed graph. We say that a pair of nodes u , v 2 V are incomparable if neither u can reach v nor v
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern