CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Similar documents
Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

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

Info 2950, Lecture 16

CSci 231 Final Review

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

W4231: Analysis of Algorithms

Minimum Spanning Trees My T. UF

Algorithm Design and Analysis

Minimum Spanning Trees Ch 23 Traversing graphs

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Homework Assignment #3 Graph

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

CMPSCI 311: Introduction to Algorithms Practice Final Exam

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Data Structures Brett Bernstein

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

CS61B Spring 2016 Guerrilla Section 6 Worksheet

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

INSTITUTE OF AERONAUTICAL ENGINEERING

Graph Algorithms. Definition

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

Graph Algorithms: Chapters Part 1: Introductory graph concepts

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Final Exam Solutions

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

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

CS521 \ Notes for the Final Exam

Second Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

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

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

Problem Set 2 Solutions

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Solutions to Exam Data structures (X and NV)

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

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Outline. Graphs. Divide and Conquer.

Lecture 3: Graphs and flows

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

CSE 331: Introduction to Algorithm Analysis and Design Graphs

END-TERM EXAMINATION

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Questions Total Points Score

22 Elementary Graph Algorithms. There are two standard ways to represent a

Contents. CS 124 Final Exam Practice Problem 5/6/17. 1 Format and Logistics 2

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Greedy algorithms is another useful way for solving optimization problems.

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Chapter 9 Graph Algorithms

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Graph. Vertex. edge. Directed Graph. Undirected Graph

CS4800: Algorithms & Data Jonathan Ullman

Randomized Algorithms, Hash Functions

Minimum Spanning Trees

Exam 3 Practice Problems

CS/COE

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

Student number: Datenstrukturen & Algorithmen page 1

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

Minimum Spanning Trees

Lecture 10 Graph algorithms: testing graph properties

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

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

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Algorithm Design (8) Graph Algorithms 1/2

Solutions to relevant spring 2000 exam problems

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

COS 226 Algorithms and Data Structures Fall Final Solutions. 10. Remark: this is essentially the same question from the midterm.

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

Lecture Summary CSC 263H. August 5, 2016

The Konigsberg Bridge Problem

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

2. CONNECTIVITY Connectivity

Copyright 2000, Kevin Wayne 1

Sample Solutions to Homework #4


n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

Lecture 9 Graph Traversal

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CISC 320 Midterm Exam

Chapter 9 Graph Algorithms

Elements of Graph Theory

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

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination T09:00

22c:31 Algorithms. Kruskal s Algorithm for MST Union-Find for Disjoint Sets

Question 2 (Strongly Connected Components, 15 points). What are the strongly connected components of the graph below?

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

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

