In other words it is assumed here that the two paths obey the rules given Our

# In other words it is assumed here that the two paths

This preview shows page 16 - 18 out of 26 pages.

). In other words, it is assumed here that the two paths obey the rules given. Our required output is C [ n, n ]. Trivially, C [1 , 1] = 0, and we let C [1 , 2] be the distance between x 1 to x 2 , defined as dist (1 , 2). We will use dynamic programming to find C [ n, n ]. So we need to figure out how any particular C [ p, q ] depends on smaller subproblems. We consider C [ a, b ] to be a sub- problem of C [ c, d ] if a < c . If a = c then we tiebreak with b and d . First, notice what happens if we want to compute C [ p, q ] for the case that q - p > 1. This means that C [ p, q - 1] exists, and q - 1 > p . For C [ p, q ], the path ending at x q must use all points between x p +1 and x q , inclusive. This doesn’t differ much from C [ p, q - 1], because here the path ending at x q - 1 must use all points between x p +1 and x q - 1 , inclusive. So we just take the solution to C [ p, q - 1] and append the edge from point x q - 1 to point x q . This implies that if we manage to calculate C [ p, p +1], obtaining all C [ p, q ] for p +2 q n can be done in linear time, O ( n - p ). This corresponds to filling in a row in a dynamic programming table (specifically to one side of a diagonal, where we would
find all solutions of the form C [ p, p ], for 1 p n ). Now, how do we obtain C [ p, p ] and C [ p, p +1]? Let’s focus on the former. C [ p, p ] corresponds to two paths from x 1 to x p . We take one of those two paths and look at all possible ways we could select its last segment. This last segment will be from some x i to x p , and once we fix that we will need to optimally complete both paths, which involves solving C [ i, p ]. Thus we need to have solved all subproblems of the form C [ i, p ], for 1 i < p . Specifically, C [ p, p ] will be the minimum of C [ i, p ] + dist ( i, p ), which can be computed in linear time, O ( p ). In the dynamic programming table, we are looking at a column above C [ p, p ]. Handling C [ p, p +1] similarly takes linear time. We can’t just extend from C [ p, p ] in constant time, because the latter already represents a closed loop. So we must still look at all possible ways to form a loop with one final hop. All together the time complexity is quadratic. The dynamic programming table has dimensions n × n . The C [ p, p ] values are down a diagonal, and each one can be com- puted in linear time if the column above it is already filled. The same holds for cells C [ p, p +1], immediately to the right of the diagonal. Once the first two cells in a row are computed, the rest to the right take constant time each, by the simple extension mentioned early on. The table is filled row by row from top to bottom. Graphs 19. Let G be a graph with red, blue and white vertices. Show how to identify all red vertices that have paths to blue vertices without going through other red vertices. Do this for both undirected and directed graphs. Argue correctness and provide a time complexity for your algorithm. Answer: (For undirected graph) This is a search problem: from every red vertex v we need to search through G to find a vertex that is blue. We know that some edges are not usable for the search from v .

#### You've reached the end of your free preview.

Want to read all 26 pages?

• Fall '19
• Insertion Sort, Big O notation, Analysis of algorithms, kx

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes