CS 170 Second Midterm ANSWERS 7 April NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt):

Similar documents
Week 11: Minimum Spanning trees

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

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Recitation 13. Minimum Spanning Trees Announcements. SegmentLab has been released, and is due Friday, November 17. It s worth 135 points.

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Exam 3 Practice Problems

Algorithms for Minimum Spanning Trees

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Homework Assignment #3 Graph

Lecture 6 Basic Graph Algorithms

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

MST worksheet By Jim Xu

Lecture 25 Spanning Trees

CS 310 Advanced Data Structures and Algorithms

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Lecture Notes on Spanning Trees

Dijkstra s algorithm for shortest paths when no edges have negative weight.

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f

Problem Pts Score Grader Problem Pts Score Grader

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?

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

CSE332 Summer 2012 Final Exam, August 15, 2012

CS521 \ Notes for the Final Exam

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

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

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

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

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

INDIAN STATISTICAL INSTITUTE

CSci 231 Final Review

tree follows. Game Trees

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

SUGGESTION : read all the questions and their values before you start.

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

CS 4349 Lecture October 18th, 2017

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

Lecture 25 Notes Spanning Trees

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.

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

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

CSE 373: Practice Final

CHAPTER 13 GRAPH ALGORITHMS

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

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

CSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Minimum Spanning Trees My T. UF

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

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

Solving problems on graph algorithms

Chapter 23. Minimum Spanning Trees

CSE 100 Practice Final Exam

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

Dist(Vertex u, Vertex v, Graph preprocessed) return u.dist v.dist

Final Examination CSE 100 UCSD (Practice)

Lecture Summary CSC 263H. August 5, 2016

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Chapter 9 Graph Algorithms

CS 561, Lecture 10. Jared Saia University of New Mexico

Greedy Algorithms. Previous Examples: Huffman coding, Minimum Spanning Tree Algorithms

CS170 First Midterm Answers 17 Feb { What are the strongly connected components of the directed graph shown below?

CS 6783 (Applied Algorithms) Lecture 5

Problem Set 1. Solution. CS4234: Optimization Algorithms. Solution Sketches

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

COMP 355 Advanced Algorithms

Algorithms Activity 6: Applications of BFS

Week 12: Minimum Spanning trees and Shortest Paths

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

Advanced Algorithms Class Notes for Monday, October 23, 2012 Min Ye, Mingfu Shao, and Bernard Moret

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

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

CS170 Discussion Section 4: 9/18

Graph Algorithms (part 3 of CSC 282),

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

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

Minimum-Cost Spanning Tree. Example

Graph Algorithms (part 3 of CSC 282),

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies

Outline. Graphs. Divide and Conquer.

ALGORITHM DESIGN GREEDY ALGORITHMS. University of Waterloo

Introduction to Algorithms April 21, 2004 Massachusetts Institute of Technology. Quiz 2 Solutions

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

Midterm 1 Solutions. (i) False. One possible counterexample is the following: n n 3

Introduction to Algorithms

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

Graphs - II. Announcements. Where did I leave that book? Where did I leave that book? Where did I leave that book? CS 2110, Fall 2016

Review of course COMP-251B winter 2010

Chapter 9 Graph Algorithms

Reference Sheet for CO142.2 Discrete Mathematics II

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

CSE 373 Autumn 2012: Midterm #2 (closed book, closed notes, NO calculators allowed)

Minimum spanning trees

CISC 320 Midterm Exam

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

CS 170 Spring 2000 Solutions and grading standards for exam 1 Clancy

CS 561, Lecture 9. Jared Saia University of New Mexico

15 210: Parallel and Sequential Data Structures and Algorithms

Transcription:

CS 170 Second Midterm ANSWERS 7 April 2010 NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt): Name of Neighbor to your right (1 pt): Instructions: This is a closed book, closed calculator, closed computer, closed network, open brain exam, but you are permited a 1 page, double-sided set of notes, large enough to read without a magnifying glass. You get one point each for filling in the 5 lines at the top of this page. Each other question is worth 20 points. No points will be subtracted for wrong answers so it s in your best interest to guess all you want. Write all your answers on this exam. If you need scratch paper, ask for it, write your name on each sheet, and attach it when you turn it in (we have a stapler). 1 2 3 Total 1