Transcription:

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018 Q1: Prove or disprove: You are given a connected undirected graph G = (V, E) with a weight function w defined over its edges. Let s V be an arbitrary vertex in G. Starting at vertex s, if you do a depth-first search (DFS) in G such that the edges going out of any vertex are always explored in increasing order of weight, then the resulting DFS tree is also a minimum spanning tree. Solution. The assertion is false. Consider the graph G = (V, E) where V = {a, b, c and E = {(a, b), (b, c), (a, c). The weight function is w(a, b) = 1, w(a, c) = 2 and w(b, c) = 3. If we perform a DFS from the vertex a using the above rule, the unique DFS tree is given by the edges {(a, b), (b, c), and its weight is 4. On the other hand, the MST is formed by edges {(a, b), (a, c), and has a weight of 3. Q2: Give an example of a weighted connected undirected graph G = (V, E) and a vertex v such that the minimum spanning tree of G is different than the shortest path tree rooted at v. Can the two trees be completely disjoint? Solution. They can t be completely disjoint as the smallest edge incident on v will be the same in both trees (assuming that the smallest weight edge incident on v is unique). The two trees can be different though, as the following example shows: consider a graph G that is a cycle on n vertices {v 0, v 1, v 2,..., v n 1. Let the edge e = (v 0, v n 1 ) have a weight of n 2 and all other edges in {(v 0, v 1 ), (v 1, v 2 ),..., (v n 2, v n 1 ) have a weight of 1. The shortest path rooted at v 0 will contain the edge e, whereas the minimum spanning tree will not contain e. Q3: Given a directed graph with n vertices and m edges, design an O(nm) algorithm to find the length of the directed cycle with the minimum number of edges (or report that the graph is acyclic). Assume n m n 2. Solution. The critical observation is that the shortest directed cycle is a shortest path (number of edges) from s to v, plus a single edge v s. For each vertex s: - Use BFS to compute shortest path from s to each other vertex. - For each edge v->s entering s, consider cycle formed by shortest path from s to v (if the path exists), plus the edge v->s. Return the shortest overall cycle. The running time is O(mn). The single-source shortest path computation from s takes O(m + n) time per s using BFS. Finding all edges entering s takes O(m + n) time by scanning all edges (though a better way is to compute the reverse graph at once and access the adjacency lists). We must do this for each vertex s. Thus the overall running time is O(n(m + n)), which resolves to O(mn) since n m n 2.

November 18, 2018 Midterm 3 Review Solution Sketches 2 Q4: Consider the following directed graph. A. Run recursive depth-first search, starting at vertex A. Assume the adjacency lists are in lexicographic order, e.g. when exploring vertex E, consider E D before E G or E H. Complete the list of vertices in preorder (visit-time). Solution. A B C E D G H F I B. Run breadth-first search, starting at vertex A. Assume the adjacency lists are in lexicographic order. Complete the list of vertices in the order in which they are enqueued. Solution. A B D E C F G H I Q5: Suppose you know the MST of a weighted graph G = (V, E). Now, a new edge (v, w) of weight c is inserted into G to form a weighted graph G. Design an O(V ) time algorithm to determine whether the MST in G is also an MST in G. You may assume all edge weights are distinct. Solution. Find the unique path between v and w in the MST of G. This takes O(V ) time using BFS or DFS because there are only V 1 edges in the MST subgraph. We claim that the MST in G is the same as the MST in G if and only if every edge on the path has length less than c (recall that we assume all edge weights are distinct). If any edge on the path has weight greater than c, we can decrease the weight of the MST by swapping the largest weight edge on the new path with (v, w). Hence weight of the MST for G is strictly less than the weight of the MST for G. If the weight of (v, w) is larger than any edge on the path between v and w, then the cycle property asserts that (v, w) is not in the MST for G (because it is the largest weight edge on the cycle consisting of the path from v to w plus the edge (v, w). Thus, the MST for G is also the MST for G. Q6: You are given an array of n integers and a number k. Determine whether there is a pair of elements in the array that sums to exactly k. For example, given the array [1, 3, 7] and k = 8, the answer is yes since 1 + 7 = 8, but given k = 6 the answer is no. Your algorithm should run in expected O(n) time. Solution. This is a famous problem called the two-sum problem. Here s the optimal O(n) space and expected O(n) time solution (in code). On the exam, please don t use code unless otherwise specified :)

November 18, 2018 Midterm 3 Review Solution Sketches 3 Q7: public boolean sumstotarget(int[] arr, int k) { Set<Integer> values = new HashSet<>(); for (int i = 0; i < arr.length; i++) { if (values.contains(k - A[i])) { return true; values.add(a[i]); return false; A. Suppose that the following keys are inserted in the order: A B C D E F G into an initially empty linear-probing hash table of size 7, using the following hash function: key hash(key,7) A 3 B 1 C 4 D 1 E 5 F 2 G 5 What is the result of the linear-probing array? Assume that the array size is fixed. Solution. 0 1 2 3 4 5 6 G B D A C E F B. Suppose we use a hash function h to hash n distinct keys into an array T of length m. Assuming simple uniform hashing and hashing with chaining, what is the expected number of collisions? Provide a short explanation. Solution. There are n(n 1)/(2m) expected collisions. We use x ij to signify the event that keys k i and k j hash to the same value. For each key pair (k i, k j ), where 1 i < j n, the likelihood of a collision is P (x ij ) = 1/m. There are a total of n(n 1)/2 possible (unordered) key pairs. So the total number of expected collisions is n(n 1)/(2m). Q8: True/False: A. If G has a unique heaviest edge e, then e cannot be a part of any MST. B. If G has a unique lightest edge e, then e must be a part of some MST. C. If G has a unique lightest edge e, then e must be a part of every MST

November 18, 2018 Midterm 3 Review Solution Sketches 4 Solution. A. False (e could be a cut edge) B. True (it is the first edge added by Kruskal) C. True: given an MST T that doesn t contain e, we can add e to T to get a cycle, then remove the heaviest edge on that cycle. This gives a spanning tree of strictly lower weight, so T wasn t actually an MST. Q9: Given a DAG G and two vertices s and t, count the total number of paths from s to t. Solution. Topologically sort the graph. Then use the following recurrence to compute the number of paths: P aths(u) = P aths(v) v Adj[u] where P aths(t) = 1. Note that by storing P aths as an array, you can implement this in O(n + m) time (if you recursively re-compute P aths(v) each time, the runtime could be exponential saving the result in an array caches it so you only compute P aths(v) once for each vertex v). Q10: Recall the fibonacci numbers f 1 = 1, f 2 = 1, f k = f k 1 + f k 2 for k > 2. Suppose you are given an alphabet of size n with characters c 1,..., c n. Suppose that the frequency of c i is f i. What is the length of the huffman code for c i? Solution Draw the tree! Should be n + 1 i for i > 1 and n 1 for i = 1. Note, you can prove that l f i < f l+2 using induction, which will rigorously show why the tree looks that way.

November 18, 2018 Midterm 3 Review Solution Sketches 5 Some Useful Facts These will be stapled to the back of your exam for reference (if needed). 1. lg n = log 2 n ln n = log e n 2. Below are some formulas that may come handy. n n i = n(n+1) 2 i 2 = n(n+1)(2n+1) 6 (a + b) n = n 3. Log rules: a log a b = b i=0 ( n ) i a i b n i log ab = log a + log b log a b = log a log b log a b = b log a log a a b = b log a a = 1 log 1 = 0 4. Expected value: E[X] = x xp (X = x) Linearity of Expectation: for any finite collections of random variables X 1, X 2,..., X n, E[ n X i ] = n E[X i ]