Lecturers: Sanjam Garg and Prasad Raghavendra Feb 21, Midterm 1 Solutions

Similar documents
Homework 1 Solutions MA 522 Fall 2017

The isoperimetric problem on the hypercube

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU)

Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees. Application: Connecting a Network

condition w i B i S maximum u i

Lecture 5. Counting Sort / Radix Sort

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

Examples and Applications of Binary Search

Module 8-7: Pascal s Triangle and the Binomial Theorem

6.854J / J Advanced Algorithms Fall 2008

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

Lecture 6. Lecturer: Ronitt Rubinfeld Scribes: Chen Ziv, Eliav Buchnik, Ophir Arie, Jonathan Gradstein

Major CSL Write your name and entry no on every sheet of the answer script. Time 2 Hrs Max Marks 70

CIS 121 Data Structures and Algorithms with Java Fall Big-Oh Notation Tuesday, September 5 (Make-up Friday, September 8)

EE123 Digital Signal Processing

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

Greedy Algorithms. Interval Scheduling. Greedy Algorithms. Interval scheduling. Greedy Algorithms. Interval Scheduling

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

Computer Science Foundation Exam. August 12, Computer Science. Section 1A. No Calculators! KEY. Solutions and Grading Criteria.

Algorithm Design Techniques. Divide and conquer Problem

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

Computational Geometry

CMPT 125 Assignment 2 Solutions

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming

n n B. How many subsets of C are there of cardinality n. We are selecting elements for such a

Lecture 1: Introduction and Strassen s Algorithm

1.2 Binomial Coefficients and Subsets

On (K t e)-saturated Graphs

1 Graph Sparsfication

Combination Labelings Of Graphs

Big-O Analysis. Asymptotics

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Data Structures and Algorithms Part 1.4

Graphs ORD SFO LAX DFW

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

Sorting 9/15/2009. Sorting Problem. Insertion Sort: Soundness. Insertion Sort. Insertion Sort: Running Time. Insertion Sort: Soundness

3. b. Present a combinatorial argument that for all positive integers n : : 2 n

Perhaps the method will give that for every e > U f() > p - 3/+e There is o o-trivial upper boud for f() ad ot eve f() < Z - e. seems to be kow, where

Design and Analysis of Algorithms Notes

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Ones Assignment Method for Solving Traveling Salesman Problem

. Written in factored form it is easy to see that the roots are 2, 2, i,

Thompson s Group F (p + 1) is not Minimally Almost Convex

Counting the Number of Minimum Roman Dominating Functions of a Graph

Our Learning Problem, Again

THE PROBLEM THE PROBLEM (1) THE PROBLEM (2) THE MINIMUM ENERGY BROADCAST PROBLEM THE MINIMUM SPANNING TREE

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

5.3 Recursive definitions and structural induction

Parabolic Path to a Best Best-Fit Line:

CSE 417: Algorithms and Computational Complexity

How do we evaluate algorithms?

Graphs. Shortest Path and Topological Sort

Chapter 3 Classification of FFT Processor Algorithms

EE University of Minnesota. Midterm Exam #1. Prof. Matthew O'Keefe TA: Eric Seppanen. Department of Electrical and Computer Engineering

CS 111: Program Design I Lecture 21: Network Analysis. Robert H. Sloan & Richard Warner University of Illinois at Chicago April 10, 2018

Alpha Individual Solutions MAΘ National Convention 2013

arxiv: v2 [cs.ds] 24 Mar 2018

Algorithm. Counting Sort Analysis of Algorithms

Analysis of Algorithms

Big-O Analysis. Asymptotics

Order statistics. Order Statistics. Randomized divide-andconquer. Example. CS Spring 2006

Chapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000.

c-dominating Sets for Families of Graphs

Polynomial Functions and Models. Learning Objectives. Polynomials. P (x) = a n x n + a n 1 x n a 1 x + a 0, a n 0

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

MR-2010I %MktBSize Macro 989. %MktBSize Macro

prerequisites: 6.046, 6.041/2, ability to do proofs Randomized algorithms: make random choices during run. Main benefits:

Strong Complementary Acyclic Domination of a Graph

Chapter 8. Strings and Vectors. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Lecture 18. Optimization in n dimensions

Random Graphs and Complex Networks T

The number n of subintervals times the length h of subintervals gives length of interval (b-a).

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

End Semester Examination CSE, III Yr. (I Sem), 30002: Computer Organization

The Adjacency Matrix and The nth Eigenvalue

Chapter 8. Strings and Vectors. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that.

Some non-existence results on Leech trees

Pattern Recognition Systems Lab 1 Least Mean Squares

Protected points in ordered trees

Lower Bounds for Sorting

Python Programming: An Introduction to Computer Science

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

Informed Search. Russell and Norvig Chap. 3

Image Segmentation EEE 508

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Counting Regions in the Plane and More 1

2. ALGORITHM ANALYSIS

Numerical Methods Lecture 6 - Curve Fitting Techniques

Fundamentals of Media Processing. Shin'ichi Satoh Kazuya Kodama Hiroshi Mo Duy-Dinh Le

Fast Fourier Transform (FFT) Algorithms

Consider the following population data for the state of California. Year Population

Lecture 9: Exam I Review

Data Structures Week #9. Sorting

Transcription:

U.C. Berkeley CS170 : Algorithms Midterm 1 Solutios Lecturers: Sajam Garg ad Prasad Raghavedra Feb 1, 017 Midterm 1 Solutios 1. (4 poits) For the directed graph below, fid all the strogly coected compoets ad draw the DAG of strogly coected compoets. Label each strogly coected compoet with all the odes it cotais. B A J C D E F G H Draw the DAG i the box below: B AJ CDEGH F. (8 poits) Execute DFS o the same graph (reproduced here for coveiece) startig at ode A ad breakig ties alphabetically. Draw the DFS tree/forest. Mark the pre ad post values of the odes with umberig startig from 1.

B A J C D E F G H Node pre post A B C D E F G H J Draw the DFS Tree/Forest i the box below: B A J C D E F G H Node pre post A 1 4 B 5 18 C 6 17 D 7 16 E 8 15 F 11 1 G 10 13 H 9 14 J 3 3. (4 poits) I the DFS executio from above, mark the followig edges as as T for Tree, F for Forward, B for Back ad C for Cross. (No justificatio ecessary)