Question 1 (20 points). We are running the following four algorithms on the graph below, where the algorithms have already processed the three bold-face edges: Dijkstra s algorithm for shortest paths, starting from S. Prim s algorithm for the Minimum Spanning Tree (MST), starting from S (ignoring edge directions). Kruskal s algorithm for the Minimum Spanning Tree (MST) (ignoring edge directions). Breadth-First-Search (BFS) starting from S (ignoring both edge directions and edge weights, but visiting neighboring vertices in lexicographic order). (a) Which 3 edges would be added next to the MST in Prim s algorithm? Be sure to indicate the order in which they are added. Answer: First (A,B), then (A,E), then (E,H). (b) Which 3 edges would be added next to the MST in Kruskal s algorithm? indicate the order in which they are added. Answer: First (G,H), then (A,B), then (E,H). Be sure to (c) Which 3 edges would be added next to the BFS-tree by BFS? Be sure to indicate the order in which they are added. Answer: First (A,B), then (A,E), then (C,F). (d) At this point in the running of Dijkstra s algorithm, S has been taken off the top of the priority queue and marked as visited. Which 4 vertices would be marked next in Dijkstra s algorithm, i.e. deleted from the priority queue and marked? What are the shortest paths, and their lengths, to these 4 vertices? Answer: First D (shortest path S-D, length 1), then C (shortest path S-C, length 2), then A (shortest path S-A, length 3), then B (shortest path S-A-B, length 3+5=8). 2

Question 1 (20 points). We are running the following four algorithms on the graph below, where the algorithms have already processed the three bold-face edges: Dijkstra s algorithm for shortest paths, starting from S. Prim s algorithm for the Minimum Spanning Tree (MST), starting from S (ignoring edge directions). Kruskal s algorithm for the Minimum Spanning Tree (MST) (ignoring edge directions). Breadth-First-Search (BFS) starting from S (ignoring both edge directions and edge weights, but visiting neighboring vertices in lexicographic order). (a) Which 3 edges would be added next to the MST in Prim s algorithm? Be sure to indicate the order in which they are added. Answer: First (U,X), then (U,Y), then (Y,Z). (b) Which 3 edges would be added next to the MST in Kruskal s algorithm? indicate the order in which they are added. Answer: First (W,Z), then (U,X), then (Y,Z). Be sure to (c) Which 3 edges would be added next to the BFS-tree by BFS? Be sure to indicate the order in which they are added. Answer: First (R,T), then (R,W), then (U,X). (d) At this point in the running of Dijkstra s algorithm, S has been taken off the top of the priority queue and marked as visited. Which 4 vertices would be marked next in Dijkstra s algorithm, i.e. deleted from the priority queue and marked? What are the shortest paths, and their lengths, to these 4 vertices? Answer: First V (shortest path S-V, length 1), then R (shortest path S-R, length 2), then U (shortest path S-U, length 3), then X (shortest path S-U-X, length 3+5=8). 3

Question 1 (20 points). We are running the following four algorithms on the graph below, where the algorithms have already processed the three bold-face edges: Dijkstra s algorithm for shortest paths, starting from S. Kruskal s algorithm for the Minimum Spanning Tree (MST) (ignoring edge directions). Prim s algorithm for the Minimum Spanning Tree (MST), starting from S (ignoring edge directions). Breadth-First-Search (BFS) starting from S (ignoring both edge directions and edge weights, but visiting neighboring vertices in lexicographic order). (a) Which 3 edges would be added next to the MST in Prim s algorithm? Be sure to indicate the order in which they are added. Answer: First (L,P), then (L,Q), then (Q,R). (b) Which 3 edges would be added next to the MST in Kruskal s algorithm? indicate the order in which they are added. Answer: First (N,R), then (L,P), then (Q,R). Be sure to (c) Which 3 edges would be added next to the BFS-tree by BFS? Be sure to indicate the order in which they are added. Answer: First (J,K), then (J,N), then (L,P). (d) At this point in the running of Dijkstra s algorithm, S has been taken off the top of the priority queue and marked as visited. Which 4 vertices would be marked next in Dijkstra s algorithm, i.e. deleted from the priority queue and marked? What are the shortest paths, and their lengths, to these 4 vertices? Answer: First M (shortest path S-M, length 1), then J (shortest path S-J, length 2), then L (shortest path S-L, length 3), then P (shortest path S-L-P, length 3+5=8). 4

Question 2 (20 points). In this problem, we will analyze a new algorithm for Minimum Spanning Tree (MST). It is based on the observation that for every vertex of a graph, the shortest edge incident on that vertex is part of an MST. In this problem, we assume we have an undirected connected graph G=(V, E), where edge weights can be positive or negative. (Part a is worth 6 points, and parts b-h are worth 2 points each.) a) The shortest edge incident on any vertex is part of an MST. Prove this fact by filling in the blanks in the following proof by contradiction: Assume for the sake of contradiction that the shortest edge e v (of weight w 0 ) incident on vertex v is not part of any MST. Consider the union of an MST T and e v, T {e v }, which will have a containing vertex v. This will have (how many?) edges incident on v. Assume we remove the edge e of weight w that was originally part of the MST T. Of course, w is (bigger than? less than? equal to?) w 0 by the original assumption. Then, by removing e from T {ev }, we obtain a new T of weight (at least? at most? equal to?) the weight of T. Hence T (containing edge e v ) is an MST - contradiction! Answer: Assume for the sake of contradiction that the shortest edge e v (of weight w 0 ) incident on vertex v is not part of any MST. Consider the union of an MST T and e v, T {e v }, which will have a cycle containing vertex v. This cycle will have two (how many?) edges incident on v. Assume we remove the edge e of weight w that was originally part of the MST T. Of course, w is bigger than (bigger than? less than? equal to?) w 0 by the original assumption. Then, by removing e from T {e v }, we obtain a new tree T of weight at most (at least? at most? equal to?) the weight of T. Hence T (containing edge e v ) is an MST - contradiction! The algorithm works by creating a series of graphs F i (i.e. F 1, F 2,...). At each step i, we create graph F i from graph F i 1 by contracting two nodes into one as shown below, and then updating the edges correspondingly, keeping only the shortest edge between any pair of vertices: F 0 = G, set T is initially empty i = 1 While F i 1 has at least two vertices: (start step i) Initialize all vertices in F i 1 to be unmarked For k=1, 2,... up to the number of vertices in F i 1 If v k is unmarked contract v k and its nearest neighbor as follows: Find the shortest edge incident on v k, call it (v k, v l ) Mark v k and v l Add (v k, v l ) to T Add a vertex v k to graph F i (v k is the contracted vertex in F i of both v k and v l ) For each edge e = (v a, v b ) of F i 1, add an edge between the contracted vertices in F i (e.g. v a and v b ) under the conditions: 5

Return T v a and v b are distinct if there already is an edge between v a and v b in F i, only keep the one of lower weight i = i + 1 For the following questions, circle the correct answer (as we previously said, a step is performed every time we update from F i 1 to F i ) b) The total number of steps will be (circle the tightest bound) O( E log E ) O( V ) O( E ) O(log V ) Answer: O(log V ) c) At each step, the amount of work is (circle the tightest bound) O( E ) O( V ) O( E 2 ) O( V 2 ) Answer: O( E ) d) Hence, the running time of this algorithm, compared to Kruskal, seems to be assymptotically: Faster The same Cannot be compared Slower Answer: The same By doing a more careful analysis, it turns out we can improve on part c. Let F i be the number of vertices of F i. e) How many edges can F i have in the worst case (circle the tightest bound) O( F i 2 ) O( F i log F i ) O( F i ) O( F i F i ) Answer: O( F i 2 ) f) Hence, the amount of work per step is the minimum of and (fill in the blanks) Answer: Hence, the amount of work per step is the minimum of O( E ) and O( F i 2 ) (fill in the blanks) 6

Now find the total running time under the following assumptions: g) If the graph is sparse ( E = Θ( V )) then the total running time is (circle the tightest bound) O( E E ) O( E log E ) O( E ) O( E 2 ) Answer: O( E log E ) h) If the graph is dense ( E = Θ( V 2 )), then the total running time is (circle the tightest bound) O( E E ) O( E 2 ) O( E log E ) O( E ) Answer: O( E ) 7

Question 2 (20 points). In this problem, we will analyze a new algorithm for Minimum Spanning Tree (MST). It is based on the observation that for every vertex of a graph, the shortest edge incident on that vertex is part of an MST. In this problem, we assume we have an undirected connected graph G=(V, E), where edge weights can be positive or negative. (Part a is worth 6 points, and parts b-h are worth 2 points each.) a) The shortest edge incident on any vertex is part of an MST. Prove this fact by filling in the blanks in the following proof by contradiction: Assume for the sake of contradiction that the shortest edge e v (of weight w 0 ) incident on vertex v is not part of any MST. Consider the union of an MST T and e v, T {e v }, which will have a containing vertex v. This will have (how many?) edges incident on v. Assume we remove the edge e of weight w that was originally part of the MST T. Of course, w is (bigger than? less than? equal to?) w 0 by the original assumption. Then, by removing e from T {ev }, we obtain a new T of weight (at least? at most? equal to?) the weight of T. Hence T (containing edge e v ) is an MST - contradiction! Answer: Assume for the sake of contradiction that the shortest edge e v (of weight w 0 ) incident on vertex v is not part of any MST. Consider the union of an MST T and e v, T {e v }, which will have a cycle containing vertex v. This cycle will have two (how many?) edges incident on v. Assume we remove the edge e of weight w that was originally part of the MST T. Of course, w is bigger than (bigger than? less than? equal to?) w 0 by the original assumption. Then, by removing e from T {e v }, we obtain a new tree T of weight at most (at least? at most? equal to?) the weight of T. Hence T (containing edge e v ) is an MST - contradiction! The algorithm works by creating a series of graphs H j (i.e. H 1, H 2,...). At each step j, we create graph H j from graph H j 1 by contracting two nodes into one as shown below, and then updating the edges correspondingly, keeping only the shortest edge between any pair of vertices: H 0 = G, set T is initially empty j = 1 While H j 1 has at least two vertices: (start step j) Initialize all vertices in H j 1 to be unmarked For l=1, 2,... up to the number of vertices in H j 1 If u l is unmarked contract u l and its nearest neighbor as follows: Find the shortest edge incident on u l, call it (u l, u i ) Mark u l and u i Add (u l, u i ) to T Add a vertex u l to graph H j (u l is the contracted vertex in H j of both u l and u i ) For each edge e = (u b, u c ) of H j 1, add an edge between the contracted vertices in H j (e.g. u b and u c ) under the conditions: 8

Return T u b and u c are distinct if there already is an edge between u b and u c in H j, only keep the one of lower weight j = j + 1 For the following questions, circle the correct answer (as we previously said, a step is performed every time we update from H j 1 to H j ) b) At each step, the amount of work is (circle the tightest bound) O( V ) O( E ) O( E 2 ) O( V 2 ) Answer: O( E ) c) The total number of steps will be (circle the tightest bound) O( E log E ) O(log V ) O( V ) O( E ) Answer: O(log V ) d) Hence, the running time of this algorithm, compared to Kruskal, seems to be assymptotically: Faster Cannot be compared The same Slower Answer: The same By doing a more careful analysis, it turns out we can improve on part c. Let H j be the number of vertices of H j. e) How many edges can H j have in the worst case (circle the tightest bound) O( H j log H j ) O( H j 2 ) O( H j ) O( H j H j ) Answer: O( H j 2 ) f) Hence, the amount of work per step is the minimum of and (fill in the blanks) Answer: Hence, the amount of work per step is the minimum of O( E ) and O( H j 2 ) (fill in the blanks) 9

Now find the total running time under the following assumptions: g) If the graph is sparse ( E = Θ( V )) then the total running time is (circle the tightest bound) O( E E ) O( E ) O( E 2 ) O( E log E ) Answer: O( E log E ) h) If the graph is dense ( E = Θ( V 2 )), then the total running time is (circle the tightest bound) O( E E ) O( E ) O( E 2 ) O( E log E ) Answer: O( E ) 10

Question 2 (20 points). In this problem, we will analyze a new algorithm for Minimum Spanning Tree (MST). It is based on the observation that for every vertex of a graph, the shortest edge incident on that vertex is part of an MST. In this problem, we assume we have an undirected connected graph G=(V, E), where edge weights can be positive or negative. (Part a is worth 6 points, and parts b-h are worth 2 points each.) a) The shortest edge incident on any vertex is part of an MST. Prove this fact by filling in the blanks in the following proof by contradiction: Assume for the sake of contradiction that the shortest edge e v (of weight w 0 ) incident on vertex v is not part of any MST. Consider the union of an MST T and e v, T {e v }, which will have a containing vertex v. This will have (how many?) edges incident on v. Assume we remove the edge e of weight w that was originally part of the MST T. Of course, w is (bigger than? less than? equal to?) w 0 by the original assumption. Then, by removing e from T {ev }, we obtain a new T of weight (at least? at most? equal to?) the weight of T. Hence T (containing edge e v ) is an MST - contradiction! Answer: Assume for the sake of contradiction that the shortest edge e v (of weight w 0 ) incident on vertex v is not part of any MST. Consider the union of an MST T and e v, T {e v }, which will have a cycle containing vertex v. This cycle will have two (how many?) edges incident on v. Assume we remove the edge e of weight w that was originally part of the MST T. Of course, w is bigger than (bigger than? less than? equal to?) w 0 by the original assumption. Then, by removing e from T {e v }, we obtain a new tree T of weight at most (at least? at most? equal to?) the weight of T. Hence T (containing edge e v ) is an MST - contradiction! The algorithm works by creating a series of graphs L k (i.e. L 1, L 2,...). At each step k, we create graph L k from graph L k 1 by contracting two nodes into one as shown below, and then updating the edges correspondingly, keeping only the shortest edge between any pair of vertices: L 0 = G, set T is initially empty k = 1 While L k 1 has at least two vertices: (start step k) Initialize all vertices in L k 1 to be unmarked For i=1, 2,... up to the number of vertices in L k 1 If w i is unmarked contract w i and its nearest neighbor as follows: Find the shortest edge incident on w i, call it (w i, w j ) Mark w i and w j Add (w i, w j ) to T Add a vertex w i to graph L k (w i is the contracted vertex in L k of both w i and w j ) For each edge e = (w c, w d ) of L k 1, add an edge between the contracted vertices in L k (e.g. w c and w d ) under the conditions: w c and w d are distinct 11