Edge G! C A! J B! A B! D Type B T C F 4. (a) (4 poits) Draw a strogly coected graph with 6 vertices with the smallest possible umber of edges i the box below. A B C D E F (b) ( poits) I geeral, the miimum umber of edges i a strogly coected directed graph with vertices is. (o justificatio ecessary) You ca t have less tha edges as it will make the graph discoected. So the miimum is simply a loop through all the vertices. 5. (6 poits) Suppose G =(V,E) is a udirected graph with positive iteger edge weights {w e e E}. We would like to fid the shortest path betwee two vertices s ad t with a additioal requiremet: if there are multiple shortest paths, we would like to fid oe that has the miimum umber of edges. We would like to defie ew weights {w 0 e e E} for the edges so that, a sigle executio of Dijkstra s algorithm o the graph G with ew weights {w 0 e}, startig from s fids the shortest path to t with this additioal requiremet. How should we set the ew weights w 0 e No justificatio ecessary. w 0 e = w e + 1 V The ituitio here is that we wat to add somethig to the edges so that we ca di eretiate a path usig 4 edges ad a path usig 5 edges but with the same distace. I the meatime, we eed to make sure the origial Dijkstra s does t break. Therefore, we add the fractio 1 V so that the total legth of ay path will be icreased by at most 1. Sice all the edges are positive itegers, Dijkstra s will ru exactly the same as before. 6. (8 poits) Here is a implemetatio of Bellma-Ford algorithm: Iput: Directed Graph G =(V,E), with edge legths {`e e E}. Output: Compute distaces dist(u) to each vertex u from a start vertex s. 3

for i =1to do dist(u) 1 prev(u) il ed for dist(s) 0 k 0 repeat for i =1to do for each directed edge (i, j) do update(i, j) ed for ed for k k +1 util all dist values stop chagig OR (k = ) Algorithm 1: Bellma-Ford Algorithm It turs out that the rutime of the above algorithm ca be very sesitive to the way i which vertices i a graph are umbered. I other words, the rutime of the algorithm o the same graph ca widely vary, if we chage the umberig of the vertices. Give oe graph G o 11 vertices ad two ways to label the vertices of G, such that i oe labellig the algorithm makes 0 calls to update, while i the other labellig the algorithm termiates i 10 calls to update. The followig graph will call update exactly 0 times. A B C D E F G H I J K The followig graph will call update exactly 10 times. K J I H G F E D C B A 7. (6 poits) We computed the miimum spaig tree T o a graph G with costs {c e } ee. Ufortuately, after computig the miimum spaig tree, we discover that the costs of all the edges i the graph have chaged as follows: the ew cost w e are give by, w e = ( c e if c e > 100 0 if c e apple 100 Is the tree T that we computed earlier, still a miimum spaig tree of the graph Write yes or o : yes If yes, prove; if o, disprove with a couterexample. Cosider a edge e that is part of the MST T composed from graph G with costs {c e } ee. will show that every such edge e must also be part of the MST T 0 after the weight update. We Case 1 Suppose c e apple 100, the w e = 0. Sice e was part of T, it is a edge with the miimal weight coectig 4

two sub-msts. If we have a edge e 0 such that c e 0 apple 100, the i the ew MST T, w e = w e 0. Thus edge e will still be part of MST T 0 as addig either e or e 0 would ot add ay cost to the total cost of T 0.Ifc e 0 > 100, the w e <w e 0 = 0 ad thus e is part of MST T 0. Case Now suppose c e > 100, the w e = c e. Suppose for the sake of cotradictio that e is ot part of MST T 0. There must be some edge e 0 part of MST T 0 coectig the two sub-msts that e is coectig. If w e 0 = 0, the c e 0 apple 100 <c e. Thus e 0 must be part of MST T rather tha e. This is a cotradictio because e is part of T ad havig both e ad e 0 coectig the two sub-msts would create a cycle ad violate the tree property. Now if w e 0,c e 0 > 100 ad we assume that e 0 is part of T 0 rather tha e, c e 0 < c e.thusc 0 e <c e. Agai e 0 must be part of MST T, a cotradictio. [The most commo mistake was to igore the case where a edge e of weight c e > 100 may be replaced by a edge e 0 with prior weight c e 0 apple 100. For full credit, we eeded a explaatio of why this case was impossible.] Alterative solutio A less commo, but correct alterative solutio was to argue that Kruskal s algorithm produces valid MSTs by cosiderig each edge e G i icreasig order of weight c e. The orderig of the edges is preserved i this update. Therefore, if T was a valid solutio produced by Kruskal s algorithm prior to the update, the T will remai a valid solutio after the update. 8. (6 poits) I this graph, some of the edge weights are kow, while the rest are ukow. B 3 E F 1 A D 4 C G H cost(a, D) =, cost(b,d) =1, cost(c,d) =4, cost(b,e) =3 List all edges that must belog to a miimum spaig tree, regardless of what the ukow edge weights tur out to be. Justify each of your edges briefly (a setece or less is eough). Edges that must belog to every MST Justificatio G H This is the oly edge that ca coect H to ay other vertex, so it must be icluded i ay MST. Remember that ay MST must spa all of the vertices. B D The cut property. More details: let S 1 = D, ad S = V S 1 = ABCEF GH. The three edges from the origial graph will coect these two forests, with edge weights: 1,, 4. So ay MST must cotai the B D edge of weight 1. B E Also by cut property. Cosider S 1 = ABD, ad S = V S 1 = CEFGH. The the 3 edge is the shortest edge coectig both forests. 5

Alterative justificatios: Ru Prim s MST algorithm, startig from H. H G is the shortest edge, so it will be added to the MST. The we kow at least oe MST will iclude this edge. Ad similarly, we ca ru Prim s, startig from D. ThetheD to the MST, so we kow at least oe MST will iclude D B. B edge is the first edge to be added But for B E, the Prim s argumet gets more complicated. You could argue that ruig Prim s startig from ay of ABD will always add the edge of weight 3, ad ever the edge of weight 4. 9. Desig a e ciet algorithm for the followig problem Iput: umbers {a 1,...,a } Goal: Compute the polyomial with a 1,...,a as its roots. I other words, compute coefficiets b 0,...,b so that (x a 1 ) (x a ) (x a )=b 0 + b 1 x +...b x. (Hit: Try divide ad coquer & use O( log ) time polyomial multiplicatio algorithm as a blackbox) (a) (10 poits) Pseudocode: procedure PolyomialWithRoots({a 1,...,a }): if =1the b 0 a 1 b 1 1 retur b 0,b 1 ed if q(x) PolyomialWithRoots({a 1,...,a b/c }) r(x) PolyomialWithRoots({a b/c,...,a }) p(x) MultiplyPolyomials(q(x), r(x)) retur coe ciets of p(x) (b) (3 poits) Write the recurrece for the ruig time of the algorithm i the box. T () = T + O( log ) (c) (6 poits) Solve the recurrece to compute the ruig time ad put your aswer i the box. Show your work below the box. T () = O( log ) To show this, we use the recurrece tree: log Sum: log log v ( log Sum: log! 4 log 4 4 log 4 4 log 4 }! 4 log 4 Sum: log 4. 6

The total, the, is log + log = log + log + log 4 + log + + 4 = (log + (log log ) + (log log 4) + ) = ((log + log + + log ) (log + log 4 + + log )) = (log ) (1 + + + log ) log log = log = log We could also use a more geeral versio of the Master Theorem, which states that if T () = at (/b) +f(), where a 1, b>1, ad f() = ( c log k )wherec = log b a,thet () = c log k+1. I this case, a = b =, c = 1 = log b a = log, ad k = 1. 10. (13 poits) You are give the road etwork G =(V,E) of a coutry, ad the legths {`e e E} of each road i the etwork. Some of the cities have airports, while others do t. Let F be the subset of cities that have a airport i them. Devise a algorithm to compute the distace from each city to the earest airport. (Assume that the graph is directed ad that all edge legths are o-egative). Remember every correct algorithm will receive a score depedig o its rutime. (ca you do it with the same ru-time as Dijkstra s). (a) Mai Idea: (try less tha 6 seteces if you ca, but do t fret if you go over) Reverse the graph. Add a dummy source vertex s to the graph. Add a zero weight edge from the source vertex to each airport. The ru Dijkstra o s to fid the distace from s to every vertex i the graph. This shortest distace from each city to the earest airport is the distace from the vertex that represets this city to vertex s. It is equivalet to merge all airports ito a sigle vertex or to ru a modified Dijkstra s i which all airports are iitialized with dist = 0. Brute force solutios were commo, ivolvig ruig Dijkstra from each vertex or just from each airport vertex. These are correct but ie ciet, as the ruig time will be O( V ( V + E ) log V ) or O( F ( V + E ) log V ), ad we have o boud o F except that it is at most V. (b) Rutime of the algorithm = O(( V + E ) log V ) (c) Proof of Correctess (try less tha 4 seteces if you ca, but do t fret if you go over) This is a directed graph ad the dummy ode s that we add is a source ode which has oly edges goig out ad o edges goig i. Thus addig the ode s wo t add a path betwee ay two odes from the origial graph. I additio, we set the legth of all edges of s to zero, so it wo t add ay additioal legth. Through Dijkstra (which we kow fids shortest distaces from a source ode to all others) we will fid the shortest distace from each ode to ode s (because 7

this is equivalet to the reversed path i the reversed graph), which is the same as the shortest path to ay airport. A brute force solutio ca be see to be correct by otig that Dijkstra will fid the shortest path betwee every relevat pair of odes, via multiple calls, ad that we ca compare them to fid the miimum i each case. 11. (10 poits) Suppose you are give a array A[1...] of sorted itegers that has bee circularly shifted k positios to the right for some k. For example, [35, 4, 5, 15, 7, 9] is a sorted array that has bee circularly shifted k = positios, while [7, 9, 35, 4, 5, 15] has bee shifted k = 4 positios. We ca obviously fid the largest elemet i A i O() time. Assumig all the itegers i the array are distict, describe a O(log ) algorithm to fid the largest elemet i A. Brief but precise descriptio of the algorithm: (try less tha 6 seteces if you ca, but do t fret if you go over) Mai idea: We wat to leverage the sorted-ess of the array, so we will implemet a modificatio of biary search where you always recurse o the usorted array. Compare A[0] ad A[mid] (where mid = d e). If A[0] <A[mid] the the left half of the array is sorted; the max value is either the mid or i the right half of the array. Store the mid as the temporary max ad repeat o the right half. If A[0] >A[mid] the the left half is usorted ad the max is somewhere i that array, so repeat o the left half. We ca also compare the mid poit with the last elemet. If A[last] <A[mid], we look at the right half of the array, icludig the mid poit. If A[last] >A[mid], we look at the left half of the array. Other splits are also possible. For example, comparig A[d 4 e]witha[d 3 4 e]. As log as a solutio compares poits that are far eough apart, it ca be used to determie the portio of the array to make the recursive call. If left poit is greater tha right poit, make the recursive call o the elemet betwee them. Otherwise the maximum elemet is i the other part of the array, which ca be obtaied by joiig the two parts i the same relative positioig, i.e. A[1,...,d 4 e]+a[d 3 4 e,...,]. A commo mistake was to compare immediate eighbors. But this ca oly be used to determie if the left elemet is the max. It does ot have eough iformatio to tell you which side the max is o ad thus caot obtai a O(log ) solutio. 8