Return T if there already is an edge between w c and w d in L k, only keep the one of lower weight k = k + 1 For the following questions, circle the correct answer (as we previously said, a step is performed every time we update from L k 1 to L k ) b) At each step, the amount of work is (circle the tightest bound) O( V ) O( E 2 ) O( E ) O( V 2 ) Answer: O( E ) c) The total number of steps will be (circle the tightest bound) O( E log E ) O( V ) O(log V ) O( E ) Answer: O(log V ) d) Hence, the running time of this algorithm, compared to Kruskal, seems to be assymptotically: Faster Cannot be compared Slower The same Answer: The same By doing a more careful analysis, it turns out we can improve on part c. Let L k be the number of vertices of L k. e) How many edges can L k have in the worst case (circle the tightest bound) O( L k log L k ) O( L k ) O( L k 2 ) O( L k L k ) Answer: O( L k 2 ) f) Hence, the amount of work per step is the minimum of and (fill in the blanks) Answer: Hence, the amount of work per step is the minimum of O( E ) and O( L k 2 ) (fill in the blanks) 12

Now find the total running time under the following assumptions: g) If the graph is dense ( E = Θ( V 2 )), then the total running time is (circle the tightest bound) O( E ) O( E E ) O( E 2 ) O( E log E ) Answer: O( E ) h) If the graph is sparse ( E = Θ( V )) then the total running time is (circle the tightest bound) O( E log E ) O( E E ) O( E ) O( E 2 ) Answer: O( E log E ) 13

Question 3 (20 points) Only answers for version 1 of the question is supplied, other versions differ in variable names only. In this question we will be given a string of characters, s = (s 1, s 2,..., s n ). A set D is called a legal set of matchings if its elements are pairs of indices (a, b) where 1 a < b n and s a = s b, and any two elements (a, b) and (x, y) are either disjoint, i.e a < b < x < y or x < y < a < b, or nested, i.e. a < x < y < b or x < a < b < y. Notice that if two pairs (a, b) and (x, y) are neither disjoint or nested, then either they share an end point a = x or a = y or b = x or b = y, or they partially overlap, a < x < b < y or x < a < y < b. In a legal set of matchings D, no 2 elements may share an endpoint or partially overlap. Graphically, if we write s on a linear line, and draw every match we pick (a, b) as an edge between s a and s b (the edge is drawn above the string, see figure), a set is a legal set of matchings if the edges representing the matchings don t intersect. Legal Matching: NOT Legal Matching: We want to find an efficient dynamic programming algorithm that returns the size of the maximal legal set of matchings for a given string s. a. (4 points) Complete the following subproblem definition: K(a, b) is the size of the maximal legal set of matchings Answer: K(a, b) is the size of the maximal legal set of matchings index a to index b. in the substring of s from 14

b. (6 points) We define: { 1 if sa = s IsMatch(a, b) = b 0 if s a s b Write out the the computation for K(a, b), using previous subproblems and the IsM atch function: Hint: It is helpful to consider the two distinct cases of trying to match a with b, or not trying to do so. { } K(a, b) = max Answer: K(a, b) = max {IsMatch(a, b) + K(a + 1, b 1), max a x<b {K(a, x) + K(x + 1, b)}} c. (5 points) Write the pseudocode for your algorithm. The base case is a K(a, a) = 0 Answer: Iterations: for j = 0,..., n 1 do for a = 1,..., n do if a + j n then compute K(a, a + j) end if end for end for return K(1, n) d. (5 points) What is the running time of your algorithm? Answer: O(n 3 ) Short Explanation: Answer: Every calculation of K(a, b) takes O(n) and there are two nested loops, each taking O(n). 15